Files
aigc-home/calliope-backend/src/calliope/comfyui/roles.py
T
总工andmultica-agent e6f761f8c8 feat(workflows): 五类用途识别与导入确认闭环 (HEL-636)
- 判型器 classify_usage:按输出节点(sink 检测)与被消费图片槽判定
  文生图/图生图/文生视频/图生视频/参考图生视频,不依赖文件名/模型名/
  固定节点 ID;歧义(单散装图+视频、同图双输出)与无输出给中文说明
- 参考图组泛化:任意 <group>.<prefix>N 连续编号且均接图源的输入成组
  (MiniMaxH3 特判仅剩标签美化);patcher 支持通用组回填
- workflows 表新增 usage/usage_source,幂等迁移回填;判不出的存量行
  停用待确认;kind 语义收窄为输出媒体,历史无损
- 导入改两步向导:分析→预览(判定依据/输出节点/媒体槽角色可改/
  参数预览/警告)→确认保存;usage_source=user 的纠正不被重分析覆盖
- 生成页按 usage 过滤;待确认工作流不进生成页且提交被拒
- 新增 test_usage_classifier / test_workflow_import_confirm;
  两个旧测试夹具补输出节点以匹配新守卫

Co-authored-by: multica-agent <github@multica.ai>
2026-09-29 12:13:33 +08:00

109 lines
3.6 KiB
Python

"""ComfyUI node title role tags: (Input:prompt), (Output:image), etc."""
from __future__ import annotations
import re
from typing import Any
# Title tag: (Input), (Input:prompt), (Output:image), …
_TITLE_TAG_RE = re.compile(
r"\((?P<kind>Input|Output)(?::(?P<role>[a-zA-Z0-9_-]+))?\)",
re.IGNORECASE,
)
# Canonical role → accepted aliases (all lowercased).
INPUT_ROLE_ALIASES: dict[str, frozenset[str]] = {
"prompt": frozenset({"prompt", "positive"}),
"negative": frozenset({"negative", "neg"}),
"width": frozenset({"width", "w"}),
"height": frozenset({"height", "h"}),
"character": frozenset({"character", "char", "portrait", "sheet", "face", "ref"}),
"location": frozenset({"location", "loc", "environment", "env", "background", "scene"}),
"image": frozenset({"image", "img"}),
"video": frozenset({"video", "vid"}),
"audio": frozenset({"audio", "sound", "sfx"}),
"seed": frozenset({"seed"}),
"duration": frozenset({"duration", "dur", "length", "seconds"}),
}
OUTPUT_ROLE_ALIASES: dict[str, frozenset[str]] = {
"image": frozenset({"image", "img"}),
"video": frozenset({"video", "vid"}),
}
# The five workflow usages the studio distinguishes (HEL-636). Derived from
# graph evidence only (output media + consumed image inputs) — never from
# file names, model names or fixed node ids.
WorkflowUsage = str
USAGE_VALUES: tuple[str, ...] = (
"text2image",
"image2image",
"text2video",
"image2video",
"ref2video",
)
def usage_to_kind(usage: str | None) -> str | None:
"""Output media kind of a usage (image / video); None when unknown."""
if usage in ("text2image", "image2image"):
return "image"
if usage in ("text2video", "image2video", "ref2video"):
return "video"
return None
def parse_title_tag(title: str) -> tuple[str | None, str | None, str]:
"""Return (kind, role, label) from a Comfy node title.
kind is ``\"input\"`` / ``\"output\"`` or None if no tag.
role is the lowercase role string or None for plain ``(Input)`` / ``(Output)``.
label is the display name with the tag stripped.
"""
if not title:
return None, None, ""
match = _TITLE_TAG_RE.search(title)
if not match:
return None, None, title.strip()
kind = match.group("kind").lower()
role_raw = match.group("role")
role = role_raw.lower() if role_raw else None
stripped = _TITLE_TAG_RE.sub("", title).strip()
# Title is only the tag, e.g. "(Input:prompt)" → use role as display label.
if stripped:
label = stripped
elif role:
label = role
else:
label = kind
return kind, role, label
def normalize_input_role(role: str | None) -> str | None:
"""Map an alias to its canonical input role, or return the role as-is if unknown."""
if not role:
return None
r = role.lower().strip()
for canonical, aliases in INPUT_ROLE_ALIASES.items():
if r in aliases or r == canonical:
return canonical
return r
def normalize_output_role(role: str | None) -> str | None:
if not role:
return None
r = role.lower().strip()
for canonical, aliases in OUTPUT_ROLE_ALIASES.items():
if r in aliases or r == canonical:
return canonical
return r
def input_has_role(inp: dict[str, Any], *canonical_roles: str) -> bool:
"""True if this parsed input's role matches any of the canonical roles (incl. aliases)."""
role = normalize_input_role(inp.get("role"))
if not role:
return False
wanted = {normalize_input_role(c) for c in canonical_roles}
return role in wanted