"""Fixed-pipeline contract parsed from the base workflow JSON. The bench locks to the Moxie pipeline used by the base workflow: MoxieMultimediaLoader -> LoraLoaderModelOnly -> MoxiePreviewOverride -> easy multiTrackEditor -> MoxieProject -> MoxieCombine -> [branch A] CreateVideo(#45) -> SaveVideo(#46) (native) -> [branch B] RTXVideoSuperResolution -> CreateVideo(#68) -> SaveVideo(#69) (upscaled) The parser walks the frontend-format workflow (nodes/links/widgets_values_named) that ComfyUI exports and needs no ComfyUI installation: link inputs carry their own names in the node's ``inputs`` array, widget values carry ``widgets_values_named``. """ from __future__ import annotations import copy import json from dataclasses import dataclass, field from pathlib import Path from typing import Any EDITOR_TYPE = "easy multiTrackEditor" STAGE_ORDER = ( "MoxieMultimediaLoader", "LoraLoaderModelOnly", "MoxiePreviewOverride", EDITOR_TYPE, "MoxieProject", "MoxieCombine", "RTXVideoSuperResolution", "CreateVideo", ) # SaveVideo x2 tracked via PipelineSpec.save_nodes (native/upscaled branches) class WorkflowError(ValueError): pass @dataclass class Stage: node_id: int type: str widget_values: dict[str, Any] = field(default_factory=dict) link_inputs: dict[str, tuple[int, int]] = field(default_factory=dict) # name -> (node_id, slot) @dataclass class PipelineSpec: raw: dict[str, Any] stages: dict[str, Stage] # type -> Stage (types unique in the fixed graph) save_nodes: dict[str, Stage] = field(default_factory=dict) # native/upscaled -> SaveVideo @property def editor_node_id(self) -> int: return self.stages[EDITOR_TYPE].node_id def widget(self, stage_type: str, name: str, default: Any = None) -> Any: st = self.stages.get(stage_type) if not st: return default return st.widget_values.get(name, default) def _parse_workflow(raw: dict[str, Any]) -> PipelineSpec: nodes = raw.get("nodes") if not isinstance(nodes, list) or not nodes: raise WorkflowError("workflow has no nodes") links: dict[int, tuple[int, int, int]] = {} for lk in raw.get("links") or []: # [link_id, from_node, from_slot, to_node, to_slot, type] links[int(lk[0])] = (int(lk[1]), int(lk[2]), int(lk[4])) stages: dict[str, Stage] = {} save_nodes: dict[str, Stage] = {} seen_create = 0 for n in nodes: ntype = n.get("type") nid = int(n.get("id")) if ntype not in STAGE_ORDER and ntype != "SaveVideo": continue named = dict(n.get("widgets_values_named") or {}) # Legacy export without named values: best effort positional mapping. if not named: named = {} stage = Stage(node_id=nid, type=ntype, widget_values=named) for inp in n.get("inputs") or []: if not isinstance(inp, dict): continue if "link" in inp and inp["link"] is not None and "widget" not in inp: frm = links.get(int(inp["link"])) if frm is None: raise WorkflowError(f"node {nid}: dangling link {inp['link']}") stage.link_inputs[inp["name"]] = (frm[0], frm[1]) if ntype == "CreateVideo": seen_create += 1 key = "CreateVideo" if seen_create == 2: stages.pop("CreateVideo#1", None) stages[f"CreateVideo#{seen_create}" if seen_create > 1 else key] = stage continue if ntype == "SaveVideo": src = stage.link_inputs.get("video", (None, None))[0] if src is not None and src in ( stages.get("CreateVideo#2").node_id if stages.get("CreateVideo#2") else -1, ): save_nodes["upscaled"] = stage else: save_nodes["native"] = stage continue if ntype in stages: raise WorkflowError(f"duplicate stage node type {ntype}") stages[ntype] = stage missing = [t for t in STAGE_ORDER if t not in stages] if missing: raise WorkflowError(f"missing pipeline stages: {', '.join(missing)}") return PipelineSpec(raw=raw, stages=stages, save_nodes=save_nodes) def load_base_workflow(path: Path) -> PipelineSpec: with open(path, "r", encoding="utf-8") as fh: raw = json.load(fh) return _parse_workflow(raw) def parse_workflow_dict(raw: dict[str, Any]) -> PipelineSpec: return _parse_workflow(copy.deepcopy(raw)) def editor_track_data(spec: PipelineSpec) -> dict[str, Any]: """Parsed track_data JSON from the editor stage.""" raw = spec.widget(EDITOR_TYPE, "track_data") if isinstance(raw, str): raw = json.loads(raw) if not isinstance(raw, dict): raise WorkflowError("editor track_data is not an object") return raw def editor_resolution(spec: PipelineSpec) -> tuple[int, int]: """Width/height chosen on the editor, falling back to the project_data.""" res = spec.widget(EDITOR_TYPE, "resolution", "") preset = _RESOLUTION_PRESETS.get(res) if preset: return preset combo = spec.stages[EDITOR_TYPE].widget_values.get("resolution") if isinstance(combo, dict): # custom: {"width": .., "height": ..} try: return int(combo.get("width")), int(combo.get("height")) except (TypeError, ValueError): pass pd = _project_data(spec) return int(pd.get("width", 0)), int(pd.get("height", 0)) def project_data(spec: PipelineSpec) -> dict[str, Any]: return _project_data(spec) def _project_data(spec: PipelineSpec) -> dict[str, Any]: raw = spec.widget("MoxieCombine", "project_data", "") or "{}" if isinstance(raw, str): raw = json.loads(raw) return raw if isinstance(raw, dict) else {} _RESOLUTION_PRESETS = { "1920 x 1080 (16:9)": (1920, 1080), "1088 x 1920 (9:16)": (1088, 1920), "1080 x 1920 (9:16)": (1080, 1920), "1280 x 720 (16:9)": (1280, 720), "720 x 1280 (9:16)": (720, 1280), "832 x 480 (16:9)": (832, 480), "480 x 832 (9:16)": (480, 832), "1024 x 1024 (1:1)": (1024, 1024), } def describe(spec: PipelineSpec) -> dict[str, Any]: """Small human-readable summary used by /api/workflow.""" w, h = editor_resolution(spec) pd = project_data(spec) return { "stages": [ {"type": t, "node_id": s.node_id, "widgets": s.widget_values} for t, s in spec.stages.items() ], "editor_resolution": {"width": w, "height": h}, "frame_rate": pd.get("frame_rate", 24), "project_name": spec.widget("MoxieProject", "project_name", "MoxieBench"), "steps": spec.widget("MoxieProject", "steps", 4), "seed": spec.widget("MoxieProject", "seed", 0), "lora": spec.widget("LoraLoaderModelOnly", "lora_name"), "lora_strength": spec.widget("LoraLoaderModelOnly", "strength_model", 1.0), "upscale": { "enabled": "RTXVideoSuperResolution" in spec.stages, "target": { "width": spec.widget("RTXVideoSuperResolution", "resize_type.width"), "height": spec.widget("RTXVideoSuperResolution", "resize_type.height"), }, "quality": spec.widget("RTXVideoSuperResolution", "quality", "ULTRA"), }, }