feat: add character workflow templates

这个提交存在于:
YokiiroBW 2026-07-05 09:35:49 +08:00
父节点 f757166e48
当前提交 06a2a41c81
共有 5 个文件被更改,包括 273 次插入 和 46 次删除

查看文件

@ -11,6 +11,9 @@ from core.audit_service import AuditService
from core.identity import RequestContext
from database.repositories.profile_repository import ProfileRepository
from database.repositories.workflow_repository import WorkflowRepository
from engines.character_flow import gradual_node_templates
from engines.fast_character_flow import fast_character_node_templates
from engines.quick_profile_flow import quick_profile_node_templates
WORKFLOW_TYPES = {"single_image", "character_gradual", "character_fast", "quick_profile"}
@ -49,14 +52,14 @@ class WorkflowService:
if workflow_type not in WORKFLOW_TYPES:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"不支持的工作流类型",
"unsupported workflow type",
{"field": "workflow_type", "allowed": sorted(WORKFLOW_TYPES)},
)
display_name = str(payload.get("display_name") or "").strip()
if not display_name:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"工作流名称不能为空",
"workflow display_name is required",
{"field": "display_name"},
)
workflow = self.workflow_repository.create_workflow(
@ -72,27 +75,13 @@ class WorkflowService:
"metadata": payload.get("metadata") or {},
}
)
nodes: list[WorkflowNode] = []
if workflow.workflow_type == "single_image":
node = self.create_node(
workflow.workflow_id,
{
"node_key": "virtual_single_image",
"title": "单图流",
"description": "单图流后端虚拟节点",
"node_type": "virtual_single_image",
"sort_order": 0,
"metadata": {"virtual": True},
},
ctx,
audit=False,
)
nodes = self._create_initial_nodes(workflow, payload, ctx)
if nodes:
self.workflow_repository.update_workflow(
workflow.workflow_id,
{"current_node_id": node.node_id},
{"current_node_id": nodes[0].node_id},
)
workflow = self.workflow_repository.get_workflow(workflow.workflow_id) or workflow
nodes.append(node)
self.audit_service.record(
ctx=ctx,
action="create_workflow",
@ -111,7 +100,7 @@ class WorkflowService:
if workflow is None:
raise ApiError(
ErrorCode.RESOURCE_NOT_FOUND,
"工作流不存在",
"workflow not found",
{"workflow_id": workflow_id},
)
return WorkflowDetail(
@ -129,13 +118,13 @@ class WorkflowService:
if "status" in payload and payload["status"] not in WORKFLOW_STATUSES:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"不支持的工作流状态",
"unsupported workflow status",
{"field": "status", "allowed": sorted(WORKFLOW_STATUSES)},
)
if "display_name" in payload and not str(payload["display_name"]).strip():
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"工作流名称不能为空",
"workflow display_name is required",
{"field": "display_name"},
)
update_payload = dict(payload)
@ -143,7 +132,7 @@ class WorkflowService:
update_payload["display_name"] = str(update_payload["display_name"]).strip()
after = self.workflow_repository.update_workflow(workflow_id, update_payload)
if after is None:
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "工作流不存在", {"workflow_id": workflow_id})
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow not found", {"workflow_id": workflow_id})
self.audit_service.record(
ctx=ctx,
action="update_workflow",
@ -166,14 +155,14 @@ class WorkflowService:
node_key = str(payload.get("node_key") or "").strip()
title = str(payload.get("title") or "").strip()
if not node_key:
raise ApiError(ErrorCode.VALIDATION_FAILED, "节点 key 不能为空", {"field": "node_key"})
raise ApiError(ErrorCode.VALIDATION_FAILED, "node_key is required", {"field": "node_key"})
if not title:
raise ApiError(ErrorCode.VALIDATION_FAILED, "节点标题不能为空", {"field": "title"})
raise ApiError(ErrorCode.VALIDATION_FAILED, "node title is required", {"field": "title"})
status = payload.get("status", "draft")
if status not in NODE_STATUSES:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"不支持的节点状态",
"unsupported node status",
{"field": "status", "allowed": sorted(NODE_STATUSES)},
)
node = self.workflow_repository.create_node(
@ -212,7 +201,7 @@ class WorkflowService:
def get_node(self, node_id: str) -> WorkflowNode:
node = self.workflow_repository.get_node(node_id)
if node is None:
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "节点不存在", {"node_id": node_id})
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "node not found", {"node_id": node_id})
return node
def update_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> WorkflowNode:
@ -220,17 +209,17 @@ class WorkflowService:
if "status" in payload and payload["status"] not in NODE_STATUSES:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"不支持的节点状态",
"unsupported node status",
{"field": "status", "allowed": sorted(NODE_STATUSES)},
)
if "title" in payload and not str(payload["title"]).strip():
raise ApiError(ErrorCode.VALIDATION_FAILED, "节点标题不能为空", {"field": "title"})
raise ApiError(ErrorCode.VALIDATION_FAILED, "node title is required", {"field": "title"})
update_payload = dict(payload)
if "title" in update_payload:
update_payload["title"] = str(update_payload["title"]).strip()
after = self.workflow_repository.update_node(node_id, update_payload)
if after is None:
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "节点不存在", {"node_id": node_id})
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "node not found", {"node_id": node_id})
self.audit_service.record(
ctx=ctx,
action="update_node",
@ -241,12 +230,30 @@ class WorkflowService:
)
return after
def _create_initial_nodes(
self,
workflow: ProfileWorkflow,
payload: dict[str, Any],
ctx: RequestContext,
) -> list[WorkflowNode]:
if payload.get("auto_create_nodes", True) is False:
return []
return [
self.create_node(
workflow.workflow_id,
template,
ctx,
audit=False,
)
for template in _initial_node_templates(workflow.workflow_type)
]
def _ensure_profile_exists(self, profile_id: str) -> None:
profile = self.profile_repository.get(profile_id)
if profile is None or profile.status == "deleted":
raise ApiError(
ErrorCode.RESOURCE_NOT_FOUND,
"角色档案不存在",
"profile not found",
{"profile_id": profile_id},
)
@ -255,7 +262,7 @@ class WorkflowService:
if workflow is None:
raise ApiError(
ErrorCode.RESOURCE_NOT_FOUND,
"工作流不存在",
"workflow not found",
{"workflow_id": workflow_id},
)
return workflow
@ -267,3 +274,24 @@ def new_workflow_id() -> str:
def new_node_id() -> str:
return f"node_{uuid4().hex}"
def _initial_node_templates(workflow_type: str) -> list[dict[str, Any]]:
if workflow_type == "single_image":
return [
{
"node_key": "virtual_single_image",
"title": "Single image",
"description": "Backend virtual node for the single image flow.",
"node_type": "virtual_single_image",
"sort_order": 0,
"metadata": {"virtual": True, "template": "single_image_v1"},
}
]
if workflow_type == "character_gradual":
return gradual_node_templates()
if workflow_type == "character_fast":
return fast_character_node_templates()
if workflow_type == "quick_profile":
return quick_profile_node_templates()
return []

