Unknown custom output nodes are never auto-typed by class-name semantics; they stay pending until the user designates the real output node(s) and media type. Picking a usage alone no longer enables a workflow, reanalyze/ migration/field edits never overwrite a user mapping, generation harvests only the mapped nodes from ComfyUI history, and media-type mismatches or missing node results fail for real instead of faking completion. Co-authored-by: multica-agent <github@multica.ai>
127 lines
5.2 KiB
Python
127 lines
5.2 KiB
Python
"""Structural checks on an imported ComfyUI API workflow.
|
||
|
||
``error`` findings mean ComfyUI will reject the prompt as exported, so the
|
||
studio blocks generation; ``warning`` findings only explain odd parameters.
|
||
Nothing here repairs a workflow — a broken export has to be re-exported from
|
||
a ComfyUI that has the missing custom nodes installed.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
REEXPORT_HINT = "请在已安装对应自定义节点的 ComfyUI 中打开该工作流,重新导出 API 格式 JSON 后再导入"
|
||
|
||
# Nodes that cannot run without a model link.
|
||
_NEEDS_MODEL = frozenset(
|
||
{"KSampler", "KSamplerAdvanced", "BasicGuider", "CFGGuider", "BasicScheduler"}
|
||
)
|
||
|
||
|
||
def _title(node_id: str, node: dict[str, Any]) -> str:
|
||
title = str((node.get("_meta") or {}).get("title") or "").strip()
|
||
return f"节点 {node_id}「{title}」" if title else f"节点 {node_id}"
|
||
|
||
|
||
def workflow_warnings(workflow: dict[str, Any]) -> list[dict[str, str]]:
|
||
if not isinstance(workflow, dict):
|
||
return []
|
||
found: list[dict[str, str]] = []
|
||
nodes = {str(k): v for k, v in workflow.items() if isinstance(v, dict)}
|
||
|
||
untyped = [nid for nid, node in nodes.items() if not str(node.get("class_type") or "").strip()]
|
||
for nid in untyped:
|
||
found.append(
|
||
{
|
||
"level": "error",
|
||
"code": "missing_class_type",
|
||
"node_id": nid,
|
||
"message": f"{_title(nid, nodes[nid])}缺少节点类型(class_type),"
|
||
"导出时 ComfyUI 未加载该自定义节点",
|
||
}
|
||
)
|
||
|
||
consumed: set[str] = set()
|
||
for nid, node in nodes.items():
|
||
inputs = node.get("inputs") or {}
|
||
for name, val in inputs.items():
|
||
if isinstance(val, list) and len(val) == 2 and isinstance(val[0], (str, int)):
|
||
src = str(val[0])
|
||
consumed.add(src)
|
||
if src not in nodes:
|
||
found.append(
|
||
{
|
||
"level": "error",
|
||
"code": "dangling_link",
|
||
"node_id": nid,
|
||
"message": f"{_title(nid, node)}的输入「{name}」"
|
||
f"连到了不存在的节点 {src}",
|
||
}
|
||
)
|
||
ct = str(node.get("class_type") or "")
|
||
if ct in _NEEDS_MODEL and not isinstance(inputs.get("model"), list):
|
||
found.append(
|
||
{
|
||
"level": "error",
|
||
"code": "missing_model_link",
|
||
"node_id": nid,
|
||
"message": f"{_title(nid, node)}({ct})没有连接模型(model),"
|
||
"模型加载链路在导出时丢失",
|
||
}
|
||
)
|
||
|
||
for nid, node in nodes.items():
|
||
ct = str(node.get("class_type") or "")
|
||
if ct.startswith("Empty") and "Latent" in ct and nid not in consumed:
|
||
found.append(
|
||
{
|
||
"level": "warning",
|
||
"code": "orphan_latent",
|
||
"node_id": nid,
|
||
"message": f"{_title(nid, nodes[nid])}没有接到任何节点,"
|
||
"它的宽高不会生效,已不在表单中显示",
|
||
}
|
||
)
|
||
|
||
# HEL-636/638: a workflow without any recognizable output node can never
|
||
# return a result — block it with a readable reason instead of letting
|
||
# ComfyUI run blind. Unknown candidates (needs_mapping) don't trigger
|
||
# this: the user designates them on the import/connections page, and
|
||
# until then the row stays disabled.
|
||
from calliope.comfyui.parser import detect_output_nodes
|
||
|
||
outputs = detect_output_nodes(workflow)
|
||
if not any(o.get("kind") in ("image", "video") for o in outputs):
|
||
unknown = [o for o in outputs if o.get("evidence") == "unknown_node"]
|
||
if not unknown:
|
||
found.append(
|
||
{
|
||
"level": "error",
|
||
"code": "no_output",
|
||
"node_id": "",
|
||
"message": "未在图中找到输出节点(SaveImage / 视频合成等),"
|
||
"请确认导出的是 API 格式且包含保存节点",
|
||
}
|
||
)
|
||
else:
|
||
names = "、".join(f"节点 {o['nodeId']}" for o in unknown[:4])
|
||
found.append(
|
||
{
|
||
"level": "warning",
|
||
"code": "unknown_output_node",
|
||
"node_id": names,
|
||
"message": f"存在无法识别的输出节点({names}),"
|
||
"需人工指定实际输出节点及其媒体类型(图片/视频)后才能生成",
|
||
}
|
||
)
|
||
return found
|
||
|
||
|
||
def blocking_problem(workflow: dict[str, Any]) -> str | None:
|
||
"""One Chinese sentence explaining why this workflow cannot be submitted."""
|
||
errors = [w["message"] for w in workflow_warnings(workflow) if w["level"] == "error"]
|
||
if not errors:
|
||
return None
|
||
shown = ";".join(errors[:4]) + (f";等 {len(errors)} 处" if len(errors) > 4 else "")
|
||
return f"该工作流文件不完整,ComfyUI 无法执行:{shown}。{REEXPORT_HINT}。"
|