"""Chat-facing orchestration built entirely on the existing ImageStudio API contract.""" from __future__ import annotations import hashlib import re from pathlib import Path from typing import Any, Callable from adapters.astrbot_command_adapter import ChatCommandContext from backend.dto.chat_command_dto import SingleImageCommand from backend.services.chat_media_import_service import ChatMediaImportService from backend.services.chat_command_guard_service import ChatCommandGuardService from backend.services.cross_review_service import CrossReviewService from core.identity import RequestContext from core.chat_access_policy import chat_action_allowed from database.db_helper import load_config_payload Dispatcher = Callable[..., dict[str, Any]] class ChatWorkflowService: def __init__(self, dispatcher: Dispatcher, config_loader: Callable[[], dict[str, Any]] = load_config_payload, media_importer: ChatMediaImportService | None = None, guard: ChatCommandGuardService | None = None, cross_reviewer_factory: Callable[[], CrossReviewService] = CrossReviewService): self.dispatcher = dispatcher self.config_loader = config_loader self.media_importer = media_importer or ChatMediaImportService() self.guard = guard or ChatCommandGuardService() self.cross_reviewer_factory = cross_reviewer_factory def submit_single_image(self, command: SingleImageCommand, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat, "single_image") config = self.config_loader() active = self._data(self.dispatcher(ctx, "GET", "jobs", query={"status": "running", "page": 1, "page_size": 100})) active_items = active.get("items", []) if isinstance(active, dict) else [] self.guard.check_generation(config, chat, size=command.size, active_jobs=active_items) profile = self._resolve_profile(ctx, command.profile_ref, chat.user_id) workflow = self._data(self.dispatcher( ctx, "POST", f"profiles/{profile['profile_id']}/workflows", payload={ "workflow_type": "single_image", "display_name": f"聊天单图 · {command.prompt[:24]}", "mode": "quick", "metadata": {"source": "astrbot_chat", "message_origin": chat.message_origin}, }, )) workflow_data = workflow["workflow"] node = workflow["nodes"][0] self._data(self.dispatcher(ctx, "POST", f"workflows/{workflow_data['workflow_id']}/start", payload={})) idempotency_source = chat.message_id or f"{chat.message_origin}:{chat.user_id}:{command.prompt}" generation = self._data(self.dispatcher( ctx, "POST", f"nodes/{node['node_id']}/generate", payload={ "prompt": command.prompt, "negative_prompt": command.negative_prompt, "model_route": command.model_route, "size": command.size, "count": command.count, "idempotency_key": "chat_" + hashlib.sha256(idempotency_source.encode("utf-8")).hexdigest()[:32], "metadata": { "source": "astrbot_chat", "platform": chat.platform, "message_origin": chat.message_origin, "user_id": chat.user_id, "group_id": chat.group_id, }, }, )) images = [ self._data(self.dispatcher(ctx, "GET", f"images/{item['image_id']}", query={})) for item in generation.get("images", []) if item.get("image_id") ] self.guard.record(chat, "single_image", target_id=generation.get("task_id"), details={"size": command.size, "count": command.count}) return { "profile": profile, "workflow": workflow_data, "node": node, "generation": generation, "images": images, } def list_node_images(self, node_id: str, chat: ChatCommandContext) -> list[dict[str, Any]]: return self._data( self.dispatcher( self._request_context(chat), "GET", f"nodes/{node_id}/images", query={"include_archived": False}, ) ) def list_jobs(self, chat: ChatCommandContext, *, task_id: str | None = None) -> Any: ctx = self._request_context(chat, "single_image") endpoint = f"jobs/{task_id}" if task_id else "jobs" return self._data(self.dispatcher(ctx, "GET", endpoint, query={"page": 1, "page_size": 10})) def cancel_job(self, chat: ChatCommandContext, task_id: str) -> Any: return self._data(self.dispatcher(self._request_context(chat, "single_image"), "POST", f"jobs/{task_id}/cancel", payload={})) def retry_job(self, chat: ChatCommandContext, task_id: str) -> Any: return self._data(self.dispatcher(self._request_context(chat, "single_image"), "POST", f"jobs/{task_id}/retry", payload={})) def create_quick_profile_draft(self, description: str, chat: ChatCommandContext) -> dict[str, Any]: text = str(description or "").strip() if not text: raise ValueError("请提供角色名称和描述,例如:/yp 快速角色 米卡 银发蓝眼的冷静少女") questionnaire = _quick_questionnaire(text) return self._data(self.dispatcher(self._request_context(chat), "POST", "profiles/quick-draft", payload={ "questionnaire": questionnaire, })) def save_quick_profile_draft(self, draft: dict[str, Any], chat: ChatCommandContext) -> dict[str, Any]: return self._data(self.dispatcher(self._request_context(chat), "POST", "profiles/quick-draft/save", payload={ **draft, "draft": draft, "create_workflow": True, "workflow_status": "draft", "metadata": {"source": "astrbot_chat", "message_origin": chat.message_origin}, })) def create_or_resume_character_workflow(self, profile_ref: str, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat, "single_image") profile = self._resolve_required_profile(ctx, profile_ref) workflows = self._data(self.dispatcher(ctx, "GET", f"profiles/{profile['profile_id']}/workflows", query={})) active = next((item for item in workflows if item.get("workflow_type") == "character_gradual" and item.get("status") not in {"completed", "cancelled", "archived"}), None) if active: detail = self._data(self.dispatcher(ctx, "GET", f"workflows/{active['workflow_id']}", query={})) return {"profile": profile, **detail, "resumed": True} detail = self._data(self.dispatcher(ctx, "POST", f"profiles/{profile['profile_id']}/workflows", payload={ "workflow_type": "character_gradual", "display_name": f"{profile['name']} 详细角色工作流", "mode": "gradual", "metadata": {"source": "astrbot_chat", "message_origin": chat.message_origin}, })) return {"profile": profile, **detail, "resumed": False} def workflow_detail(self, workflow_id: str, chat: ChatCommandContext) -> dict[str, Any]: return self._data(self.dispatcher(self._request_context(chat), "GET", f"workflows/{workflow_id}", query={})) def generate_node(self, node_id: str, chat: ChatCommandContext, *, prompt: str = "") -> dict[str, Any]: payload = {"prompt": prompt} if prompt else {} ctx = self._request_context(chat) result = self._data(self.dispatcher(ctx, "POST", f"nodes/{node_id}/generate", payload=payload)) result["images"] = [ self._data(self.dispatcher(ctx, "GET", f"images/{item['image_id']}", query={})) for item in result.get("images", []) if item.get("image_id") ] return result def retry_node(self, node_id: str, chat: ChatCommandContext) -> dict[str, Any]: return self._data(self.dispatcher(self._request_context(chat), "POST", f"nodes/{node_id}/retry", payload={})) def complete_node(self, node_id: str, chat: ChatCommandContext) -> dict[str, Any]: return self._data(self.dispatcher(self._request_context(chat), "POST", f"nodes/{node_id}/complete", payload={})) def next_workflow_step(self, workflow_id: str, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat) detail = self._data(self.dispatcher(ctx, "GET", f"workflows/{workflow_id}", query={})) workflow = detail["workflow"] nodes = detail.get("nodes", []) current = next((item for item in nodes if item.get("node_id") == workflow.get("current_node_id")), None) candidate = current if current and current.get("status") not in {"completed", "skipped"} else next((item for item in nodes if item.get("status") in {"pending", "draft", "failed"}), None) if candidate is None: return {"completed": True, "workflow": workflow} if candidate.get("node_id") != workflow.get("current_node_id"): self._data(self.dispatcher(ctx, "PUT", f"workflows/{workflow_id}", payload={"current_node_id": candidate["node_id"]})) generation = self._data(self.dispatcher(ctx, "POST", f"nodes/{candidate['node_id']}/generate", payload={})) return {"completed": False, "workflow": workflow, "node": candidate, "generation": generation} def select_node_image(self, node_id: str, image_id: str, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat) selected = self._data(self.dispatcher(ctx, "POST", f"nodes/{node_id}/select-image", payload={"image_id": image_id})) image = self._data(self.dispatcher(ctx, "GET", f"images/{image_id}", query={})) if image.get("profile_id") and image.get("image_uri"): self._data(self.dispatcher(ctx, "PUT", f"profiles/{image['profile_id']}", payload={"avatar_image_uri": image["image_uri"]})) return selected def select_node_image_by_number(self, node_id: str, number: int, chat: ChatCommandContext) -> dict[str, Any]: images = self.list_node_images(node_id, chat) if number < 1 or number > len(images): raise ValueError(f"图片序号必须在 1 到 {len(images)} 之间") return self.select_node_image(node_id, images[number - 1]["image_id"], chat) def review(self, target_id: str, chat: ChatCommandContext) -> dict[str, Any]: prefix = "workflows" if target_id.startswith(("wf_", "workflow_")) else "nodes" ctx = self._request_context(chat) rules = self._data(self.dispatcher(ctx, "GET", f"{prefix}/{target_id}/review", query={})) node_ids = [target_id] if prefix == "workflows": detail = self._data(self.dispatcher(ctx, "GET", f"workflows/{target_id}", query={})) node_ids = [item["node_id"] for item in detail.get("nodes", []) if item.get("status") == "completed"] model_reviews = {node_id: self.cross_reviewer_factory().review_node(node_id) for node_id in node_ids} return {"rules": rules, "model_reviews": model_reviews} def hires_fix(self, image_id: str, size: str, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat) image = self._data(self.dispatcher(ctx, "GET", f"images/{image_id}", query={})) workflow = self._data(self.dispatcher( ctx, "POST", f"profiles/{image['profile_id']}/workflows", payload={ "workflow_type": "single_image", "display_name": f"高清修复 · {image_id}", "mode": "quick", "metadata": {"source": "astrbot_chat_hires", "source_image_id": image_id}, }, )) workflow_data = workflow["workflow"] node = workflow["nodes"][0] self._data(self.dispatcher(ctx, "POST", f"workflows/{workflow_data['workflow_id']}/start", payload={})) config = load_config_payload() assignments = config.get("model_assignments") if isinstance(config.get("model_assignments"), dict) else {} generation = self._data(self.dispatcher(ctx, "POST", f"nodes/{node['node_id']}/generate", payload={ "prompt": "high resolution restoration, preserve identity, composition, clothing and original style", "negative_prompt": "identity change, composition change, clothing change, oversharpening, artifacts", "model_route": assignments.get("hires_fix"), "size": size, "count": 1, "input_images": [{"image_uri": image["image_uri"]}], "metadata": {"source": "astrbot_chat_hires", "source_image_id": image_id}, })) images = [self._data(self.dispatcher(ctx, "GET", f"images/{item['image_id']}", query={})) for item in generation.get("images", [])] return {"source_image": image, "workflow": workflow_data, "node": node, "generation": generation, "images": images} def import_material(self, image_reference: str, category_ref: str, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat) categories = self._data(self.dispatcher(ctx, "GET", "material-categories", query={})) category = next((item for item in categories if item.get("category_id") == category_ref or item.get("name") == category_ref), None) if category is None: raise ValueError(f"未找到素材分类:{category_ref}") uri, filename, size_bytes = self.media_importer.import_image(image_reference) material = self._data(self.dispatcher(ctx, "POST", "materials/upload", payload={ "category_id": category["category_id"], "display_name": Path(filename).stem, "image_uri": uri, "thumbnail_uri": uri, "filename": filename, "size_bytes": size_bytes, "metadata": {"source": "astrbot_chat", "message_origin": chat.message_origin}, })) recognized = self._data(self.dispatcher(ctx, "POST", f"materials/{material['material_id']}/recognize", payload={})) profiles = self._data(self.dispatcher(ctx, "GET", "profiles", query={"page": 1, "page_size": 20})) return {"category": category, "material": recognized, "profiles": profiles.get("items", []) if isinstance(profiles, dict) else []} def confirm_material(self, material_id: str, chat: ChatCommandContext) -> dict[str, Any]: return self._data(self.dispatcher(self._request_context(chat), "POST", f"materials/{material_id}/confirm", payload={})) def re_recognize_material(self, material_id: str, chat: ChatCommandContext) -> dict[str, Any]: return self._data(self.dispatcher(self._request_context(chat), "POST", f"materials/{material_id}/re-recognize", payload={})) def add_material_to_wardrobe(self, material_id: str, profile_ref: str, wardrobe_category: str, chat: ChatCommandContext) -> dict[str, Any]: ctx = self._request_context(chat) profile = self._resolve_required_profile(ctx, profile_ref) return self._data(self.dispatcher(ctx, "POST", f"profiles/{profile['profile_id']}/wardrobe/import-material", payload={ "material_id": material_id, "wardrobe_category": wardrobe_category, })) def _resolve_profile(self, ctx: RequestContext, profile_ref: str | None, user_id: str) -> dict[str, Any]: keyword = profile_ref or f"聊天单图 {user_id}" result = self._data(self.dispatcher(ctx, "GET", "profiles", query={"keyword": keyword, "page": 1, "page_size": 30})) items = result.get("items", []) if isinstance(result, dict) else [] if profile_ref: exact = next((item for item in items if item.get("profile_id") == profile_ref or item.get("name") == profile_ref), None) if exact: return exact raise ValueError(f"未找到角色:{profile_ref}") if items: return items[0] return self._data(self.dispatcher(ctx, "POST", "profiles", payload={ "name": f"聊天单图 {user_id}", "profile_type": "quick", "description": "AstrBot 聊天单图专用角色容器", "metadata": {"source": "astrbot_chat", "system_container": True}, })) def _resolve_required_profile(self, ctx: RequestContext, profile_ref: str) -> dict[str, Any]: text = str(profile_ref or "").strip() if not text: raise ValueError("请提供角色名称或角色 ID") result = self._data(self.dispatcher(ctx, "GET", "profiles", query={"keyword": text, "page": 1, "page_size": 30})) items = result.get("items", []) if isinstance(result, dict) else [] profile = next((item for item in items if item.get("profile_id") == text or item.get("name") == text), None) if profile is None: raise ValueError(f"未找到角色:{text}") return profile def _request_context(self, chat: ChatCommandContext, action: str = "character_workflow") -> RequestContext: config = self.config_loader() if not chat_action_allowed(config, action, group_id=chat.group_id, user_id=chat.user_id): try: self.guard.record(chat, action, allowed=False, details={"reason": "allowlist"}) except Exception: pass raise ValueError("当前群聊或用户不满足聊天白名单规则") return RequestContext.from_astrbot( username=chat.user_id or "chat-user", user_id=chat.user_id or "chat-user", permissions={"can_write", "can_retry_tasks", "can_manage_materials"}, source=f"astrbot_chat:{chat.platform}", ) @staticmethod def _data(response: dict[str, Any]) -> Any: if not response.get("ok"): raise ValueError(str(response.get("message") or response.get("error_code") or "ImageStudio 请求失败")) return response.get("data") def _quick_questionnaire(text: str) -> dict[str, Any]: parts = text.split(maxsplit=1) if len(parts) < 2: raise ValueError("请同时提供角色名和角色描述,例如:/yp 快速角色 米卡 银发蓝眼的冷静少女") name, description = parts[0].strip(), parts[1].strip() colors = re.findall(r"([黑白红橙黄绿青蓝紫粉灰棕金银]{1,2})色?(?:头发|发|眼睛|眼)", description) hair = next((value for value in colors if f"{value}发" in description or f"{value}色头发" in description), "") eyes = next((value for value in colors if f"{value}眼" in description or f"{value}色眼睛" in description), "") personality_keywords = [item for item in ("冷静", "温柔", "活泼", "开朗", "内向", "傲娇", "成熟", "勇敢", "神秘") if item in description] clothing_keywords = [item for item in ("制服", "礼服", "外套", "裙", "衬衫", "盔甲", "斗篷", "和服", "运动服") if item in description] hairstyle = next((item for item in ("长发", "短发", "双马尾", "马尾", "卷发", "直发", "齐刘海") if item in description), "") return { "name": name, "identity": description, "personality": personality_keywords, "hair_color": hair, "hairstyle": hairstyle, "eye_color": eyes, "clothing_keywords": clothing_keywords, }