查看文件

@ -1 +1,78 @@
"""Gradual character workflow engine placeholder."""
"""Gradual character workflow templates."""
from __future__ import annotations
from typing import Any
CHARACTER_GRADUAL_STEPS: tuple[dict[str, Any], ...] = (
{
"node_key": "profile_brief",
"title": "Profile brief",
"step_name": "profile_brief",
"description": "Core identity, personality, and visual anchors.",
"sort_order": 10,
},
{
"node_key": "headshot",
"title": "Headshot",
"step_name": "headshot",
"description": "Face, hair, eyes, and expression direction.",
"sort_order": 20,
},
{
"node_key": "half_body",
"title": "Half body",
"step_name": "half_body",
"description": "Upper body costume, posture, and silhouette.",
"sort_order": 30,
},
{
"node_key": "full_body",
"title": "Full body",
"step_name": "full_body",
"description": "Full outfit, proportions, and full-body design.",
"sort_order": 40,
},
{
"node_key": "expression_sheet",
"title": "Expression sheet",
"step_name": "expression_sheet",
"description": "Reusable expression variants.",
"sort_order": 50,
},
{
"node_key": "pose_sheet",
"title": "Pose sheet",
"step_name": "pose_sheet",
"description": "Reusable pose and action variants.",
"sort_order": 60,
},
{
"node_key": "style_lock",
"title": "Style lock",
"step_name": "style_lock",
"description": "Final style pass and prompt consistency lock.",
"sort_order": 70,
},
{
"node_key": "final_pack",
"title": "Final pack",
"step_name": "final_pack",
"description": "Final accepted images and production metadata.",
"sort_order": 80,
},
)
def gradual_node_templates() -> list[dict[str, Any]]:
return [
{
**step,
"node_type": "generation",
"status": "draft",
"branch_name": "main",
"metadata": {"template": "character_gradual_v1"},
}
for step in CHARACTER_GRADUAL_STEPS
]

