From 239041f42629cb29f2d47d41abb483d65bf3af0b Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Fri, 10 Jul 2026 01:25:15 +0800 Subject: [PATCH] feat: extend workflow and materials workbench --- .../services/character_workflow_service.py | 2 + backend/services/generation_service.py | 9 +- backend/services/history_service.py | 125 +- .../material_binding_schema_service.py | 297 ++ backend/services/material_service.py | 6 + .../node_dependency_context_service.py | 22 +- .../node_injection_template_service.py | 357 ++ backend/services/prompt_expansion_service.py | 38 + .../services/prompt_plan_schema_service.py | 366 ++ backend/services/review_service.py | 3 +- backend/services/workflow_service.py | 107 +- database/seed.py | 98 +- docs/ui-progress-2026-07-09.md | 272 ++ docs/ui-progress-2026-07-10.md | 101 + engines/character_flow.py | 32 +- engines/review_flow.py | 204 +- frontend/shared/api/client.ts | 10 + frontend/standalone-web/src/App.vue | 4251 ++++++++++++++++- main.py | 4 +- tests/test_character_workflow_service.py | 4 +- tests/test_history_service.py | 65 + tests/test_material_binding_schema_service.py | 116 + tests/test_material_service.py | 21 +- tests/test_node_dependency_context_service.py | 106 + tests/test_plugin_entrypoint.py | 20 +- tests/test_prompt_expansion_service.py | 93 +- tests/test_prompt_plan_schema_service.py | 115 + tests/test_review_service.py | 48 + tests/test_s0_infrastructure.py | 9 + tests/test_workflow_service.py | 127 +- web/assets/index-B2NiW3rX.js | 50 - web/assets/index-C91GfaJN.css | 1 + web/assets/index-CPo7NaFF.css | 1 - web/assets/index-uM7AKRxw.js | 51 + web/index.html | 4 +- 35 files changed, 6932 insertions(+), 203 deletions(-) create mode 100644 backend/services/material_binding_schema_service.py create mode 100644 backend/services/node_injection_template_service.py create mode 100644 backend/services/prompt_plan_schema_service.py create mode 100644 docs/ui-progress-2026-07-09.md create mode 100644 docs/ui-progress-2026-07-10.md create mode 100644 tests/test_material_binding_schema_service.py create mode 100644 tests/test_prompt_plan_schema_service.py delete mode 100644 web/assets/index-B2NiW3rX.js create mode 100644 web/assets/index-C91GfaJN.css delete mode 100644 web/assets/index-CPo7NaFF.css create mode 100644 web/assets/index-uM7AKRxw.js diff --git a/backend/services/character_workflow_service.py b/backend/services/character_workflow_service.py index b8e1784..fd039d4 100644 --- a/backend/services/character_workflow_service.py +++ b/backend/services/character_workflow_service.py @@ -122,6 +122,8 @@ def _step_instruction(workflow_type: str, node_key: str) -> str: } gradual = { "profile_brief": "core identity and visual anchors", + "prompt_plan": "prompt matrix for portrait, half body, full body, and design sheet nodes", + "material_bind": "reference material binding rules for style, composition, clothing, shoes, and accessories", "headshot": "headshot, face, hair, eyes, expression", "half_body": "half body, upper outfit, posture and silhouette", "full_body": "full body, complete outfit and proportions", diff --git a/backend/services/generation_service.py b/backend/services/generation_service.py index e648b39..783e528 100644 --- a/backend/services/generation_service.py +++ b/backend/services/generation_service.py @@ -9,6 +9,7 @@ from backend.dto.job_dto import GenerationTask from backend.services.cost_guard_service import CostGuardService from backend.services.image_service import ImageService from backend.services.model_provider_service import ModelProviderService +from backend.services.node_dependency_context_service import NodeDependencyContextService from backend.services.prompt_expansion_service import PromptExpansionService from backend.services.queue_service import QueueService from backend.services.workflow_service import ( @@ -247,7 +248,13 @@ class GenerationService: profile = self.workflow_service.profile_repository.get(profile_id) if profile is None: return None - return self.prompt_expansion_service.expand_for_node(profile, node, payload) + enriched_payload = dict(payload) + if "dependency_context" not in enriched_payload: + enriched_payload["dependency_context"] = NodeDependencyContextService( + self.workflow_service, + self.image_service, + ).get_context(node.node_id) + return self.prompt_expansion_service.expand_for_node(profile, node, enriched_payload) def _publish(self, event_type: str, payload: dict[str, Any]) -> None: if self.event_bus: diff --git a/backend/services/history_service.py b/backend/services/history_service.py index 8c22b9f..88fad09 100644 --- a/backend/services/history_service.py +++ b/backend/services/history_service.py @@ -7,6 +7,7 @@ from uuid import uuid4 from backend.dto.common import ApiError, ErrorCode from backend.dto.workflow_dto import WorkflowHistoryEntry +from backend.dto.workflow_dto import ProfileWorkflow, WorkflowNode from backend.services.image_service import ImageService from backend.services.workflow_service import WorkflowService from core.audit_service import AuditService @@ -35,9 +36,11 @@ class HistoryService: ) -> WorkflowHistoryEntry: node = self.workflow_service.get_node(node_id) workflow = self.workflow_service.get_workflow(node.workflow_id).workflow + payload_metadata = _mapping(payload.get("metadata")) image_id = str(payload.get("image_id") or "").strip() if not image_id: raise ApiError(ErrorCode.VALIDATION_FAILED, "image_id is required", {"field": "image_id"}) + next_node = self._resolve_handoff_next_node(workflow, payload_metadata) image = self.image_service.get_image(image_id) if image.node_id != node_id: raise ApiError( @@ -47,12 +50,15 @@ class HistoryService: ) if image.is_archived: raise ApiError(ErrorCode.VALIDATION_FAILED, "archived image cannot be adopted", {"image_id": image_id}) - selected = self.image_service.select_image(image_id, ctx) if not image.is_selected else image updated_node = self.workflow_service.update_node( node_id, - {"status": "selected", "selected_image_id": selected.image_id}, + {"status": "selected", "selected_image_id": image.image_id}, ctx, + allow_locked_adoption=True, ) + selected = self.image_service.image_repository.select_exclusive(image) if not image.is_selected else image + if selected is None: + raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "image not found", {"image_id": image_id}) history = self.workflow_repository.create_history( { "history_id": payload.get("history_id") or new_history_id(), @@ -68,10 +74,19 @@ class HistoryService: "image_uri": selected.image_uri, "thumbnail_uri": selected.thumbnail_uri, "source": "node_adoption", - **(payload.get("metadata") or {}), + **payload_metadata, }, } ) + self._persist_stage_handoff( + workflow, + updated_node, + selected, + history, + payload_metadata, + next_node, + ctx, + ) self.audit_service.record( ctx=ctx, action="adopt_image", @@ -81,6 +96,103 @@ class HistoryService: ) return history + def _resolve_handoff_next_node( + self, + workflow: ProfileWorkflow, + metadata: dict[str, Any], + ) -> WorkflowNode | None: + next_node_id = _optional_text(metadata.get("next_backend_node_id")) + if not next_node_id: + return None + next_node = self.workflow_service.get_node(next_node_id) + if next_node.workflow_id != workflow.workflow_id: + raise ApiError( + ErrorCode.VALIDATION_FAILED, + "next_backend_node_id does not belong to this workflow", + {"workflow_id": workflow.workflow_id, "next_backend_node_id": next_node_id}, + ) + if next_node.branch_name != workflow.current_branch: + raise ApiError( + ErrorCode.VALIDATION_FAILED, + "next_backend_node_id does not belong to the current branch", + { + "workflow_id": workflow.workflow_id, + "current_branch": workflow.current_branch, + "next_backend_node_id": next_node_id, + "next_branch_name": next_node.branch_name, + }, + ) + return next_node + + def _persist_stage_handoff( + self, + workflow: ProfileWorkflow, + source_node: WorkflowNode, + selected, + history: WorkflowHistoryEntry, + metadata: dict[str, Any], + next_node: WorkflowNode | None, + ctx: RequestContext, + ) -> None: + if next_node is None: + return + existing_metadata = dict(workflow.metadata or {}) + existing_stage_inputs = existing_metadata.get("stage_inputs") + stage_inputs = dict(existing_stage_inputs) if isinstance(existing_stage_inputs, dict) else {} + handoff_record = { + "schema_version": STAGE_INPUT_SCHEMA_VERSION, + "image_id": selected.image_id, + "image_uri": selected.image_uri, + "thumbnail_uri": selected.thumbnail_uri, + "from_node_id": source_node.node_id, + "from_node_key": source_node.node_key, + "from_review_node_id": metadata.get("ui_review_node_id"), + "source_ui_node_id": metadata.get("source_ui_node_id"), + "next_ui_node_id": metadata.get("next_ui_node_id"), + "history_id": history.history_id, + "role": metadata.get("handoff_role") or "next_stage_input", + "handoff_bucket": metadata.get("handoff_bucket"), + "branch_name": source_node.branch_name, + "model_route": selected.model_route, + "model_used": selected.model_used, + "positive_prompt": selected.positive_prompt or source_node.prompt, + "negative_prompt": selected.negative_prompt or source_node.negative_prompt, + "locked_source_node": True, + "confirmed_at": history.created_at, + } + stage_inputs[next_node.node_id] = handoff_record + handoff_log = existing_metadata.get("stage_review_handoffs") + if not isinstance(handoff_log, list): + handoff_log = [] + handoff_log = [ + *handoff_log, + { + "history_id": history.history_id, + "from_node_id": source_node.node_id, + "to_node_id": next_node.node_id, + "image_id": selected.image_id, + "role": handoff_record["role"], + "confirmed_at": history.created_at, + }, + ][-20:] + self.workflow_service.update_workflow( + workflow.workflow_id, + { + "current_node_id": next_node.node_id, + "metadata": { + **existing_metadata, + "stage_input_schema_version": STAGE_INPUT_SCHEMA_VERSION, + "stage_inputs": stage_inputs, + "stage_review_handoffs": handoff_log, + "last_stage_review_confirmed_at": history.created_at, + "last_stage_review_node_id": metadata.get("ui_review_node_id"), + "last_stage_review_image_id": selected.image_id, + "current_stage_input_image_id": selected.image_id, + }, + }, + ctx, + ) + def list_workflow_history( self, workflow_id: str, @@ -148,8 +260,15 @@ def new_history_id() -> str: return f"hist_{uuid4().hex}" +STAGE_INPUT_SCHEMA_VERSION = "2026-07-09.stage-input-bucket.v1" + + def _optional_text(value: Any) -> str | None: if value is None: return None text = str(value).strip() return text or None + + +def _mapping(value: Any) -> dict[str, Any]: + return value if isinstance(value, dict) else {} diff --git a/backend/services/material_binding_schema_service.py b/backend/services/material_binding_schema_service.py new file mode 100644 index 0000000..14ad536 --- /dev/null +++ b/backend/services/material_binding_schema_service.py @@ -0,0 +1,297 @@ +"""Normalize workflow material binding payloads for generation nodes.""" + +from __future__ import annotations + +from copy import deepcopy +from typing import Any + +from backend.dto.workflow_dto import WorkflowNode + + +MATERIAL_BINDING_SCHEMA_VERSION = "2026-07-09.material-binding-schema.v1" + +REFERENCE_POLICY = { + "mode": "explicit_only", + "default_use_reference": False, + "empty_material_means": "do_not_use_reference", +} + +STAGE_NODE_KEYS: dict[str, tuple[str, ...]] = { + "profile-prep": ("profile_brief", "prompt_plan", "material_bind"), + "portrait-draft": ("headshot", "fast_candidates"), + "halfbody-draft": ("half_body",), + "body-sheet": ("full_body", "expression_sheet", "pose_sheet"), + "review-archive": ("style_lock", "final_pack", "fast_final"), +} + +STRENGTHS: dict[str, tuple[str, float]] = { + "强": ("strong", 0.85), + "strong": ("strong", 0.85), + "high": ("strong", 0.85), + "中": ("medium", 0.55), + "medium": ("medium", 0.55), + "mid": ("medium", 0.55), + "弱": ("weak", 0.3), + "weak": ("weak", 0.3), + "low": ("weak", 0.3), +} + + +def normalize_material_bindings( + payload: Any, + workflow_nodes: list[WorkflowNode] | None = None, +) -> dict[str, Any]: + if isinstance(payload, dict) and payload.get("schema_version") == MATERIAL_BINDING_SCHEMA_VERSION: + normalized = deepcopy(payload) + if workflow_nodes: + rules = normalized.get("active_rules") + if not isinstance(rules, list): + rules = [ + rule + for rule in normalized.get("rules", []) + if isinstance(rule, dict) and rule.get("use_reference") + ] + normalized["by_node_id"] = _index_by_node_id(rules, workflow_nodes) + return normalized + + source = _extract_source_payload(payload) + rules = [ + _normalize_rule(rule, index + 1) + for index, rule in enumerate(source.get("rules", []) if isinstance(source.get("rules"), list) else []) + if isinstance(rule, dict) + ] + active_rules = [rule for rule in rules if rule["use_reference"]] + by_stage = _index_by_stage(active_rules) + by_node_key = _index_by_node_key(active_rules) + by_node_id = _index_by_node_id(active_rules, workflow_nodes or []) + selected_material_ids = _string_list(source.get("selected_material_ids")) + if not selected_material_ids: + selected_material_ids = [rule["material"]["material_id"] for rule in active_rules] + return { + "schema_version": MATERIAL_BINDING_SCHEMA_VERSION, + "reference_policy": deepcopy(REFERENCE_POLICY), + "profile_id": _optional_text(source.get("profile_id")), + "profile_name": _optional_text(source.get("profile_name")), + "selected_material_ids": _unique_texts(selected_material_ids), + "rules": rules, + "active_rules": active_rules, + "by_stage": by_stage, + "by_node_key": by_node_key, + "by_node_id": by_node_id, + "node_parameters": by_node_id, + "prompt_context": _prompt_context(active_rules), + "summary": { + "rule_count": len(rules), + "active_rule_count": len(active_rules), + "has_references": bool(active_rules), + "disabled_categories": [rule["category"] for rule in rules if not rule["use_reference"]], + "target_stage_ids": sorted(by_stage), + "target_node_keys": sorted(by_node_key), + }, + } + + +def material_bindings_for_node( + workflow_metadata: dict[str, Any], + node: WorkflowNode, + workflow_nodes: list[WorkflowNode] | None = None, +) -> dict[str, Any]: + normalized = normalize_material_bindings(workflow_metadata.get("material_bindings"), workflow_nodes) + return node_material_binding_view(normalized, node) + + +def node_material_binding_view(normalized: dict[str, Any], node: WorkflowNode) -> dict[str, Any]: + by_node_id = normalized.get("by_node_id") if isinstance(normalized.get("by_node_id"), dict) else {} + by_node_key = normalized.get("by_node_key") if isinstance(normalized.get("by_node_key"), dict) else {} + node_view = by_node_id.get(node.node_id) + if not isinstance(node_view, dict): + node_view = _node_view( + active_rules=by_node_key.get(node.node_key, []), + node_id=node.node_id, + node_key=node.node_key, + title=node.title, + ) + return { + "schema_version": normalized.get("schema_version", MATERIAL_BINDING_SCHEMA_VERSION), + "reference_policy": normalized.get("reference_policy", deepcopy(REFERENCE_POLICY)), + "summary": normalized.get("summary", {}), + "target_node_id": node.node_id, + "target_node_key": node.node_key, + "target_node_title": node.title, + **node_view, + } + + +def _extract_source_payload(payload: Any) -> dict[str, Any]: + if not isinstance(payload, dict): + return {} + material_bindings = payload.get("material_bindings") + if isinstance(material_bindings, dict) and isinstance(material_bindings.get("rules"), list): + return material_bindings + if isinstance(payload.get("rules"), list): + return payload + return {} + + +def _normalize_rule(rule: dict[str, Any], ordinal: int) -> dict[str, Any]: + material_id = _optional_text(rule.get("material_id")) + material_prompt = _optional_text(rule.get("material_prompt")) + category = _optional_text(rule.get("category")) or "other" + target_stages = _target_stages(rule.get("bind_to_stages")) + target_node_keys = _unique_texts( + node_key for stage in target_stages for node_key in STAGE_NODE_KEYS.get(stage["stage_id"], ()) + ) + strength_label = _optional_text(rule.get("reference_strength")) or "medium" + strength_key, strength_weight = STRENGTHS.get(strength_label.lower(), STRENGTHS.get(strength_label, ("medium", 0.55))) + use_reference = bool(material_id or material_prompt) + return { + "id": _optional_text(rule.get("id")) or f"material_binding_{ordinal:02d}", + "category": category, + "use_reference": use_reference, + "required": bool(rule.get("required")), + "status": _optional_text(rule.get("status")) or ("ready" if use_reference else "pending"), + "conflict": _optional_text(rule.get("conflict")), + "material": { + "material_id": material_id, + "label": _optional_text(rule.get("material_label")), + "group": _optional_text(rule.get("material_group")), + "prompt": material_prompt, + "colors": _string_list(rule.get("material_colors")), + }, + "target_stages": target_stages, + "target_node_keys": target_node_keys, + "inheritance_rule": _optional_text(rule.get("inheritance_rule")) or "", + "reference_strength": { + "label": strength_label, + "key": strength_key, + "weight": strength_weight, + }, + "generation_params": { + "reference_policy": "explicit_only" if use_reference else "do_not_use_reference", + "reference_strength": strength_key if use_reference else "none", + "reference_weight": strength_weight if use_reference else 0, + }, + "expand_to_nodes_later": True, + } + + +def _target_stages(value: Any) -> list[dict[str, str]]: + if not isinstance(value, list): + return [] + stages: list[dict[str, str]] = [] + for item in value: + if not isinstance(item, dict): + continue + stage_id = _optional_text(item.get("stage_id") or item.get("id")) + if not stage_id: + continue + stage = { + "stage_id": stage_id, + "stage_title": _optional_text(item.get("stage_title") or item.get("title")) or stage_id, + } + if stage not in stages: + stages.append(stage) + return stages + + +def _index_by_stage(rules: list[dict[str, Any]]) -> dict[str, dict[str, Any]]: + result: dict[str, dict[str, Any]] = {} + for rule in rules: + for stage in rule["target_stages"]: + stage_id = stage["stage_id"] + bucket = result.setdefault( + stage_id, + { + "stage_id": stage_id, + "stage_title": stage["stage_title"], + "active_bindings": [], + "material_prompts": [], + }, + ) + bucket["active_bindings"].append(rule) + if rule["material"]["prompt"]: + bucket["material_prompts"].append(rule["material"]["prompt"]) + return result + + +def _index_by_node_key(rules: list[dict[str, Any]]) -> dict[str, list[dict[str, Any]]]: + result: dict[str, list[dict[str, Any]]] = {} + for rule in rules: + for node_key in rule["target_node_keys"]: + result.setdefault(node_key, []).append(rule) + return result + + +def _index_by_node_id( + rules: list[dict[str, Any]], + workflow_nodes: list[WorkflowNode], +) -> dict[str, dict[str, Any]]: + by_node_key = _index_by_node_key(rules) + return { + node.node_id: _node_view(by_node_key.get(node.node_key, []), node.node_id, node.node_key, node.title) + for node in workflow_nodes + if by_node_key.get(node.node_key) + } + + +def _node_view( + active_rules: list[dict[str, Any]], + node_id: str | None, + node_key: str, + title: str, +) -> dict[str, Any]: + prompts = [rule["material"]["prompt"] for rule in active_rules if rule["material"]["prompt"]] + return { + "target_node_id": node_id, + "target_node_key": node_key, + "target_node_title": title, + "reference_policy": deepcopy(REFERENCE_POLICY), + "has_references": bool(active_rules), + "active_bindings": deepcopy(active_rules), + "material_prompts": prompts, + "prompt_context": ", ".join(prompts), + "generation_params": { + "reference_material_ids": [ + rule["material"]["material_id"] for rule in active_rules if rule["material"]["material_id"] + ], + "reference_prompts": prompts, + "reference_strengths": [rule["reference_strength"] for rule in active_rules], + }, + } + + +def _prompt_context(rules: list[dict[str, Any]]) -> list[dict[str, str]]: + return [ + { + "category": rule["category"], + "material_id": rule["material"]["material_id"] or "", + "prompt": rule["material"]["prompt"] or "", + "target_node_keys": ", ".join(rule["target_node_keys"]), + } + for rule in rules + if rule["material"]["prompt"] + ] + + +def _string_list(value: Any) -> list[str]: + if isinstance(value, str): + return [value] if value.strip() else [] + if not isinstance(value, list): + return [] + return _unique_texts(str(item).strip() for item in value if str(item or "").strip()) + + +def _unique_texts(values: Any) -> list[str]: + result: list[str] = [] + for value in values: + text = str(value or "").strip() + if text and text not in result: + result.append(text) + return result + + +def _optional_text(value: Any) -> str | None: + if value is None: + return None + text = str(value).strip() + return text or None diff --git a/backend/services/material_service.py b/backend/services/material_service.py index 2ba89ad..6a01570 100644 --- a/backend/services/material_service.py +++ b/backend/services/material_service.py @@ -78,6 +78,12 @@ class MaterialService: before = self.get_category(category_id) if "name" in payload and not str(payload["name"]).strip(): raise ApiError(ErrorCode.VALIDATION_FAILED, "category name is required", {"field": "name"}) + parent_id = payload.get("parent_id") if "parent_id" in payload else None + if parent_id: + if parent_id == category_id: + raise ApiError(ErrorCode.VALIDATION_FAILED, "category cannot be its own parent", {"field": "parent_id"}) + if self.repository.get_category(parent_id) is None: + raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "parent category not found", {"parent_id": parent_id}) after = self.repository.update_category(category_id, payload) if after is None: raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "category not found", {"category_id": category_id}) diff --git a/backend/services/node_dependency_context_service.py b/backend/services/node_dependency_context_service.py index 732e88a..d7a745b 100644 --- a/backend/services/node_dependency_context_service.py +++ b/backend/services/node_dependency_context_service.py @@ -5,7 +5,10 @@ from __future__ import annotations from typing import Any from backend.dto.common import ApiError, ErrorCode +from backend.services.material_binding_schema_service import material_bindings_for_node from backend.services.image_service import ImageService +from backend.services.node_injection_template_service import build_node_injection_contract +from backend.services.prompt_plan_schema_service import prompt_plan_for_node from backend.services.workflow_service import WorkflowService @@ -37,7 +40,7 @@ class NodeDependencyContextService: for item in dependencies if isinstance(item.get("selected_image"), dict) ] - return { + result = { "workflow_id": target.workflow_id, "node_id": target.node_id, "branch_name": target.branch_name, @@ -45,7 +48,17 @@ class NodeDependencyContextService: "dependencies": dependencies, "prompt_context": ", ".join(prompt_parts), "selected_image_ids": selected_image_ids, + "material_bindings": material_bindings_for_node(detail.workflow.metadata, target, detail.nodes), + "prompt_plan": prompt_plan_for_node(detail.workflow.metadata, target, detail.nodes), } + result["injection_template"] = build_node_injection_contract( + target, + dependency_context=result, + material_bindings=result["material_bindings"], + prompt_plan=result["prompt_plan"], + stage_inputs=_stage_inputs_for_node(detail.workflow.metadata, target.node_id), + ) + return result def _node_context(self, node) -> dict[str, Any]: selected_image = None @@ -74,3 +87,10 @@ class NodeDependencyContextService: "selected_image": selected_image, "metadata": node.metadata, } + + +def _stage_inputs_for_node(workflow_metadata: dict[str, Any], node_id: str) -> Any: + stage_inputs = workflow_metadata.get("stage_inputs") + if not isinstance(stage_inputs, dict): + return {} + return stage_inputs.get(node_id) or {} diff --git a/backend/services/node_injection_template_service.py b/backend/services/node_injection_template_service.py new file mode 100644 index 0000000..6adb47b --- /dev/null +++ b/backend/services/node_injection_template_service.py @@ -0,0 +1,357 @@ +"""Node-specific workflow chat injection contracts.""" + +from __future__ import annotations + +from copy import deepcopy +from typing import Any + +from backend.dto.workflow_dto import WorkflowNode + + +NODE_INJECTION_TEMPLATE_VERSION = "2026-07-09.node-injection-schema.v1" + +NODE_KEY_TEMPLATE_KINDS: dict[str, str] = { + "profile_brief": "persona_split", + "prompt_plan": "prompt_plan", + "material_bind": "material_bind", + "headshot": "portrait_generation", + "half_body": "halfbody_generation", + "full_body": "fullbody_generation", + "expression_sheet": "sheet_generation", + "pose_sheet": "pose_generation", + "style_lock": "hires_fix", + "final_pack": "archive", + "fast_profile": "persona_split", + "fast_candidates": "portrait_generation", + "fast_final": "archive", + "questionnaire": "profile_import", + "preview": "prompt_plan", + "virtual_single_image": "single_image_generation", +} + +TEMPLATE_INTENTS: dict[str, str] = { + "profile_import": "upsert_profile_fields", + "persona_split": "update_structured_persona", + "prompt_plan": "update_prompt_matrix", + "material_bind": "update_material_bindings", + "portrait_generation": "update_portrait_node", + "halfbody_generation": "update_halfbody_node", + "fullbody_generation": "update_fullbody_node", + "sheet_generation": "update_design_sheet_node", + "pose_generation": "update_pose_sheet_node", + "stage_review": "review_stage_candidate", + "candidate_review": "review_candidates", + "hires_fix": "update_final_refinement", + "archive": "archive_character_workflow", + "single_image_generation": "single_image_generation", + "generic_workflow_node": "update_current_node", +} + +EXPECTED_RESPONSE_SCHEMAS: dict[str, dict[str, Any]] = { + "profile_import": { + "intent": "upsert_profile_fields", + "target_language": "zh-CN", + "profile_patch": { + "name": "string?", + "identity": "string?", + "personality": "string?", + "appearance": "string?", + "outfit": "string?", + "style": "string?", + "negative": "string?", + }, + "missing_fields": ["string"], + "conflicts": ["string"], + "next_action": "refresh_profile_import | ask_user | enter_persona_split", + }, + "persona_split": { + "intent": "update_structured_persona", + "split_fields_patch": { + "identity": "string?", + "appearance": "string?", + "outfit": "string?", + "materials": "string?", + "guardrails": "string?", + }, + "locked_fields": ["string"], + "conflicts": ["string"], + "next_action": "refresh_persona_split | enter_prompt_plan", + }, + "prompt_plan": { + "intent": "update_prompt_matrix", + "schema_version": "2026-07-09.prompt-plan-schema.v1", + "target_language": "en", + "matrix": [ + { + "target_id": "portrait | bust | fullbody | sheet | all", + "target_node_keys": ["headshot | half_body | full_body | expression_sheet | pose_sheet"], + "positive_patch": "string", + "negative_patch": "string?", + "weight_suggestions": [{"label": "string", "value": "number?", "reason": "string"}], + "weight_basis": "string", + "inheritance_fields": ["string"], + "inheritance_changes": ["string"], + "conflicts": ["string"], + "status": "ready | review | pending", + } + ], + "guardrails": { + "negative_prompt": "string", + "checks": [{"key": "string", "label": "string", "value": "string", "status": "ready | review | pending"}], + }, + "conflicts": [{"target_id": "string", "message": "string", "severity": "info | warn | error"}], + "next_action": "refresh_prompt_plan | enter_material_bind", + }, + "material_bind": { + "intent": "update_material_bindings", + "schema_version": "2026-07-09.material-binding-schema.v1", + "reference_policy": { + "mode": "explicit_only", + "default_use_reference": False, + "empty_material_means": "do_not_use_reference", + }, + "bindings": [ + { + "material_category": "style | composition | clothing | socks | shoes | accessory | other", + "bind_to_stages": [{"stage_id": "string", "stage_title": "string"}], + "target_node_keys": ["headshot | half_body | full_body | expression_sheet | pose_sheet | style_lock"], + "inheritance_rule": "string", + "reference_strength": "string?", + "use_reference": "boolean", + "expand_to_nodes_later": True, + } + ], + "conflicts": ["string"], + "next_action": "refresh_material_bind | enter_draft_generation", + }, + "portrait_generation": { + "intent": "update_portrait_node", + "target_language": "en", + "prompt_patch": "string", + "negative_patch": "string?", + "face_locks": ["face shape | hairstyle | hair color | eye color | expression"], + "generation_params": {"count": "number?", "aspect_ratio": "1:1", "reference_strength": "string?"}, + "next_action": "generate_portrait | rerun_portrait | confirm_use | ask_user", + }, + "halfbody_generation": { + "intent": "update_halfbody_node", + "target_language": "en", + "prompt_patch": "string", + "negative_patch": "string?", + "inherited_locks": ["portrait baseline | face | hair | eye direction"], + "outfit_patch": "string?", + "pose_patch": "string?", + "generation_params": {"count": "number?", "aspect_ratio": "3:4", "reference_strength": "string?"}, + "next_action": "generate_halfbody | rerun_halfbody | confirm_use | ask_user", + }, + "fullbody_generation": { + "intent": "update_fullbody_node", + "target_language": "en", + "prompt_patch": "string", + "negative_patch": "string?", + "proportion_locks": ["head ratio | body proportion | outfit silhouette | shoes and socks"], + "accessory_requirements": ["string"], + "generation_params": {"count": "number?", "aspect_ratio": "2:3", "reference_strength": "string?"}, + "next_action": "generate_fullbody | rerun_fullbody | confirm_use | ask_user", + }, + "sheet_generation": { + "intent": "update_design_sheet_node", + "target_language": "en", + "prompt_patch": "string", + "negative_patch": "string?", + "sheet_layout": "front view | detail callouts | expression set | material labels", + "inherited_locks": ["portrait | halfbody | fullbody | detail nodes"], + "next_action": "generate_sheet | rerun_sheet | confirm_use | ask_user", + }, + "pose_generation": { + "intent": "update_pose_sheet_node", + "target_language": "en", + "prompt_patch": "string", + "negative_patch": "string?", + "pose_targets": ["standing | action | hand pose | prop interaction"], + "inherited_locks": ["identity | outfit | style"], + "next_action": "generate_pose_sheet | rerun_pose_sheet | ask_user", + }, + "stage_review": { + "intent": "review_stage_candidate", + "target_language": "en", + "review_stage": "portrait | halfbody | body_sheet", + "same_prompt_regeneration": True, + "primary_image_id": "string?", + "selected_image_ids": ["string"], + "model_route_notes": ["string"], + "defects_to_regenerate": ["string"], + "locked_items": ["string"], + "selection_reason": "string", + "next_action": "regenerate_selected_image | confirm_next_flow | ask_user", + }, + "candidate_review": { + "intent": "review_candidates", + "review_dimensions": [ + "identity consistency", + "outfit inheritance", + "style consistency", + "image defects", + "policy/negative constraints", + ], + "primary_image_id": "string?", + "selected_image_ids": ["string"], + "reject_image_ids": ["string"], + "selection_reason": "string", + "next_round_direction": "string", + "next_action": "select_candidate | regenerate_node | enter_hires_fix", + }, + "hires_fix": { + "intent": "update_final_refinement", + "target_language": "en", + "prompt_patch": "string", + "defects_to_fix": ["string"], + "keep_locked_items": ["string"], + "next_action": "regenerate_hires | enter_archive", + }, + "archive": { + "intent": "archive_character_workflow", + "archive_summary": "string", + "reusable_tags": ["string"], + "version_note": "string", + "next_action": "save_archive | ask_user", + }, + "single_image_generation": { + "intent": "single_image_generation", + "target_language": "en", + "prompt_patch": "string", + "negative_patch": "string?", + "generation_params": {"count": "number?", "aspect_ratio": "string?"}, + "next_action": "generate_image | ask_user", + }, + "generic_workflow_node": { + "intent": "update_current_node", + "target_language": "en", + "prompt_patch": "string", + "metadata_patch": "object", + "next_action": "refresh_current_node | ask_user", + }, +} + +CONTEXT_REQUIREMENTS: dict[str, list[str]] = { + "profile_import": ["user_instruction", "current_profile", "source_language"], + "persona_split": ["user_instruction", "current_profile", "structured_persona_fields"], + "prompt_plan": ["user_instruction", "structured_persona_fields", "material_bindings"], + "material_bind": ["user_instruction", "available_materials", "target_stage_ids"], + "portrait_generation": ["user_instruction", "current_profile", "material_bindings"], + "halfbody_generation": ["user_instruction", "upstream_selected_images", "material_bindings"], + "fullbody_generation": ["user_instruction", "upstream_selected_images", "material_bindings"], + "sheet_generation": ["user_instruction", "upstream_selected_images", "material_bindings"], + "pose_generation": ["user_instruction", "upstream_selected_images", "material_bindings"], + "stage_review": ["user_instruction", "selected_draft_image_ids", "upstream_selected_images"], + "candidate_review": ["user_instruction", "selected_draft_image_ids", "review_policy"], + "hires_fix": ["user_instruction", "stage_inputs", "upstream_selected_images"], + "archive": ["user_instruction", "accepted_images", "workflow_history"], + "single_image_generation": ["user_instruction", "current_profile"], + "generic_workflow_node": ["user_instruction", "node_context"], +} + + +def template_kind_for_node(node: WorkflowNode, requested_kind: str | None = None) -> str: + normalized = str(requested_kind or "").strip() + if normalized in EXPECTED_RESPONSE_SCHEMAS: + return normalized + return NODE_KEY_TEMPLATE_KINDS.get(node.node_key, "generic_workflow_node") + + +def build_node_injection_contract( + node: WorkflowNode, + *, + requested_kind: str | None = None, + user_instruction: str | None = None, + dependency_context: dict[str, Any] | None = None, + material_bindings: Any | None = None, + prompt_plan: Any | None = None, + stage_inputs: Any | None = None, + selected_draft_image_ids: list[str] | None = None, + ui_context: dict[str, Any] | None = None, +) -> dict[str, Any]: + kind = template_kind_for_node(node, requested_kind) + dependency_context = dependency_context if isinstance(dependency_context, dict) else {} + return { + "version": NODE_INJECTION_TEMPLATE_VERSION, + "template_kind": kind, + "node_key": node.node_key, + "node_id": node.node_id, + "intent": TEMPLATE_INTENTS.get(kind, TEMPLATE_INTENTS["generic_workflow_node"]), + "language_policy": { + "source_language": "auto", + "target_prompt_language": "en", + "translation_mode": "backend_llm", + "preserve_character_names": True, + }, + "user_instruction": { + "raw_text": str(user_instruction or "").strip(), + "source_language": "auto", + }, + "node_context": { + "title": node.title, + "step_name": node.step_name, + "node_type": node.node_type, + "status": node.status, + "prompt": node.prompt, + "negative_prompt": node.negative_prompt, + "selected_image_id": node.selected_image_id, + "metadata": node.metadata, + }, + "dependency_context": { + "dependency_count": dependency_context.get("dependency_count", 0), + "prompt_context": dependency_context.get("prompt_context", ""), + "selected_image_ids": dependency_context.get("selected_image_ids", []), + "dependencies": dependency_context.get("dependencies", []), + }, + "material_bindings": material_bindings if material_bindings is not None else [], + "prompt_plan": prompt_plan if prompt_plan is not None else {}, + "stage_inputs": stage_inputs if stage_inputs is not None else {}, + "selected_draft_image_ids": selected_draft_image_ids or [], + "ui_context": ui_context or {}, + "context_requirements": CONTEXT_REQUIREMENTS.get(kind, CONTEXT_REQUIREMENTS["generic_workflow_node"]), + "expected_response_schema": deepcopy( + EXPECTED_RESPONSE_SCHEMAS.get(kind, EXPECTED_RESPONSE_SCHEMAS["generic_workflow_node"]) + ), + "prompt_contract": [ + "Understand the user's source-language instruction.", + "Translate user-facing edits into concise English image-generation prompt language.", + "Preserve locked identity traits, selected upstream images, material bindings, and negative constraints.", + "Return a JSON-compatible object matching expected_response_schema.", + "If the target node is locked, return a branch-required next_action instead of mutating the original chain.", + ], + } + + +def injection_payload_from_generation_request(payload: dict[str, Any]) -> dict[str, Any]: + metadata = payload.get("metadata") if isinstance(payload.get("metadata"), dict) else {} + user_instruction = ( + metadata.get("user_instruction") + or payload.get("user_instruction") + or payload.get("prompt_patch") + or payload.get("extra_prompt") + or "" + ) + return { + "requested_kind": metadata.get("node_injection_kind") or payload.get("node_injection_kind"), + "user_instruction": str(user_instruction or ""), + "material_bindings": metadata.get("material_binding_rules") or payload.get("material_bindings") or [], + "stage_inputs": metadata.get("stage_inputs") or payload.get("stage_inputs") or {}, + "selected_draft_image_ids": _string_list( + metadata.get("selected_draft_image_ids") or payload.get("selected_draft_image_ids") + ), + "ui_context": { + "ui_node_id": metadata.get("ui_node_id"), + "ui_node_title": metadata.get("ui_node_title"), + "ui_node_phase": metadata.get("ui_node_phase"), + "review_source_ui_node_id": metadata.get("review_source_ui_node_id"), + "review_next_ui_node_id": metadata.get("review_next_ui_node_id"), + }, + } + + +def _string_list(value: Any) -> list[str]: + if not isinstance(value, list): + return [] + return [str(item) for item in value if str(item or "").strip()] diff --git a/backend/services/prompt_expansion_service.py b/backend/services/prompt_expansion_service.py index 1ff7731..271bfe7 100644 --- a/backend/services/prompt_expansion_service.py +++ b/backend/services/prompt_expansion_service.py @@ -6,6 +6,10 @@ from typing import Any from backend.dto.profile_dto import CharacterProfile from backend.dto.workflow_dto import WorkflowNode +from backend.services.node_injection_template_service import ( + build_node_injection_contract, + injection_payload_from_generation_request, +) from core.prompt_pipeline import merge_prompt_sections @@ -26,6 +30,11 @@ class PromptExpansionService: node: WorkflowNode, payload: dict[str, Any], ) -> dict[str, Any]: + dependency_context = payload.get("dependency_context") if isinstance(payload.get("dependency_context"), dict) else {} + prompt_plan = dependency_context.get("prompt_plan") if isinstance(dependency_context.get("prompt_plan"), dict) else {} + prompt_plan_generation = ( + prompt_plan.get("generation_params") if isinstance(prompt_plan.get("generation_params"), dict) else {} + ) variables = { "profile_name": profile.name, "node_title": node.title, @@ -43,17 +52,46 @@ class PromptExpansionService: "negative_parts": [node.negative_prompt], "variables": variables, }, + { + "parts": _as_list(prompt_plan_generation.get("prompt_patch")), + "negative_parts": _as_list(prompt_plan_generation.get("negative_patch")), + "variables": variables, + }, { "parts": _payload_parts(payload), "negative_parts": _payload_negative_parts(payload), "variables": variables, }, ) + injection_payload = injection_payload_from_generation_request(payload) + dependency_injection = ( + dependency_context.get("injection_template") + if isinstance(dependency_context.get("injection_template"), dict) + else {} + ) + material_bindings = ( + injection_payload["material_bindings"] + or dependency_context.get("material_bindings") + or dependency_injection.get("material_bindings") + or [] + ) + node_injection = build_node_injection_contract( + node, + requested_kind=injection_payload["requested_kind"], + user_instruction=injection_payload["user_instruction"], + dependency_context=dependency_context, + material_bindings=material_bindings, + prompt_plan=prompt_plan or dependency_injection.get("prompt_plan") or {}, + stage_inputs=injection_payload["stage_inputs"], + selected_draft_image_ids=injection_payload["selected_draft_image_ids"], + ui_context=injection_payload["ui_context"], + ) return { **result.to_dict(), "source": "profile_node", "profile_id": profile.profile_id, "node_id": node.node_id, + "node_injection": node_injection, } diff --git a/backend/services/prompt_plan_schema_service.py b/backend/services/prompt_plan_schema_service.py new file mode 100644 index 0000000..1b56839 --- /dev/null +++ b/backend/services/prompt_plan_schema_service.py @@ -0,0 +1,366 @@ +"""Normalize prompt planning output for workflow generation nodes.""" + +from __future__ import annotations + +from copy import deepcopy +from typing import Any + +from backend.dto.workflow_dto import WorkflowNode + + +PROMPT_PLAN_SCHEMA_VERSION = "2026-07-09.prompt-plan-schema.v1" + +TARGET_NODE_KEYS: dict[str, tuple[str, ...]] = { + "portrait": ("headshot", "fast_candidates"), + "headshot": ("headshot", "fast_candidates"), + "face": ("headshot", "fast_candidates"), + "bust": ("half_body",), + "halfbody": ("half_body",), + "half_body": ("half_body",), + "fullbody": ("full_body",), + "full_body": ("full_body",), + "body": ("full_body",), + "sheet": ("expression_sheet", "pose_sheet"), + "design_sheet": ("expression_sheet", "pose_sheet"), + "expression_sheet": ("expression_sheet",), + "pose_sheet": ("pose_sheet",), + "all": ("headshot", "fast_candidates", "half_body", "full_body", "expression_sheet", "pose_sheet"), +} + +TARGET_TITLES: dict[str, str] = { + "portrait": "Portrait/headshot", + "bust": "Half body", + "fullbody": "Full body", + "sheet": "Design sheet", + "all": "All generation nodes", +} + + +def normalize_prompt_plan( + payload: Any, + workflow_nodes: list[WorkflowNode] | None = None, +) -> dict[str, Any]: + if isinstance(payload, dict) and payload.get("schema_version") == PROMPT_PLAN_SCHEMA_VERSION: + normalized = deepcopy(payload) + if workflow_nodes: + matrix = normalized.get("matrix") if isinstance(normalized.get("matrix"), list) else [] + normalized["by_node_id"] = _index_by_node_id(matrix, workflow_nodes) + normalized["node_parameters"] = normalized["by_node_id"] + return normalized + + source = _extract_source_payload(payload) + matrix = [ + _normalize_matrix_item(item, index + 1) + for index, item in enumerate(_matrix_items(source)) + if isinstance(item, dict) + ] + guardrails = _normalize_guardrails(source, matrix) + conflicts = _collect_conflicts(matrix, guardrails) + by_target = {item["target_id"]: item for item in matrix} + by_node_key = _index_by_node_key(matrix) + by_node_id = _index_by_node_id(matrix, workflow_nodes or []) + return { + "schema_version": PROMPT_PLAN_SCHEMA_VERSION, + "target_prompt_language": "en", + "profile_id": _optional_text(source.get("profile_id")), + "profile_name": _optional_text(source.get("profile_name")), + "matrix": matrix, + "guardrails": guardrails, + "conflicts": conflicts, + "by_target": by_target, + "by_node_key": by_node_key, + "by_node_id": by_node_id, + "node_parameters": by_node_id, + "prompt_context": _prompt_context(matrix), + "summary": { + "target_count": len(matrix), + "ready_count": len([item for item in matrix if item["status"] == "ready"]), + "review_count": len([item for item in matrix if item["status"] == "review"]), + "conflict_count": len(conflicts), + "negative_prompt": guardrails["negative_prompt"], + "target_node_keys": sorted(by_node_key), + }, + } + + +def prompt_plan_for_node( + workflow_metadata: dict[str, Any], + node: WorkflowNode, + workflow_nodes: list[WorkflowNode] | None = None, +) -> dict[str, Any]: + normalized = normalize_prompt_plan(workflow_metadata.get("prompt_plan"), workflow_nodes) + return node_prompt_plan_view(normalized, node) + + +def node_prompt_plan_view(normalized: dict[str, Any], node: WorkflowNode) -> dict[str, Any]: + by_node_id = normalized.get("by_node_id") if isinstance(normalized.get("by_node_id"), dict) else {} + by_node_key = normalized.get("by_node_key") if isinstance(normalized.get("by_node_key"), dict) else {} + node_view = by_node_id.get(node.node_id) + if not isinstance(node_view, dict): + node_view = _node_view(by_node_key.get(node.node_key, []), node.node_id, node.node_key, node.title) + return { + "schema_version": normalized.get("schema_version", PROMPT_PLAN_SCHEMA_VERSION), + "target_prompt_language": normalized.get("target_prompt_language", "en"), + "summary": normalized.get("summary", {}), + "guardrails": normalized.get("guardrails", _normalize_guardrails({}, [])), + "conflicts": normalized.get("conflicts", []), + "target_node_id": node.node_id, + "target_node_key": node.node_key, + "target_node_title": node.title, + **node_view, + } + + +def _extract_source_payload(payload: Any) -> dict[str, Any]: + if not isinstance(payload, dict): + return {} + for key in ("prompt_plan", "prompt_matrix"): + value = payload.get(key) + if isinstance(value, dict): + return value + return payload + + +def _matrix_items(source: dict[str, Any]) -> list[Any]: + for key in ("matrix", "items", "targets", "updates"): + value = source.get(key) + if isinstance(value, list): + return value + return [] + + +def _normalize_matrix_item(item: dict[str, Any], ordinal: int) -> dict[str, Any]: + raw_target = _optional_text(item.get("target") or item.get("target_id") or item.get("id")) or f"target_{ordinal:02d}" + target_id = _canonical_target(raw_target) + target_node_keys = _target_node_keys(item, target_id) + positive_prompt = _optional_text( + item.get("positive_prompt") + or item.get("prompt") + or item.get("positive_patch") + or item.get("prompt_patch") + ) + negative_prompt = _optional_text(item.get("negative_prompt") or item.get("negative") or item.get("negative_patch")) + conflicts = _string_list(item.get("conflicts") or item.get("conflict")) + status = _status(item.get("status"), conflicts) + return { + "id": _optional_text(item.get("id")) or target_id, + "target_id": target_id, + "target_title": _optional_text(item.get("title") or item.get("target_title")) or TARGET_TITLES.get(target_id, raw_target), + "target_label": _optional_text(item.get("target_label") or item.get("target")) or raw_target, + "target_node_keys": target_node_keys, + "positive_prompt": positive_prompt or "", + "positive_patch": _optional_text(item.get("positive_patch") or item.get("prompt_patch")) or positive_prompt or "", + "negative_prompt": negative_prompt or "", + "negative_patch": _optional_text(item.get("negative_patch")) or negative_prompt or "", + "weight_suggestions": _weight_suggestions(item), + "weight_basis": _optional_text(item.get("weight_basis") or item.get("weightBasis")) or "", + "inheritance_fields": _string_list(item.get("inheritance_fields") or item.get("inherits")), + "inheritance_changes": _string_list(item.get("inheritance_changes")), + "conflicts": conflicts, + "status": status, + "next_action": _optional_text(item.get("next_action")) or ("review_conflicts" if conflicts else "apply_prompt_plan"), + } + + +def _normalize_guardrails(source: dict[str, Any], matrix: list[dict[str, Any]]) -> dict[str, Any]: + raw_guards = source.get("guardrails") or source.get("guards") or source.get("guard_checks") or [] + guard_items = raw_guards if isinstance(raw_guards, list) else [] + negative_prompt = _optional_text(source.get("negative_prompt") or source.get("negative")) + if not negative_prompt: + negative_prompt = next((item["negative_prompt"] for item in matrix if item.get("negative_prompt")), "") + checks = [_normalize_guard_item(item) for item in guard_items if isinstance(item, dict)] + if negative_prompt and not any(item["key"] == "negative_prompt" for item in checks): + checks.insert( + 0, + { + "key": "negative_prompt", + "label": "Negative prompt", + "value": negative_prompt, + "status": "ready", + "severity": "info", + }, + ) + return { + "negative_prompt": negative_prompt or "", + "checks": checks, + "language_policy": { + "source_language": "auto", + "target_prompt_language": "en", + "translation_mode": "backend_llm", + }, + } + + +def _normalize_guard_item(item: dict[str, Any]) -> dict[str, str]: + label = _optional_text(item.get("label") or item.get("key")) or "Guard" + status = _status(item.get("status"), _string_list(item.get("conflicts"))) + return { + "key": _key(label), + "label": label, + "value": _optional_text(item.get("value")) or "", + "status": status, + "severity": _optional_text(item.get("severity")) or ("warn" if status == "review" else "info"), + } + + +def _index_by_node_key(matrix: list[dict[str, Any]]) -> dict[str, list[dict[str, Any]]]: + result: dict[str, list[dict[str, Any]]] = {} + for item in matrix: + for node_key in item["target_node_keys"]: + result.setdefault(node_key, []).append(item) + return result + + +def _index_by_node_id( + matrix: list[dict[str, Any]], + workflow_nodes: list[WorkflowNode], +) -> dict[str, dict[str, Any]]: + by_node_key = _index_by_node_key(matrix) + return { + node.node_id: _node_view(by_node_key.get(node.node_key, []), node.node_id, node.node_key, node.title) + for node in workflow_nodes + if by_node_key.get(node.node_key) + } + + +def _node_view( + items: list[dict[str, Any]], + node_id: str | None, + node_key: str, + title: str, +) -> dict[str, Any]: + positive_parts = [item["positive_patch"] for item in items if item.get("positive_patch")] + negative_parts = [item["negative_patch"] for item in items if item.get("negative_patch")] + return { + "target_node_id": node_id, + "target_node_key": node_key, + "target_node_title": title, + "has_prompt_plan": bool(items), + "matrix_items": deepcopy(items), + "positive_prompt_patches": positive_parts, + "negative_prompt_patches": negative_parts, + "prompt_context": ", ".join(positive_parts), + "generation_params": { + "prompt_patch": ", ".join(positive_parts), + "negative_patch": ", ".join(negative_parts), + "weight_suggestions": [suggestion for item in items for suggestion in item["weight_suggestions"]], + "inheritance_fields": _unique_texts(field for item in items for field in item["inheritance_fields"]), + }, + } + + +def _target_node_keys(item: dict[str, Any], target_id: str) -> list[str]: + explicit = _string_list(item.get("target_node_keys") or item.get("node_keys")) + if explicit: + return explicit + return list(TARGET_NODE_KEYS.get(target_id, TARGET_NODE_KEYS.get("all", ()))) + + +def _canonical_target(value: str) -> str: + normalized = value.strip().lower().replace("-", "_").replace(" ", "_") + aliases = { + "头像": "portrait", + "头像节点": "portrait", + "portrait": "portrait", + "headshot": "portrait", + "face": "portrait", + "半身": "bust", + "半身节点": "bust", + "bust": "bust", + "halfbody": "bust", + "half_body": "bust", + "全身": "fullbody", + "全身节点": "fullbody", + "body": "fullbody", + "fullbody": "fullbody", + "full_body": "fullbody", + "设定图": "sheet", + "设定图节点": "sheet", + "sheet": "sheet", + "design_sheet": "sheet", + "all": "all", + } + return aliases.get(normalized, normalized) + + +def _weight_suggestions(item: dict[str, Any]) -> list[dict[str, Any]]: + raw = item.get("weight_suggestions") or item.get("weight_changes") or item.get("weight") + if isinstance(raw, str): + return [{"label": raw, "value": None, "reason": _optional_text(item.get("weight_basis") or item.get("weightBasis")) or ""}] + if not isinstance(raw, list): + return [] + suggestions = [] + for entry in raw: + if isinstance(entry, dict): + suggestions.append( + { + "label": _optional_text(entry.get("label") or entry.get("target")) or "weight", + "value": entry.get("value"), + "reason": _optional_text(entry.get("reason") or entry.get("basis")) or "", + } + ) + else: + text = _optional_text(entry) + if text: + suggestions.append({"label": text, "value": None, "reason": ""}) + return suggestions + + +def _collect_conflicts(matrix: list[dict[str, Any]], guardrails: dict[str, Any]) -> list[dict[str, str]]: + conflicts = [ + {"target_id": item["target_id"], "message": conflict, "severity": "warn"} + for item in matrix + for conflict in item["conflicts"] + ] + for guard in guardrails.get("checks", []): + if guard.get("status") == "review": + conflicts.append({"target_id": "guardrails", "message": guard.get("value", ""), "severity": "warn"}) + return conflicts + + +def _prompt_context(matrix: list[dict[str, Any]]) -> list[dict[str, str]]: + return [ + { + "target_id": item["target_id"], + "target_node_keys": ", ".join(item["target_node_keys"]), + "positive_patch": item["positive_patch"], + "negative_patch": item["negative_patch"], + } + for item in matrix + if item.get("positive_patch") or item.get("negative_patch") + ] + + +def _status(value: Any, conflicts: list[str]) -> str: + status = str(value or "").strip().lower() + if status in {"ready", "review", "pending"}: + return status + return "review" if conflicts else "ready" + + +def _string_list(value: Any) -> list[str]: + if isinstance(value, str): + return [value] if value.strip() else [] + if not isinstance(value, list): + return [] + return _unique_texts(str(item).strip() for item in value if str(item or "").strip()) + + +def _unique_texts(values: Any) -> list[str]: + result: list[str] = [] + for value in values: + text = str(value or "").strip() + if text and text not in result: + result.append(text) + return result + + +def _optional_text(value: Any) -> str | None: + if value is None: + return None + text = str(value).strip() + return text or None + + +def _key(value: str) -> str: + return "_".join(value.lower().replace("/", " ").replace("-", " ").split()) or "guard" diff --git a/backend/services/review_service.py b/backend/services/review_service.py index 84f48a9..51846be 100644 --- a/backend/services/review_service.py +++ b/backend/services/review_service.py @@ -45,8 +45,9 @@ class ReviewService: def review_node(self, node_id: str) -> dict[str, Any]: node = self.workflow_service.get_node(node_id) + workflow = self.workflow_service.get_workflow(node.workflow_id).workflow images = self.image_repository.list_by_node(node_id, include_archived=True) - return self.review_engine.review_node(node, images) + return self.review_engine.review_node(node, images, workflow.metadata) def review_workflow(self, workflow_id: str) -> dict[str, Any]: detail = self.workflow_service.get_workflow(workflow_id) diff --git a/backend/services/workflow_service.py b/backend/services/workflow_service.py index 6ed0cec..6a73323 100644 --- a/backend/services/workflow_service.py +++ b/backend/services/workflow_service.py @@ -7,6 +7,14 @@ from uuid import uuid4 from backend.dto.common import ApiError, ErrorCode from backend.dto.workflow_dto import ProfileWorkflow, WorkflowBranch, WorkflowDetail, WorkflowNode +from backend.services.material_binding_schema_service import ( + MATERIAL_BINDING_SCHEMA_VERSION, + normalize_material_bindings, +) +from backend.services.prompt_plan_schema_service import ( + PROMPT_PLAN_SCHEMA_VERSION, + normalize_prompt_plan, +) from core.audit_service import AuditService from core.event_bus import EventBus from core.fsm import node_transition, workflow_transition @@ -141,6 +149,8 @@ class WorkflowService: update_payload = dict(payload) if "display_name" in update_payload: update_payload["display_name"] = str(update_payload["display_name"]).strip() + if "metadata" in update_payload and isinstance(update_payload["metadata"], dict): + update_payload["metadata"] = self._normalize_workflow_metadata(before, update_payload["metadata"]) after = self.workflow_repository.update_workflow(workflow_id, update_payload) if after is None: raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow not found", {"workflow_id": workflow_id}) @@ -328,10 +338,18 @@ class WorkflowService: 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: + def update_node( + self, + node_id: str, + payload: dict[str, Any], + ctx: RequestContext, + *, + allow_locked_adoption: bool = False, + ) -> WorkflowNode: before = self.get_node(node_id) if before.status in LOCKED_NODE_STATUSES and _node_payload_changes(before, payload): - raise locked_node_edit_error(before) + if not (allow_locked_adoption and _is_locked_image_adoption(before, payload)): + raise locked_node_edit_error(before) if "status" in payload: self._validate_node_status_transition(before.status, str(payload["status"])) if "title" in payload and not str(payload["title"]).strip(): @@ -342,6 +360,8 @@ class WorkflowService: after = self.workflow_repository.update_node(node_id, update_payload) if after is None: raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "node not found", {"node_id": node_id}) + self._sync_material_bindings_from_node(after) + self._sync_prompt_plan_from_node(after) self.audit_service.record( ctx=ctx, action="update_node", @@ -487,6 +507,74 @@ class WorkflowService: if self.event_bus: self.event_bus.publish(event_type, payload) + def _normalize_workflow_metadata( + self, + workflow: ProfileWorkflow, + metadata: dict[str, Any], + ) -> dict[str, Any]: + material_bindings = metadata.get("material_bindings") + prompt_plan = metadata.get("prompt_plan") or metadata.get("prompt_matrix") + if material_bindings is None and prompt_plan is None: + return metadata + nodes = self.workflow_repository.list_nodes(workflow.workflow_id, branch_name=workflow.current_branch) + normalized = dict(metadata) + if material_bindings is not None: + normalized["material_binding_schema_version"] = MATERIAL_BINDING_SCHEMA_VERSION + normalized["material_bindings"] = normalize_material_bindings(material_bindings, nodes) + if prompt_plan is not None: + normalized["prompt_plan_schema_version"] = PROMPT_PLAN_SCHEMA_VERSION + normalized["prompt_plan"] = normalize_prompt_plan(prompt_plan, nodes) + normalized.pop("prompt_matrix", None) + return normalized + + def _sync_material_bindings_from_node(self, node: WorkflowNode) -> None: + if node.node_key != "material_bind" or not isinstance(node.metadata, dict): + return + raw_bindings = node.metadata.get("material_bindings") + if raw_bindings is None: + return + workflow = self.workflow_repository.get_workflow(node.workflow_id) + if workflow is None: + return + nodes = self.workflow_repository.list_nodes(node.workflow_id, branch_name=node.branch_name) + normalized = normalize_material_bindings(raw_bindings, nodes) + self.workflow_repository.update_workflow( + workflow.workflow_id, + { + "metadata": { + **(workflow.metadata or {}), + "material_binding_schema_version": MATERIAL_BINDING_SCHEMA_VERSION, + "material_binding_source_node_id": node.node_id, + "material_binding_updated_at": node.updated_at, + "material_bindings": normalized, + } + }, + ) + + def _sync_prompt_plan_from_node(self, node: WorkflowNode) -> None: + if node.node_key != "prompt_plan" or not isinstance(node.metadata, dict): + return + raw_plan = node.metadata.get("prompt_plan") or node.metadata.get("prompt_matrix") + if raw_plan is None: + return + workflow = self.workflow_repository.get_workflow(node.workflow_id) + if workflow is None: + return + nodes = self.workflow_repository.list_nodes(node.workflow_id, branch_name=node.branch_name) + normalized = normalize_prompt_plan(raw_plan, nodes) + self.workflow_repository.update_workflow( + workflow.workflow_id, + { + "metadata": { + **(workflow.metadata or {}), + "prompt_plan_schema_version": PROMPT_PLAN_SCHEMA_VERSION, + "prompt_plan_source_node_id": node.node_id, + "prompt_plan_updated_at": node.updated_at, + "prompt_plan": normalized, + } + }, + ) + def new_workflow_id() -> str: return f"workflow_{uuid4().hex}" @@ -546,6 +634,21 @@ def _node_payload_changes(node: WorkflowNode, payload: dict[str, Any]) -> bool: return False +def _is_locked_image_adoption(node: WorkflowNode, payload: dict[str, Any]) -> bool: + allowed_keys = {"status", "selected_image_id"} + if any(key not in allowed_keys for key in payload): + return False + selected_image_id = payload.get("selected_image_id") + if not selected_image_id: + return False + target_status = payload.get("status", node.status) + if target_status != "selected": + return False + if node.status == "completed": + return True + return node.status == "selected" and node.selected_image_id == selected_image_id + + def _branch_copy_status(node: WorkflowNode, base_sort_order: int | None) -> str: if base_sort_order is None: return "draft" diff --git a/database/seed.py b/database/seed.py index e78f177..e9c0444 100644 --- a/database/seed.py +++ b/database/seed.py @@ -13,6 +13,7 @@ from database.db_helper import Database class SeedCategory: category_id: str name: str + parent_id: str | None = None category_type: str = "material" sort_order: int = 0 metadata: dict[str, Any] = field(default_factory=dict) @@ -29,6 +30,100 @@ DEFAULT_MATERIAL_CATEGORIES: tuple[SeedCategory, ...] = ( sort_order=50, metadata={"seed": True, "wardrobe_category": "accessory"}, ), + SeedCategory( + "cat_imagestudio_构图", + "构图", + sort_order=110, + metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "构图"}, + ), + SeedCategory( + "cat_imagestudio_服装", + "服装", + sort_order=120, + metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "服装"}, + ), + SeedCategory( + "cat_imagestudio_风格", + "风格", + sort_order=130, + metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "风格"}, + ), + SeedCategory( + "cat_imagestudio_配饰", + "配饰", + sort_order=140, + metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_鞋袜", + "鞋袜", + sort_order=150, + metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "鞋袜"}, + ), + SeedCategory( + "cat_imagestudio_accessory_眼部", + "眼部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=210, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_头部", + "头部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=220, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_手部", + "手部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=230, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_胸前", + "胸前", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=240, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_颈部", + "颈部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=250, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_腰部", + "腰部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=260, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_脸部", + "脸部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=270, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), + SeedCategory( + "cat_imagestudio_accessory_臂部", + "臂部", + parent_id="cat_imagestudio_配饰", + category_type="accessory_group", + sort_order=280, + metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, + ), ) @@ -51,12 +146,13 @@ def seed_default_material_categories(database: Database) -> dict[str, Any]: INSERT INTO material_categories ( category_id, name, parent_id, category_type, sort_order, metadata_json ) VALUES ( - {placeholder}, {placeholder}, NULL, {placeholder}, {placeholder}, {placeholder} + {placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder} ) """, ( category.category_id, category.name, + category.parent_id, category.category_type, category.sort_order, json.dumps(category.metadata, ensure_ascii=False, sort_keys=True), diff --git a/docs/ui-progress-2026-07-09.md b/docs/ui-progress-2026-07-09.md new file mode 100644 index 0000000..ca3c426 --- /dev/null +++ b/docs/ui-progress-2026-07-09.md @@ -0,0 +1,272 @@ +# ImageStudio UI / Workflow Progress Handoff + +日期:2026-07-09 +范围:生成页、快速角色、角色工作流、阶段审查、锁定与分支、后端接口闭环 + +## 当前总体状态 + +- 生成页已经形成三条主入口:`图片生成`、`快速角色`、`角色工作流`。 +- 三条入口都已经有前端工作台,并已接入或预留对应后端接口: + - 图片生成:走单图工作流与 `nodes/:nodeId/generate`。 + - 快速角色:走 `profiles/quick-draft/save` 保存档案并创建工作流关联。 + - 角色工作流:走 `profiles/:profileId/workflows`、`workflows/:workflowId/nodes` 和节点级生成/审查/历史接口。 +- 当前重点已经从纯 UI 进入“角色工作流闭环”:档案导入、设定拆解、提示词规划、素材绑定、头像/半身/设定图、审查、高清修复、入库。 + +## 已完成内容 + +### 快速角色与角色档案 + +- 创建角色档案问卷已支持简单/复杂模式。 +- 复杂问卷字段包含世界观、背景、身高/身材、身体特征、喜好、关系、故事钩子、生成风格、镜头偏好等。 +- 问卷字段可留空,不要求全部填写,避免破坏创建体验。 +- 生成档案按钮语义已从“生成预览”改为“生成档案”。 +- 生成档案后会在右侧角色卡区域生成/选中新角色卡。 +- 外部角色卡导入已提供弹窗入口,可粘贴外部人设文本并回填问卷字段。 +- 快速角色参考素材选择沿用分类素材卡,默认生产语义应为“不使用参考”,避免无意引入参考图。 + +### 前期人设准备 + +- `前期人设准备` 页面布局已调整:导入入口放到侧面,上方主卡片下移,侧边空间更有效。 +- 该阶段包含: + - 导入粗略人设 + - LLM 设定拆解 + - 提示词规划 + - 参考素材绑定 +- LLM 设定拆解卡片按组展示身份性格、外观锚点、服装素材、约束负面项。 +- 大卡片尺寸保持稳定,内部小项目按顺序排列,内容超出时滚动。 +- 底部对话框会根据当前节点注入上下文,包括角色档案、节点信息、素材、拆解字段和用户原文,并标记后端 LLM 翻译为英文提示词。 + +### 提示词规划 + +- 提示词规划页面已搭建矩阵化展示。 +- 支持头像、半身、全身、设定图等目标节点的提示词条目。 +- 已加入继承字段、权重依据、负面词和冲突项校验区域。 +- 修复过卡片重叠、内容被遮挡、胶囊超出卡片下沿等布局问题。 +- 提示词规划 LLM 输出结构已补齐后端 schema,版本为 `2026-07-09.prompt-plan-schema.v1`。 +- 后端提示词规划 schema 包含: + - `matrix`:头像、半身、全身、设定图等目标提示词矩阵。 + - `weight_suggestions`:权重建议、数值和依据。 + - `inheritance_fields` / `inheritance_changes`:继承字段与变更建议。 + - `guardrails`:负面提示词、语言策略和检查项。 + - `conflicts`:按目标节点归属的冲突项。 + - `by_target` / `by_node_key` / `by_node_id`:按目标、后端节点类型和真实节点索引。 +- 保存 `prompt_plan` 节点 metadata 或 workflow metadata 时,后端会自动规范化为正式 prompt plan schema。 +- 节点依赖上下文会按当前目标节点过滤提示词规划,例如头像节点只拿头像 prompt patch,全身节点只拿全身 prompt patch。 +- `PromptExpansionService` 会从 dependency context 自动继承 prompt plan 的 positive/negative patch,并注入生成链路与 `node_injection.prompt_plan`。 + +### 参考素材绑定 + +- 参考素材绑定页面已建立。 +- 绑定节点已翻译为中文,并改为直接绑定大节点,不再绑定后期生成才会出现的小节点。 +- 已按“快速角色参考素材”的交互方式提供素材选择。 +- 参考素材选择弹窗已改为悬浮在上方,而不是向下撑开布局。 +- 默认生产语义应为“不使用参考”,只有用户显式选择后才注入素材。 +- 素材绑定规则已接入后端 metadata 写入与 workflow 级规范化落库。 +- 后端素材绑定 schema 版本为 `2026-07-09.material-binding-schema.v1`。 +- 保存 `material_bind` 节点时,后端会自动生成: + - `rules`:完整规则,包含未选择素材的“不使用参考”分类。 + - `active_rules`:用户显式选择素材后才启用的规则。 + - `by_stage`:按前端大阶段索引。 + - `by_node_key`:按后端节点类型索引。 + - `by_node_id` / `node_parameters`:按真实 workflow node 索引。 +- 后端默认参考策略为 `explicit_only`,`default_use_reference=false`,空素材分类不会进入生成参考。 +- 节点依赖上下文会按当前目标节点过滤素材,例如头像只拿头像阶段绑定,半身只拿半身阶段绑定。 +- `PromptExpansionService` 会从 dependency context 自动继承规范化素材绑定,供后端 LLM 和生成链路注入。 + +### 角色工作流大节点 + +- 前端大节点已对齐后端设计,当前角色工作流大节点包括: + - 前期人设准备 + - 头像 + - 半身像 + - 全身与设定图 + - 高清修复并入库 +- 已补齐半身像节点。 +- 前期人设准备暂时不再调整,后续重点转入生成链路与审查链路。 + +### 头像 / 半身 / 设定图生成逻辑 + +- 头像节点: + - 第一个节点按钮为“生成头像”。 + - 后续轮次按钮为“再跑一轮”。 + - 原“再跑一轮”语义下的确认按钮已改为“确认使用”。 +- 半身像和全身/设定图已按同样逻辑整理。 +- 选中候选后可进入对应阶段审查节点。 +- 下方对话框已按当前节点分别注入节点上下文。 + +### 阶段审查节点 + +- 已加入 UI 层阶段审查节点: + - `portrait-review`:头像审查 + - `halfbody-review`:半身像审查 + - `body-sheet-review`:设定图审查 +- 阶段审查映射到真实后端节点: + - 头像审查映射到 `headshot` + - 半身像审查映射到 `half_body` + - 设定图审查映射到 `expression_sheet` / 设定图相关节点 +- 审查阶段按钮已调整为: + - `选中的图片重新生成` + - `对比候选` + - `确认` +- `选中的图片重新生成` 用于同提示词不同模型复跑,解决某个模型偶尔异常的问题。 +- `确认` 用于通过审查并推进到下一阶段。 + +### 审查确认闭环 + +- 审查阶段点击 `确认` 已接入真实闭环: + - 调用 `nodes/:nodeId/adopt-image`。 + - 将选中图片写入后端采用历史。 + - 将上一真实生成节点状态收敛为 `selected`。 + - 上一节点进入锁定状态,后续修改需要从该点创建分支。 + - 推进 `workflow.current_node_id` 到下一真实节点。 + - 在 workflow metadata 的 `stage_inputs` 中记录阶段输入交接。 +- 设定图审查确认后,选中的成品稿会放入 `高清修复` 输入桶。 +- 前端维护 `workflowInputImagesByNodeId`,用于展示“下一节点输入图”,不伪造图片真实归属。 +- 如果下一节点已经锁定,前端不会再尝试修改下一节点 metadata,只记录 workflow 级交接信息。 +- 后端 `adopt_node_image` 已消费前端传入的 `next_backend_node_id`、`handoff_bucket`、`handoff_role` 等 metadata。 +- 后端会将阶段交接正式写入 `workflow.metadata.stage_inputs`,schema 版本为 `2026-07-09.stage-input-bucket.v1`。 +- 后端会同步推进 `workflow.current_node_id` 到下一真实节点,并保留最近阶段审查交接日志 `stage_review_handoffs`。 +- 前端已补齐 `workflowReviewAdopt` 操作: + - 头像/半身/设定图阶段显示 `确认采用`。 + - 高清修复阶段显示 `确认修复结果`。 + - 采用前要求选中的图片是真实后端图片,且必须属于当前后端节点。 + - `completed` 节点允许本地切换候选图并执行一次确认采用,但不允许重新生成、重跑或修改提示词。 + - `selected` 节点视为最终锁定,只显示创建分支和保存快照。 + - 头像/半身采用后优先按后端 `sort_order` 推进到下一真实节点。 + - 设定图采用后固定交接到 `高清修复与统一风格`。 + - 高清修复采用后交接到 `入库`。 + - 采用成功后前端会刷新 workflow、nodes 和 node images,并跳转到下一 UI 节点。 + +### 后端注入与审查结构 + +- 已新增 `backend/services/node_injection_template_service.py`,统一定义每个节点的对话注入契约。 +- 注入契约包含用户原文、节点上下文、上游选图、素材绑定、阶段输入、语言翻译策略和预期 JSON 返回结构。 +- 已补齐 `stage_review` 与 `candidate_review` 注入模板,支持审查节点下方对话框按节点独立注入。 +- `prompt_plan` 注入模板已补入正式提示词规划 schema、权重建议、继承字段、负面词和冲突返回结构。 +- `material_bind` 注入模板已补入正式素材绑定 schema、默认参考策略和目标后端节点字段。 +- `NodeDependencyContextService` 已返回 `injection_template`,供前端或后续 LLM 编排读取。 +- `PromptExpansionService` 已返回 `node_injection`,生成链路可以把节点注入契约一并传给后端 LLM。 +- `ReviewFlowEngine.review_node` 已返回正式 `stage_review` 结构,schema 版本为 `2026-07-09.stage-review-result.v1`。 +- 阶段审查结果结构包含: + - 同提示词多模型候选 `candidates` + - 候选评分 `score` + - 画面缺陷与警告 `defects` / `warnings` + - 选中候选 `selected_candidate` + - 重生成记录 `regeneration_records` + - 人工确认动作 `manual_confirmation` + - 下一阶段输入桶预期结构 `handoff` + +### 锁定与分支 + +- 后端锁定状态包括 `completed` 和 `selected`。 +- 普通编辑、重跑、选图、归档或删除锁定节点绑定图片会被阻止。 +- 前端在锁定节点显示“从此节点创建分支”入口。 +- 创建分支调用: + - `POST workflows/:workflowId/branches` + - `POST workflows/:workflowId/switch-branch` +- 分支规则: + - 切点之前的节点保留完成状态和选中图。 + - 切点及之后重置为 `draft` 并清空选中图。 + - metadata 记录 `copied_from_node_id`、切点和分支继承信息。 +- 为审查确认补充了后端受控例外: + - 允许 `completed -> selected` 的采用收敛。 + - 已经 `selected` 后切换到另一张图仍会拒绝,继续要求新建分支。 + +## 后端接口状态 + +### 已接入或已使用 + +- `profiles` +- `profiles/quick-draft` +- `profiles/quick-draft/save` +- `profiles/:profileId/workflows` +- `workflows/:workflowId` +- `workflows/:workflowId/nodes` +- `workflows/:workflowId/execute-character` +- `workflows/:workflowId/branches` +- `workflows/:workflowId/switch-branch` +- `workflows/:workflowId/history` +- `workflows/:workflowId/review` +- `nodes/:nodeId` +- `nodes/:nodeId/generate` +- `nodes/:nodeId/retry` +- `nodes/:nodeId/complete` +- `nodes/:nodeId/select-image` +- `nodes/:nodeId/adopt-image` +- `nodes/:nodeId/review` +- `nodes/:nodeId/images` +- `images/:imageId` + +### 已预留但仍需后端深化 + +- 最终角色版本入库 schema。 +- 角色图库与素材库联动。 + +## 关键代码位置 + +- 前端主页面:`frontend/standalone-web/src/App.vue` +- 前端接口定义:`frontend/shared/api/endpoints.ts` +- 后端 workflow 服务:`backend/services/workflow_service.py` +- 后端 history/adopt 服务:`backend/services/history_service.py` +- 后端提示词规划 schema:`backend/services/prompt_plan_schema_service.py` +- 后端素材绑定 schema:`backend/services/material_binding_schema_service.py` +- 后端节点注入模板:`backend/services/node_injection_template_service.py` +- 后端节点依赖上下文:`backend/services/node_dependency_context_service.py` +- 后端审查引擎:`engines/review_flow.py` +- 后端角色工作流服务:`backend/services/character_workflow_service.py` +- 角色工作流节点模板:`engines/character_flow.py` +- 相关测试: + - `tests/test_history_service.py` + - `tests/test_review_service.py` + - `tests/test_prompt_plan_schema_service.py` + - `tests/test_material_binding_schema_service.py` + - `tests/test_prompt_expansion_service.py` + - `tests/test_node_dependency_context_service.py` + - `tests/test_workflow_service.py` + - `tests/test_character_workflow_service.py` + +## 已验证 + +- 前端类型检查: + - `npm.cmd run typecheck` + - 结果:通过。 +- 后端 history 测试: + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_history_service` + - 结果:4 tests OK。 +- 后端审查/历史/节点注入/单图生成相关测试: + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_review_service tests.test_history_service tests.test_prompt_expansion_service tests.test_node_dependency_context_service tests.test_single_flow_generation` + - 结果:28 tests OK。 +- 后端素材绑定 schema / workflow 同步 / 上下文注入测试: + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_material_binding_schema_service tests.test_workflow_service tests.test_node_dependency_context_service tests.test_prompt_expansion_service tests.test_review_service tests.test_history_service` + - 结果:37 tests OK。 +- 后端生成链路补充测试: + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_single_flow_generation tests.test_character_workflow_service` + - 结果:15 tests OK。 +- 后端提示词规划 schema / workflow 同步 / 上下文注入测试: + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_prompt_plan_schema_service tests.test_workflow_service tests.test_node_dependency_context_service tests.test_prompt_expansion_service` + - 结果:26 tests OK。 +- 后端角色工作流核心回归: + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_prompt_plan_schema_service tests.test_material_binding_schema_service tests.test_workflow_service tests.test_node_dependency_context_service tests.test_prompt_expansion_service tests.test_review_service tests.test_history_service tests.test_single_flow_generation tests.test_character_workflow_service` + - 结果:57 tests OK。 +- 审查确认采用前端闭环补充验证: + - `npm.cmd run typecheck` + - 结果:通过。 + - 使用 Codex bundled Python 运行 `python -m unittest tests.test_prompt_plan_schema_service tests.test_material_binding_schema_service tests.test_review_service tests.test_quick_profile_service tests.test_workflow_service tests.test_node_dependency_context_service tests.test_prompt_expansion_service tests.test_history_service` + - 结果:44 tests OK。 +- 本地 dev server: + - `http://127.0.0.1:5173/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/?view=generate` + - HTTP 返回 200。 + +## 当前限制与风险 + +- 浏览器插件在一次 reload 验证时超时重置,因此没有执行页面内真实点击确认验证,避免误改本地工作流数据。 +- `web/` 静态产物当前有旧资产删除和新资产生成,属于之前构建结果,提交前需要统一确认是否纳入。 +- 工作区存在多处既有改动,提交前需要按功能拆分检查,不要把无关改动混进同一版。 +- 阶段审查与高清修复输入桶已有后端结构,但还没有做完整页面点击回归。 +- 最终入库节点仍需补齐角色版本、图片、提示词、素材引用、审查记录和分支信息的正式 schema。 + +## 建议下一步 + +1. 完成最终入库节点:保存角色版本、图片、提示词、素材引用、审查记录和分支信息。 +2. 做一次完整浏览器手动回归:头像确认、半身确认、设定图确认、进入高清修复、锁定后创建分支。 +3. 视提交策略决定是否纳入当前 `web/` 静态构建产物,避免把无关构建文件混入功能提交。 diff --git a/docs/ui-progress-2026-07-10.md b/docs/ui-progress-2026-07-10.md new file mode 100644 index 0000000..7323206 --- /dev/null +++ b/docs/ui-progress-2026-07-10.md @@ -0,0 +1,101 @@ +# ImageStudio UI / Workflow Progress Handoff + +日期:2026-07-10 +分支:`codex/ui-navigation-s130-s145` +范围:未提交变更检查、功能验证、静态构建、推送前总结。 + +## 推送前状态 + +- 已执行 `git fetch origin`。 +- 检查结果:本地分支相对 `origin/codex/ui-navigation-s130-s145` 为 `0 ahead / 0 behind`,也就是没有“已提交但未推送”的 commit。 +- 当前待发布内容来自工作区未提交改动,主要集中在完整工作台 UI、素材库工作台、队列工作台、角色工作流闭环、后端 schema/注入服务和对应测试。 + +## 已完成内容 + +### 前端工作台 + +- `frontend/standalone-web/src/App.vue` 扩展为更完整的 ImageStudio 工作台。 +- 顶部导航现在聚焦:概览、生成、素材、角色、图库、设置、日志。 +- 新增素材库工作台: + - 素材分类筛选。 + - 示例素材卡和后端素材卡统一展示。 + - 素材详情弹层。 + - 添加分类弹层。 + - 刷新素材、上传素材、识别、重识别、确认入库、导入素材包、并入衣橱、进入素材绑定等操作入口。 +- 新增队列工作台: + - 活跃、失败、完成、全部筛选。 + - 任务详情和批量操作入口。 +- 角色工作流继续补齐: + - 提示词规划节点页面。 + - 参考素材绑定节点页面。 + - 阶段审查和候选图采用动作。 + - 锁定节点创建分支入口。 + - 最终归档入口的 UI 语义。 +- 前端 API 客户端补充 `PUT` 请求方法。 + +### 后端闭环 + +- 新增提示词规划 schema 服务: + - `backend/services/prompt_plan_schema_service.py` + - 支持 prompt matrix、target/node 索引、继承字段、权重建议、guardrails 和 conflicts。 +- 新增素材绑定 schema 服务: + - `backend/services/material_binding_schema_service.py` + - 支持 explicit-only 默认策略、rules、active rules、by stage、by node key、by node id 和 node parameters。 +- 新增节点注入模板服务: + - `backend/services/node_injection_template_service.py` + - 统一生成节点对话注入契约,包含用户原文、节点上下文、素材绑定、提示词规划、阶段输入、草稿图选择和预期返回结构。 +- `WorkflowService`: + - 保存 workflow/node metadata 时同步规范化 prompt plan 和 material bindings。 + - 允许 completed 节点在采用图片时受控进入 selected。 +- `NodeDependencyContextService`: + - 返回 prompt plan、material bindings 和 injection template。 + - 按目标节点过滤上游上下文。 +- `PromptExpansionService`: + - 注入 prompt plan patch、negative patch 和 node injection。 +- `MaterialService`: + - 更新分类时校验 parent_id,避免自引用和无效父分类。 +- 审查和历史相关服务继续补齐阶段审查、图片采用、分支锁定和 stage input handoff。 + +### 静态产物 + +- 已重新执行 `npm.cmd run build:web`。 +- `web/index.html` 已指向新构建产物。 +- 新产物: + - `web/assets/index-C91GfaJN.css` + - `web/assets/index-uM7AKRxw.js` +- 旧产物已被构建替换: + - `web/assets/index-B2NiW3rX.js` + - `web/assets/index-CPo7NaFF.css` + +## 已验证 + +- `npm.cmd run typecheck` + - 结果:通过。 +- `python -m unittest tests.test_prompt_plan_schema_service tests.test_material_binding_schema_service tests.test_workflow_service tests.test_node_dependency_context_service tests.test_prompt_expansion_service tests.test_review_service tests.test_history_service tests.test_material_service tests.test_character_workflow_service tests.test_plugin_entrypoint tests.test_s0_infrastructure` + - 使用 Codex bundled Python。 + - 结果:81 tests OK。 +- `npm.cmd run build:web` + - 结果:通过,生成最新 `web/` 静态产物。 +- 内置浏览器打开: + - `http://127.0.0.1:5173/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web/?view=materials` + - 结果:素材页标题、预览模式提示、示例素材卡、素材操作卡正常渲染。 + - 控制台:未发现 error/warn。 + - 素材详情弹层:点击示例素材卡后可打开。 + +## 未完成或仍需补齐 + +- 当前浏览器验证仍在前端预览模式下完成,没有连接真实 AstrBot 插件后端,因此未覆盖真实落库、文件上传、素材 VLM 识别和衣橱写入。 +- 添加分类弹层需要再做一次真实浏览器交互验收;本次自动点击使用的选择器不够稳定,未作为通过项记录。 +- 最终入库节点仍需要补齐正式 schema:角色版本、最终图片、提示词、素材引用、审查记录、分支信息和归档元数据。 +- 角色图库与素材库的真实联动仍需继续深化。 +- 素材上传入口、素材包导入入口目前以 UI/接口入口为主,文件选择、dry-run 预览和导入执行流程还需要后续完整实现。 +- 需要在真实后端环境做一次端到端验收:快速角色创建、工作流生成、提示词规划保存、素材绑定保存、阶段审查采用、锁定后分支、最终归档。 + +## 备注 + +- 本次检查开始时没有未推送 commit;推送动作应在提交当前工作区改动后执行。 +- 已创建本地提交:`feat: extend workflow and materials workbench`。 +- 已尝试执行 `git push origin codex/ui-navigation-s130-s145`,但远端 Git HTTP 返回凭据不正确或已过期。 +- Forgejo API 认证仍可用,并确认远端 `codex/ui-navigation-s130-s145` 当前仍停在 `b61c6274e5a7ddbcbad1d67f0f0fcfe7da7f3264`。 +- 因此当前状态为:本地分支 ahead 1,等待更新本机 Git 凭据后再次推送。 +- Windows shell 提示多处 LF/CRLF 警告,未发现影响构建或测试的行为问题。 diff --git a/engines/character_flow.py b/engines/character_flow.py index 4b17ba8..c160cc4 100644 --- a/engines/character_flow.py +++ b/engines/character_flow.py @@ -13,54 +13,70 @@ CHARACTER_GRADUAL_STEPS: tuple[dict[str, Any], ...] = ( "description": "Core identity, personality, and visual anchors.", "sort_order": 10, }, + { + "node_key": "prompt_plan", + "title": "Prompt plan", + "step_name": "prompt_plan", + "description": "Prompt matrix for portrait, half body, full body, and design sheet nodes.", + "node_type": "planning", + "sort_order": 20, + }, + { + "node_key": "material_bind", + "title": "Material binding", + "step_name": "material_bind", + "description": "Bind style, composition, clothing, shoes, and accessory references to downstream nodes.", + "node_type": "reference_binding", + "sort_order": 30, + }, { "node_key": "headshot", "title": "Headshot", "step_name": "headshot", "description": "Face, hair, eyes, and expression direction.", - "sort_order": 20, + "sort_order": 40, }, { "node_key": "half_body", "title": "Half body", "step_name": "half_body", "description": "Upper body costume, posture, and silhouette.", - "sort_order": 30, + "sort_order": 50, }, { "node_key": "full_body", "title": "Full body", "step_name": "full_body", "description": "Full outfit, proportions, and full-body design.", - "sort_order": 40, + "sort_order": 60, }, { "node_key": "expression_sheet", "title": "Expression sheet", "step_name": "expression_sheet", "description": "Reusable expression variants.", - "sort_order": 50, + "sort_order": 70, }, { "node_key": "pose_sheet", "title": "Pose sheet", "step_name": "pose_sheet", "description": "Reusable pose and action variants.", - "sort_order": 60, + "sort_order": 80, }, { "node_key": "style_lock", "title": "Style lock", "step_name": "style_lock", "description": "Final style pass and prompt consistency lock.", - "sort_order": 70, + "sort_order": 90, }, { "node_key": "final_pack", "title": "Final pack", "step_name": "final_pack", "description": "Final accepted images and production metadata.", - "sort_order": 80, + "sort_order": 100, }, ) @@ -69,7 +85,7 @@ def gradual_node_templates() -> list[dict[str, Any]]: return [ { **step, - "node_type": "generation", + "node_type": step.get("node_type", "generation"), "status": "draft", "branch_name": "main", "metadata": {"template": "character_gradual_v1"}, diff --git a/engines/review_flow.py b/engines/review_flow.py index 0f38edc..91d8a17 100644 --- a/engines/review_flow.py +++ b/engines/review_flow.py @@ -8,6 +8,8 @@ from typing import Any from backend.dto.image_dto import GenerationImage from backend.dto.workflow_dto import ProfileWorkflow, WorkflowNode +STAGE_REVIEW_RESULT_VERSION = "2026-07-09.stage-review-result.v1" + _KNOWN_NODE_STATUSES = { "draft", @@ -107,7 +109,12 @@ class ReviewFlowEngine: policy, errors = ReviewPolicy.from_mapping(payload) return {"valid": not errors, "policy": policy.to_dict(), "errors": errors} - def review_node(self, node: WorkflowNode, images: list[GenerationImage]) -> dict[str, Any]: + def review_node( + self, + node: WorkflowNode, + images: list[GenerationImage], + workflow_metadata: dict[str, Any] | None = None, + ) -> dict[str, Any]: warnings: list[dict[str, Any]] = [] errors: list[dict[str, Any]] = [] selected_image = next((image for image in images if image.image_id == node.selected_image_id), None) @@ -158,6 +165,7 @@ class ReviewFlowEngine: "node_model_route": node.model_route, "policy_version": self.policy.version, }, + "stage_review": build_stage_review_result(node, images, workflow_metadata or {}), } def review_workflow( @@ -227,3 +235,197 @@ def _normalize_status_list(value: Any) -> tuple[str, ...]: def _normalize_prompt(value: str) -> str: return " ".join(str(value or "").lower().replace(",", " ").split()) + + +def build_stage_review_result( + node: WorkflowNode, + images: list[GenerationImage], + workflow_metadata: dict[str, Any] | None = None, +) -> dict[str, Any]: + candidates = [_review_candidate(node, image, index + 1) for index, image in enumerate(images)] + selected_candidate = next( + ( + { + "candidate_id": candidate["candidate_id"], + "image_id": candidate["image_id"], + "model_route": candidate["model_route"], + "score": candidate["score"], + "status": candidate["status"], + } + for candidate in candidates + if candidate["image_id"] == node.selected_image_id or candidate["is_selected"] + ), + None, + ) + return { + "schema_version": STAGE_REVIEW_RESULT_VERSION, + "node_id": node.node_id, + "node_key": node.node_key, + "review_mode": "same_prompt_multi_model", + "prompt_contract": { + "positive_prompt": node.prompt, + "negative_prompt": node.negative_prompt, + "model_route": node.model_route, + "locked_after_confirm": True, + }, + "scoring_dimensions": [ + "identity_consistency", + "prompt_consistency", + "model_route_consistency", + "visual_defects", + "archive_state", + ], + "candidates": candidates, + "selected_candidate": selected_candidate, + "regeneration_records": [ + candidate["regeneration"] + for candidate in candidates + if candidate.get("regeneration", {}).get("is_regeneration") + ], + "manual_confirmation": { + "action": "adopt_image", + "endpoint": f"nodes/{node.node_id}/adopt-image", + "requires_image_id": True, + "changes_node_status_to": "selected", + "locks_source_node": True, + "branch_required_after_confirm": True, + }, + "handoff": { + "current_node_input": _stage_input_for_node(workflow_metadata or {}, node.node_id), + "metadata_key": "stage_inputs", + "expected_next_input_shape": { + "image_id": "string", + "from_node_id": node.node_id, + "history_id": "string", + "role": "next_stage_input | hires_fix_input", + }, + }, + } + + +def _review_candidate(node: WorkflowNode, image: GenerationImage, rank: int) -> dict[str, Any]: + metadata = image.metadata if isinstance(image.metadata, dict) else {} + warnings = _candidate_warnings(node, image) + defects = _string_list(metadata.get("review_defects") or metadata.get("defects")) + score_value, score_basis = _candidate_score(metadata, warnings, defects, image.is_archived) + status = "selected" if image.is_selected or image.image_id == node.selected_image_id else "candidate" + if image.is_archived: + status = "archived" + return { + "candidate_id": f"{node.node_id}:{image.image_id}", + "rank": rank, + "image_id": image.image_id, + "image_uri": image.image_uri, + "thumbnail_uri": image.thumbnail_uri, + "model_route": image.model_route, + "model_used": image.model_used, + "seed": image.seed, + "width": image.width, + "height": image.height, + "positive_prompt": image.positive_prompt, + "negative_prompt": image.negative_prompt, + "is_selected": image.is_selected or image.image_id == node.selected_image_id, + "is_archived": image.is_archived, + "status": status, + "warnings": warnings, + "defects": defects, + "score": { + "value": score_value, + "basis": score_basis, + "warnings_count": len(warnings), + "defects_count": len(defects), + }, + "regeneration": _regeneration_record(node, image, metadata), + } + + +def _candidate_warnings(node: WorkflowNode, image: GenerationImage) -> list[dict[str, Any]]: + warnings: list[dict[str, Any]] = [] + if image.is_archived: + warnings.append({"code": "ARCHIVED", "message": "candidate image is archived"}) + if node.model_route and image.model_route and node.model_route != image.model_route: + warnings.append( + { + "code": "MODEL_ROUTE_MISMATCH", + "message": "candidate model_route differs from node model_route", + "node_model_route": node.model_route, + "image_model_route": image.model_route, + } + ) + if node.prompt and image.positive_prompt: + node_prompt = _normalize_prompt(node.prompt) + image_prompt = _normalize_prompt(image.positive_prompt) + if node_prompt and image_prompt and node_prompt not in image_prompt and image_prompt not in node_prompt: + warnings.append({"code": "PROMPT_MISMATCH", "message": "candidate prompt differs from node prompt"}) + return warnings + + +def _candidate_score( + metadata: dict[str, Any], + warnings: list[dict[str, Any]], + defects: list[str], + is_archived: bool, +) -> tuple[int, str]: + explicit = metadata.get("review_score") + if isinstance(explicit, (int, float)): + return max(0, min(100, int(explicit))), "metadata.review_score" + score = 100 - len(warnings) * 10 - len(defects) * 8 + if is_archived: + score -= 40 + return max(0, min(100, score)), "heuristic" + + +def _regeneration_record(node: WorkflowNode, image: GenerationImage, metadata: dict[str, Any]) -> dict[str, Any]: + source_image_id = _optional_text( + metadata.get("regenerated_from_image_id") + or metadata.get("source_image_id") + or metadata.get("review_source_image_id") + ) + is_regeneration = bool( + source_image_id + or metadata.get("same_prompt_regeneration") + or metadata.get("review_regeneration") + or metadata.get("regeneration_attempt") + ) + return { + "is_regeneration": is_regeneration, + "image_id": image.image_id, + "source_image_id": source_image_id, + "node_id": node.node_id, + "same_prompt": bool(metadata.get("same_prompt_regeneration") or metadata.get("review_regeneration")), + "attempt": _optional_int(metadata.get("regeneration_attempt") or metadata.get("review_attempt")), + "model_route": image.model_route, + "created_at": image.created_at, + } + + +def _stage_input_for_node(workflow_metadata: dict[str, Any], node_id: str) -> dict[str, Any]: + stage_inputs = workflow_metadata.get("stage_inputs") + if not isinstance(stage_inputs, dict): + return {} + value = stage_inputs.get(node_id) + return value if isinstance(value, dict) else {} + + +def _string_list(value: Any) -> list[str]: + if isinstance(value, str): + return [value] if value.strip() else [] + if not isinstance(value, list): + return [] + return [str(item).strip() for item in value if str(item or "").strip()] + + +def _optional_text(value: Any) -> str | None: + if value is None: + return None + text = str(value).strip() + return text or None + + +def _optional_int(value: Any) -> int | None: + if value is None: + return None + try: + return int(value) + except (TypeError, ValueError): + return None diff --git a/frontend/shared/api/client.ts b/frontend/shared/api/client.ts index e86c15d..bfd5c07 100644 --- a/frontend/shared/api/client.ts +++ b/frontend/shared/api/client.ts @@ -23,6 +23,10 @@ export async function postJson(endpoint: string, body?: unknown) { return request(endpoint, { method: "POST", body }) } +export async function putJson(endpoint: string, body?: unknown) { + return request(endpoint, { method: "PUT", body }) +} + export function getApiTransport(): ApiTransport { const bridge = typeof window === "undefined" ? undefined : window.AstrBotPluginPage return bridge && (typeof bridge.apiGet === "function" || typeof bridge.apiPost === "function") @@ -95,6 +99,12 @@ async function requestViaPluginBridge( if (method === "POST" && typeof bridge.apiPost === "function") { return bridge.apiPost>(endpoint, options.body) } + if (method === "PUT" && typeof bridge.apiPut === "function") { + return bridge.apiPut>(endpoint, options.body) + } + if (method === "DELETE" && typeof bridge.apiDelete === "function") { + return bridge.apiDelete>(endpoint) + } return undefined } diff --git a/frontend/standalone-web/src/App.vue b/frontend/standalone-web/src/App.vue index c2935ee..49e859f 100644 --- a/frontend/standalone-web/src/App.vue +++ b/frontend/standalone-web/src/App.vue @@ -1,14 +1,20 @@ - + +