diff --git a/backend/dto/workflow_dto.py b/backend/dto/workflow_dto.py index 0bf5fce..6bba5e4 100644 --- a/backend/dto/workflow_dto.py +++ b/backend/dto/workflow_dto.py @@ -120,3 +120,29 @@ class WorkflowHistoryEntry: "metadata": self.metadata, "created_at": self.created_at, } + + +@dataclass(frozen=True) +class WorkflowBranch: + branch_id: str + workflow_id: str + branch_name: str + base_branch_name: str | None + base_node_id: str | None + status: str + metadata: dict[str, Any] + created_at: str + updated_at: str + + def to_dict(self) -> dict[str, Any]: + return { + "branch_id": self.branch_id, + "workflow_id": self.workflow_id, + "branch_name": self.branch_name, + "base_branch_name": self.base_branch_name, + "base_node_id": self.base_node_id, + "status": self.status, + "metadata": self.metadata, + "created_at": self.created_at, + "updated_at": self.updated_at, + } diff --git a/backend/routes/branch_routes.py b/backend/routes/branch_routes.py new file mode 100644 index 0000000..4c913ce --- /dev/null +++ b/backend/routes/branch_routes.py @@ -0,0 +1,68 @@ +"""Workflow branch route handlers.""" + +from __future__ import annotations + +from typing import Any + +from backend.routes.common import handle_request +from backend.services.workflow_service import WorkflowService +from core.audit_service import AuditService +from core.identity import RequestContext +from core.security import require_authenticated, require_write +from database.db_helper import Database, load_database_settings +from database.repositories.profile_repository import ProfileRepository +from database.repositories.workflow_repository import WorkflowRepository + + +def list_workflow_branches( + ctx: RequestContext, + workflow_id: str, + service: WorkflowService | None = None, +) -> dict: + workflow_service = service or _create_workflow_service() + + def action() -> list[dict[str, Any]]: + require_authenticated(ctx) + return [branch.to_dict() for branch in workflow_service.list_branches(workflow_id)] + + return handle_request(ctx, action) + + +def create_workflow_branch( + ctx: RequestContext, + workflow_id: str, + payload: dict[str, Any], + service: WorkflowService | None = None, +) -> dict: + workflow_service = service or _create_workflow_service() + + def action() -> dict[str, Any]: + require_write(ctx) + return workflow_service.create_branch(workflow_id, payload, ctx).to_dict() + + return handle_request(ctx, action) + + +def switch_workflow_branch( + ctx: RequestContext, + workflow_id: str, + payload: dict[str, Any], + service: WorkflowService | None = None, +) -> dict: + workflow_service = service or _create_workflow_service() + + def action() -> dict[str, Any]: + require_write(ctx) + branch_name = str(payload.get("branch_name") or "").strip() + return workflow_service.switch_branch(workflow_id, branch_name, ctx).to_dict() + + return handle_request(ctx, action) + + +def _create_workflow_service() -> WorkflowService: + database = Database(load_database_settings()) + return WorkflowService( + workflow_repository=WorkflowRepository(database), + profile_repository=ProfileRepository(database), + audit_service=AuditService(database), + ) diff --git a/backend/services/workflow_service.py b/backend/services/workflow_service.py index bd4df28..07d5ac3 100644 --- a/backend/services/workflow_service.py +++ b/backend/services/workflow_service.py @@ -6,7 +6,7 @@ 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 backend.dto.workflow_dto import ProfileWorkflow, WorkflowBranch, WorkflowDetail, WorkflowNode from core.audit_service import AuditService from core.identity import RequestContext from database.repositories.profile_repository import ProfileRepository @@ -18,6 +18,7 @@ from engines.quick_profile_flow import quick_profile_node_templates WORKFLOW_TYPES = {"single_image", "character_gradual", "character_fast", "quick_profile"} WORKFLOW_STATUSES = {"draft", "active", "running", "completed", "failed", "archived", "cancelled"} +BRANCH_STATUSES = {"active", "archived"} NODE_STATUSES = { "draft", "queued", @@ -143,6 +144,93 @@ class WorkflowService: ) return after + def create_branch( + self, + workflow_id: str, + payload: dict[str, Any], + ctx: RequestContext, + ) -> WorkflowBranch: + workflow = self._ensure_workflow_exists(workflow_id) + branch_name = str(payload.get("branch_name") or "").strip() + if not branch_name: + raise ApiError(ErrorCode.VALIDATION_FAILED, "branch_name is required", {"field": "branch_name"}) + if self.workflow_repository.get_branch(workflow_id, branch_name): + raise ApiError( + ErrorCode.VALIDATION_FAILED, + "branch already exists", + {"workflow_id": workflow_id, "branch_name": branch_name}, + ) + base_branch_name = str(payload.get("base_branch_name") or workflow.current_branch or "main") + base_node_id = payload.get("base_node_id") or workflow.current_node_id + branch = self.workflow_repository.create_branch( + { + "branch_id": payload.get("branch_id") or new_branch_id(), + "workflow_id": workflow_id, + "branch_name": branch_name, + "base_branch_name": base_branch_name, + "base_node_id": base_node_id, + "status": payload.get("status", "active"), + "metadata": payload.get("metadata") or {}, + } + ) + copied_nodes = [] + if payload.get("copy_nodes", True): + copied_nodes = self._copy_branch_nodes(workflow_id, base_branch_name, branch_name, ctx) + self.audit_service.record( + ctx=ctx, + action="create_branch", + target_type="workflow_branch", + target_id=branch.branch_id, + after_snapshot={**branch.to_dict(), "copied_node_ids": [node.node_id for node in copied_nodes]}, + ) + return branch + + def list_branches(self, workflow_id: str) -> list[WorkflowBranch]: + self._ensure_workflow_exists(workflow_id) + branches = self.workflow_repository.list_branches(workflow_id) + if branches: + return branches + return [ + WorkflowBranch( + branch_id="branch_virtual_main", + workflow_id=workflow_id, + branch_name="main", + base_branch_name=None, + base_node_id=None, + status="active", + metadata={"virtual": True}, + created_at="", + updated_at="", + ) + ] + + def switch_branch(self, workflow_id: str, branch_name: str, ctx: RequestContext) -> ProfileWorkflow: + before = self._ensure_workflow_exists(workflow_id) + if branch_name != "main" and self.workflow_repository.get_branch(workflow_id, branch_name) is None: + raise ApiError( + ErrorCode.RESOURCE_NOT_FOUND, + "branch not found", + {"workflow_id": workflow_id, "branch_name": branch_name}, + ) + branch_nodes = self.workflow_repository.list_nodes(workflow_id, branch_name=branch_name) + after = self.update_workflow( + workflow_id, + { + "current_branch": branch_name, + "current_node_id": branch_nodes[0].node_id if branch_nodes else before.current_node_id, + }, + ctx, + ) + self.audit_service.record( + ctx=ctx, + action="switch_branch", + 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, @@ -248,6 +336,39 @@ class WorkflowService: for template in _initial_node_templates(workflow.workflow_type) ] + def _copy_branch_nodes( + self, + workflow_id: str, + from_branch: str, + to_branch: str, + ctx: RequestContext, + ) -> list[WorkflowNode]: + source_nodes = self.workflow_repository.list_nodes(workflow_id, branch_name=from_branch) + if not source_nodes and from_branch != "main": + source_nodes = self.workflow_repository.list_nodes(workflow_id, branch_name="main") + return [ + self.create_node( + workflow_id, + { + "node_key": node.node_key, + "title": node.title, + "description": node.description, + "step_name": node.step_name, + "node_type": node.node_type, + "status": "draft", + "branch_name": to_branch, + "prompt": node.prompt, + "negative_prompt": node.negative_prompt, + "model_route": node.model_route, + "sort_order": node.sort_order, + "metadata": {**node.metadata, "copied_from_node_id": node.node_id}, + }, + ctx, + audit=False, + ) + for node in source_nodes + ] + def _ensure_profile_exists(self, profile_id: str) -> None: profile = self.profile_repository.get(profile_id) if profile is None or profile.status == "deleted": @@ -276,6 +397,10 @@ def new_node_id() -> str: return f"node_{uuid4().hex}" +def new_branch_id() -> str: + return f"branch_{uuid4().hex}" + + def _initial_node_templates(workflow_type: str) -> list[dict[str, Any]]: if workflow_type == "single_image": return [ diff --git a/database/migrations/0007_workflow_branches.sql b/database/migrations/0007_workflow_branches.sql new file mode 100644 index 0000000..ca3ecf6 --- /dev/null +++ b/database/migrations/0007_workflow_branches.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS workflow_branches ( + branch_id TEXT PRIMARY KEY, + workflow_id TEXT NOT NULL, + branch_name TEXT NOT NULL, + base_branch_name TEXT, + base_node_id TEXT, + status TEXT NOT NULL DEFAULT 'active', + 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, + FOREIGN KEY(base_node_id) REFERENCES workflow_nodes(node_id) ON DELETE SET NULL +); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_workflow_branches_name + ON workflow_branches(workflow_id, branch_name); + +CREATE INDEX IF NOT EXISTS idx_workflow_branches_workflow + ON workflow_branches(workflow_id, status); diff --git a/database/repositories/workflow_repository.py b/database/repositories/workflow_repository.py index c8d25e4..cbdd092 100644 --- a/database/repositories/workflow_repository.py +++ b/database/repositories/workflow_repository.py @@ -5,7 +5,7 @@ from __future__ import annotations import json from typing import Any -from backend.dto.workflow_dto import ProfileWorkflow, WorkflowHistoryEntry, WorkflowNode +from backend.dto.workflow_dto import ProfileWorkflow, WorkflowBranch, WorkflowHistoryEntry, WorkflowNode from database.db_helper import Database @@ -58,6 +58,18 @@ HISTORY_COLUMNS = """ created_at """ +BRANCH_COLUMNS = """ + branch_id, + workflow_id, + branch_name, + base_branch_name, + base_node_id, + status, + metadata_json, + created_at, + updated_at +""" + class WorkflowRepository: def __init__(self, database: Database): @@ -250,18 +262,23 @@ class WorkflowRepository: conn.close() return _row_to_node(row) if row else None - def list_nodes(self, workflow_id: str) -> list[WorkflowNode]: + def list_nodes(self, workflow_id: str, branch_name: str | None = None) -> list[WorkflowNode]: placeholder = self.placeholder + where = [f"workflow_id = {placeholder}"] + params: list[Any] = [workflow_id] + if branch_name: + where.append(f"branch_name = {placeholder}") + params.append(branch_name) conn = self.database.connect() try: rows = conn.execute( f""" SELECT {NODE_COLUMNS} FROM workflow_nodes - WHERE workflow_id = {placeholder} + WHERE {" AND ".join(where)} ORDER BY sort_order ASC, created_at ASC """, - (workflow_id,), + tuple(params), ).fetchall() finally: conn.close() @@ -389,6 +406,76 @@ class WorkflowRepository: conn.close() return [_row_to_history(row) for row in rows] + def create_branch(self, payload: dict[str, Any]) -> WorkflowBranch: + placeholder = self.placeholder + conn = self.database.connect() + try: + conn.execute( + f""" + INSERT INTO workflow_branches ( + branch_id, + workflow_id, + branch_name, + base_branch_name, + base_node_id, + status, + metadata_json + ) VALUES ( + {placeholder}, {placeholder}, {placeholder}, {placeholder}, + {placeholder}, {placeholder}, {placeholder} + ) + """, + ( + payload["branch_id"], + payload["workflow_id"], + payload["branch_name"], + payload.get("base_branch_name"), + payload.get("base_node_id"), + payload.get("status", "active"), + _json_or_none(payload.get("metadata")), + ), + ) + conn.commit() + finally: + conn.close() + branch = self.get_branch(payload["workflow_id"], payload["branch_name"]) + if branch is None: + raise RuntimeError("workflow branch was inserted but could not be read back") + return branch + + def get_branch(self, workflow_id: str, branch_name: str) -> WorkflowBranch | None: + placeholder = self.placeholder + conn = self.database.connect() + try: + row = conn.execute( + f""" + SELECT {BRANCH_COLUMNS} + FROM workflow_branches + WHERE workflow_id = {placeholder} AND branch_name = {placeholder} + """, + (workflow_id, branch_name), + ).fetchone() + finally: + conn.close() + return _row_to_branch(row) if row else None + + def list_branches(self, workflow_id: str) -> list[WorkflowBranch]: + placeholder = self.placeholder + conn = self.database.connect() + try: + rows = conn.execute( + f""" + SELECT {BRANCH_COLUMNS} + FROM workflow_branches + WHERE workflow_id = {placeholder} + ORDER BY created_at ASC + """, + (workflow_id,), + ).fetchall() + finally: + conn.close() + return [_row_to_branch(row) for row in rows] + def _row_to_workflow(row) -> ProfileWorkflow: return ProfileWorkflow( @@ -445,6 +532,20 @@ def _row_to_history(row) -> WorkflowHistoryEntry: ) +def _row_to_branch(row) -> WorkflowBranch: + return WorkflowBranch( + branch_id=row["branch_id"], + workflow_id=row["workflow_id"], + branch_name=row["branch_name"], + base_branch_name=row["base_branch_name"], + base_node_id=row["base_node_id"], + status=row["status"], + 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 diff --git a/tests/test_s2_routes.py b/tests/test_s2_routes.py index a9908f1..7804dbd 100644 --- a/tests/test_s2_routes.py +++ b/tests/test_s2_routes.py @@ -5,6 +5,7 @@ import unittest from pathlib import Path from backend.routes.dashboard_routes import get_dashboard_summary +from backend.routes.branch_routes import create_workflow_branch, list_workflow_branches, switch_workflow_branch from backend.routes.image_routes import ( archive_image, delete_image, @@ -652,6 +653,46 @@ security: self.assertFalse(denied_generate["ok"]) self.assertEqual(denied_generate["error_code"], "FORBIDDEN") + def test_workflow_branch_routes_wrap_responses(self): + profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx) + workflow = self.workflow_service.create_workflow( + profile.profile_id, + {"workflow_type": "character_fast", "display_name": "Fast"}, + self.ctx, + ) + + created = create_workflow_branch( + self.ctx, + workflow.workflow.workflow_id, + {"branch_name": "alt_pose"}, + self.workflow_service, + ) + self.assertTrue(created["ok"]) + self.assertEqual(created["data"]["branch_name"], "alt_pose") + + listed = list_workflow_branches(self.ctx, workflow.workflow.workflow_id, self.workflow_service) + self.assertTrue(listed["ok"]) + self.assertEqual([branch["branch_name"] for branch in listed["data"]], ["alt_pose"]) + + switched = switch_workflow_branch( + self.ctx, + workflow.workflow.workflow_id, + {"branch_name": "alt_pose"}, + self.workflow_service, + ) + self.assertTrue(switched["ok"]) + self.assertEqual(switched["data"]["current_branch"], "alt_pose") + + read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001") + denied = create_workflow_branch( + read_only, + workflow.workflow.workflow_id, + {"branch_name": "denied"}, + self.workflow_service, + ) + self.assertFalse(denied["ok"]) + self.assertEqual(denied["error_code"], "FORBIDDEN") + if __name__ == "__main__": unittest.main() diff --git a/tests/test_workflow_service.py b/tests/test_workflow_service.py index 3b06d33..f58419a 100644 --- a/tests/test_workflow_service.py +++ b/tests/test_workflow_service.py @@ -123,6 +123,35 @@ class WorkflowServiceTests(unittest.TestCase): self.assertEqual([node.node_key for node in detail.nodes], ["questionnaire", "preview"]) self.assertEqual(detail.nodes[0].node_type, "profile_draft") + def test_create_branch_copies_nodes_and_switches_current_branch(self): + detail = self.workflow_service.create_workflow( + self.profile.profile_id, + { + "workflow_type": "character_fast", + "display_name": "Fast character", + }, + self.ctx, + ) + + branch = self.workflow_service.create_branch( + detail.workflow.workflow_id, + {"branch_name": "alt_pose", "copy_nodes": True}, + self.ctx, + ) + + self.assertEqual(branch.branch_name, "alt_pose") + copied_nodes = self.workflow_repository.list_nodes(detail.workflow.workflow_id, branch_name="alt_pose") + self.assertEqual(len(copied_nodes), 3) + self.assertEqual([node.node_key for node in copied_nodes], ["fast_profile", "fast_candidates", "fast_final"]) + self.assertTrue(copied_nodes[0].metadata["copied_from_node_id"].startswith("node_")) + + switched = self.workflow_service.switch_branch(detail.workflow.workflow_id, "alt_pose", self.ctx) + self.assertEqual(switched.current_branch, "alt_pose") + self.assertEqual(switched.current_node_id, copied_nodes[0].node_id) + + branches = self.workflow_service.list_branches(detail.workflow.workflow_id) + self.assertEqual([item.branch_name for item in branches], ["alt_pose"]) + def test_create_and_list_nodes_ordered_by_sort_order(self): detail = self.workflow_service.create_workflow( self.profile.profile_id,