查看文件

@ -1 +1,43 @@
"""Fast character package workflow engine placeholder."""
"""Fast character workflow templates."""
from __future__ import annotations
from typing import Any
FAST_CHARACTER_STEPS: tuple[dict[str, Any], ...] = (
{
"node_key": "fast_profile",
"title": "Fast profile",
"step_name": "fast_profile",
"description": "Condensed character prompt and core visual direction.",
"sort_order": 10,
},
{
"node_key": "fast_candidates",
"title": "Fast candidates",
"step_name": "fast_candidates",
"description": "Candidate image batch for quick selection.",
"sort_order": 20,
},
{
"node_key": "fast_final",
"title": "Fast final",
"step_name": "fast_final",
"description": "Selected final image and reusable metadata.",
"sort_order": 30,
},
)
def fast_character_node_templates() -> list[dict[str, Any]]:
return [
{
**step,
"node_type": "generation",
"status": "draft",
"branch_name": "main",
"metadata": {"template": "character_fast_v1"},
}
for step in FAST_CHARACTER_STEPS
]

查看文件

@ -1 +1,32 @@
"""Quick profile draft workflow placeholder."""
"""Quick profile workflow templates."""
from __future__ import annotations
from typing import Any
def quick_profile_node_templates() -> list[dict[str, Any]]:
return [
{
"node_key": "questionnaire",
"title": "Questionnaire",
"description": "User answers and normalized draft profile.",
"step_name": "questionnaire",
"node_type": "profile_draft",
"status": "draft",
"branch_name": "main",
"sort_order": 10,
"metadata": {"template": "quick_profile_v1"},
},
{
"node_key": "preview",
"title": "Preview",
"description": "Preview prompts before saving a character profile.",
"step_name": "preview",
"node_type": "profile_preview",
"status": "draft",
"branch_name": "main",
"sort_order": 20,
"metadata": {"template": "quick_profile_v1"},
},
]

查看文件

