astrbot_plugin_imagestudio/backend/services/quick_profile_service.py

76 行
2.5 KiB
Python

2026-07-05 00:20:59 +08:00
"""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,
2026-07-05 13:33:49 +08:00
"\u7f3a\u5c11\u5feb\u901f\u4eba\u8bbe\u95ee\u5377",
2026-07-05 00:20:59 +08:00
{"field": "questionnaire"},
)
name = str(questionnaire.get("name") or "").strip()
if not name:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
2026-07-05 13:33:49 +08:00
"\u89d2\u8272\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a",
2026-07-05 00:20:59 +08:00
{"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)