85 行
3.1 KiB
Python
85 行
3.1 KiB
Python
"""External plugin integration service."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
from backend.services.wardrobe_service import WardrobeService
|
|
from core.identity import RequestContext
|
|
from database.repositories.integration_repository import IntegrationRepository
|
|
|
|
|
|
PRIVATE_COMPANION = "astrbot_plugin_private_companion"
|
|
|
|
|
|
class IntegrationService:
|
|
def __init__(
|
|
self,
|
|
wardrobe_service: WardrobeService,
|
|
integration_repository: IntegrationRepository,
|
|
):
|
|
self.wardrobe_service = wardrobe_service
|
|
self.integration_repository = integration_repository
|
|
|
|
def daily_outfit(self, payload: dict[str, Any], ctx: RequestContext) -> dict[str, Any]:
|
|
source = str(payload.get("request_source") or PRIVATE_COMPANION)
|
|
if source != PRIVATE_COMPANION:
|
|
raise ApiError(
|
|
ErrorCode.INTEGRATION_TOKEN_INVALID,
|
|
"unsupported integration source",
|
|
{"request_source": source},
|
|
)
|
|
profile_id = str(payload.get("profile_id") or "").strip()
|
|
if not profile_id:
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "profile_id is required", {"field": "profile_id"})
|
|
categories = payload.get("categories") or _categories_from_keywords(payload.get("keywords") or [])
|
|
match = self.wardrobe_service.match(profile_id, {"categories": categories}, ctx)
|
|
fallback_prompt = _fallback_prompt(payload)
|
|
final_prompt = match["outfit_prompt"] or fallback_prompt
|
|
response = {
|
|
"matched": bool(match["selected_materials"]),
|
|
"selected_materials": match["selected_materials"],
|
|
"final_prompt": final_prompt,
|
|
"job_id": None,
|
|
"fallback_used": not bool(match["selected_materials"]),
|
|
}
|
|
self.integration_repository.record(
|
|
{
|
|
"integration_log_id": new_integration_log_id(),
|
|
"integration_name": source,
|
|
"action": "daily_outfit",
|
|
"status": "succeeded",
|
|
"request": payload,
|
|
"response": response,
|
|
}
|
|
)
|
|
return response
|
|
|
|
def list_integration_logs(self, integration_name: str | None = None) -> list[dict[str, Any]]:
|
|
return self.integration_repository.list(integration_name=integration_name)
|
|
|
|
|
|
def new_integration_log_id() -> str:
|
|
return f"intlog_{uuid4().hex}"
|
|
|
|
|
|
def _categories_from_keywords(keywords: list[Any]) -> list[str]:
|
|
lowered = {str(keyword).lower() for keyword in keywords}
|
|
categories = []
|
|
for category in ("top", "bottom", "dress", "shoes", "accessory"):
|
|
if category in lowered:
|
|
categories.append(category)
|
|
return categories or ["top", "bottom", "dress", "shoes", "accessory"]
|
|
|
|
|
|
def _fallback_prompt(payload: dict[str, Any]) -> str:
|
|
parts = ["daily outfit"]
|
|
for key in ("mood", "weather"):
|
|
value = payload.get(key)
|
|
if value:
|
|
parts.append(str(value))
|
|
keywords = payload.get("keywords") or []
|
|
parts.extend(str(keyword) for keyword in keywords)
|
|
return ", ".join(parts)
|