@ -37,7 +37,7 @@ class WorkflowServiceTests(unittest.TestCase):
user_id="user_001",
permissions={"can_write"},
)
self.profile = self.profile_service.create_profile({"name": "莉娅"}, self.ctx)
self.profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx)
def tearDown(self):
self.tmp.cleanup()
@ -47,7 +47,7 @@ class WorkflowServiceTests(unittest.TestCase):
self.profile.profile_id,
{
"workflow_type": "single_image",
"display_name": "单图流 2026-07-04",
"display_name": "Single image 2026-07-04",
"mode": "quick",
},
self.ctx,
@ -77,24 +77,71 @@ class WorkflowServiceTests(unittest.TestCase):
self.assertEqual(audit["action"], "create_workflow")
self.assertEqual(audit["target_type"], "profile_workflow")
def test_create_character_gradual_workflow_creates_eight_template_nodes(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{
"workflow_type": "character_gradual",
"display_name": "Character gradual",
"mode": "professional",
},
self.ctx,
)
self.assertEqual(len(detail.nodes), 8)
self.assertEqual(detail.nodes[0].node_key, "profile_brief")
self.assertEqual(detail.nodes[-1].node_key, "final_pack")
self.assertEqual(detail.workflow.current_node_id, detail.nodes[0].node_id)
self.assertEqual(
[node.sort_order for node in self.workflow_service.list_nodes(detail.workflow.workflow_id)],
[10, 20, 30, 40, 50, 60, 70, 80],
)
def test_create_fast_character_workflow_creates_fast_template_nodes(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{
"workflow_type": "character_fast",
"display_name": "Fast character",
},
self.ctx,
)
self.assertEqual([node.node_key for node in detail.nodes], ["fast_profile", "fast_candidates", "fast_final"])
self.assertEqual(detail.workflow.current_node_id, detail.nodes[0].node_id)
def test_create_quick_profile_workflow_creates_profile_draft_nodes(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{
"workflow_type": "quick_profile",
"display_name": "Quick profile",
},
self.ctx,
)
self.assertEqual([node.node_key for node in detail.nodes], ["questionnaire", "preview"])
self.assertEqual(detail.nodes[0].node_type, "profile_draft")
def test_create_and_list_nodes_ordered_by_sort_order(self):
detail = self.workflow_service.create_workflow(
self.profile.profile_id,
{
"workflow_type": "character_gradual",
"display_name": "标准角色流",
"display_name": "Manual character flow",
"mode": "standard",
"auto_create_nodes": False,
},
self.ctx,
)
second = self.workflow_service.create_node(
detail.workflow.workflow_id,
{"node_key": "full_body", "title": "全身", "sort_order": 20},
{"node_key": "full_body", "title": "Full body", "sort_order": 20},
self.ctx,
)
first = self.workflow_service.create_node(
detail.workflow.workflow_id,
{"node_key": "portrait", "title": "头像", "sort_order": 10},
{"node_key": "portrait", "title": "Portrait", "sort_order": 10},
self.ctx,
)
@ -107,13 +154,14 @@ class WorkflowServiceTests(unittest.TestCase):
self.profile.profile_id,
{
"workflow_type": "character_gradual",
"display_name": "标准角色流",
"display_name": "Manual character flow",
"auto_create_nodes": False,
},
self.ctx,
)
node = self.workflow_service.create_node(
detail.workflow.workflow_id,
{"node_key": "portrait", "title": "头像"},
{"node_key": "portrait", "title": "Portrait"},
self.ctx,
)
@ -150,7 +198,7 @@ class WorkflowServiceTests(unittest.TestCase):
self.profile.profile_id,
{
"workflow_type": "unknown",
"display_name": "坏工作流",
"display_name": "Bad workflow",
},
self.ctx,
)
@ -162,7 +210,8 @@ class WorkflowServiceTests(unittest.TestCase):
self.profile.profile_id,
{
"workflow_type": "character_gradual",
"display_name": "标准角色流",
"display_name": "Manual character flow",
"auto_create_nodes": False,
},
self.ctx,
)
@ -172,7 +221,7 @@ class WorkflowServiceTests(unittest.TestCase):
detail.workflow.workflow_id,
{
"node_key": "portrait",
"title": "头像",
"title": "Portrait",
"status": "mystery",
},
self.ctx,
@ -186,7 +235,7 @@ class WorkflowServiceTests(unittest.TestCase):
"profile_missing",
{
"workflow_type": "single_image",
"display_name": "孤儿工作流",
"display_name": "Orphan workflow",
},
self.ctx,
)