from __future__ import annotations import json from typing import Any from fastapi import APIRouter, HTTPException from calliope.comfyui.endpoints import client_for, endpoint_snapshot from calliope.comfyui.lint import blocking_problem, workflow_warnings from calliope.comfyui.output_map import ( auto_output_map, load_output_map, mapping_problem, needs_user_output_map, validate_output_map, ) from calliope.comfyui.parser import ( classify_usage, effective_input_schema, parse_dynamic_inputs, parse_dynamic_outputs, ) from calliope.comfyui.preflight import check_workflow_against from calliope.comfyui.profiles import detect_prompt_profile from calliope.comfyui.roles import usage_to_kind from calliope.comfyui.validate import validate_input_values from calliope.config import settings from calliope.db import get_db, row_to_dict from calliope.events.bus import event_bus from calliope.models.schemas import ( WorkflowAnalyze, WorkflowCheckRequest, WorkflowCreate, WorkflowTestRequest, WorkflowUpdate, ) from calliope.queue.manager import queue_manager from calliope.queue.workflow_test import ( TEST_SOURCE, find_active_test_job, list_test_jobs, purge_workflow_tests, ) router = APIRouter() def _serialize_workflow(row: Any, *, include_document: bool = True) -> dict[str, Any]: data = row_to_dict(row) data["workflow_json"] = json.loads(data["workflow_json"]) if not include_document: data.pop("doc_json", None) for field in (("doc_json", "import_issues") if include_document else ("import_issues",)): data[field] = json.loads(data[field]) if data.get(field) else None data.pop("raw_source_json", None) data.pop("library_fingerprint", None) stored = json.loads(data["input_schema"]) if data.get("input_schema") else [] data["input_schema"] = effective_input_schema(stored, data["workflow_json"]) # Outputs are pure graph derivation (no manual overrides) — always fresh, # so parser upgrades reach old rows without a re-import. data["output_schema"] = parse_dynamic_outputs(data["workflow_json"]) data["is_enabled"] = bool(data.get("is_enabled")) data["warnings"] = workflow_warnings(data["workflow_json"]) raw_map = data.get("output_map") data["output_map"] = load_output_map(raw_map) # Pending flag: unknown output candidates the user has not mapped yet. data["needs_mapping"] = ( mapping_problem(data["workflow_json"], raw_map, data.get("output_map_source")) is not None ) return data async def _connection_check( endpoint_id: str | None, workflow_json: dict[str, Any], refresh: bool = False ) -> dict[str, Any]: """L2 preflight (HEL-643): workflow vs the target ComfyUI's /object_info. An unreachable connection degrades to ``reachable: False`` with an honest 「无法核查」 message — it never blocks and never pretends to pass. """ endpoint = settings.resolve_comfyui_endpoint(endpoint_id) if endpoint is None: return { "ok": False, "reachable": False, "endpoint_id": endpoint_id, "endpoint_name": None, "findings": [], "message": "没有可用的 ComfyUI 连接,无法核查节点与模型", } base = { "endpoint_id": endpoint.get("id"), "endpoint_name": endpoint.get("name"), "base_url": endpoint.get("base_url"), } client = client_for(endpoint) try: object_info = await client.object_info(force_refresh=refresh) except Exception as exc: return { **base, "ok": False, "reachable": False, "findings": [], "message": f"无法核查(连不上该 ComfyUI):{type(exc).__name__}", } finally: await client.close() findings = check_workflow_against(object_info, workflow_json) ok = not any(f.get("level") == "error" for f in findings) return {**base, "ok": ok, "reachable": True, "findings": findings} @router.post("/analyze") async def analyze_workflow(payload: WorkflowAnalyze) -> dict[str, Any]: inputs = parse_dynamic_inputs(payload.workflow_json) outputs = parse_dynamic_outputs(payload.workflow_json) judgment = classify_usage(payload.workflow_json) result: dict[str, Any] = { "inputs": inputs, "outputs": outputs, "suggested_profile": detect_prompt_profile(payload.workflow_json), "warnings": workflow_warnings(payload.workflow_json), "usage": judgment["usage"], "usage_ambiguous": judgment["ambiguous"], "needs_mapping": judgment["needs_mapping"], "usage_problem": judgment["problem"], "usage_evidence": judgment["evidence"], "usable_usage": judgment["usable_usage"], } # L2 (optional): with a target connection the analyze response also # reports which nodes/models that ComfyUI actually has (HEL-643). if payload.endpoint_id: result["connection_check"] = await _connection_check( payload.endpoint_id, payload.workflow_json, payload.refresh ) return result def _stored_schema(payload_schema: list[dict[str, Any]] | None, workflow: dict[str, Any]) -> str: """Schema to persist: the confirm page's corrected copy, else the auto one.""" if payload_schema and all(isinstance(e, dict) for e in payload_schema): return json.dumps(payload_schema) return json.dumps(parse_dynamic_inputs(workflow)) @router.post("/{workflow_id}/reanalyze") async def reanalyze_workflow(workflow_id: int) -> dict[str, Any]: """Recompute usage and input/output schemas from the stored workflow JSON. Workflows imported before a parser upgrade keep stale (often empty) schemas — this refreshes them without re-importing. A usage or output mapping the user set by hand is never overwritten; only ``auto`` (and pending NULL) rows get the new judgment. """ conn = get_db(settings.db_path) try: row = conn.execute("SELECT * FROM workflows WHERE id = ?", (workflow_id,)).fetchone() if not row: raise HTTPException(status_code=404, detail="Workflow not found") data = row_to_dict(row) workflow = json.loads(data["workflow_json"]) judgment = classify_usage(workflow) usage = data["usage"] usage_source = data["usage_source"] if data["usage_source"] != "user" and judgment["usage"]: usage = judgment["usage"] usage_source = "auto" stored = json.loads(data["input_schema"]) if data["input_schema"] else [] schema = effective_input_schema(stored, workflow) outputs = parse_dynamic_outputs(workflow) if data["output_map_source"] == "user": output_map, output_map_source = data["output_map"], "user" elif needs_user_output_map(workflow): # Unknown output candidates: no auto map, and the row must not # generate until the user designates the output nodes. output_map, output_map_source = None, None else: output_map, output_map_source = json.dumps(auto_output_map(workflow)), "auto" is_enabled = data["is_enabled"] if data.get("import_state") in ("needs_conversion", "pending_check", "rolled_back"): is_enabled = 0 if output_map_source is None or usage is None: is_enabled = 0 conn.execute( "UPDATE workflows SET input_schema = ?, output_schema = ?," " usage = ?, usage_source = ?, output_map = ?, output_map_source = ?," " is_enabled = ? WHERE id = ?", ( json.dumps(schema), json.dumps(outputs), usage, usage_source, output_map, output_map_source, is_enabled, workflow_id, ), ) conn.commit() fresh = conn.execute( "SELECT * FROM workflows WHERE id = ?", (workflow_id,) ).fetchone() return _serialize_workflow(fresh) finally: conn.close() def _last_test_summary(job: dict[str, Any]) -> dict[str, Any]: """Slim "上次测试" record for a workflow row (queried live, never stored).""" try: outputs = json.loads(job.get("output_paths_json") or "[]") except (json.JSONDecodeError, TypeError): outputs = [] return { "id": job["id"], "status": job.get("status"), "phase": job.get("phase"), "error": job.get("error"), "created_at": job.get("created_at"), "completed_at": job.get("completed_at"), "output_count": len(outputs) if isinstance(outputs, list) else 0, } @router.get("") async def list_workflows() -> list[dict[str, Any]]: conn = get_db(settings.db_path) try: rows = conn.execute("SELECT * FROM workflows ORDER BY id DESC").fetchall() out = [_serialize_workflow(r, include_document=False) for r in rows] # 上次测试 state is read live from the newest test job per workflow — # no cached column that could go stale (HEL-643). latest: dict[int, dict[str, Any]] = {} for jr in conn.execute( "SELECT * FROM jobs WHERE origin = 'workflow_test' AND id IN" " (SELECT MAX(id) FROM jobs WHERE origin = 'workflow_test' GROUP BY workflow_id)" ).fetchall(): latest[int(jr["workflow_id"])] = _last_test_summary(dict(jr)) for data in out: data["last_test"] = latest.get(int(data["id"])) return out finally: conn.close() @router.post("/{workflow_id}/check") async def check_workflow(workflow_id: int, payload: WorkflowCheckRequest) -> dict[str, Any]: """L2 re-check of a saved workflow against its (or a chosen) connection. The user installs missing nodes/models in ComfyUI, comes back here and re-checks — no re-import needed. """ conn = get_db(settings.db_path) try: row = conn.execute( "SELECT workflow_json, endpoint_id, import_state FROM workflows WHERE id = ?", (workflow_id,) ).fetchone() finally: conn.close() if not row: raise HTTPException(status_code=404, detail="Workflow not found") if row["import_state"] in ("needs_conversion", "rolled_back"): raise HTTPException(status_code=400, detail="请先完成转换或恢复库条目,不能核查空图") endpoint_id = payload.endpoint_id or row["endpoint_id"] return await _connection_check(endpoint_id, json.loads(row["workflow_json"]), payload.refresh) @router.post("/dedupe") async def dedupe_workflows() -> dict[str, Any]: """Delete byte-identical duplicate workflows, keeping the lowest id. Two rows are duplicates only when name, kind AND workflow_json all match — same name with different JSON is a user's variant, never touched. """ conn = get_db(settings.db_path) try: rows = conn.execute( "SELECT id, name, kind, workflow_json FROM workflows" " WHERE source_repo_path IS NULL ORDER BY id" ).fetchall() seen: dict[tuple[str, str, str], int] = {} drop_ids: list[int] = [] for r in rows: key = (r["name"], r["kind"], r["workflow_json"]) if key in seen: drop_ids.append(r["id"]) else: seen[key] = r["id"] removed = 0 for wid in drop_ids: cur = conn.execute("DELETE FROM workflows WHERE id = ?", (wid,)) removed += cur.rowcount conn.commit() return { "ok": True, "removed": removed, "kept_ids": sorted(seen.values()), "removed_ids": drop_ids, } finally: conn.close() @router.post("") async def create_workflow(payload: WorkflowCreate) -> dict[str, Any]: judgment = classify_usage(payload.workflow_json) usage = payload.usage or judgment["usage"] usage_source = "user" if payload.usage else "auto" kind = payload.kind or usage_to_kind(usage) if kind is None: # Indeterminate and nothing chosen: classify_output hint keeps old # callers working while the workflow stays pending confirmation. out_kinds = { o.get("kind") for o in parse_dynamic_outputs(payload.workflow_json) if o.get("kind") in ("image", "video", "audio", "file") } kind = next(iter(out_kinds)) if len(out_kinds) == 1 else "image" profile = payload.prompt_profile or detect_prompt_profile(payload.workflow_json) # HEL-638: an explicit user output map wins; otherwise the trusted auto # map. Either way a graph with unknown output candidates is unusable # until the user designates the output nodes. if payload.output_map: problems = validate_output_map(payload.workflow_json, payload.output_map) if problems: raise HTTPException(status_code=422, detail=";".join(problems)) output_map = json.dumps(payload.output_map) output_map_source = payload.output_map_source or "user" else: auto_map = auto_output_map(payload.workflow_json) output_map = json.dumps(auto_map) if auto_map else None output_map_source = "auto" if auto_map else None mapping_ok = not needs_user_output_map(payload.workflow_json) or bool(payload.output_map) conn = get_db(settings.db_path) try: # Byte-identical re-imports are the main way the library fills up with # same-named clones (the "@" typeahead then shows 20 identical rows). existing = conn.execute( "SELECT id, name FROM workflows WHERE name = ? AND kind = ? AND workflow_json = ?", (payload.name, kind, json.dumps(payload.workflow_json)), ).fetchone() if existing: raise HTTPException( status_code=409, detail=( f"An identical workflow already exists (id {existing['id']}," f" “{existing['name']}”)." ), ) # Unconfirmed workflows (ambiguous usage / unknown outputs without a # user mapping) are stored but disabled: they must not reach the # generate pages until the user confirms usage AND, for unknown # output nodes, designates the outputs on the connections page. is_enabled = 1 if (usage and mapping_ok) else 0 cur = conn.execute( """ INSERT INTO workflows (name, kind, workflow_json, input_schema, output_schema, description, prompt_profile, endpoint_id, is_enabled, usage, usage_source, output_map, output_map_source) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( payload.name, kind, json.dumps(payload.workflow_json), _stored_schema(payload.input_schema, payload.workflow_json), json.dumps(parse_dynamic_outputs(payload.workflow_json)), payload.description, profile, payload.endpoint_id, is_enabled, usage, usage_source, output_map, output_map_source, ), ) conn.commit() row = conn.execute("SELECT * FROM workflows WHERE id = ?", (cur.lastrowid,)).fetchone() return _serialize_workflow(row) finally: conn.close() @router.get("/{workflow_id}") async def get_workflow(workflow_id: int) -> dict[str, Any]: conn = get_db(settings.db_path) try: row = conn.execute("SELECT * FROM workflows WHERE id = ?", (workflow_id,)).fetchone() if not row: raise HTTPException(status_code=404, detail="Workflow not found") return _serialize_workflow(row) finally: conn.close() @router.patch("/{workflow_id}") async def update_workflow(workflow_id: int, payload: WorkflowUpdate) -> dict[str, Any]: conn = get_db(settings.db_path) try: existing = conn.execute("SELECT * FROM workflows WHERE id = ?", (workflow_id,)).fetchone() if not existing: raise HTTPException(status_code=404, detail="Workflow not found") data = payload.model_dump(exclude_unset=True) if existing["import_state"] in ("needs_conversion", "pending_check", "rolled_back"): if data.get("is_enabled"): raise HTTPException( status_code=400, detail="该库条目尚未完成转换或依赖核对,不能启用" ) data["is_enabled"] = 0 if "is_enabled" in data and data["is_enabled"] is not None: data["is_enabled"] = 1 if data["is_enabled"] else 0 workflow_json = json.loads(existing["workflow_json"]) # HEL-638: output_map payload. A non-empty list validates against the # stored JSON and becomes the user decision; an explicit empty list # resets the row to pending (disabled). if "output_map" in data: if data["output_map"]: problems = validate_output_map(workflow_json, data["output_map"]) if problems: raise HTTPException(status_code=422, detail=";".join(problems)) data["output_map"] = json.dumps(data["output_map"]) data.setdefault("output_map_source", "user") else: data["output_map"] = None data["output_map_source"] = None data["is_enabled"] = 0 if data.get("usage"): # Confirming / correcting the usage is an explicit user decision: # kind (output media) follows the usage. Whether it re-enables is # decided by the gate below — with unknown output nodes the user # must also designate the output nodes (HEL-638). data.setdefault("usage_source", "user") data["kind"] = usage_to_kind(data["usage"]) or existing["kind"] if "input_schema" in data and data["input_schema"] is not None: if not all(isinstance(e, dict) for e in data["input_schema"]): raise HTTPException(status_code=422, detail="input_schema 必须是参数对象数组") data["input_schema"] = json.dumps(data["input_schema"]) # endpoint_id: explicit null clears the binding; other Nones are skips # (an output_map reset keeps its explicit NULLs). explicit_null = {"endpoint_id", "output_map", "output_map_source"} & { k for k, v in data.items() if v is None } data = {k: v for k, v in data.items() if v is not None or k in explicit_null} # Enable gate: usage must be settled and, when the graph carries # unknown custom output nodes, a user output mapping must exist. usage_ok = bool(data.get("usage") or existing["usage"]) map_raw = data.get("output_map", existing["output_map"]) map_source = data.get("output_map_source", existing["output_map_source"]) map_problem = mapping_problem(workflow_json, map_raw, map_source) if data.get("is_enabled"): if not usage_ok: raise HTTPException(status_code=400, detail="请先选择工作流用途,再启用该工作流") if map_problem: raise HTTPException(status_code=400, detail=map_problem) elif (data.get("usage") or data.get("output_map")) and "is_enabled" not in data: # A usage-confirm or output-map-confirm path: enabled only when # both decisions are settled. data["is_enabled"] = 1 if (usage_ok and not map_problem) else 0 if data: fields = ", ".join(f"{k} = :{k}" for k in data) data["id"] = workflow_id conn.execute(f"UPDATE workflows SET {fields} WHERE id = :id", data) conn.commit() row = conn.execute("SELECT * FROM workflows WHERE id = ?", (workflow_id,)).fetchone() return _serialize_workflow(row) finally: conn.close() def _job_public(job: dict[str, Any]) -> dict[str, Any]: out = dict(job) out["payload"] = json.loads(job.get("payload_json") or "{}") out["output_paths"] = json.loads(job.get("output_paths_json") or "[]") out.pop("payload_json", None) out.pop("output_paths_json", None) return out @router.post("/{workflow_id}/test") async def test_workflow(workflow_id: int, payload: WorkflowTestRequest) -> dict[str, Any]: """Launch (or re-attach to) one REAL test run of a workflow (HEL-643 L3). The job goes through the ordinary queue → ComfyUI → outputs pipeline; only its artifacts and bookkeeping are isolated (origin='workflow_test'). A dry-run setting is refused outright — placeholder output must never count as a test pass. Repeat clicks reuse the in-flight job. """ from calliope import config if config.settings.dry_run: raise HTTPException( status_code=400, detail=( "测试工作流必须真实运行:当前设置开启了 Dry-run(占位模式)," "请到「设置」关闭 Dry-run 后再测试" ), ) conn = get_db(settings.db_path) try: wf = conn.execute( "SELECT id, kind, workflow_json, input_schema, output_map, output_map_source," " endpoint_id, import_state FROM workflows WHERE id = ?", (workflow_id,), ).fetchone() if not wf: raise HTTPException(status_code=404, detail="Workflow not found") if wf["import_state"] in ("needs_conversion", "pending_check", "rolled_back"): raise HTTPException(status_code=400, detail="该库条目尚未完成转换或依赖核对,不能测试") workflow_json = json.loads(wf["workflow_json"]) broken = blocking_problem(workflow_json) if broken: raise HTTPException(status_code=400, detail=broken) map_problem = mapping_problem(workflow_json, wf["output_map"], wf["output_map_source"]) if map_problem: raise HTTPException(status_code=400, detail=map_problem) schema = effective_input_schema( json.loads(wf["input_schema"]) if wf["input_schema"] else [], workflow_json ) input_values, problems = validate_input_values(schema, payload.input_values) if problems: raise HTTPException(status_code=400, detail="参数有误:" + ";".join(problems)) endpoint_id = payload.endpoint_id or wf["endpoint_id"] or None snapshot = endpoint_snapshot(endpoint_id) active = find_active_test_job(workflow_id, (snapshot or {}).get("id")) if active is not None: return {"ok": True, "job": _job_public(active), "reused": True} from calliope.routers.playground import ensure_playground_project pid = ensure_playground_project(conn) finally: conn.close() job = queue_manager.enqueue( project_id=pid, kind=wf["kind"], workflow_id=workflow_id, scene_id=None, payload={ "input_values": input_values, "source": TEST_SOURCE, **({"endpoint_id": payload.endpoint_id} if payload.endpoint_id else {}), }, origin="workflow_test", ) await event_bus.publish( "job.created", {"job_id": job["id"], "kind": job["kind"], "source": TEST_SOURCE, "project_id": job["project_id"], "workflow_test": True}, ) return {"ok": True, "job": _job_public(job), "reused": False} @router.get("/{workflow_id}/tests") async def list_workflow_tests(workflow_id: int) -> list[dict[str, Any]]: conn = get_db(settings.db_path) try: row = conn.execute("SELECT id FROM workflows WHERE id = ?", (workflow_id,)).fetchone() finally: conn.close() if not row: raise HTTPException(status_code=404, detail="Workflow not found") return list_test_jobs(workflow_id) @router.post("/{workflow_id}/clear-tests") async def clear_workflow_tests(workflow_id: int) -> dict[str, Any]: """Delete every test record and artifact for this workflow.""" conn = get_db(settings.db_path) try: row = conn.execute("SELECT id FROM workflows WHERE id = ?", (workflow_id,)).fetchone() finally: conn.close() if not row: raise HTTPException(status_code=404, detail="Workflow not found") try: removed = purge_workflow_tests(workflow_id) except RuntimeError as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc await event_bus.publish("job.deleted", {"workflow_id": workflow_id, "source": TEST_SOURCE}) return {"ok": True, "removed": removed} @router.delete("/{workflow_id}") async def delete_workflow(workflow_id: int) -> dict[str, bool]: conn = get_db(settings.db_path) try: # HEL-619: refuse to delete while queued/running jobs still use the # workflow — deleting sets their workflow_id NULL and the job dies # with "工作流已被删除" only after the user waits for it. row = conn.execute( "SELECT id FROM workflows WHERE id = ?", (workflow_id,) ).fetchone() if not row: raise HTTPException(status_code=404, detail="Workflow not found") pending = conn.execute( "SELECT COUNT(*) AS n FROM jobs" " WHERE workflow_id = ? AND status IN ('pending', 'running')", (workflow_id,), ).fetchone()["n"] if pending: raise HTTPException( status_code=409, detail=( f"该工作流还有 {pending} 个排队或执行中的任务," "请先等任务完成或取消任务,再删除该工作流" ), ) # Test records/artifacts belong to the workflow — cascade (HEL-643). # The 409 above guarantees none are in flight here. purge_workflow_tests(workflow_id, allow_active=True) conn.execute("DELETE FROM workflows WHERE id = ?", (workflow_id,)) conn.commit() return {"ok": True} finally: conn.close()