feat: add workflow and node persistence
这个提交存在于:
父节点
027a2a1c16
当前提交
8340e4f4d9
共有 7 个文件被更改,包括 967 次插入 和 7 次删除
|
|
@ -10,6 +10,7 @@ Prepared:
|
||||||
- v3 top-level module layout: `adapters`, `core`, `engines`, `backend`, `database`, `assets_builtin`, `frontend`, `pages`, `web`, and `data`.
|
- v3 top-level module layout: `adapters`, `core`, `engines`, `backend`, `database`, `assets_builtin`, `frontend`, `pages`, `web`, and `data`.
|
||||||
- Database helper foundation with SQLite default and optional PostgreSQL selection from `config.yaml`.
|
- Database helper foundation with SQLite default and optional PostgreSQL selection from `config.yaml`.
|
||||||
- `character_profiles` persistence and service layer with audit logging.
|
- `character_profiles` persistence and service layer with audit logging.
|
||||||
|
- `profile_workflows` and `workflow_nodes` persistence and service layer with audit logging.
|
||||||
- Frontend source skeleton for Plugin Page UI and Standalone Web UI.
|
- Frontend source skeleton for Plugin Page UI and Standalone Web UI.
|
||||||
- Runtime data directories with `.gitkeep` files only.
|
- Runtime data directories with `.gitkeep` files only.
|
||||||
- Generated data, local virtual environments, node modules, and build artifacts are ignored.
|
- Generated data, local virtual environments, node modules, and build artifacts are ignored.
|
||||||
|
|
@ -37,10 +38,10 @@ C:\Users\Administrator\.cache\codex-runtimes\codex-primary-runtime\dependencies\
|
||||||
Follow `imagestudio_codex_hermes_dev_flow_v3.md` and continue with:
|
Follow `imagestudio_codex_hermes_dev_flow_v3.md` and continue with:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
S1-2 实现 profile_workflows 和 workflow_nodes
|
|
||||||
S1-3 实现 generation_images
|
S1-3 实现 generation_images
|
||||||
S2-1 实现 ui/bootstrap
|
S2-1 实现 ui/bootstrap
|
||||||
S2-2 实现 dashboard/summary
|
S2-2 实现 dashboard/summary
|
||||||
|
S2-3 实现 profiles CRUD routes
|
||||||
```
|
```
|
||||||
|
|
||||||
## P0 Frozen API Checklist
|
## P0 Frozen API Checklist
|
||||||
|
|
|
||||||
11
README.md
11
README.md
|
|
@ -39,14 +39,15 @@ Implemented foundation:
|
||||||
- Database helper with SQLite default and optional PostgreSQL selection.
|
- Database helper with SQLite default and optional PostgreSQL selection.
|
||||||
- Initial `audit_logs` migration and audit recording service.
|
- Initial `audit_logs` migration and audit recording service.
|
||||||
- `character_profiles` migration, repository, service, and audit-backed create/update/delete.
|
- `character_profiles` migration, repository, service, and audit-backed create/update/delete.
|
||||||
|
- `profile_workflows` and `workflow_nodes` migrations, repositories, services, and audit-backed create/update.
|
||||||
|
|
||||||
Next tasks:
|
Next tasks:
|
||||||
|
|
||||||
1. `S1-2` `profile_workflows` and `workflow_nodes`.
|
1. `S1-3` `generation_images`.
|
||||||
2. `S1-3` `generation_images`.
|
2. Finish route-level usage of S0 helpers.
|
||||||
3. Finish route-level usage of S0 helpers.
|
3. `S2-1` `ui/bootstrap`.
|
||||||
4. `S2-1` `ui/bootstrap`.
|
4. `S2-2` `dashboard/summary`.
|
||||||
5. `S2-2` `dashboard/summary`.
|
5. `S2-3` profiles CRUD routes.
|
||||||
|
|
||||||
## Repository Layout
|
## Repository Layout
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1,92 @@
|
||||||
"""Workflow DTO placeholder."""
|
"""Workflow and node DTOs."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ProfileWorkflow:
|
||||||
|
workflow_id: str
|
||||||
|
profile_id: str
|
||||||
|
workflow_type: str
|
||||||
|
display_name: str
|
||||||
|
mode: str | None
|
||||||
|
status: str
|
||||||
|
current_node_id: str | None
|
||||||
|
current_branch: str
|
||||||
|
session_key: str | None
|
||||||
|
metadata: dict[str, Any]
|
||||||
|
created_at: str
|
||||||
|
updated_at: str
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"workflow_id": self.workflow_id,
|
||||||
|
"profile_id": self.profile_id,
|
||||||
|
"workflow_type": self.workflow_type,
|
||||||
|
"display_name": self.display_name,
|
||||||
|
"mode": self.mode,
|
||||||
|
"status": self.status,
|
||||||
|
"current_node_id": self.current_node_id,
|
||||||
|
"current_branch": self.current_branch,
|
||||||
|
"session_key": self.session_key,
|
||||||
|
"metadata": self.metadata,
|
||||||
|
"created_at": self.created_at,
|
||||||
|
"updated_at": self.updated_at,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class WorkflowNode:
|
||||||
|
node_id: str
|
||||||
|
workflow_id: str
|
||||||
|
node_key: str
|
||||||
|
title: str
|
||||||
|
description: str | None
|
||||||
|
step_name: str | None
|
||||||
|
node_type: str
|
||||||
|
status: str
|
||||||
|
branch_name: str
|
||||||
|
prompt: str | None
|
||||||
|
negative_prompt: str | None
|
||||||
|
model_route: str | None
|
||||||
|
selected_image_id: str | None
|
||||||
|
sort_order: int
|
||||||
|
metadata: dict[str, Any]
|
||||||
|
created_at: str
|
||||||
|
updated_at: str
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"node_id": self.node_id,
|
||||||
|
"workflow_id": self.workflow_id,
|
||||||
|
"node_key": self.node_key,
|
||||||
|
"title": self.title,
|
||||||
|
"description": self.description,
|
||||||
|
"step_name": self.step_name,
|
||||||
|
"node_type": self.node_type,
|
||||||
|
"status": self.status,
|
||||||
|
"branch_name": self.branch_name,
|
||||||
|
"prompt": self.prompt,
|
||||||
|
"negative_prompt": self.negative_prompt,
|
||||||
|
"model_route": self.model_route,
|
||||||
|
"selected_image_id": self.selected_image_id,
|
||||||
|
"sort_order": self.sort_order,
|
||||||
|
"metadata": self.metadata,
|
||||||
|
"created_at": self.created_at,
|
||||||
|
"updated_at": self.updated_at,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class WorkflowDetail:
|
||||||
|
workflow: ProfileWorkflow
|
||||||
|
nodes: list[WorkflowNode]
|
||||||
|
|
||||||
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
return {
|
||||||
|
"workflow": self.workflow.to_dict(),
|
||||||
|
"nodes": [node.to_dict() for node in self.nodes],
|
||||||
|
}
|
||||||
|
|
|
||||||
269
backend/services/workflow_service.py
普通文件
269
backend/services/workflow_service.py
普通文件
|
|
@ -0,0 +1,269 @@
|
||||||
|
"""Profile workflow and workflow node service."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
|
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowDetail, WorkflowNode
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
WORKFLOW_TYPES = {"single_image", "character_gradual", "character_fast", "quick_profile"}
|
||||||
|
WORKFLOW_STATUSES = {"draft", "active", "running", "completed", "failed", "archived", "cancelled"}
|
||||||
|
NODE_STATUSES = {
|
||||||
|
"draft",
|
||||||
|
"queued",
|
||||||
|
"running",
|
||||||
|
"completed",
|
||||||
|
"selected",
|
||||||
|
"failed",
|
||||||
|
"skipped",
|
||||||
|
"discarded",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class WorkflowService:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
workflow_repository: WorkflowRepository,
|
||||||
|
profile_repository: ProfileRepository,
|
||||||
|
audit_service: AuditService,
|
||||||
|
):
|
||||||
|
self.workflow_repository = workflow_repository
|
||||||
|
self.profile_repository = profile_repository
|
||||||
|
self.audit_service = audit_service
|
||||||
|
|
||||||
|
def create_workflow(
|
||||||
|
self,
|
||||||
|
profile_id: str,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
ctx: RequestContext,
|
||||||
|
) -> WorkflowDetail:
|
||||||
|
self._ensure_profile_exists(profile_id)
|
||||||
|
workflow_type = str(payload.get("workflow_type") or "").strip()
|
||||||
|
if workflow_type not in WORKFLOW_TYPES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"不支持的工作流类型",
|
||||||
|
{"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,
|
||||||
|
"工作流名称不能为空",
|
||||||
|
{"field": "display_name"},
|
||||||
|
)
|
||||||
|
workflow = self.workflow_repository.create_workflow(
|
||||||
|
{
|
||||||
|
"workflow_id": payload.get("workflow_id") or new_workflow_id(),
|
||||||
|
"profile_id": profile_id,
|
||||||
|
"workflow_type": workflow_type,
|
||||||
|
"display_name": display_name,
|
||||||
|
"mode": payload.get("mode"),
|
||||||
|
"status": payload.get("status", "draft"),
|
||||||
|
"current_branch": payload.get("current_branch", "main"),
|
||||||
|
"session_key": payload.get("session_key") or ctx.session_key,
|
||||||
|
"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,
|
||||||
|
)
|
||||||
|
self.workflow_repository.update_workflow(
|
||||||
|
workflow.workflow_id,
|
||||||
|
{"current_node_id": node.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",
|
||||||
|
target_type="profile_workflow",
|
||||||
|
target_id=workflow.workflow_id,
|
||||||
|
after_snapshot=workflow.to_dict(),
|
||||||
|
)
|
||||||
|
return WorkflowDetail(workflow=workflow, nodes=nodes)
|
||||||
|
|
||||||
|
def list_workflows(self, profile_id: str) -> list[ProfileWorkflow]:
|
||||||
|
self._ensure_profile_exists(profile_id)
|
||||||
|
return self.workflow_repository.list_workflows(profile_id)
|
||||||
|
|
||||||
|
def get_workflow(self, workflow_id: str) -> WorkflowDetail:
|
||||||
|
workflow = self.workflow_repository.get_workflow(workflow_id)
|
||||||
|
if workflow is None:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.RESOURCE_NOT_FOUND,
|
||||||
|
"工作流不存在",
|
||||||
|
{"workflow_id": workflow_id},
|
||||||
|
)
|
||||||
|
return WorkflowDetail(
|
||||||
|
workflow=workflow,
|
||||||
|
nodes=self.workflow_repository.list_nodes(workflow_id),
|
||||||
|
)
|
||||||
|
|
||||||
|
def update_workflow(
|
||||||
|
self,
|
||||||
|
workflow_id: str,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
ctx: RequestContext,
|
||||||
|
) -> ProfileWorkflow:
|
||||||
|
before = self.get_workflow(workflow_id).workflow
|
||||||
|
if "status" in payload and payload["status"] not in WORKFLOW_STATUSES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"不支持的工作流状态",
|
||||||
|
{"field": "status", "allowed": sorted(WORKFLOW_STATUSES)},
|
||||||
|
)
|
||||||
|
if "display_name" in payload and not str(payload["display_name"]).strip():
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"工作流名称不能为空",
|
||||||
|
{"field": "display_name"},
|
||||||
|
)
|
||||||
|
update_payload = dict(payload)
|
||||||
|
if "display_name" in update_payload:
|
||||||
|
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})
|
||||||
|
self.audit_service.record(
|
||||||
|
ctx=ctx,
|
||||||
|
action="update_workflow",
|
||||||
|
target_type="profile_workflow",
|
||||||
|
target_id=workflow_id,
|
||||||
|
before_snapshot=before.to_dict(),
|
||||||
|
after_snapshot=after.to_dict(),
|
||||||
|
)
|
||||||
|
return after
|
||||||
|
|
||||||
|
def create_node(
|
||||||
|
self,
|
||||||
|
workflow_id: str,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
ctx: RequestContext,
|
||||||
|
*,
|
||||||
|
audit: bool = True,
|
||||||
|
) -> WorkflowNode:
|
||||||
|
self._ensure_workflow_exists(workflow_id)
|
||||||
|
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"})
|
||||||
|
if not title:
|
||||||
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "节点标题不能为空", {"field": "title"})
|
||||||
|
status = payload.get("status", "draft")
|
||||||
|
if status not in NODE_STATUSES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"不支持的节点状态",
|
||||||
|
{"field": "status", "allowed": sorted(NODE_STATUSES)},
|
||||||
|
)
|
||||||
|
node = self.workflow_repository.create_node(
|
||||||
|
{
|
||||||
|
"node_id": payload.get("node_id") or new_node_id(),
|
||||||
|
"workflow_id": workflow_id,
|
||||||
|
"node_key": node_key,
|
||||||
|
"title": title,
|
||||||
|
"description": payload.get("description"),
|
||||||
|
"step_name": payload.get("step_name"),
|
||||||
|
"node_type": payload.get("node_type", "generation"),
|
||||||
|
"status": status,
|
||||||
|
"branch_name": payload.get("branch_name", "main"),
|
||||||
|
"prompt": payload.get("prompt"),
|
||||||
|
"negative_prompt": payload.get("negative_prompt"),
|
||||||
|
"model_route": payload.get("model_route"),
|
||||||
|
"selected_image_id": payload.get("selected_image_id"),
|
||||||
|
"sort_order": payload.get("sort_order", 0),
|
||||||
|
"metadata": payload.get("metadata") or {},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if audit:
|
||||||
|
self.audit_service.record(
|
||||||
|
ctx=ctx,
|
||||||
|
action="create_node",
|
||||||
|
target_type="workflow_node",
|
||||||
|
target_id=node.node_id,
|
||||||
|
after_snapshot=node.to_dict(),
|
||||||
|
)
|
||||||
|
return node
|
||||||
|
|
||||||
|
def list_nodes(self, workflow_id: str) -> list[WorkflowNode]:
|
||||||
|
self._ensure_workflow_exists(workflow_id)
|
||||||
|
return self.workflow_repository.list_nodes(workflow_id)
|
||||||
|
|
||||||
|
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})
|
||||||
|
return node
|
||||||
|
|
||||||
|
def update_node(self, node_id: str, payload: dict[str, Any], ctx: RequestContext) -> WorkflowNode:
|
||||||
|
before = self.get_node(node_id)
|
||||||
|
if "status" in payload and payload["status"] not in NODE_STATUSES:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"不支持的节点状态",
|
||||||
|
{"field": "status", "allowed": sorted(NODE_STATUSES)},
|
||||||
|
)
|
||||||
|
if "title" in payload and not str(payload["title"]).strip():
|
||||||
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "节点标题不能为空", {"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})
|
||||||
|
self.audit_service.record(
|
||||||
|
ctx=ctx,
|
||||||
|
action="update_node",
|
||||||
|
target_type="workflow_node",
|
||||||
|
target_id=node_id,
|
||||||
|
before_snapshot=before.to_dict(),
|
||||||
|
after_snapshot=after.to_dict(),
|
||||||
|
)
|
||||||
|
return after
|
||||||
|
|
||||||
|
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_id": profile_id},
|
||||||
|
)
|
||||||
|
|
||||||
|
def _ensure_workflow_exists(self, workflow_id: str) -> ProfileWorkflow:
|
||||||
|
workflow = self.workflow_repository.get_workflow(workflow_id)
|
||||||
|
if workflow is None:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.RESOURCE_NOT_FOUND,
|
||||||
|
"工作流不存在",
|
||||||
|
{"workflow_id": workflow_id},
|
||||||
|
)
|
||||||
|
return workflow
|
||||||
|
|
||||||
|
|
||||||
|
def new_workflow_id() -> str:
|
||||||
|
return f"workflow_{uuid4().hex}"
|
||||||
|
|
||||||
|
|
||||||
|
def new_node_id() -> str:
|
||||||
|
return f"node_{uuid4().hex}"
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS profile_workflows (
|
||||||
|
workflow_id TEXT PRIMARY KEY,
|
||||||
|
profile_id TEXT NOT NULL,
|
||||||
|
workflow_type TEXT NOT NULL,
|
||||||
|
display_name TEXT NOT NULL,
|
||||||
|
mode TEXT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'draft',
|
||||||
|
current_node_id TEXT,
|
||||||
|
current_branch TEXT DEFAULT 'main',
|
||||||
|
session_key TEXT,
|
||||||
|
metadata_json TEXT,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY(profile_id) REFERENCES character_profiles(profile_id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_profile_workflows_profile
|
||||||
|
ON profile_workflows(profile_id, workflow_type, status);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_profile_workflows_status
|
||||||
|
ON profile_workflows(status);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS workflow_nodes (
|
||||||
|
node_id TEXT PRIMARY KEY,
|
||||||
|
workflow_id TEXT NOT NULL,
|
||||||
|
node_key TEXT NOT NULL,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
description TEXT,
|
||||||
|
step_name TEXT,
|
||||||
|
node_type TEXT DEFAULT 'generation',
|
||||||
|
status TEXT DEFAULT 'draft',
|
||||||
|
branch_name TEXT DEFAULT 'main',
|
||||||
|
prompt TEXT,
|
||||||
|
negative_prompt TEXT,
|
||||||
|
model_route TEXT,
|
||||||
|
selected_image_id TEXT,
|
||||||
|
sort_order INTEGER DEFAULT 0,
|
||||||
|
metadata_json TEXT,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY(workflow_id) REFERENCES profile_workflows(workflow_id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_nodes_workflow
|
||||||
|
ON workflow_nodes(workflow_id, sort_order);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_nodes_status
|
||||||
|
ON workflow_nodes(status);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_nodes_key
|
||||||
|
ON workflow_nodes(workflow_id, node_key);
|
||||||
|
|
@ -0,0 +1,349 @@
|
||||||
|
"""Repository for profile_workflows and workflow_nodes."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowNode
|
||||||
|
from database.db_helper import Database
|
||||||
|
|
||||||
|
|
||||||
|
WORKFLOW_COLUMNS = """
|
||||||
|
workflow_id,
|
||||||
|
profile_id,
|
||||||
|
workflow_type,
|
||||||
|
display_name,
|
||||||
|
mode,
|
||||||
|
status,
|
||||||
|
current_node_id,
|
||||||
|
current_branch,
|
||||||
|
session_key,
|
||||||
|
metadata_json,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
"""
|
||||||
|
|
||||||
|
NODE_COLUMNS = """
|
||||||
|
node_id,
|
||||||
|
workflow_id,
|
||||||
|
node_key,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
step_name,
|
||||||
|
node_type,
|
||||||
|
status,
|
||||||
|
branch_name,
|
||||||
|
prompt,
|
||||||
|
negative_prompt,
|
||||||
|
model_route,
|
||||||
|
selected_image_id,
|
||||||
|
sort_order,
|
||||||
|
metadata_json,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
class WorkflowRepository:
|
||||||
|
def __init__(self, database: Database):
|
||||||
|
self.database = database
|
||||||
|
|
||||||
|
@property
|
||||||
|
def placeholder(self) -> str:
|
||||||
|
return self.database._placeholder
|
||||||
|
|
||||||
|
def create_workflow(self, payload: dict[str, Any]) -> ProfileWorkflow:
|
||||||
|
placeholder = self.placeholder
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
f"""
|
||||||
|
INSERT INTO profile_workflows (
|
||||||
|
workflow_id,
|
||||||
|
profile_id,
|
||||||
|
workflow_type,
|
||||||
|
display_name,
|
||||||
|
mode,
|
||||||
|
status,
|
||||||
|
current_node_id,
|
||||||
|
current_branch,
|
||||||
|
session_key,
|
||||||
|
metadata_json
|
||||||
|
) VALUES (
|
||||||
|
{placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||||
|
{placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||||
|
{placeholder}, {placeholder}
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
payload["workflow_id"],
|
||||||
|
payload["profile_id"],
|
||||||
|
payload["workflow_type"],
|
||||||
|
payload["display_name"],
|
||||||
|
payload.get("mode"),
|
||||||
|
payload.get("status", "draft"),
|
||||||
|
payload.get("current_node_id"),
|
||||||
|
payload.get("current_branch", "main"),
|
||||||
|
payload.get("session_key"),
|
||||||
|
_json_or_none(payload.get("metadata")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
workflow = self.get_workflow(payload["workflow_id"])
|
||||||
|
if workflow is None:
|
||||||
|
raise RuntimeError("workflow was inserted but could not be read back")
|
||||||
|
return workflow
|
||||||
|
|
||||||
|
def get_workflow(self, workflow_id: str) -> ProfileWorkflow | None:
|
||||||
|
placeholder = self.placeholder
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
row = conn.execute(
|
||||||
|
f"SELECT {WORKFLOW_COLUMNS} FROM profile_workflows WHERE workflow_id = {placeholder}",
|
||||||
|
(workflow_id,),
|
||||||
|
).fetchone()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return _row_to_workflow(row) if row else None
|
||||||
|
|
||||||
|
def list_workflows(self, profile_id: str, status: str | None = None) -> list[ProfileWorkflow]:
|
||||||
|
where = [f"profile_id = {self.placeholder}"]
|
||||||
|
params: list[Any] = [profile_id]
|
||||||
|
if status:
|
||||||
|
where.append(f"status = {self.placeholder}")
|
||||||
|
params.append(status)
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
rows = conn.execute(
|
||||||
|
f"""
|
||||||
|
SELECT {WORKFLOW_COLUMNS}
|
||||||
|
FROM profile_workflows
|
||||||
|
WHERE {" AND ".join(where)}
|
||||||
|
ORDER BY updated_at DESC, created_at DESC
|
||||||
|
""",
|
||||||
|
tuple(params),
|
||||||
|
).fetchall()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return [_row_to_workflow(row) for row in rows]
|
||||||
|
|
||||||
|
def update_workflow(self, workflow_id: str, payload: dict[str, Any]) -> ProfileWorkflow | None:
|
||||||
|
allowed = {
|
||||||
|
"display_name",
|
||||||
|
"mode",
|
||||||
|
"status",
|
||||||
|
"current_node_id",
|
||||||
|
"current_branch",
|
||||||
|
"session_key",
|
||||||
|
"metadata",
|
||||||
|
}
|
||||||
|
assignments: list[str] = []
|
||||||
|
params: list[Any] = []
|
||||||
|
placeholder = self.placeholder
|
||||||
|
for key in allowed:
|
||||||
|
if key not in payload:
|
||||||
|
continue
|
||||||
|
column = "metadata_json" if key == "metadata" else key
|
||||||
|
value = _json_or_none(payload[key]) if key == "metadata" else payload[key]
|
||||||
|
assignments.append(f"{column} = {placeholder}")
|
||||||
|
params.append(value)
|
||||||
|
if not assignments:
|
||||||
|
return self.get_workflow(workflow_id)
|
||||||
|
assignments.append("updated_at = CURRENT_TIMESTAMP")
|
||||||
|
params.append(workflow_id)
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
f"""
|
||||||
|
UPDATE profile_workflows
|
||||||
|
SET {", ".join(assignments)}
|
||||||
|
WHERE workflow_id = {placeholder}
|
||||||
|
""",
|
||||||
|
tuple(params),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return self.get_workflow(workflow_id)
|
||||||
|
|
||||||
|
def create_node(self, payload: dict[str, Any]) -> WorkflowNode:
|
||||||
|
placeholder = self.placeholder
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
f"""
|
||||||
|
INSERT INTO workflow_nodes (
|
||||||
|
node_id,
|
||||||
|
workflow_id,
|
||||||
|
node_key,
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
step_name,
|
||||||
|
node_type,
|
||||||
|
status,
|
||||||
|
branch_name,
|
||||||
|
prompt,
|
||||||
|
negative_prompt,
|
||||||
|
model_route,
|
||||||
|
selected_image_id,
|
||||||
|
sort_order,
|
||||||
|
metadata_json
|
||||||
|
) VALUES (
|
||||||
|
{placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||||
|
{placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||||
|
{placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||||
|
{placeholder}, {placeholder}, {placeholder}
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
payload["node_id"],
|
||||||
|
payload["workflow_id"],
|
||||||
|
payload["node_key"],
|
||||||
|
payload["title"],
|
||||||
|
payload.get("description"),
|
||||||
|
payload.get("step_name"),
|
||||||
|
payload.get("node_type", "generation"),
|
||||||
|
payload.get("status", "draft"),
|
||||||
|
payload.get("branch_name", "main"),
|
||||||
|
payload.get("prompt"),
|
||||||
|
payload.get("negative_prompt"),
|
||||||
|
payload.get("model_route"),
|
||||||
|
payload.get("selected_image_id"),
|
||||||
|
int(payload.get("sort_order", 0)),
|
||||||
|
_json_or_none(payload.get("metadata")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
node = self.get_node(payload["node_id"])
|
||||||
|
if node is None:
|
||||||
|
raise RuntimeError("node was inserted but could not be read back")
|
||||||
|
return node
|
||||||
|
|
||||||
|
def get_node(self, node_id: str) -> WorkflowNode | None:
|
||||||
|
placeholder = self.placeholder
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
row = conn.execute(
|
||||||
|
f"SELECT {NODE_COLUMNS} FROM workflow_nodes WHERE node_id = {placeholder}",
|
||||||
|
(node_id,),
|
||||||
|
).fetchone()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return _row_to_node(row) if row else None
|
||||||
|
|
||||||
|
def list_nodes(self, workflow_id: str) -> list[WorkflowNode]:
|
||||||
|
placeholder = self.placeholder
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
rows = conn.execute(
|
||||||
|
f"""
|
||||||
|
SELECT {NODE_COLUMNS}
|
||||||
|
FROM workflow_nodes
|
||||||
|
WHERE workflow_id = {placeholder}
|
||||||
|
ORDER BY sort_order ASC, created_at ASC
|
||||||
|
""",
|
||||||
|
(workflow_id,),
|
||||||
|
).fetchall()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return [_row_to_node(row) for row in rows]
|
||||||
|
|
||||||
|
def update_node(self, node_id: str, payload: dict[str, Any]) -> WorkflowNode | None:
|
||||||
|
allowed = {
|
||||||
|
"title",
|
||||||
|
"description",
|
||||||
|
"step_name",
|
||||||
|
"node_type",
|
||||||
|
"status",
|
||||||
|
"branch_name",
|
||||||
|
"prompt",
|
||||||
|
"negative_prompt",
|
||||||
|
"model_route",
|
||||||
|
"selected_image_id",
|
||||||
|
"sort_order",
|
||||||
|
"metadata",
|
||||||
|
}
|
||||||
|
assignments: list[str] = []
|
||||||
|
params: list[Any] = []
|
||||||
|
placeholder = self.placeholder
|
||||||
|
for key in allowed:
|
||||||
|
if key not in payload:
|
||||||
|
continue
|
||||||
|
column = "metadata_json" if key == "metadata" else key
|
||||||
|
value = _json_or_none(payload[key]) if key == "metadata" else payload[key]
|
||||||
|
assignments.append(f"{column} = {placeholder}")
|
||||||
|
params.append(value)
|
||||||
|
if not assignments:
|
||||||
|
return self.get_node(node_id)
|
||||||
|
assignments.append("updated_at = CURRENT_TIMESTAMP")
|
||||||
|
params.append(node_id)
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
conn.execute(
|
||||||
|
f"""
|
||||||
|
UPDATE workflow_nodes
|
||||||
|
SET {", ".join(assignments)}
|
||||||
|
WHERE node_id = {placeholder}
|
||||||
|
""",
|
||||||
|
tuple(params),
|
||||||
|
)
|
||||||
|
conn.commit()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return self.get_node(node_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _row_to_workflow(row) -> ProfileWorkflow:
|
||||||
|
return ProfileWorkflow(
|
||||||
|
workflow_id=row["workflow_id"],
|
||||||
|
profile_id=row["profile_id"],
|
||||||
|
workflow_type=row["workflow_type"],
|
||||||
|
display_name=row["display_name"],
|
||||||
|
mode=row["mode"],
|
||||||
|
status=row["status"],
|
||||||
|
current_node_id=row["current_node_id"],
|
||||||
|
current_branch=row["current_branch"] or "main",
|
||||||
|
session_key=row["session_key"],
|
||||||
|
metadata=_json_loads(row["metadata_json"]),
|
||||||
|
created_at=str(row["created_at"]),
|
||||||
|
updated_at=str(row["updated_at"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _row_to_node(row) -> WorkflowNode:
|
||||||
|
return WorkflowNode(
|
||||||
|
node_id=row["node_id"],
|
||||||
|
workflow_id=row["workflow_id"],
|
||||||
|
node_key=row["node_key"],
|
||||||
|
title=row["title"],
|
||||||
|
description=row["description"],
|
||||||
|
step_name=row["step_name"],
|
||||||
|
node_type=row["node_type"],
|
||||||
|
status=row["status"],
|
||||||
|
branch_name=row["branch_name"] or "main",
|
||||||
|
prompt=row["prompt"],
|
||||||
|
negative_prompt=row["negative_prompt"],
|
||||||
|
model_route=row["model_route"],
|
||||||
|
selected_image_id=row["selected_image_id"],
|
||||||
|
sort_order=int(row["sort_order"] or 0),
|
||||||
|
metadata=_json_loads(row["metadata_json"]),
|
||||||
|
created_at=str(row["created_at"]),
|
||||||
|
updated_at=str(row["updated_at"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _json_or_none(value: Any | None) -> str | None:
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _json_loads(value: str | None) -> dict[str, Any]:
|
||||||
|
if not value:
|
||||||
|
return {}
|
||||||
|
return json.loads(value)
|
||||||
198
tests/test_workflow_service.py
普通文件
198
tests/test_workflow_service.py
普通文件
|
|
@ -0,0 +1,198 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
|
from backend.services.profile_service import ProfileService
|
||||||
|
from backend.services.workflow_service import WorkflowService
|
||||||
|
from core.audit_service import AuditService
|
||||||
|
from core.identity import RequestContext
|
||||||
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||||
|
from database.repositories.profile_repository import ProfileRepository
|
||||||
|
from database.repositories.workflow_repository import WorkflowRepository
|
||||||
|
|
||||||
|
|
||||||
|
class WorkflowServiceTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
db_path = Path(self.tmp.name) / "imagestudio.db"
|
||||||
|
self.database = Database(DatabaseSettings(sqlite=SqliteSettings(path=str(db_path))))
|
||||||
|
self.database.initialize()
|
||||||
|
self.audit_service = AuditService(self.database)
|
||||||
|
self.profile_repository = ProfileRepository(self.database)
|
||||||
|
self.workflow_repository = WorkflowRepository(self.database)
|
||||||
|
self.profile_service = ProfileService(
|
||||||
|
repository=self.profile_repository,
|
||||||
|
audit_service=self.audit_service,
|
||||||
|
)
|
||||||
|
self.workflow_service = WorkflowService(
|
||||||
|
workflow_repository=self.workflow_repository,
|
||||||
|
profile_repository=self.profile_repository,
|
||||||
|
audit_service=self.audit_service,
|
||||||
|
)
|
||||||
|
self.ctx = RequestContext.from_astrbot(
|
||||||
|
username="admin",
|
||||||
|
user_id="user_001",
|
||||||
|
permissions={"can_write"},
|
||||||
|
)
|
||||||
|
self.profile = self.profile_service.create_profile({"name": "莉娅"}, self.ctx)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.tmp.cleanup()
|
||||||
|
|
||||||
|
def test_create_single_image_workflow_creates_virtual_node(self):
|
||||||
|
detail = self.workflow_service.create_workflow(
|
||||||
|
self.profile.profile_id,
|
||||||
|
{
|
||||||
|
"workflow_type": "single_image",
|
||||||
|
"display_name": "单图流 2026-07-04",
|
||||||
|
"mode": "quick",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertTrue(detail.workflow.workflow_id.startswith("workflow_"))
|
||||||
|
self.assertEqual(detail.workflow.workflow_type, "single_image")
|
||||||
|
self.assertEqual(len(detail.nodes), 1)
|
||||||
|
self.assertEqual(detail.nodes[0].node_key, "virtual_single_image")
|
||||||
|
self.assertEqual(detail.nodes[0].node_type, "virtual_single_image")
|
||||||
|
self.assertEqual(detail.workflow.current_node_id, detail.nodes[0].node_id)
|
||||||
|
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
audit = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT action, target_type
|
||||||
|
FROM audit_logs
|
||||||
|
WHERE target_id = ?
|
||||||
|
""",
|
||||||
|
(detail.workflow.workflow_id,),
|
||||||
|
).fetchone()
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
self.assertIsNotNone(audit)
|
||||||
|
self.assertEqual(audit["action"], "create_workflow")
|
||||||
|
self.assertEqual(audit["target_type"], "profile_workflow")
|
||||||
|
|
||||||
|
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": "标准角色流",
|
||||||
|
"mode": "standard",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
second = self.workflow_service.create_node(
|
||||||
|
detail.workflow.workflow_id,
|
||||||
|
{"node_key": "full_body", "title": "全身", "sort_order": 20},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
first = self.workflow_service.create_node(
|
||||||
|
detail.workflow.workflow_id,
|
||||||
|
{"node_key": "portrait", "title": "头像", "sort_order": 10},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
nodes = self.workflow_service.list_nodes(detail.workflow.workflow_id)
|
||||||
|
|
||||||
|
self.assertEqual([node.node_id for node in nodes], [first.node_id, second.node_id])
|
||||||
|
|
||||||
|
def test_update_node_changes_status_and_writes_audit(self):
|
||||||
|
detail = self.workflow_service.create_workflow(
|
||||||
|
self.profile.profile_id,
|
||||||
|
{
|
||||||
|
"workflow_type": "character_gradual",
|
||||||
|
"display_name": "标准角色流",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
node = self.workflow_service.create_node(
|
||||||
|
detail.workflow.workflow_id,
|
||||||
|
{"node_key": "portrait", "title": "头像"},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
updated = self.workflow_service.update_node(
|
||||||
|
node.node_id,
|
||||||
|
{
|
||||||
|
"status": "completed",
|
||||||
|
"prompt": "portrait, silver hair",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(updated.status, "completed")
|
||||||
|
self.assertEqual(updated.prompt, "portrait, silver hair")
|
||||||
|
|
||||||
|
conn = self.database.connect()
|
||||||
|
try:
|
||||||
|
count = conn.execute(
|
||||||
|
"""
|
||||||
|
SELECT COUNT(*) AS total
|
||||||
|
FROM audit_logs
|
||||||
|
WHERE target_id = ? AND action = 'update_node'
|
||||||
|
""",
|
||||||
|
(node.node_id,),
|
||||||
|
).fetchone()["total"]
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
self.assertEqual(count, 1)
|
||||||
|
|
||||||
|
def test_invalid_workflow_type_is_rejected(self):
|
||||||
|
with self.assertRaises(ApiError) as raised:
|
||||||
|
self.workflow_service.create_workflow(
|
||||||
|
self.profile.profile_id,
|
||||||
|
{
|
||||||
|
"workflow_type": "unknown",
|
||||||
|
"display_name": "坏工作流",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(raised.exception.error_code, ErrorCode.VALIDATION_FAILED)
|
||||||
|
|
||||||
|
def test_invalid_node_status_is_rejected(self):
|
||||||
|
detail = self.workflow_service.create_workflow(
|
||||||
|
self.profile.profile_id,
|
||||||
|
{
|
||||||
|
"workflow_type": "character_gradual",
|
||||||
|
"display_name": "标准角色流",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(ApiError) as raised:
|
||||||
|
self.workflow_service.create_node(
|
||||||
|
detail.workflow.workflow_id,
|
||||||
|
{
|
||||||
|
"node_key": "portrait",
|
||||||
|
"title": "头像",
|
||||||
|
"status": "mystery",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(raised.exception.error_code, ErrorCode.VALIDATION_FAILED)
|
||||||
|
|
||||||
|
def test_missing_profile_is_rejected(self):
|
||||||
|
with self.assertRaises(ApiError) as raised:
|
||||||
|
self.workflow_service.create_workflow(
|
||||||
|
"profile_missing",
|
||||||
|
{
|
||||||
|
"workflow_type": "single_image",
|
||||||
|
"display_name": "孤儿工作流",
|
||||||
|
},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(raised.exception.error_code, ErrorCode.RESOURCE_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
正在加载…
添加表格
在新工单中引用