"""L2 connection preflight: check a workflow against a live /object_info (HEL-642/643). Layer 1 (lint.py) judges the JSON on its own; this module judges the JSON against what the target ComfyUI actually has installed. Two generic rules, zero hard-coding for any specific workflow or model name: 1. Node existence — every ``class_type`` in the JSON must exist as a key of the server's /object_info catalogue; missing ones are custom nodes the user has not installed. 2. Combo value legality — for every combo input the server declares (an input whose spec carries a candidate list — unet_name/clip_name/ sampler/scheduler/…), the JSON's current widget value must be one of the candidates; a miss usually means a model file or component is missing. File-upload widgets (LoadImage.image and friends) are skipped: their combo lists are the server's input directory at export time and the value is replaced by an upload before every submit, so a miss there says nothing. """ from __future__ import annotations from typing import Any from calliope.comfyui.registry import AUDIO_CLASSES, IMAGE_CLASSES, VIDEO_CLASSES # Media-loader classes and the widget names that hold their file values — # upload-backed combos that cannot be judged before an upload happens. _MEDIA_LOADER_FIELDS: tuple[frozenset[str], frozenset[str]] = ( (IMAGE_CLASSES, frozenset({"image"})), (AUDIO_CLASSES, frozenset({"audio", "audio:"})), (VIDEO_CLASSES, frozenset({"file", "video"})), ) # object_info widget-config keys that mark an input as file-upload backed. _UPLOAD_CONFIG_KEYS = ("image_upload", "audio_upload", "video_upload", "upload") _MAX_CANDIDATES_SHOWN = 6 def _node_title(node: dict[str, Any]) -> str: title = (node.get("_meta") or {}).get("title") return str(title).strip() if title else "" def _is_upload_backed(class_type: str, input_name: str, config: Any) -> bool: if isinstance(config, dict) and any(config.get(k) for k in _UPLOAD_CONFIG_KEYS): return True for classes, fields in _MEDIA_LOADER_FIELDS: if class_type in classes and input_name in fields: return True return False def _combo_candidates(spec: Any) -> list[str] | None: """The candidate list when ``spec`` is a combo input, else None. A combo spec is ``[["a", "b"], {config}]`` — first element a list of plain strings. Typed inputs (``["FLOAT", {…}]``) return None. """ if not (isinstance(spec, list) and len(spec) >= 1): return None values = spec[0] if not isinstance(values, list) or not values: return None if not all(isinstance(v, str) for v in values): return None return values def check_workflow_against( object_info: dict[str, Any], workflow: dict[str, Any] ) -> list[dict[str, Any]]: """Findings for every problem the target server's catalogue reveals. Each finding is ``{level, code, node_id, node_title, message, hint}`` (``level`` is always ``"error"`` for now — both rules are hard misses). """ findings: list[dict[str, Any]] = [] if not isinstance(object_info, dict) or not isinstance(workflow, dict): return findings for node_id, node in workflow.items(): if not isinstance(node, dict): continue class_type = str(node.get("class_type") or "") title = _node_title(node) label = f"节点 {node_id}「{title}」" if title else f"节点 {node_id}" spec = object_info.get(class_type) if not isinstance(spec, dict): findings.append( { "level": "error", "code": "missing_node", "node_id": str(node_id), "node_title": title, "message": ( f"缺少自定义节点 {class_type or '(未知类名)'}({label})——" "该 ComfyUI 未安装此节点,需先安装对应的节点包" ), "hint": "安装节点包后回到本页点「重新检查」即可复查,无需重新导入。", } ) continue declared = (spec.get("input") or {}) if isinstance(spec.get("input"), dict) else {} inputs = node.get("inputs") or {} for section in ("required", "optional"): entries = declared.get(section) if not isinstance(entries, dict): continue for input_name, in_spec in entries.items(): value = inputs.get(input_name) # Wired inputs (links) and absent widgets are not combos to judge. if isinstance(value, (list, dict)) or value is None: continue config = in_spec[1] if isinstance(in_spec, list) and len(in_spec) > 1 else None if _is_upload_backed(class_type, str(input_name), config): continue candidates = _combo_candidates(in_spec) if candidates is None: continue if value in candidates: continue shown = "、".join(candidates[:_MAX_CANDIDATES_SHOWN]) if len(candidates) > _MAX_CANDIDATES_SHOWN: shown += f" 等 {len(candidates)} 项" findings.append( { "level": "error", "code": "value_not_in_list", "node_id": str(node_id), "node_title": title, "message": ( f"{label}:值「{value}」不在 ComfyUI 的可选列表" f"(参数 {input_name})——多为对应的模型文件或组件未安装" ), "hint": f"该 ComfyUI 上可选:{shown}" if shown else None, } ) return findings