Files
ComfyUI-H3-Continuum-Tiled/prompt_builder.py
T
chris.dumas a53651496c
tests / test (push) Canceled after 0s
tests / windows-storage (push) Canceled after 0s
Repeat shared context in every chunk
2026-10-15 09:19:13 +00:00

174 lines
7.0 KiB
Python

"""UI-backed sequence prompt construction for H3 Continuum."""
from __future__ import annotations
import json
from typing import Any, Mapping
MAX_CLIPS = 16
DEFAULT_CLIP_SECONDS = 4.0
def _number(value: Any, name: str, *, minimum: float = 0.0) -> float:
if isinstance(value, bool):
raise ValueError(f"{name} must be a number")
try:
result = float(value)
except (TypeError, ValueError) as exc:
raise ValueError(f"{name} must be a number") from exc
if result < minimum:
raise ValueError(f"{name} must be >= {minimum}")
return result
def default_sequence(clip_count: int = 2, clip_seconds: float = DEFAULT_CLIP_SECONDS) -> dict[str, Any]:
"""Return a portable editor state with one slot per clip."""
count = int(clip_count)
if count < 1 or count > MAX_CLIPS:
raise ValueError(f"clip_count must be between 1 and {MAX_CLIPS}")
duration = _number(clip_seconds, "clip_seconds", minimum=0.1)
return {
"version": 1,
"clips": [
{
"duration": duration,
"slots": [{"start": 0.0, "end": duration, "text": ""}],
}
for _ in range(count)
],
}
def normalize_sequence(value: Any, *, clip_count: int, default_seconds: float) -> dict[str, Any]:
"""Validate and normalize editor JSON without changing user-authored text."""
if isinstance(value, str):
try:
value = json.loads(value or "{}")
except json.JSONDecodeError as exc:
raise ValueError(f"timeline_json is not valid JSON: {exc.msg}") from exc
if not isinstance(value, Mapping):
raise ValueError("timeline_json must be a JSON object")
raw_clips = value.get("clips", [])
if not isinstance(raw_clips, list):
raise ValueError("timeline_json.clips must be a list")
count = int(clip_count)
if count < 1 or count > MAX_CLIPS:
raise ValueError(f"clip_count must be between 1 and {MAX_CLIPS}")
fallback_duration = _number(default_seconds, "default_chunk_seconds", minimum=0.1)
clips: list[dict[str, Any]] = []
for clip_index in range(count):
raw_clip = raw_clips[clip_index] if clip_index < len(raw_clips) else {}
if not isinstance(raw_clip, Mapping):
raise ValueError(f"clip {clip_index + 1} must be an object")
# Continuum projects use one chunk duration for every clip. Older
# editor state may still contain per-clip durations; ignore them so
# the single node control remains authoritative.
duration = fallback_duration
raw_slots = raw_clip.get("slots", [])
if not isinstance(raw_slots, list):
raise ValueError(f"clip {clip_index + 1} slots must be a list")
slots: list[dict[str, Any]] = []
for slot_index, raw_slot in enumerate(raw_slots):
if not isinstance(raw_slot, Mapping):
raise ValueError(f"clip {clip_index + 1} slot {slot_index + 1} must be an object")
start = _number(raw_slot.get("start", 0), f"clip {clip_index + 1} slot {slot_index + 1} start")
end = _number(raw_slot.get("end", duration), f"clip {clip_index + 1} slot {slot_index + 1} end")
if end <= start:
raise ValueError(f"clip {clip_index + 1} slot {slot_index + 1} end must be after start")
if end > duration + 1e-6:
raise ValueError(f"clip {clip_index + 1} slot {slot_index + 1} ends after the clip duration")
text = str(raw_slot.get("text", "")).strip()
if text:
slots.append({"start": start, "end": end, "text": text})
slots.sort(key=lambda slot: (slot["start"], slot["end"]))
if slots:
# The final shot owns the end of every uniform-duration chunk;
# there must be no unassigned tail on the editor track.
slots[-1]["end"] = duration
clips.append({"duration": duration, "slots": slots})
return {"version": 1, "clips": clips}
def format_sequence_prompt(
*,
shared_directive: str,
sequence: Mapping[str, Any],
soundscape: str = "",
music: str = "",
) -> str:
"""Format uniform clips as chunk blocks with relative shot timings.
Continuum treats square-bracket ranges as physical/logical chunk markers.
Shot timings belong inside each chunk and must therefore use plain
``start-end s:`` lines rather than creating additional Timeline sections.
"""
directive = str(shared_directive or "").strip()
sound = str(soundscape or "").strip() or "N/A"
music_text = str(music or "").strip() or "N/A"
sections: list[str] = []
cursor = 0.0
for clip in sequence["clips"]:
duration = float(clip["duration"])
chunk_lines = [f"[{cursor:g}-{cursor + duration:g}s]"]
if directive:
chunk_lines.append(directive)
for slot in clip["slots"]:
start = float(slot["start"])
end = float(slot["end"])
chunk_lines.append(f"{start:g}-{end:g}s: {slot['text']}")
chunk_lines.append(f"overall_soundscape: {sound}")
chunk_lines.append(f"non_diegetic_music: {music_text}")
sections.append("\n".join(chunk_lines))
cursor += duration
return "\n\n".join(sections).strip()
class DumasH3SequencePromptBuilder:
"""Build one Timeline prompt from clip tabs and clip-local time slots."""
DESCRIPTION = (
"DUMAS H3 prompt editor. Define clips and local time slots in the UI; "
"the node emits one absolute Timeline prompt for Sequence Prompt."
)
CATEGORY = "DUMAS H3/Prompt"
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("sequence_prompt",)
FUNCTION = "build"
@classmethod
def INPUT_TYPES(cls):
state = json.dumps(default_sequence(), separators=(",", ":"))
return {
"required": {
"clip_count": ("INT", {"default": 2, "min": 1, "max": MAX_CLIPS, "step": 1}),
"default_chunk_seconds": ("FLOAT", {"default": DEFAULT_CLIP_SECONDS, "min": 0.1, "max": 30.0, "step": 0.1}),
"shared_directive": ("STRING", {"default": "", "multiline": True}),
"timeline_json": ("STRING", {"default": state, "multiline": True}),
"soundscape": ("STRING", {"default": "", "multiline": True}),
"music": ("STRING", {"default": "", "multiline": True}),
}
}
def build(self, clip_count, default_chunk_seconds, shared_directive, timeline_json, soundscape, music):
sequence = normalize_sequence(
timeline_json,
clip_count=int(clip_count),
default_seconds=float(default_chunk_seconds),
)
return (format_sequence_prompt(
shared_directive=shared_directive,
sequence=sequence,
soundscape=soundscape,
music=music,
),)
NODE_CLASS_MAPPINGS = {"DumasH3SequencePromptBuilder": DumasH3SequencePromptBuilder}
NODE_DISPLAY_NAME_MAPPINGS = {
"DumasH3SequencePromptBuilder": "DUMAS H3 Sequence Prompt Builder",
}