diff --git a/README.md b/README.md index 1562fcf..f42d311 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # DumasNodes -Generic ComfyUI utility nodes under the Dumas brand. The repository contains -image, JSON and video utilities and intentionally has no model-specific video -sampler dependency. +Generic ComfyUI utility nodes under the Dumas brand. MiniMax H3-specific +nodes are grouped under `Dumas/MiniMax H3/...` so they can be installed +alongside the maintained upstream `ComfyUI-H3-Continuum` pack without +overriding its node IDs. ## Sequential video assembly @@ -30,7 +31,7 @@ or accept a re-encoded workflow. ## Project rendering -## MiniMax-H3 tiled latent upscale +## MiniMax H3 tiled latent upscale `DumasMMH3TiledLatentUpscale` is a thin integration node for the native `MMH3UltimateUpscale` node used by the MiniMax-H3 workflow. It accepts the @@ -56,6 +57,30 @@ current socket types are `H3_UPSCALE_PARAM`, `H3_TEMPORAL_PARAM`, and `H3_SPATIAL_PARAM`; these are deliberately used unchanged for workflow compatibility. +### DUMAS H3 Sequence Prompt Builder + +`DumasH3SequencePromptBuilder` provides the tabbed, timeline-oriented prompt +editor used for multi-chunk H3 jobs. It emits one absolute sequence prompt, +the chunk count, the shared chunk duration, and a per-chunk reference bundle. +Reference images are inserted as `@R1` through `@R9`; routing is based on the +shared directive, soundscape, music, and each chunk's full prompt text. + +The prompt builder intentionally uses the `ReferenceImagesV39Bundle` class +from the installed upstream H3 Continuum pack. Install that pack alongside +DumasNodes; DumasNodes does not copy or replace the upstream reference-image +implementation. + +### DUMAS H3 Continuum Tiled Latent Upscale (Per Clip) + +`DumasH3ContinuumTiledLatentUpscalePerClip` applies the native H3 tiled +upscale independently to each physical Continuum clip. It accepts the +Continuum `refine_context` and uses the upstream refine bridge when no direct +conditioning is connected. Its sampler, scheduler, sigma input, steps, +denoise, CFG, and noise seed are separate from the base generation pass. +Connected sockets take precedence over node defaults. The node preserves one +LATENT list item per physical clip to avoid concatenating a long sequence +into one oversized VRAM allocation. + The CSGlide `Render Project` control is the generation orchestrator: it renders clips sequentially, adopts each completed render, extracts the previous clip's tail, fills the next clip's `CONTINUE FROM` guide, and waits before advancing. diff --git a/__init__.py b/__init__.py index fed49b3..da8f9bc 100644 --- a/__init__.py +++ b/__init__.py @@ -19,18 +19,30 @@ from .dumas_minimax_nodes import ( NODE_CLASS_MAPPINGS as MINIMAX_NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS as MINIMAX_NODE_DISPLAY_NAME_MAPPINGS, ) +from .dumas_h3_prompt_nodes import ( + NODE_CLASS_MAPPINGS as H3_PROMPT_NODE_CLASS_MAPPINGS, + NODE_DISPLAY_NAME_MAPPINGS as H3_PROMPT_NODE_DISPLAY_NAME_MAPPINGS, +) +from .dumas_h3_upscale_nodes import ( + NODE_CLASS_MAPPINGS as H3_UPSCALE_NODE_CLASS_MAPPINGS, + NODE_DISPLAY_NAME_MAPPINGS as H3_UPSCALE_NODE_DISPLAY_NAME_MAPPINGS, +) NODE_CLASS_MAPPINGS = {} NODE_CLASS_MAPPINGS.update(JSON_NODE_CLASS_MAPPINGS) NODE_CLASS_MAPPINGS.update(IMAGE_NODE_CLASS_MAPPINGS) NODE_CLASS_MAPPINGS.update(VIDEO_NODE_CLASS_MAPPINGS) NODE_CLASS_MAPPINGS.update(MINIMAX_NODE_CLASS_MAPPINGS) +NODE_CLASS_MAPPINGS.update(H3_PROMPT_NODE_CLASS_MAPPINGS) +NODE_CLASS_MAPPINGS.update(H3_UPSCALE_NODE_CLASS_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS = {} NODE_DISPLAY_NAME_MAPPINGS.update(JSON_NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(IMAGE_NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(VIDEO_NODE_DISPLAY_NAME_MAPPINGS) NODE_DISPLAY_NAME_MAPPINGS.update(MINIMAX_NODE_DISPLAY_NAME_MAPPINGS) +NODE_DISPLAY_NAME_MAPPINGS.update(H3_PROMPT_NODE_DISPLAY_NAME_MAPPINGS) +NODE_DISPLAY_NAME_MAPPINGS.update(H3_UPSCALE_NODE_DISPLAY_NAME_MAPPINGS) WEB_DIRECTORY = "./js" diff --git a/dumas_h3_prompt_nodes.py b/dumas_h3_prompt_nodes.py new file mode 100644 index 0000000..6d30249 --- /dev/null +++ b/dumas_h3_prompt_nodes.py @@ -0,0 +1,248 @@ +"""UI-backed sequence prompt construction for H3 Continuum.""" + +from __future__ import annotations + +import json +import re +from typing import Any, Mapping + +import sys + + +REFERENCE_IMAGES_V39_TYPE = "H3_CONTINUUM_REFERENCE_IMAGES_V39" + + +def _continuum_reference_bundle_type(): + """Find the upstream H3 bundle class without copying its implementation.""" + candidates = [ + module for name, module in list(sys.modules.items()) + if module is not None and name.endswith(".v3.reference_images_v39") + ] + try: + import importlib + candidates.append(importlib.import_module("v3.reference_images_v39")) + except Exception: + pass + for module in candidates: + bundle = getattr(module, "ReferenceImagesV39Bundle", None) + if bundle is not None: + return bundle + raise RuntimeError( + "DUMAS H3 Sequence Prompt Builder requires the original " + "ComfyUI-H3-Continuum reference_images_v39 module to be installed." + ) + + +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", "INT", "FLOAT", REFERENCE_IMAGES_V39_TYPE) + RETURN_NAMES = ("sequence_prompt", "chunk_count", "chunk_duration", "reference_images") + 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}), + }, + "optional": { + f"reference_image_{index}": ("IMAGE", { + "display_name": f"Reference Image {index} (@R{index})", + "tooltip": f"Fixed Reference Image {index}; use @R{index} in a clip prompt.", + }) for index in range(1, 10) + } + } + + def build( + self, + clip_count, + default_chunk_seconds, + shared_directive, + timeline_json, + soundscape, + music, + **kwargs, + ): + sequence = normalize_sequence( + timeline_json, + clip_count=int(clip_count), + default_seconds=float(default_chunk_seconds), + ) + selectors = [] + directive = str(shared_directive or "").strip() + sound = str(soundscape or "").strip() or "N/A" + music_text = str(music or "").strip() or "N/A" + for reference_index in range(1, 10): + chunks = [] + for chunk_index, clip in enumerate(sequence["clips"], start=1): + # Match against the same per-chunk content emitted by + # format_sequence_prompt, not only the editable shot lines. + text = "\n".join([ + directive, + *(str(slot.get("text", "")) for slot in clip["slots"]), + f"overall_soundscape: {sound}", + f"non_diegetic_music: {music_text}", + ]) + if kwargs.get(f"reference_image_{reference_index}") is not None and re.search( + rf"@(R{reference_index}|image{reference_index})(?![A-Za-z0-9_])", + text, + re.IGNORECASE, + ): + chunks.append(chunk_index) + selectors.append(",".join(map(str, chunks)) or "off") + images = tuple(kwargs.get(f"reference_image_{index}") for index in range(1, 10)) + reference_bundle = _continuum_reference_bundle_type()( + images=images, + reference_use="Per chunk", + selectors=tuple(selectors), + ) + return ( + format_sequence_prompt( + shared_directive=shared_directive, + sequence=sequence, + soundscape=soundscape, + music=music, + ), + len(sequence["clips"]), + float(default_chunk_seconds), + reference_bundle, + ) + + +NODE_CLASS_MAPPINGS = {"DumasH3SequencePromptBuilder": DumasH3SequencePromptBuilder} +NODE_DISPLAY_NAME_MAPPINGS = { + "DumasH3SequencePromptBuilder": "DUMAS H3 Sequence Prompt Builder", +} diff --git a/dumas_h3_upscale_nodes.py b/dumas_h3_upscale_nodes.py new file mode 100644 index 0000000..751e543 --- /dev/null +++ b/dumas_h3_upscale_nodes.py @@ -0,0 +1,374 @@ +"""Per-clip tiled latent upscaling for MiniMax H3 Continuum. + +Continuum exposes physical video groups as a LATENT list. The native +``MMH3UltimateUpscale`` node operates on one H3 latent at a time, so this +adapter deliberately applies it independently to each physical clip instead +of concatenating the clips into one larger VRAM allocation. +""" + +from __future__ import annotations + +from typing import Any + + +def _sampler_options() -> list[str]: + try: + import comfy.samplers + + return list(comfy.samplers.KSampler.SAMPLERS) + except Exception: # pragma: no cover - exercised on a ComfyUI host + return ["euler", "euler_ancestral", "res_multistep", "sa_solver"] + + +def _scheduler_options() -> list[str]: + try: + import comfy.samplers + + return list(comfy.samplers.KSampler.SCHEDULERS) + except Exception: # pragma: no cover - exercised on a ComfyUI host + return ["simple", "beta", "sgm_uniform", "karras", "exponential"] + + +def _continuum_helper(name: str): + """Resolve a helper from the installed upstream H3 Continuum package.""" + import sys + for module_name, module in list(sys.modules.items()): + if module is not None and module_name.endswith(".v3.second_pass"): + helper = getattr(module, name, None) + if helper is not None: + return helper + try: + import importlib + return getattr(importlib.import_module("v3.second_pass"), name) + except Exception as exc: + raise RuntimeError( + "DUMAS H3 per-clip upscale requires the original " + "ComfyUI-H3-Continuum second_pass module." + ) from exc + + +def _native_node(name: str): + try: + import nodes + + return (getattr(nodes, "NODE_CLASS_MAPPINGS", {}) or {}).get(name) + except Exception: + return None + + +def _unwrap_node_output(value: Any) -> Any: + """Extract values returned by ComfyUI's newer NodeOutput API. + + The v3 API wraps node results in ``NodeOutput`` while legacy nodes expect + the underlying tensor/object directly. Keep this adapter compatible with + both APIs without importing ComfyUI internals. + """ + + result = getattr(value, "result", None) + if result is None and not hasattr(value, "result"): + return value + return result + + +def _unwrap_single_output(value: Any, name: str) -> Any: + marker = object() + result = getattr(value, "result", marker) + # Ordinary tuples are valid values for the lightweight test doubles and + # for some legacy ComfyUI helpers. Only unwrap an actual NodeOutput-like + # object, identified by its result property. + if result is marker: + return value + if isinstance(result, (tuple, list)): + if len(result) != 1: + raise ValueError(f"{name} returned {len(result)} outputs; expected one") + return result[0] + return result + + +def _call_native(node_class, kwargs: dict[str, Any]): + """Invoke a ComfyUI node through its declared FUNCTION contract. + + ComfyUI node classes do not have a universal Python method name. The live + MiniMax node currently declares its executor through ``FUNCTION`` rather + than exposing a method named ``upscale``. + """ + + instance = node_class() + declared = getattr(node_class, "FUNCTION", None) + candidates = [] + if isinstance(declared, str) and declared: + candidates.append(declared) + candidates.extend(("execute", "run", "process", "sample", "upscale")) + seen = set() + for name in candidates: + if name in seen: + continue + seen.add(name) + method = getattr(instance, name, None) + if callable(method): + return _unwrap_node_output(method(**kwargs)) + raise RuntimeError( + "MMH3UltimateUpscale exposes no callable ComfyUI FUNCTION method " + f"(declared={declared!r})" + ) + + +def _one(value: Any, name: str) -> Any: + """Unwrap ComfyUI list inputs while rejecting ambiguous values.""" + + if isinstance(value, list): + if len(value) != 1: + raise ValueError(f"{name} must contain one shared value") + return value[0] + return value + + +def _clips(value: Any) -> list[Any]: + if isinstance(value, list): + return value + return [value] + + +def _per_clip(value: Any, count: int, name: str) -> list[Any]: + values = _clips(value) + if len(values) == 1: + return values * count + if len(values) != count: + raise ValueError(f"{name} has {len(values)} values for {count} clips") + return values + + +class DumasH3ContinuumTiledLatentUpscalePerClip: + """Upscale each Continuum physical clip separately with tiled H3 latent work. + + This is intentionally a thin adapter around ComfyUI's native + ``MMH3UltimateUpscale`` implementation. It does not vendor or replace + the model-specific implementation; it only supplies an independent + sampler/scheduler/noise/CFG schedule for each clip and preserves list + boundaries so clips never become one oversized VRAM job. + """ + + DESCRIPTION = ( + "Apply native MiniMax H3 tiled latent upscaling independently to every " + "Continuum physical clip. Clips remain separate LATENT list items." + ) + SEARCH_ALIASES = [ + "H3 tiled upscale per clip", + "MiniMax H3 clip upscale", + "Continuum latent upscale", + ] + + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "model": ("MODEL",), + "clip": ("CLIP",), + "video_latents": ("LATENT",), + "assembly_plan": ("H3_CONTINUUM_ASSEMBLY_PLAN",), + "refine_context": ("H3_CONTINUUM_REFINE_CONTEXT",), + "latent_upscale_param": ("H3_UPSCALE_PARAM",), + "spatial_split_param": ("H3_SPATIAL_PARAM",), + "upscale_sampler": ( + _sampler_options(), + {"default": "euler_ancestral"}, + ), + "upscale_scheduler": ( + _scheduler_options(), + {"default": "simple"}, + ), + "upscale_steps": ("INT", {"default": 3, "min": 1, "max": 50}), + "upscale_denoise": ( + "FLOAT", + {"default": 0.2, "min": 0.0, "max": 1.0, "step": 0.01}, + ), + "upscale_cfg": ( + "FLOAT", + {"default": 1.0, "min": 0.0, "max": 20.0, "step": 0.1}, + ), + "upscale_noise_seed": ( + "INT", + { + "default": 363742558835919, + "min": 0, + "max": 0xFFFFFFFFFFFFFFFF, + "control_after_generate": True, + }, + ), + }, + "optional": { + "conditioning": ("CONDITIONING",), + "video_vae": ("VAE",), + "negative": ("CONDITIONING",), + "temporal_split_param": ("H3_TEMPORAL_PARAM",), + "upscale_sigmas": ("SIGMAS",), + }, + } + + INPUT_IS_LIST = True + RETURN_TYPES = ("LATENT", "STRING") + RETURN_NAMES = ("upscaled_video_latents", "report") + OUTPUT_IS_LIST = (True, False) + FUNCTION = "upscale" + CATEGORY = "Dumas/MiniMax H3/Upscale" + + def upscale( + self, + model, + clip, + video_latents, + assembly_plan, + refine_context, + latent_upscale_param, + spatial_split_param, + upscale_sampler, + upscale_scheduler, + upscale_steps, + upscale_denoise, + upscale_cfg, + upscale_noise_seed, + conditioning=None, + video_vae=None, + negative=None, + temporal_split_param=None, + upscale_sigmas=None, + ): + native = _native_node("MMH3UltimateUpscale") + random_noise = _native_node("RandomNoise") + sampler_select = _native_node("KSamplerSelect") + scheduler = _native_node("BasicScheduler") + missing = [ + name + for name, value in ( + ("MMH3UltimateUpscale", native), + ("RandomNoise", random_noise), + ("KSamplerSelect", sampler_select), + ("BasicScheduler", scheduler), + ) + if value is None + ] + if missing: + raise RuntimeError("Missing native ComfyUI nodes: " + ", ".join(missing)) + + latents = _clips(video_latents) + if not latents: + raise ValueError("video_latents must contain at least one clip") + if conditioning is None: + # The Continuum sampler's refine context is a runtime contract, not + # a Core CONDITIONING value. Reuse Continuum's own bridge so each + # physical clip receives its captured prompt/reference conditioning + # and its context-aware model clone at the target geometry. + prepare_physical_refine_groups = _continuum_helper("prepare_physical_refine_groups") + prepared = prepare_physical_refine_groups( + model=_one(model, "model"), + clip=_one(clip, "clip"), + video_latents=latents, + assembly_plan=_one(assembly_plan, "assembly_plan"), + refine_context=_one(refine_context, "refine_context"), + video_vae=( + None if video_vae is None else _one(video_vae, "video_vae") + ), + ) + models = prepared["group_models"] + conditionings = prepared["conditioning"] + if len(models) != len(latents) or len(conditionings) != len(latents): + raise ValueError( + "refine context bridge did not produce one model and conditioning " + "value per physical clip" + ) + else: + models = _per_clip(model, len(latents), "model") + conditionings = _per_clip(conditioning, len(latents), "conditioning") + negatives = ( + [None] * len(latents) + if negative is None + else _per_clip(negative, len(latents), "negative") + ) + temporal = ( + [None] * len(latents) + if temporal_split_param is None + else _per_clip(temporal_split_param, len(latents), "temporal_split_param") + ) + sigma_values = ( + [None] * len(latents) + if upscale_sigmas is None + else _per_clip(upscale_sigmas, len(latents), "upscale_sigmas") + ) + + sampler_name = str(_one(upscale_sampler, "upscale_sampler")) + scheduler_name = str(_one(upscale_scheduler, "upscale_scheduler")) + steps = int(_one(upscale_steps, "upscale_steps")) + denoise = float(_one(upscale_denoise, "upscale_denoise")) + cfg = float(_one(upscale_cfg, "upscale_cfg")) + seed = int(_one(upscale_noise_seed, "upscale_noise_seed")) + upscale_param = _one(latent_upscale_param, "latent_upscale_param") + spatial_param = _one(spatial_split_param, "spatial_split_param") + + outputs = [] + reports = [] + for index, (clip_latent, clip_model, clip_conditioning) in enumerate( + zip(latents, models, conditionings), start=1 + ): + samples = clip_latent.get("samples") if isinstance(clip_latent, dict) else None + if samples is None or getattr(samples, "ndim", None) != 5: + raise ValueError(f"clip {index} is not a 5-D H3 LATENT") + noise = _unwrap_single_output( + random_noise().get_noise(seed + index - 1), "RandomNoise" + ) + sampler = _unwrap_single_output( + sampler_select().get_sampler(sampler_name), "KSamplerSelect" + ) + sigmas = sigma_values[index - 1] + if sigmas is None: + sigmas = _unwrap_single_output( + scheduler().get_sigmas( + clip_model, scheduler_name, steps, denoise + ), + "BasicScheduler", + ) + kwargs = { + "model": clip_model, + "conditioning": clip_conditioning, + "latent": clip_latent, + "noise": noise, + "sampler": sampler, + "sigmas": sigmas, + "cfg": cfg, + "latent_upscale_param": upscale_param, + "temporal_split_param": temporal[index - 1], + "spatial_split_param": spatial_param, + } + if negatives[index - 1] is not None: + kwargs["negative"] = negatives[index - 1] + result = _call_native(native, kwargs) + if not isinstance(result, (tuple, list)) or not result: + raise RuntimeError(f"native upscale returned no output for clip {index}") + output = result[0] + output_samples = output.get("samples") if isinstance(output, dict) else None + if output_samples is None or output_samples.ndim != 5: + raise RuntimeError(f"native upscale returned an invalid clip {index}") + if output_samples.shape[2] != samples.shape[2]: + raise RuntimeError(f"tiled upscale changed temporal length for clip {index}") + outputs.append(output) + reports.append( + f"clip {index}: {tuple(samples.shape)} -> {tuple(output_samples.shape)}" + ) + + return ( + outputs, + "H3 Continuum per-clip tiled upscale; " + f"clips={len(outputs)}, sampler={sampler_name}, scheduler={scheduler_name}, " + f"steps={steps}, denoise={denoise:g}, cfg={cfg:g}, seed_base={seed}; " + + "; ".join(reports), + ) + + +NODE_CLASS_MAPPINGS = { + "DumasH3ContinuumTiledLatentUpscalePerClip": DumasH3ContinuumTiledLatentUpscalePerClip, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "DumasH3ContinuumTiledLatentUpscalePerClip": ( + "DUMAS H3 Continuum Tiled Latent Upscale (Per Clip)" + ), +} diff --git a/dumas_minimax_nodes.py b/dumas_minimax_nodes.py index be1887d..9d717b9 100644 --- a/dumas_minimax_nodes.py +++ b/dumas_minimax_nodes.py @@ -71,7 +71,7 @@ class DumasMMH3TiledLatentUpscale: RETURN_TYPES = ("LATENT", "STRING") RETURN_NAMES = ("upscaled_latent", "info") FUNCTION = "upscale" - CATEGORY = "Dumas/MiniMax" + CATEGORY = "Dumas/MiniMax H3/Upscale" def upscale( self, model, conditioning, latent, latent_upscale_param, @@ -146,5 +146,5 @@ NODE_CLASS_MAPPINGS = { } NODE_DISPLAY_NAME_MAPPINGS = { - "DumasMMH3TiledLatentUpscale": "Dumas MiniMax-H3 Tiled Latent Upscale", + "DumasMMH3TiledLatentUpscale": "DUMAS MiniMax-H3 Tiled Latent Upscale", } diff --git a/js/sequence_prompt_builder.js b/js/sequence_prompt_builder.js new file mode 100644 index 0000000..5d9e80e --- /dev/null +++ b/js/sequence_prompt_builder.js @@ -0,0 +1,657 @@ +import { app } from "../../scripts/app.js"; + +const CLASS = "DumasH3SequencePromptBuilder"; +const MAX_CLIPS = 16; +const FIXED_TEXT_WIDGETS = [ + ["shared_directive", 84], + ["soundscape", 60], + ["music", 60], +]; +const DURATION_OPTIONS = [ + ["1–5", 5, 2, 0.21], ["6–22", 22, 7, 0.92], ["23–39", 39, 12, 1.63], + ["40–56", 56, 17, 2.33], ["57–73", 73, 22, 3.04], ["74–90", 90, 27, 3.75], + ["91–107", 107, 32, 4.46], ["108–124", 124, 37, 5.17], ["125–141", 141, 42, 5.88], + ["142–158", 158, 47, 6.58], ["159–175", 175, 52, 7.29], ["176–192", 192, 57, 8.00], + ["193–209", 209, 62, 8.71], ["210–226", 226, 67, 9.42], ["227–243", 243, 72, 10.13], + ["244–260", 260, 77, 10.83], ["261–277", 277, 82, 11.54], ["278–294", 294, 87, 12.25], + ["295–311", 311, 92, 12.96], ["312–328", 328, 97, 13.67], ["329–345", 345, 102, 14.38], + ["346–362", 362, 107, 15.08], +].map(([requested, actual, latent, labelSeconds]) => ({ + requested, actual, latent, labelSeconds, seconds: actual / 24, +})); +const CAMERA_HELPERS = [ + ["Push / Pull", ["The camera pushes in", "The camera pulls out"]], + ["Zoom", ["The camera zooms in", "The camera zooms out"]], + ["Pan", ["The camera pans left", "The camera pans right"]], + ["Truck", ["The camera trucks left", "The camera trucks right"]], + ["Tilt", ["The camera tilts up", "The camera tilts down"]], + ["Pedestal", ["The camera pedestals up", "The camera pedestals down"]], + ["Movement", ["The camera holds a static shot", "The camera arcs around the subject", "The camera tracks the subject"]], + ["POV", ["The camera holds the subject's point of view"]], + ["Roll", ["The camera rolls clockwise", "The camera rolls counterclockwise"]], + ["Shake", ["The camera shakes slightly", "The camera shakes strongly"]], +]; +const get = (node, name) => node.widgets?.find((widget) => widget.name === name); +const el = (tag, text = "") => { const item = document.createElement(tag); item.textContent = text; return item; }; + +function defaultState(count, seconds) { + return { version: 1, clips: Array.from({ length: count }, () => ({ + duration: seconds, slots: [{ start: 0, end: seconds, text: "" }], prompt: "", + })) }; +} + +function readState(widget, count, seconds) { + try { + const value = JSON.parse(String(widget?.value || "")); + if (Array.isArray(value.clips)) return value; + } catch (_) { /* replace malformed editor state with a usable default */ } + return defaultState(count, seconds); +} + +function hide(widget) { + if (!widget) return; + widget.type = "hidden"; widget.hidden = true; widget.options ||= {}; + widget.options.hidden = true; widget.computeSize = () => [0, 0]; widget.draw = () => {}; +} + +function configure(node) { + if (node?.comfyClass !== CLASS || typeof document === "undefined" || typeof node.addDOMWidget !== "function") return; + if (node.__dumasSequencePromptBuilder) return node.__dumasSequencePromptBuilder(); + const countWidget = get(node, "clip_count"); + const secondsWidget = get(node, "default_chunk_seconds"); + const stateWidget = get(node, "timeline_json"); + if (!countWidget || !secondsWidget || !stateWidget) return; + hide(secondsWidget); + hide(stateWidget); + FIXED_TEXT_WIDGETS.forEach(([name, height]) => { + const widget = get(node, name); + if (!widget) return; + widget.options ||= {}; + widget.options.multiline = true; + widget.__dumasFixedHeight = height; + widget.computeSize = (width) => [ + Math.max(300, Number(width) || Number(node.size?.[0]) || 420), + height, + ]; + }); + const panel = el("div"); + panel.style.cssText = "box-sizing:border-box;position:relative;width:calc(100% + 14px);margin-left:-7px;min-width:420px;padding:9px;color:#d9e3dc;background:#292929;border:1px solid #454545;border-radius:4px;font:12px Arial;pointer-events:auto"; + const host = el("div"); host.style.cssText = "box-sizing:border-box;pointer-events:auto;background:#292929;width:100%"; host.append(panel); + ["pointerdown", "mousedown", "mouseup", "dblclick"].forEach((name) => host.addEventListener(name, (event) => event.stopPropagation())); + const display = node.addDOMWidget("clip_tabs", "dumas_h3_clip_tabs", host, { serialize: false }); + if (!node.__dumasSequenceResizeWrapped) { + const previousResize = node.onResize; + node.onResize = function (size) { + if (this.__dumasSequenceMinHeight > 0 && size[1] < this.__dumasSequenceMinHeight) { + size[1] = this.__dumasSequenceMinHeight; + } + return previousResize?.apply(this, arguments); + }; + node.__dumasSequenceResizeWrapped = true; + } + display.options ||= {}; display.options.serialize = false; + let active = 0; + let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4); + let promptText = null; + // Writing the hidden widget value is safe while a textarea owns focus. + // Calling its callback is not: ComfyUI callbacks redraw the DOM widget, + // which destroys the focused textarea during blur/pointer transitions. + const persistState = () => { + stateWidget.value = JSON.stringify(state); + node.setDirtyCanvas?.(true, true); + }; + const setState = () => { + persistState(); + stateWidget.callback?.(stateWidget.value); + }; + const input = (type, value, onChange) => { + const item = el("input"); item.type = type; item.value = value; + item.style.cssText = type === "range" + ? "width:100%;box-sizing:border-box;margin:2px 0;accent-color:#79c98a" + : "width:100%;box-sizing:border-box;margin:2px 0;color:#111"; + item.onchange = () => onChange(item.value); return item; + }; + const addSlot = (clip, afterIndex = clip.slots.length - 1) => { + const duration = Number(clip.duration) || 0.1; + if (!clip.slots.length) { + clip.slots.push({ start: 0, end: duration, text: "" }); + return; + } + const newCount = clip.slots.length + 1; + // Keep a minimum only when the selected duration allows it. The old + // fixed 0.1s floor made later shots collapse at short frame choices. + const minSlice = Math.min(0.1, duration / newCount); + let splitIndex = Math.max(0, Math.min(afterIndex, clip.slots.length - 1)); + const lengthOf = (slot) => Math.max(0, Number(slot.end) - Number(slot.start)); + if (lengthOf(clip.slots[splitIndex]) < minSlice * 2) { + splitIndex = clip.slots.reduce( + (best, slot, index) => lengthOf(slot) > lengthOf(clip.slots[best]) ? index : best, + 0, + ); + } + const target = clip.slots[splitIndex]; + const start = Number(target?.start) || 0; + const end = Math.min(duration, Number(target?.end) || duration); + let split = start + (end - start) / 2; + split = Math.max(start + minSlice, Math.min(end - minSlice, split)); + if (!(split > start && split < end)) { + // This is only reachable for malformed or extremely short state; + // redistribute boundaries evenly so the new shot is still real. + const texts = clip.slots.map((slot) => slot.text || ""); + texts.splice(splitIndex + 1, 0, ""); + clip.slots = texts.map((text, index) => ({ + start: duration * index / newCount, + end: duration * (index + 1) / newCount, + text, + })); + return; + } + target.end = split; + clip.slots.splice(splitIndex + 1, 0, { start: split, end, text: "" }); + }; + const formatTime = (seconds) => `00:${Number(seconds).toFixed(3).padStart(6, "0")}`; + // Keep the user's script as the source of truth, like CSGlide does. The + // slots are only the timeline model used to paint the bar and to locate + // boundaries. Rebuilding the textarea from slots is lossy (and was the + // reason a redraw could erase text that had just been typed). + const formatPromptFromSlots = (clip) => clip.slots.map((slot, index) => + `[Shot ${index + 1}] At ${formatTime(slot.start)}, ${slot.text || ""}`).join("\n\n"); + const promptFor = (clip) => { + if (typeof clip.prompt === "string" && clip.prompt.length) return clip.prompt; + return formatPromptFromSlots(clip); + }; + const parseTime = (value) => { + const parts = String(value || "").split(":").map(Number); + if (parts.some((part) => !Number.isFinite(part))) return NaN; + if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2]; + if (parts.length === 2) return parts[0] * 60 + parts[1]; + return parts[0]; + }; + const parsePromptSlots = (clip, value) => { + const text = String(value || ""); + const markerPattern = /\[Shot\s+\d+\]\s+At\s+((?:\d+(?::\d+){1,2}(?:\.\d+)?|\d+(?:\.\d+)?))\s*,\s*/gi; + const markers = [...text.matchAll(markerPattern)]; + if (!markers.length) return false; + const duration = Math.max(0.1, Number(clip.duration) || 0.1); + const slots = markers.map((marker, index) => { + const rawStart = parseTime(marker[1]); + const start = Number.isFinite(rawStart) + ? Math.min(Math.max(0, rawStart), Math.max(0, duration - 0.1)) + : 0; + const next = markers[index + 1]; + const rawEnd = next ? parseTime(next[1]) : duration; + const end = Math.min(duration, Math.max(start + 0.1, Number.isFinite(rawEnd) ? rawEnd : duration)); + const from = marker.index + marker[0].length; + const to = next ? next.index : text.length; + return { start, end, text: text.slice(from, to).trim() }; + }); + slots.forEach((slot, index) => { + if (index > 0) slot.start = slots[index - 1].end; + if (slot.end <= slot.start) slot.end = Math.min(duration, slot.start + 0.1); + }); + slots[slots.length - 1].end = duration; + clip.slots = slots; + return true; + }; + const syncPrompt = (clip, value) => { + const text = String(value || ""); + if (parsePromptSlots(clip, text)) return true; + // The shot description starts on the same line as the marker in the + // CSGlide-style editor. Only consume the marker prefix through its + // timestamp comma; consuming the whole line would discard text such + // as `[Shot 1] At 00:00.000, the character walks...` when a new shot + // is added and the marker scaffold is rebuilt. + const marks = [...text.matchAll(/\[Shot\s+(\d+)\]\s+At\s+[^,\n]+,\s*/gi)]; + if (!marks.length) { if (clip.slots[0]) clip.slots[0].text = text.trim(); return; } + marks.forEach((mark, index) => { + const slotIndex = Number(mark[1]) - 1; + if (!clip.slots[slotIndex]) return; + const from = mark.index + mark[0].length; + const to = index + 1 < marks.length ? marks[index + 1].index : text.length; + clip.slots[slotIndex].text = text.slice(from, to).trim(); + }); + return false; + }; + const retimePrompt = (text, clip) => { + let index = 0; + return String(text || "").replace(/(\[Shot\s+\d+\]\s+At\s+)[^,\n]+(,?)/gi, (whole, prefix, suffix) => { + const start = clip.slots[index++]?.start ?? 0; + return `${prefix}${formatTime(start)}${suffix}`; + }); + }; + const commitPrompt = (clip, value, redraw = false) => { + clip.prompt = String(value ?? ""); + syncPrompt(clip, clip.prompt); + // Do not invoke the widget callback for ordinary text edits. The + // callback redraws the DOM and can run while the textarea is blurring + // to a timeline handle, exactly the interaction CSGlide avoids. + persistState(); + if (redraw) render(); + }; + const rebuildPromptAfterStructureChange = (clip) => { + // The caller syncs the current text before changing the slot list. + // Do not parse clip.prompt again here: after adding a shot it still + // has the old marker count, and reparsing would discard the new slot. + clip.prompt = formatPromptFromSlots(clip); + }; + const render = () => { + const count = Math.max(1, Math.min(MAX_CLIPS, Number(countWidget.value) || 1)); + const currentSeconds = Number(secondsWidget.value) || DURATION_OPTIONS[6].seconds; + const selectedDuration = DURATION_OPTIONS.reduce((best, option) => + Math.abs(option.seconds - currentSeconds) < Math.abs(best.seconds - currentSeconds) ? option : best, + DURATION_OPTIONS[6]); + if (Number(secondsWidget.value) !== selectedDuration.seconds) secondsWidget.value = selectedDuration.seconds; + const chunkDuration = selectedDuration.seconds; + while (state.clips.length < count) state.clips.push({ duration: chunkDuration, slots: [] }); + state.clips = state.clips.slice(0, count); active = Math.min(active, count - 1); + panel.replaceChildren(); + const tabs = el("div"); tabs.style.cssText = "display:flex;flex-wrap:wrap;gap:4px;margin-bottom:8px;border-bottom:1px solid #444;padding-bottom:6px"; + state.clips.forEach((clip, index) => { + const button = el("button", `Clip ${index + 1}`); button.type = "button"; button.disabled = index === active; + button.style.cssText = `padding:3px 10px;border:1px solid ${index === active ? "#8bc99b" : "#555"};border-radius:3px;background:${index === active ? "#4b6f55" : "#333"};color:${index === active ? "#fff" : "#bbb"};cursor:pointer`; + button.onclick = () => { active = index; render(); }; tabs.append(button); + }); + panel.append(tabs); + const clip = state.clips[active]; + // All clips share the one authoritative project duration widget. + const duration = chunkDuration; + clip.duration = duration; + if (clip.slots.length) clip.slots[clip.slots.length - 1].end = duration; + promptText = promptFor(clip); + const durationControl = el("div"); + durationControl.style.cssText = "display:flex;align-items:center;gap:8px;margin:0 0 8px;color:#b7d6bf"; + const durationLabel = el("span", "Chunk duration / frame choice"); + durationLabel.style.cssText = "font-weight:bold;white-space:nowrap"; + const durationSelect = document.createElement("select"); + durationSelect.style.cssText = "flex:1;min-width:0;box-sizing:border-box;padding:4px 6px;color:#e8e8e8;background:#171717;border:1px solid #596b60;border-radius:4px;font:11px monospace"; + DURATION_OPTIONS.forEach((option) => { + const item = document.createElement("option"); + item.value = String(option.seconds); + item.textContent = `${option.actual} Real Frames - ${option.latent} Latent Frames - ${option.labelSeconds.toFixed(2)}s`; + item.selected = option === selectedDuration; + durationSelect.append(item); + }); + durationSelect.title = "Choose a native H3 frame length; actual frames are encoded from the latent-frame choice."; + durationSelect.onchange = () => { + secondsWidget.value = Number(durationSelect.value); + secondsWidget.callback?.(secondsWidget.value); + }; + durationControl.append(durationLabel, durationSelect); + panel.append(durationControl); + const durationRow = el("div"); durationRow.style.cssText = "display:flex;align-items:center;gap:8px;margin:4px 0 6px;color:#b7d6bf"; + durationRow.append(el("strong", `Clip ${active + 1}`), el("span", `· ${selectedDuration.actual} actual frames · ${selectedDuration.latent} latent frames · ${selectedDuration.labelSeconds.toFixed(2)}s shared chunk duration`)); panel.append(durationRow); + const timeline = el("div"); timeline.style.cssText = "position:relative;height:38px;margin:8px 0 4px;background:#171717;border-radius:4px;overflow:hidden;border:1px solid #555"; + const palette = ["#4b9b67", "#4d7fb1", "#a87945", "#8c5aa8", "#a44f5d", "#3f8e8a"]; + let prompt = null; + let promptSelectionStart = 0; + let promptSelectionEnd = 0; + let autocomplete = null; + let autocompleteRange = null; + let promptFrame = null; + let suggestionLayer = null; + let encoder = null; + const focusPrompt = () => { + if (!prompt) return; + try { + prompt.focus({ preventScroll: true }); + } catch (_) { + prompt.focus(); + } + }; + const segments = []; + const handles = []; + const updateTimeline = () => { + clip.slots.forEach((slot, index) => { + const left = Math.max(0, Number(slot.start) / duration * 100); + const width = Math.max(0.1, (Number(slot.end) - Number(slot.start)) / duration * 100); + segments[index].style.left = `${left}%`; + segments[index].style.width = `${width}%`; + segments[index].title = `${Number(slot.start).toFixed(1)}s–${Number(slot.end).toFixed(1)}s`; + }); + handles.forEach(({ handle, slot }) => { handle.style.left = `${Number(slot.end) / duration * 100}%`; }); + }; + clip.slots.forEach((slot, index) => { + const segment = el("button", `Shot ${index + 1}\n${(slot.text || "unscripted").slice(0, 18)}`); segment.type = "button"; + segment.style.cssText = `position:absolute;top:0;height:100%;border:0;border-right:2px solid #292929;color:white;background:${palette[index % palette.length]};white-space:pre-wrap;font-size:10px;overflow:hidden;padding:2px;cursor:pointer`; + segment.onclick = (event) => { + event.preventDefault(); event.stopPropagation(); + focusPrompt(); + }; segments.push(segment); timeline.append(segment); + }); + clip.slots.slice(0, -1).forEach((slot, index) => { + const next = clip.slots[index + 1]; + const handle = el("div"); handle.title = `Resize boundary between Shot ${index + 1} and Shot ${index + 2}`; + handle.style.cssText = "position:absolute;top:0;width:17px;height:100%;transform:translateX(-50%);background:transparent;border:0;border-radius:3px;cursor:ew-resize;z-index:5;box-sizing:border-box;touch-action:none"; + const ink = el("div"); + ink.style.cssText = "position:absolute;left:7px;top:0;width:2px;height:100%;background:#f1f7f2;border:0;border-radius:1px;pointer-events:none"; + handle.append(ink); + const move = (event) => { + event.preventDefault(); event.stopPropagation(); + const rect = timeline.getBoundingClientRect(); + const point = Math.min(duration - 0.1, Math.max(0.1, ((event.clientX - rect.left) / rect.width) * duration)); + slot.end = point; next.start = point; updateTimeline(); + }; + let dragging = false; + const finish = (event) => { + if (!dragging) return; + dragging = false; + event?.preventDefault?.(); event?.stopPropagation?.(); + document.removeEventListener("pointermove", move, true); + window.removeEventListener("pointerup", finish, true); + document.removeEventListener("mousemove", moveMouse, true); + document.removeEventListener("mouseup", finish, true); + promptText = retimePrompt(promptText ?? prompt?.value ?? clip.prompt ?? "", clip); + commitPrompt(clip, promptText); + // The timeline is already updated in place. Avoid rebuilding + // the panel, which moves the textarea and canvas viewport. + if (prompt) prompt.value = promptText; + if (encoder) encoder.textContent = clip.slots + .map((item, itemIndex) => `Shot ${itemIndex + 1} ${formatTime(item.start)}`) + .join(" "); + }; + const moveMouse = (event) => { if (dragging) move(event); }; + handle.onpointerdown = (event) => { + event.preventDefault(); + event.stopPropagation(); + dragging = true; + // Textarea edits are intentionally committed on change, but a + // drag can happen while it still has focus. Capture them before + // the timeline redraws the prompt from state. + if (prompt) { promptText = prompt.value; clip.prompt = promptText; } + handle.setPointerCapture?.(event.pointerId); + document.addEventListener("pointermove", move, true); + window.addEventListener("pointerup", finish, true); + document.addEventListener("mousemove", moveMouse, true); + document.addEventListener("mouseup", finish, true); + }; + handles.push({ handle, slot }); timeline.append(handle); + }); + updateTimeline(); + panel.append(timeline); + const sliderHint = el("div", "Drag the white handles to resize shots. The bar represents the complete chunk duration."); sliderHint.style.cssText = "color:#aab6ad;margin-bottom:6px;font-size:11px"; panel.append(sliderHint); + + const shotChips = el("div"); shotChips.style.cssText = "display:flex;gap:4px;overflow:hidden;margin:3px 0 5px"; + clip.slots.forEach((slot, index) => { + const chipText = (slot.text || `Shot ${index + 1}`).replace(/\s+/g, " ").trim().slice(0, 31); + const chip = el("button", chipText || `Shot ${index + 1}`); chip.type = "button"; + chip.title = `Jump to Shot ${index + 1}`; + chip.style.cssText = `max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding:3px 8px;border:1px solid ${palette[index % palette.length]};border-radius:4px;background:#1e1e1e;color:#e6e6e6;font:11px Arial;cursor:pointer`; + chip.onclick = (event) => { + event.preventDefault(); event.stopPropagation(); + if (!prompt) return; + const marker = `[Shot ${index + 1}]`; + const position = prompt.value.indexOf(marker); + focusPrompt(); + if (position >= 0) prompt.setSelectionRange(position, position + marker.length); + }; + shotChips.append(chip); + }); + panel.append(shotChips); + const referenceRow = el("div"); referenceRow.style.cssText = "display:flex;gap:8px;overflow:hidden;margin:7px 0 8px;align-items:center;flex-wrap:wrap"; + const connectedReferences = Array.from({ length: 9 }, (_, offset) => offset + 1) + .filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null)); + if (connectedReferences.length) { + connectedReferences.forEach((index) => { + const button = el("button"); button.type = "button"; + button.title = `Insert reference image ${index} as @image${index}`; + button.style.cssText = "display:inline-flex;align-items:center;gap:7px;height:31px;padding:2px 11px 2px 3px;border:2px solid #49c9f5;border-radius:18px;background:#202b31;color:#70d8ff;font:16px monospace;cursor:pointer;white-space:nowrap"; + const thumbnail = document.createElement("img"); + thumbnail.alt = ""; thumbnail.draggable = false; + thumbnail.style.cssText = "width:25px;height:25px;object-fit:cover;border-radius:50%;background:#68727a;display:block"; + const linkId = node.inputs?.find((input) => input.name === `reference_image_${index}`)?.link; + const link = linkId == null ? null : (app.graph?.links?.get?.(linkId) ?? app.graph?.links?.[linkId]); + const source = link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id); + const imageWidget = source?.widgets?.find((widget) => ["image", "filename", "file"].includes(widget.name)); + const imageValue = imageWidget?.value; + if (imageValue && typeof imageValue === "object" && imageValue.filename) { + const params = new URLSearchParams({ filename: imageValue.filename, type: imageValue.type || "input", subfolder: imageValue.subfolder || "" }); + thumbnail.src = `/view?${params.toString()}`; + } else if (typeof imageValue === "string" && imageValue) { + const params = new URLSearchParams({ filename: imageValue, type: "input", subfolder: "" }); + thumbnail.src = `/view?${params.toString()}`; + } else { + thumbnail.src = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='25' height='25' viewBox='0 0 25 25'%3E%3Crect width='25' height='25' fill='%2368727a'/%3E%3Cpath d='M4 19l5-6 3 3 3-4 6 7' fill='none' stroke='%23dce8ed' stroke-width='2'/%3E%3Ccircle cx='17' cy='8' r='2' fill='%23dce8ed'/%3E%3C/svg%3E"; + } + thumbnail.onerror = () => { thumbnail.style.opacity = "0.35"; }; + button.append(thumbnail, el("span", `@image${index}`)); + const rememberPromptSelection = () => { + if (!prompt) return; + promptSelectionStart = prompt.selectionStart ?? prompt.value.length; + promptSelectionEnd = prompt.selectionEnd ?? promptSelectionStart; + }; + button.onpointerdown = (event) => { + rememberPromptSelection(); + event.preventDefault(); + event.stopPropagation(); + }; + button.onmousedown = (event) => { + event.preventDefault(); + event.stopPropagation(); + }; + button.onclick = (event) => { + event.preventDefault(); event.stopPropagation(); + if (!prompt) return; + const start = Math.min(promptSelectionStart, prompt.value.length); + const end = Math.min(Math.max(start, promptSelectionEnd), prompt.value.length); + const token = `@R${index} `; + prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end); + const caret = start + token.length; + focusPrompt(); prompt.setSelectionRange(caret, caret); + promptSelectionStart = caret; promptSelectionEnd = caret; + promptText = prompt.value; clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); persistState(); + }; + referenceRow.append(button); + }); + } + panel.append(referenceRow); + const promptLabel = el("div", "PROMPT SCRIPT"); promptLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:4px 0 3px"; panel.append(promptLabel); + prompt = el("textarea"); prompt.rows = 10; prompt.value = promptText; prompt.placeholder = "[Shot 1] At 00:00.000, describe the opening action...\n\nType @ at the start of a new line to add the next shot."; prompt.style.cssText = "width:100%;box-sizing:border-box;color:#e8e8e8;background:#171717;border:1px solid #4e4e4e;border-radius:4px;padding:8px;font:12px/1.45 monospace;margin-top:2px;resize:vertical;outline:none"; + const rememberPromptSelection = () => { + promptSelectionStart = prompt.selectionStart ?? prompt.value.length; + promptSelectionEnd = prompt.selectionEnd ?? promptSelectionStart; + }; + const closeAutocomplete = () => { + autocomplete?.remove(); + autocomplete = null; + autocompleteRange = null; + }; + const sourceThumbnail = (index) => { + const thumbnail = document.createElement("img"); + thumbnail.alt = ""; thumbnail.draggable = false; + thumbnail.style.cssText = "width:24px;height:24px;object-fit:cover;border-radius:50%;background:#68727a;display:block;flex:none"; + const linkId = node.inputs?.find((input) => input.name === `reference_image_${index}`)?.link; + const link = linkId == null ? null : (app.graph?.links?.get?.(linkId) ?? app.graph?.links?.[linkId]); + const source = link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id); + const imageWidget = source?.widgets?.find((widget) => ["image", "filename", "file"].includes(widget.name)); + const imageValue = imageWidget?.value; + if (imageValue && typeof imageValue === "object" && imageValue.filename) { + const params = new URLSearchParams({ filename: imageValue.filename, type: imageValue.type || "input", subfolder: imageValue.subfolder || "" }); + thumbnail.src = `/view?${params.toString()}`; + } else if (typeof imageValue === "string" && imageValue) { + const params = new URLSearchParams({ filename: imageValue, type: "input", subfolder: "" }); + thumbnail.src = `/view?${params.toString()}`; + } + thumbnail.onerror = () => { thumbnail.style.opacity = "0.35"; }; + return thumbnail; + }; + const openAutocomplete = (at) => { + closeAutocomplete(); + autocompleteRange = { start: at, end: at + 1 }; + const menu = el("div"); + menu.style.cssText = "display:block;visibility:visible;opacity:1;position:absolute;z-index:100;left:4px;min-width:220px;max-width:310px;padding:5px;background:#25282b;border:1px solid #4b5960;border-radius:8px;box-shadow:0 8px 24px #000b;color:#e7edf0;pointer-events:auto"; + menu.style.top = "0px"; + const connected = Array.from({ length: 9 }, (_, offset) => offset + 1) + .filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null)); + const addOption = (content, title, action) => { + const option = el("button"); option.type = "button"; option.title = title; + option.style.cssText = "display:flex;align-items:center;gap:9px;width:100%;padding:6px 8px;border:0;border-radius:5px;background:transparent;color:#e7edf0;text-align:left;font:12px monospace;cursor:pointer"; + option.onpointerdown = (event) => { event.preventDefault(); event.stopPropagation(); }; + option.onmouseenter = () => { option.style.background = "#3b5661"; }; + option.onmouseleave = () => { option.style.background = "transparent"; }; + option.onclick = (event) => { event.preventDefault(); event.stopPropagation(); action(); }; + option.append(...content); menu.append(option); + }; + connected.forEach((index) => addOption([ + sourceThumbnail(index), + el("span", `@image${index}`), + (() => { const hint = el("span", ``); hint.style.cssText = "margin-left:auto;color:#84939a;font-size:10px"; return hint; })(), + ], `Insert @R${index}`, () => { + const start = autocompleteRange?.start ?? prompt.value.length; + const end = autocompleteRange?.end ?? start; + const token = `@R${index} `; + prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end); + const caret = start + token.length; + focusPrompt(); prompt.setSelectionRange(caret, caret); + promptSelectionStart = caret; promptSelectionEnd = caret; + promptText = prompt.value; clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); persistState(); closeAutocomplete(); + })); + addOption([ + (() => { const icon = el("span", "S"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#397a91;color:#dff7ff;font-weight:bold"; return icon; })(), + el("span", "[Shot " + (clip.slots.length + 1) + "]"), + (() => { const hint = el("span", "new shot"); hint.style.cssText = "margin-left:auto;color:#84939a;font-size:10px"; return hint; })(), + ], "Create the next timed shot", () => { + const start = autocompleteRange?.start ?? prompt.value.length; + prompt.value = prompt.value.slice(0, start) + prompt.value.slice(start + 1); + promptText = prompt.value; clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); addSlot(clip); rebuildPromptAfterStructureChange(clip); + closeAutocomplete(); setState(); render(); + }); + panel.append(menu); + menu.style.left = `${Math.max(5, (suggestionLayer?.offsetLeft || 0) + 4)}px`; + menu.style.top = `${Math.max(5, suggestionLayer?.offsetTop || 0)}px`; + autocomplete = menu; + }; + prompt.oninput = () => { rememberPromptSelection(); promptText = prompt.value; clip.prompt = prompt.value; }; + prompt.onpaste = () => { + // Let the browser insert the pasted text first, then parse its + // shot markers and redraw the track without losing the paste. + setTimeout(() => { + const changed = syncPrompt(clip, prompt.value); + promptText = prompt.value; + clip.prompt = prompt.value; + persistState(); + if (changed) render(); + }, 0); + }; + prompt.onselect = rememberPromptSelection; + prompt.onkeyup = rememberPromptSelection; + prompt.onmouseup = rememberPromptSelection; + prompt.onblur = () => { rememberPromptSelection(); clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); persistState(); }; + prompt.onchange = () => { commitPrompt(clip, prompt.value); }; + prompt.onpointerdown = (event) => event.stopPropagation(); + prompt.onkeydown = (event) => { + if (event.key !== "@") return; + event.preventDefault(); + const start = prompt.selectionStart ?? prompt.value.length; + const end = prompt.selectionEnd ?? start; + prompt.value = prompt.value.slice(0, start) + "@" + prompt.value.slice(end); + prompt.setSelectionRange(start + 1, start + 1); + promptSelectionStart = start; + promptSelectionEnd = start + 1; + promptText = prompt.value; clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); persistState(); + openAutocomplete(start); + }; + promptFrame = el("div"); + promptFrame.style.cssText = "position:relative;overflow:visible;width:100%;box-sizing:border-box"; + suggestionLayer = el("div"); + suggestionLayer.style.cssText = "position:relative;z-index:100;height:0;width:100%;pointer-events:none;overflow:visible"; + promptFrame.append(prompt); + panel.append(suggestionLayer); + panel.append(promptFrame); + promptSelectionStart = prompt.value.length; + promptSelectionEnd = prompt.value.length; + const insertPromptText = (text) => { + if (!prompt) return; + const start = Math.min(promptSelectionStart, prompt.value.length); + const end = Math.min(Math.max(start, promptSelectionEnd), prompt.value.length); + const before = prompt.value.slice(0, start); + const after = prompt.value.slice(end); + const prefix = before && !/\s$/.test(before) ? " " : ""; + const token = `${prefix}${text} `; + prompt.value = before + token + after; + const caret = before.length + token.length; + focusPrompt(); prompt.setSelectionRange(caret, caret); + promptSelectionStart = caret; promptSelectionEnd = caret; + promptText = prompt.value; clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); persistState(); + }; + const helperPanel = el("div"); + helperPanel.style.cssText = "display:flex;flex-wrap:wrap;gap:4px;align-items:center;margin:5px 0 7px;padding:5px 6px;background:#202020;border:1px solid #414141;border-radius:4px"; + const helperTitle = el("span", "CAMERA HELPERS"); + helperTitle.style.cssText = "width:100%;color:#8fa898;font-size:10px;letter-spacing:.08em;margin-bottom:1px"; + helperPanel.append(helperTitle); + CAMERA_HELPERS.forEach(([category, phrases]) => phrases.forEach((phrase) => { + const button = el("button", phrase.replace(/^The camera /, "")); + button.type = "button"; + button.title = `${category}: insert \"${phrase}\"`; + button.style.cssText = "padding:3px 7px;border:1px solid #596b60;border-radius:4px;background:#2b3b31;color:#b9e6c4;font:11px Arial;cursor:pointer;white-space:nowrap"; + button.onpointerdown = (event) => { event.preventDefault(); event.stopPropagation(); }; + button.onmousedown = (event) => { event.preventDefault(); event.stopPropagation(); }; + button.onclick = (event) => { event.preventDefault(); event.stopPropagation(); insertPromptText(phrase); }; + helperPanel.append(button); + })); + panel.insertBefore(helperPanel, promptFrame); + const checkLabel = el("div", "PROMPT CHECK"); checkLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(checkLabel); + const check = el("div", `${clip.slots.length} shot${clip.slots.length === 1 ? "" : "s"} · ${duration.toFixed(1)}s chunk · @ references preserved`); check.style.cssText = "padding:6px 8px;background:#171717;border:1px solid #444;border-radius:4px;color:#9bd0a8;font:11px monospace"; panel.append(check); + const encoderLabel = el("div", "SENT TO ENCODER AS"); encoderLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(encoderLabel); + encoder = el("div", clip.slots.map((slot, index) => `Shot ${index + 1} ${formatTime(slot.start)}`).join(" ")); encoder.style.cssText = "padding:6px 8px;background:#171717;border:1px solid #444;border-radius:4px;color:#7cc4d5;font:11px monospace;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"; panel.append(encoder); + const actions = el("div"); actions.style.cssText = "display:flex;gap:5px;margin-top:5px"; + const add = el("button", "@ Add shot"); add.type = "button"; add.onclick = () => { clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); addSlot(clip); rebuildPromptAfterStructureChange(clip); setState(); render(); }; actions.append(add); + if (clip.slots.length > 1) { const remove = el("button", "Remove last shot"); remove.type = "button"; remove.onclick = () => { clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); clip.slots.pop(); clip.slots.at(-1).end = clip.duration; rebuildPromptAfterStructureChange(clip); setState(); render(); }; actions.append(remove); } + panel.append(actions); + // The editor now contains chips, the prompt check and encoder summary; + // the old slot-count estimate left a large blank tail in the node. + // Measure the actual DOM panel instead of guessing from slot count. + const measuredHeight = Math.ceil(panel.getBoundingClientRect().height || panel.scrollHeight || 0); + display.computeSize = (width) => [Math.max(420, Number(width) || 420), measuredHeight + 2]; + // CSGlide keeps the DOM panel floating but stores a node-level floor + // from measured content. Temporarily hide this widget from the + // ordinary LiteGraph calculation to get the non-panel chrome, then + // add the real panel height and clamp future manual resizes. + const realComputeSize = display.computeSize; + display.computeSize = () => [420, 0]; + const chromeHeight = Number(node.computeSize?.()[1]) || 0; + display.computeSize = realComputeSize; + const width = Math.max(420, Number(node.size?.[0]) || 420); + const desiredHeight = chromeHeight + measuredHeight + 2; + node.__dumasSequenceMinHeight = desiredHeight; + if (typeof node.setSize === "function" + && (Number(node.size?.[0]) !== width || Number(node.size?.[1]) < desiredHeight)) { + node.setSize([width, desiredHeight]); + } + }; + const refresh = () => { state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4); render(); setState(); }; + countWidget.callback = ((previous) => function(...args) { const result = previous?.apply(this, args); refresh(); return result; })(countWidget.callback); + secondsWidget.callback = ((previous) => function(...args) { + const result = previous?.apply(this, args); + const duration = Math.max(0.1, Number(secondsWidget.value) || 0.1); + state.clips.forEach((clip) => { + clip.duration = duration; + clip.slots.forEach((slot) => { + slot.start = Math.min(Number(slot.start) || 0, Math.max(0, duration - 0.1)); + slot.end = Math.min(Math.max(Number(slot.end) || duration, slot.start + 0.1), duration); + }); + }); + setState(); render(); return result; + })(secondsWidget.callback); + stateWidget.callback = ((previous) => function(...args) { const result = previous?.apply(this, args); render(); return result; })(stateWidget.callback); + const previousConnections = node.onConnectionsChange; + node.onConnectionsChange = function(...args) { + const result = previousConnections?.apply(this, args); + render(); + return result; + }; + node.__dumasSequencePromptBuilder = refresh; + refresh(); +} + +app.registerExtension({ + name: "DUMAS.H3.SequencePromptBuilder", + nodeCreated(node) { setTimeout(() => configure(node), 0); }, + loadedGraphNode(node) { setTimeout(() => configure(node), 0); }, + afterConfigureGraph() { for (const node of app.graph?._nodes || []) configure(node); }, +}); + diff --git a/tests/test_dumas_h3_nodes.py b/tests/test_dumas_h3_nodes.py new file mode 100644 index 0000000..5343322 --- /dev/null +++ b/tests/test_dumas_h3_nodes.py @@ -0,0 +1,116 @@ +from __future__ import annotations + +import importlib.util +import pathlib +import sys +import types +import unittest +from dataclasses import dataclass + + +ROOT = pathlib.Path(__file__).parents[1] + + +def load_module(name, path): + spec = importlib.util.spec_from_file_location(name, ROOT / path) + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + spec.loader.exec_module(module) + return module + + +PROMPT = load_module("dumas_h3_prompt_test", "dumas_h3_prompt_nodes.py") +UPSCALE = load_module("dumas_h3_upscale_test", "dumas_h3_upscale_nodes.py") + + +class FakeSamples: + def __init__(self, shape): + self.shape = shape + self.ndim = len(shape) + + +class FakeNoise: + def get_noise(self, seed): + return ("noise", seed) + + +class FakeSampler: + def get_sampler(self, name): + return ("sampler", name) + + +class FakeScheduler: + def get_sigmas(self, model, scheduler, steps, denoise): + return ("sigmas", scheduler, steps, denoise) + + +class FakeUpscale: + FUNCTION = "execute" + calls = [] + + def execute(self, **kwargs): + self.calls.append(kwargs) + source = kwargs["latent"]["samples"] + _, _, frames, height, width = source.shape + return ({"samples": FakeSamples((1, 16, frames, height * 2, width * 2))},) + + +@dataclass(frozen=True) +class FakeReferenceBundle: + images: tuple + reference_use: str + selectors: tuple + + +class H3NodeTests(unittest.TestCase): + def test_prompt_schema_and_full_chunk_reference_routing(self): + reference_module = types.ModuleType("upstream.v3.reference_images_v39") + reference_module.ReferenceImagesV39Bundle = FakeReferenceBundle + sys.modules[reference_module.__name__] = reference_module + try: + node = PROMPT.DumasH3SequencePromptBuilder() + result = node.build( + 2, 4, "Keep @R1 present.", + '{"clips":[{"slots":[{"start":0,"end":4,"text":"Walk."}]},' + '{"slots":[{"start":0,"end":4,"text":"Turn."}]}]}', + "Use @R2 ambience.", "Music.", + reference_image_1=object(), reference_image_2=object(), + ) + self.assertEqual(result[1], 2) + self.assertEqual(result[2], 4.0) + self.assertEqual(result[3].selectors, ("1,2", "1,2") + ("off",) * 7) + self.assertIn("[0-4s]", result[0]) + self.assertIn("[4-8s]", result[0]) + finally: + sys.modules.pop(reference_module.__name__, None) + + def test_per_clip_upscale_uses_connected_sigmas(self): + original = UPSCALE._native_node + FakeUpscale.calls = [] + UPSCALE._native_node = lambda name: { + "MMH3UltimateUpscale": FakeUpscale, + "RandomNoise": FakeNoise, + "KSamplerSelect": FakeSampler, + "BasicScheduler": FakeScheduler, + }.get(name) + try: + node = UPSCALE.DumasH3ContinuumTiledLatentUpscalePerClip() + outputs, report = node.upscale( + model=["model"], clip=["clip"], + video_latents=[{"samples": FakeSamples((1, 16, 85, 48, 84))}], + assembly_plan=["plan"], refine_context=["context"], + latent_upscale_param=["upscale"], spatial_split_param=["spatial"], + upscale_sampler=["euler"], upscale_scheduler=["simple"], + upscale_steps=[3], upscale_denoise=[0.2], upscale_cfg=[1.0], + upscale_noise_seed=[100], conditioning=["conditioning"], + upscale_sigmas=["connected-sigmas"], + ) + self.assertEqual(len(outputs), 1) + self.assertEqual(FakeUpscale.calls[0]["sigmas"], "connected-sigmas") + self.assertIn("clips=1", report) + finally: + UPSCALE._native_node = original + + +if __name__ == "__main__": + unittest.main()