"""Quick profile draft service. P0 uses a deterministic draft builder. A later text-model adapter can replace the prompt expansion internals without changing the API contract. """ from __future__ import annotations from typing import Any from uuid import uuid4 from backend.dto.common import ApiError, ErrorCode class QuickProfileService: def create_draft(self, payload: dict[str, Any]) -> dict[str, Any]: questionnaire = payload.get("questionnaire") if not isinstance(questionnaire, dict): raise ApiError( ErrorCode.VALIDATION_FAILED, "缺少快速人设问卷", {"field": "questionnaire"}, ) name = str(questionnaire.get("name") or "").strip() if not name: raise ApiError( ErrorCode.VALIDATION_FAILED, "角色名称不能为空", {"field": "questionnaire.name"}, ) identity = _text(questionnaire.get("identity")) personality = _join(questionnaire.get("personality")) hair_color = _text(questionnaire.get("hair_color")) hairstyle = _text(questionnaire.get("hairstyle")) eye_color = _text(questionnaire.get("eye_color")) style = _text(questionnaire.get("style")) clothing = _join(questionnaire.get("clothing_keywords")) base_parts = [name] if identity: base_parts.append(identity) if personality: base_parts.append(f"personality: {personality}") appearance_parts = [] if hair_color: appearance_parts.append(f"{hair_color} hair") if hairstyle: appearance_parts.append(hairstyle) if eye_color: appearance_parts.append(f"{eye_color} eyes") if clothing: appearance_parts.append(clothing) return { "draft_id": f"draft_{uuid4().hex}", "profile_preview": { "name": name, "base_prompt": ", ".join(base_parts), "appearance_prompt": ", ".join(appearance_parts), "style_prompt": style, "negative_prompt": "low quality, blurry, bad anatomy, extra fingers", }, } def _text(value: Any) -> str: return str(value or "").strip() def _join(value: Any) -> str: if isinstance(value, list): return ", ".join(str(item).strip() for item in value if str(item).strip()) return _text(value)