2026-07-04 23:15:37 +08:00
|
|
|
"""Character profile service."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
|
|
|
from backend.dto.profile_dto import CharacterProfile, ProfileListResult
|
|
|
|
|
from core.audit_service import AuditService
|
|
|
|
|
from core.identity import RequestContext
|
|
|
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProfileService:
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
repository: ProfileRepository,
|
|
|
|
|
audit_service: AuditService,
|
|
|
|
|
):
|
|
|
|
|
self.repository = repository
|
|
|
|
|
self.audit_service = audit_service
|
|
|
|
|
|
|
|
|
|
def create_profile(self, payload: dict[str, Any], ctx: RequestContext) -> CharacterProfile:
|
|
|
|
|
self._validate_create_payload(payload)
|
2026-07-11 21:50:34 +08:00
|
|
|
# Ownership comes from the authenticated caller. Accepting an
|
|
|
|
|
# arbitrary owner_user_id would allow cross-tenant profile creation.
|
|
|
|
|
owner_user_id = ctx.actor_id
|
2026-07-04 23:15:37 +08:00
|
|
|
if not owner_user_id:
|
|
|
|
|
raise ApiError(
|
|
|
|
|
ErrorCode.VALIDATION_FAILED,
|
|
|
|
|
"缺少角色档案归属用户",
|
|
|
|
|
{"field": "owner_user_id"},
|
|
|
|
|
)
|
|
|
|
|
profile_payload = {
|
|
|
|
|
"profile_id": payload.get("profile_id") or new_profile_id(),
|
|
|
|
|
"name": payload["name"].strip(),
|
|
|
|
|
"description": payload.get("description"),
|
|
|
|
|
"base_prompt": payload.get("base_prompt"),
|
|
|
|
|
"appearance_prompt": payload.get("appearance_prompt"),
|
|
|
|
|
"style_prompt": payload.get("style_prompt"),
|
|
|
|
|
"negative_prompt": payload.get("negative_prompt"),
|
|
|
|
|
"default_model_route": payload.get("default_model_route"),
|
|
|
|
|
"owner_platform": payload.get("owner_platform") or ctx.platform,
|
|
|
|
|
"owner_context_id": payload.get("owner_context_id"),
|
|
|
|
|
"owner_user_id": owner_user_id,
|
|
|
|
|
"owner_session_key": payload.get("owner_session_key") or ctx.session_key,
|
|
|
|
|
"avatar_image_uri": payload.get("avatar_image_uri"),
|
|
|
|
|
"status": payload.get("status", "active"),
|
|
|
|
|
"metadata": payload.get("metadata") or {},
|
|
|
|
|
}
|
|
|
|
|
profile = self.repository.create(profile_payload)
|
|
|
|
|
self.audit_service.record(
|
|
|
|
|
ctx=ctx,
|
|
|
|
|
action="create_profile",
|
|
|
|
|
target_type="character_profile",
|
|
|
|
|
target_id=profile.profile_id,
|
|
|
|
|
after_snapshot=profile.to_dict(),
|
|
|
|
|
)
|
|
|
|
|
return profile
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
def get_profile(self, profile_id: str, ctx: RequestContext | None = None) -> CharacterProfile:
|
2026-07-04 23:15:37 +08:00
|
|
|
profile = self.repository.get(profile_id)
|
|
|
|
|
if profile is None or profile.status == "deleted":
|
|
|
|
|
raise ApiError(
|
|
|
|
|
ErrorCode.RESOURCE_NOT_FOUND,
|
|
|
|
|
"角色档案不存在",
|
|
|
|
|
{"profile_id": profile_id},
|
|
|
|
|
)
|
2026-07-11 21:50:34 +08:00
|
|
|
if ctx is not None:
|
|
|
|
|
self._require_owner(profile, ctx)
|
2026-07-04 23:15:37 +08:00
|
|
|
return profile
|
|
|
|
|
|
|
|
|
|
def list_profiles(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
keyword: str | None = None,
|
|
|
|
|
status: str | None = "active",
|
|
|
|
|
page: int = 1,
|
|
|
|
|
page_size: int = 30,
|
|
|
|
|
) -> ProfileListResult:
|
|
|
|
|
return self.repository.list(
|
|
|
|
|
owner_user_id=ctx.actor_id,
|
|
|
|
|
keyword=keyword,
|
|
|
|
|
status=status,
|
|
|
|
|
page=max(page, 1),
|
|
|
|
|
page_size=min(max(page_size, 1), 100),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def update_profile(
|
|
|
|
|
self,
|
|
|
|
|
profile_id: str,
|
|
|
|
|
payload: dict[str, Any],
|
|
|
|
|
ctx: RequestContext,
|
|
|
|
|
) -> CharacterProfile:
|
2026-07-11 21:50:34 +08:00
|
|
|
before = self.get_profile(profile_id, ctx)
|
2026-07-04 23:15:37 +08:00
|
|
|
if "name" in payload and not str(payload["name"]).strip():
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "角色名称不能为空", {"field": "name"})
|
|
|
|
|
update_payload = dict(payload)
|
|
|
|
|
if "name" in update_payload:
|
|
|
|
|
update_payload["name"] = str(update_payload["name"]).strip()
|
|
|
|
|
after = self.repository.update(profile_id, update_payload)
|
|
|
|
|
if after is None:
|
|
|
|
|
raise ApiError(
|
|
|
|
|
ErrorCode.RESOURCE_NOT_FOUND,
|
|
|
|
|
"角色档案不存在",
|
|
|
|
|
{"profile_id": profile_id},
|
|
|
|
|
)
|
|
|
|
|
self.audit_service.record(
|
|
|
|
|
ctx=ctx,
|
|
|
|
|
action="update_profile",
|
|
|
|
|
target_type="character_profile",
|
|
|
|
|
target_id=profile_id,
|
|
|
|
|
before_snapshot=before.to_dict(),
|
|
|
|
|
after_snapshot=after.to_dict(),
|
|
|
|
|
)
|
|
|
|
|
return after
|
|
|
|
|
|
|
|
|
|
def delete_profile(self, profile_id: str, ctx: RequestContext) -> CharacterProfile:
|
2026-07-11 21:50:34 +08:00
|
|
|
before = self.get_profile(profile_id, ctx)
|
2026-07-04 23:15:37 +08:00
|
|
|
after = self.repository.soft_delete(profile_id)
|
|
|
|
|
if after is None:
|
|
|
|
|
raise ApiError(
|
|
|
|
|
ErrorCode.RESOURCE_NOT_FOUND,
|
|
|
|
|
"角色档案不存在",
|
|
|
|
|
{"profile_id": profile_id},
|
|
|
|
|
)
|
|
|
|
|
self.audit_service.record(
|
|
|
|
|
ctx=ctx,
|
|
|
|
|
action="delete_profile",
|
|
|
|
|
target_type="character_profile",
|
|
|
|
|
target_id=profile_id,
|
|
|
|
|
before_snapshot=before.to_dict(),
|
|
|
|
|
after_snapshot=after.to_dict(),
|
|
|
|
|
)
|
|
|
|
|
return after
|
|
|
|
|
|
|
|
|
|
def _validate_create_payload(self, payload: dict[str, Any]) -> None:
|
|
|
|
|
name = str(payload.get("name") or "").strip()
|
|
|
|
|
if not name:
|
|
|
|
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "角色名称不能为空", {"field": "name"})
|
|
|
|
|
|
2026-07-11 21:50:34 +08:00
|
|
|
def _require_owner(self, profile: CharacterProfile, ctx: RequestContext) -> None:
|
|
|
|
|
if ctx.actor_id and ctx.actor_id == profile.owner_user_id:
|
|
|
|
|
return
|
|
|
|
|
# Use not-found semantics so guessed resource IDs cannot enumerate
|
|
|
|
|
# another user's profiles.
|
|
|
|
|
raise ApiError(
|
|
|
|
|
ErrorCode.RESOURCE_NOT_FOUND,
|
|
|
|
|
"角色档案不存在",
|
|
|
|
|
{"profile_id": profile.profile_id},
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-04 23:15:37 +08:00
|
|
|
|
|
|
|
|
def new_profile_id() -> str:
|
|
|
|
|
return f"profile_{uuid4().hex}"
|