feat: 工作流画布支持草稿试运行(target=draft 执行当前草稿图)

This commit is contained in:
董宏凯 committed 2026-08-30 20:02:15 +08:00
1 parent 3384f2e51c
commit 222595d6c4
7 files changed
+198 -32

No files matched your search

@@ -149,26 +149,36 @@ def _public_run(run: dict[str, Any]) -> dict[str, Any]:
async def start_run(request: Request, workflow_id: str, body: StartRunBody) -> dict[str, Any]:
_require_enabled()
user_id = await get_current_user(request)
if str(body.target or "").strip().lower() == "draft":
# Never silently fall back to the published version when the client
# thinks it is exercising the draft — draft snapshots need the explicit
# test-run API (not built yet).
raise HTTPException(
status_code=422,
detail={
"code": "WORKFLOW_DRAFT_RUN_UNSUPPORTED",
"message": "暂不支持运行草稿,请先发布或指定 versionId",
},
)
definitions = _definitions(request)
definition = await definitions.get_definition(workflow_id, include_draft=False)
is_draft_run = str(body.target or "").strip().lower() == "draft"
if is_draft_run:
# Test-run semantics: execute the CURRENT draft graph (what the canvas
# shows right now), versioned as the literal "draft". The compiled
# execution graph travels in run.context (never the canvas document),
# and the executor prefers it over any published version.
definition = await definitions.get_definition(workflow_id, include_draft=True)
else:
definition = await definitions.get_definition(workflow_id, include_draft=False)
if definition is None:
raise HTTPException(status_code=404, detail="工作流不存在")
if definition.get("owner_id") != user_id and not await _is_admin(request):
raise HTTPException(status_code=403, detail={"code": "WORKFLOW_FORBIDDEN", "message": "无权运行该工作流"})
draft_graph_raw: dict[str, Any] | None = None
if is_draft_run:
draft_graph_raw = definition.get("draft_graph") or {}
if not draft_graph_raw.get("nodes"):
raise HTTPException(
status_code=400,
detail={
"code": "WORKFLOW_DRAFT_EMPTY",
"message": "草稿还没有可运行的执行图,请先在画布保存一次",
},
)
version_id = body.version_id
if not version_id:
if not version_id and not is_draft_run:
versions = await definitions.list_versions(workflow_id)
if not versions:
raise HTTPException(
@@ -176,11 +186,14 @@ async def start_run(request: Request, workflow_id: str, body: StartRunBody) -> d
detail={"code": "WORKFLOW_VERSION_NOT_FOUND", "message": "工作流尚未发布任何版本"},
)
version_id = versions[0]["id"]
version = await definitions.get_version(version_id)
if version is None or version.get("workflow_id") != workflow_id:
raise HTTPException(status_code=404, detail={"code": "WORKFLOW_VERSION_NOT_FOUND", "message": "版本不存在"})
if is_draft_run:
version_id = "draft"
else:
version = await definitions.get_version(version_id)
if version is None or version.get("workflow_id") != workflow_id:
raise HTTPException(status_code=404, detail={"code": "WORKFLOW_VERSION_NOT_FOUND", "message": "版本不存在"})
graph = WorkflowGraph.model_validate(version.get("graph") or {})
graph = WorkflowGraph.model_validate(draft_graph_raw if is_draft_run else (version.get("graph") or {}))
inputs = body.resolved_inputs()
missing = [key for key in (graph.input_schema.get("required") or []) if inputs.get(str(key)) in (None, "")]
if missing:
@@ -218,6 +231,11 @@ async def start_run(request: Request, workflow_id: str, body: StartRunBody) -> d
# Scope the idempotency key per workflow so two workflows can share a client key.
key = f"{workflow_id}:{body.idempotency_key}" if body.idempotency_key else None
context: dict[str, Any] = {"depth": 0}
if is_draft_run:
# The draft snapshot rides with the run; the executor prefers it over
# the published version. Execution graph only — never the canvas doc.
context["draftGraph"] = draft_graph_raw
run, created = await runs.create_run(
{
"workflow_id": workflow_id,
@@ -225,7 +243,7 @@ async def start_run(request: Request, workflow_id: str, body: StartRunBody) -> d
"owner_id": user_id,
"input": inputs,
"idempotency_key": key,
"context": {"depth": 0},
"context": context,
}
)
if created: