astrbot_plugin_imagestudio/backend/services/profile_document_import_service.py

148 行
7.1 KiB
Python

"""Turn character documents into editable profile questionnaire drafts."""
from __future__ import annotations
import json
import re
from typing import Any
from backend.dto.common import ApiError, ErrorCode
from backend.services.model_provider_runtime import ModelProviderRuntime
from backend.services.model_provider_service import ModelProviderService
from core.astrbot_provider_bridge import astrbot_structured_text_callable
from database.db_helper import Database, load_config_payload, load_database_settings
from database.repositories.model_provider_repository import ModelProviderRepository
MAX_DOCUMENT_CHARS = 120_000
ALLOWED_EXTENSIONS = {".md", ".markdown", ".txt"}
QUESTIONNAIRE_FIELDS = (
"role_core", "gender_presentation", "age_stage", "species", "body_type",
"distinctive_features", "personality", "appearance", "outfit", "scene", "avoid",
"worldview", "background", "height_build", "body_features", "likes", "dislikes",
"habits", "relationships", "story_hooks", "generation_style", "shot_preference", "mood", "extra_notes",
)
class ProfileDocumentImportService:
def import_document(self, payload: dict[str, Any]) -> dict[str, Any]:
filename = str(payload.get("filename") or "character.txt").strip()
content = str(payload.get("content") or "")
self._validate_document(filename, content)
seed = payload.get("questionnaire") if isinstance(payload.get("questionnaire"), dict) else {}
route = self._resolve_route(payload.get("model_route"))
callable_ = self._resolve_callable(route)
if callable_ is None:
return self._fallback(content, filename, seed, route)
try:
raw = callable_(self._model_request(filename, content, seed))
result = self._normalize_result(raw, seed)
except Exception as exc:
raise ApiError(
ErrorCode.GENERATION_FAILED,
"角色文档识别失败,请检查文本模型配置后重试",
{"reason": str(exc)[:500], "model_route": route or None},
) from exc
return {**result, "filename": filename, "model_route": route or None, "llm_used": True}
def _validate_document(self, filename: str, content: str) -> None:
normalized = filename.lower().split("?")[0]
if not any(normalized.endswith(extension) for extension in ALLOWED_EXTENSIONS):
raise ApiError(ErrorCode.UNSUPPORTED_FILE_TYPE, "角色文档仅支持 Markdown 或 TXT 文件", {"filename": filename})
if not content.strip():
raise ApiError(ErrorCode.VALIDATION_FAILED, "角色文档内容不能为空", {"field": "content"})
if len(content) > MAX_DOCUMENT_CHARS:
raise ApiError(ErrorCode.UPLOAD_TOO_LARGE, "角色文档过大,请控制在 120000 个字符以内", {"max_chars": MAX_DOCUMENT_CHARS})
def _resolve_route(self, requested: Any) -> str:
if str(requested or "").strip():
return str(requested).strip()
config = load_config_payload()
assignments = config.get("model_assignments") if isinstance(config.get("model_assignments"), dict) else {}
for key in ("profile_import", "prompt_planning", "cross_review_primary"):
route = str(assignments.get(key) or "").strip()
if route:
return route
return ""
def _resolve_callable(self, route: str):
if not route:
return None
database = Database(load_database_settings())
runtime = ModelProviderRuntime(ModelProviderService(ModelProviderRepository(database)))
return runtime.structured_text_callable(route) or astrbot_structured_text_callable(route)
def _model_request(self, filename: str, content: str, seed: dict[str, Any]) -> dict[str, Any]:
return {
"purpose": "extract an editable character persona questionnaire from a document",
"filename": filename,
"document": content,
"existing_questionnaire": seed,
"rules": [
"Return one JSON object only.",
"Only extract facts supported by the document; do not invent details.",
"The result is an editable draft, not a sealed profile card.",
"Use empty strings for unknown fields.",
],
"questionnaire_schema": {field: "string; leave empty when unknown" for field in QUESTIONNAIRE_FIELDS},
}
def _normalize_result(self, raw: Any, seed: dict[str, Any]) -> dict[str, Any]:
parsed = _json_object(raw)
source = parsed.get("questionnaire") if isinstance(parsed.get("questionnaire"), dict) else parsed
questionnaire: dict[str, str] = {}
recognized: list[str] = []
for field in QUESTIONNAIRE_FIELDS:
value = _text(source.get(field))
if value:
questionnaire[field] = value[:8000]
recognized.append(field)
elif field in seed:
questionnaire[field] = _text(seed.get(field))[:8000]
return {
"questionnaire": questionnaire,
"summary": _text(parsed.get("summary") or parsed.get("description"))[:4000],
"recognized_fields": recognized,
"unresolved_fields": [field for field in QUESTIONNAIRE_FIELDS if field not in recognized],
"warnings": _string_list(parsed.get("warnings")),
}
def _fallback(self, content: str, filename: str, seed: dict[str, Any], route: str) -> dict[str, Any]:
questionnaire = {field: _text(seed.get(field))[:8000] for field in QUESTIONNAIRE_FIELDS if _text(seed.get(field))}
questionnaire.setdefault("role_core", content.strip()[:1000])
questionnaire.setdefault("extra_notes", content.strip()[:8000])
return {
"filename": filename,
"model_route": route or None,
"llm_used": False,
"questionnaire": questionnaire,
"summary": "未配置可用文本模型,已保留原文供你手动整理。",
"recognized_fields": [field for field in ("role_core", "extra_notes") if field in questionnaire],
"unresolved_fields": [field for field in QUESTIONNAIRE_FIELDS if field not in questionnaire],
"warnings": ["未找到可用文本模型,未执行 LLM 识别。"],
}
def _json_object(raw: Any) -> dict[str, Any]:
if isinstance(raw, dict):
return raw
text = re.sub(r"^```(?:json)?\s*|\s*```$", "", str(raw or "").strip(), flags=re.IGNORECASE).strip()
start, end = text.find("{"), text.rfind("}")
if start >= 0 and end > start:
text = text[start : end + 1]
try:
value = json.loads(re.sub(r",\s*([}\]])", r"\1", text))
except json.JSONDecodeError as exc:
raise ValueError("structured response was not valid JSON") from exc
if not isinstance(value, dict):
raise ValueError("structured response must be an object")
return value
def _text(value: Any) -> str:
return str(value or "").strip()
def _string_list(value: Any) -> list[str]:
values = value.split(",") if isinstance(value, str) else value if isinstance(value, list) else []
return [_text(item)[:500] for item in values if _text(item)]