astrbot_plugin_imagestudio/backend/services/wardrobe_service.py

141 行
5.7 KiB
Python

2026-07-05 09:59:26 +08:00
"""Wardrobe service and matching."""
from __future__ import annotations
from typing import Any
from uuid import uuid4
from backend.dto.common import ApiError, ErrorCode
from backend.dto.wardrobe_dto import WardrobeItem
from core.audit_service import AuditService
from core.identity import RequestContext
from database.repositories.material_repository import MaterialRepository
from database.repositories.profile_repository import ProfileRepository
from database.repositories.wardrobe_repository import WardrobeRepository
class WardrobeService:
def __init__(
self,
wardrobe_repository: WardrobeRepository,
profile_repository: ProfileRepository,
material_repository: MaterialRepository,
audit_service: AuditService,
):
self.wardrobe_repository = wardrobe_repository
self.profile_repository = profile_repository
self.material_repository = material_repository
self.audit_service = audit_service
def import_material(self, profile_id: str, payload: dict[str, Any], ctx: RequestContext) -> WardrobeItem:
self._ensure_profile(profile_id)
material_id = str(payload.get("material_id") or "").strip()
wardrobe_category = str(payload.get("wardrobe_category") or "").strip()
if not material_id:
raise ApiError(ErrorCode.VALIDATION_FAILED, "material_id is required", {"field": "material_id"})
if not wardrobe_category:
raise ApiError(
ErrorCode.VALIDATION_FAILED,
"wardrobe_category is required",
{"field": "wardrobe_category"},
)
material = self.material_repository.get_material(material_id)
if material is None or material.recognition_status != "confirmed":
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "confirmed material not found", {"material_id": material_id})
item = self.wardrobe_repository.create(
{
"wardrobe_item_id": payload.get("wardrobe_item_id") or new_wardrobe_item_id(),
"profile_id": profile_id,
"material_id": material_id,
"wardrobe_category": wardrobe_category,
"tags": payload.get("tags") or material.tags,
"season": payload.get("season"),
"style": payload.get("style"),
"is_favorite": payload.get("is_favorite", False),
}
)
self.audit_service.record(
ctx=ctx,
action="add_wardrobe_item",
target_type="wardrobe_item",
target_id=item.wardrobe_item_id,
after_snapshot=item.to_dict(),
)
return item
def list_items(self, profile_id: str, wardrobe_category: str | None = None) -> list[WardrobeItem]:
self._ensure_profile(profile_id)
return self.wardrobe_repository.list_by_profile(profile_id, wardrobe_category)
def delete_item(self, profile_id: str, wardrobe_item_id: str, ctx: RequestContext) -> WardrobeItem:
self._ensure_profile(profile_id)
before = self.wardrobe_repository.get(wardrobe_item_id)
if before is None or before.profile_id != profile_id:
raise ApiError(
ErrorCode.RESOURCE_NOT_FOUND,
"wardrobe item not found",
{"wardrobe_item_id": wardrobe_item_id},
)
deleted = self.wardrobe_repository.delete(wardrobe_item_id)
assert deleted is not None
self.audit_service.record(
ctx=ctx,
action="delete_wardrobe_item",
target_type="wardrobe_item",
target_id=wardrobe_item_id,
before_snapshot=deleted.to_dict(),
)
return deleted
def match(self, profile_id: str, payload: dict[str, Any], ctx: RequestContext) -> dict[str, Any]:
self._ensure_profile(profile_id)
categories = payload.get("categories") or []
if not categories:
categories = ["top", "bottom", "dress", "shoes", "accessory"]
selected: list[dict[str, Any]] = []
prompt_parts: list[str] = []
for category in categories:
candidates = self.wardrobe_repository.list_by_profile(profile_id, str(category))
if not candidates:
continue
item = candidates[0]
material = self.material_repository.get_material(item.material_id)
if material is None:
continue
self.wardrobe_repository.mark_used(item.wardrobe_item_id)
selected.append(
{
"wardrobe_item_id": item.wardrobe_item_id,
"material_id": material.material_id,
"wardrobe_category": item.wardrobe_category,
"display_name": material.display_name,
"prompt": material.prompt,
"tags": item.tags,
}
)
if material.prompt:
prompt_parts.append(material.prompt)
result = {
"profile_id": profile_id,
"selected_materials": selected,
"outfit_prompt": ", ".join(prompt_parts),
"fallback_used": not selected,
}
self.audit_service.record(
ctx=ctx,
action="wardrobe_match",
target_type="character_profile",
target_id=profile_id,
after_snapshot=result,
)
return result
def _ensure_profile(self, profile_id: str) -> None:
profile = self.profile_repository.get(profile_id)
if profile is None or profile.status == "deleted":
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "profile not found", {"profile_id": profile_id})
def new_wardrobe_item_id() -> str:
return f"wardrobe_{uuid4().hex}"