S89 快速人设保存后端
这个提交存在于:
父节点
37948ca3bc
当前提交
4b42c20291
共有 5 个文件被更改,包括 146 次插入 和 0 次删除
|
|
@ -29,6 +29,7 @@ ROUTES: tuple[RouteSpec, ...] = (
|
||||||
RouteSpec("GET", "profiles", "backend.routes.profile_routes.list_profiles", "read"),
|
RouteSpec("GET", "profiles", "backend.routes.profile_routes.list_profiles", "read"),
|
||||||
RouteSpec("POST", "profiles", "backend.routes.profile_routes.create_profile", "write"),
|
RouteSpec("POST", "profiles", "backend.routes.profile_routes.create_profile", "write"),
|
||||||
RouteSpec("POST", "profiles/quick-draft", "backend.routes.profile_routes.create_quick_profile_draft", "read"),
|
RouteSpec("POST", "profiles/quick-draft", "backend.routes.profile_routes.create_quick_profile_draft", "read"),
|
||||||
|
RouteSpec("POST", "profiles/quick-draft/save", "backend.routes.profile_routes.save_quick_profile_draft", "write"),
|
||||||
RouteSpec("GET", "profiles/{profile_id}", "backend.routes.profile_routes.get_profile", "read"),
|
RouteSpec("GET", "profiles/{profile_id}", "backend.routes.profile_routes.get_profile", "read"),
|
||||||
RouteSpec("PUT", "profiles/{profile_id}", "backend.routes.profile_routes.update_profile", "write"),
|
RouteSpec("PUT", "profiles/{profile_id}", "backend.routes.profile_routes.update_profile", "write"),
|
||||||
RouteSpec("DELETE", "profiles/{profile_id}", "backend.routes.profile_routes.delete_profile", "write"),
|
RouteSpec("DELETE", "profiles/{profile_id}", "backend.routes.profile_routes.delete_profile", "write"),
|
||||||
|
|
|
||||||
|
|
@ -115,6 +115,22 @@ def create_quick_profile_draft(
|
||||||
return handle_request(ctx, action)
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def save_quick_profile_draft(
|
||||||
|
ctx: RequestContext,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
service: QuickProfileService | None = None,
|
||||||
|
profile_service: ProfileService | None = None,
|
||||||
|
) -> dict:
|
||||||
|
quick_service = service or QuickProfileService()
|
||||||
|
resolved_profile_service = profile_service or _create_profile_service()
|
||||||
|
|
||||||
|
def action() -> dict:
|
||||||
|
require_write(ctx)
|
||||||
|
return quick_service.save_draft(payload, resolved_profile_service, ctx)
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
def _create_profile_service() -> ProfileService:
|
def _create_profile_service() -> ProfileService:
|
||||||
database = Database(load_database_settings())
|
database = Database(load_database_settings())
|
||||||
return ProfileService(
|
return ProfileService(
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ from typing import Any
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from backend.dto.common import ApiError, ErrorCode
|
from backend.dto.common import ApiError, ErrorCode
|
||||||
|
from backend.services.profile_service import ProfileService
|
||||||
|
from core.identity import RequestContext
|
||||||
|
|
||||||
|
|
||||||
class QuickProfileService:
|
class QuickProfileService:
|
||||||
|
|
@ -64,6 +66,48 @@ class QuickProfileService:
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def save_draft(
|
||||||
|
self,
|
||||||
|
payload: dict[str, Any],
|
||||||
|
profile_service: ProfileService,
|
||||||
|
ctx: RequestContext,
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
draft = payload.get("draft") if isinstance(payload.get("draft"), dict) else {}
|
||||||
|
preview = payload.get("profile_preview") if isinstance(payload.get("profile_preview"), dict) else None
|
||||||
|
preview = preview or (draft.get("profile_preview") if isinstance(draft.get("profile_preview"), dict) else None)
|
||||||
|
if not preview:
|
||||||
|
raise ApiError(
|
||||||
|
ErrorCode.VALIDATION_FAILED,
|
||||||
|
"\u7f3a\u5c11\u5feb\u901f\u4eba\u8bbe\u9884\u89c8",
|
||||||
|
{"field": "profile_preview"},
|
||||||
|
)
|
||||||
|
name = _text(preview.get("name"))
|
||||||
|
if not name:
|
||||||
|
raise ApiError(ErrorCode.VALIDATION_FAILED, "\u89d2\u8272\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a", {"field": "name"})
|
||||||
|
draft_id = _text(payload.get("draft_id") or draft.get("draft_id"))
|
||||||
|
profile = profile_service.create_profile(
|
||||||
|
{
|
||||||
|
"name": name,
|
||||||
|
"description": payload.get("description"),
|
||||||
|
"base_prompt": preview.get("base_prompt"),
|
||||||
|
"appearance_prompt": preview.get("appearance_prompt"),
|
||||||
|
"style_prompt": preview.get("style_prompt"),
|
||||||
|
"negative_prompt": preview.get("negative_prompt"),
|
||||||
|
"default_model_route": payload.get("default_model_route") or preview.get("default_model_route"),
|
||||||
|
"avatar_image_uri": payload.get("avatar_image_uri") or preview.get("avatar_image_uri"),
|
||||||
|
"metadata": {
|
||||||
|
**(payload.get("metadata") if isinstance(payload.get("metadata"), dict) else {}),
|
||||||
|
"source": "quick_profile",
|
||||||
|
**({"draft_id": draft_id} if draft_id else {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ctx,
|
||||||
|
)
|
||||||
|
return {
|
||||||
|
"draft_id": draft_id or None,
|
||||||
|
"profile": profile.to_dict(),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def _text(value: Any) -> str:
|
def _text(value: Any) -> str:
|
||||||
return str(value or "").strip()
|
return str(value or "").strip()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,84 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from backend.routes.profile_routes import save_quick_profile_draft
|
||||||
|
from backend.services.profile_service import ProfileService
|
||||||
|
from backend.services.quick_profile_service import QuickProfileService
|
||||||
|
from core.audit_service import AuditService
|
||||||
|
from core.identity import RequestContext
|
||||||
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||||
|
from database.repositories.profile_repository import ProfileRepository
|
||||||
|
|
||||||
|
|
||||||
|
class QuickProfileServiceTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.database = Database(
|
||||||
|
DatabaseSettings(sqlite=SqliteSettings(path=str(Path(self.tmp.name) / "imagestudio.db")))
|
||||||
|
)
|
||||||
|
self.database.initialize()
|
||||||
|
self.profile_service = ProfileService(ProfileRepository(self.database), AuditService(self.database))
|
||||||
|
self.quick_service = QuickProfileService()
|
||||||
|
self.ctx = RequestContext.from_astrbot(
|
||||||
|
username="admin",
|
||||||
|
user_id="user_001",
|
||||||
|
permissions={"can_write"},
|
||||||
|
)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.tmp.cleanup()
|
||||||
|
|
||||||
|
def test_save_draft_persists_preview_as_character_profile(self):
|
||||||
|
draft = self.quick_service.create_draft(
|
||||||
|
{
|
||||||
|
"questionnaire": {
|
||||||
|
"name": "Nova",
|
||||||
|
"identity": "mage student",
|
||||||
|
"personality": ["calm", "kind"],
|
||||||
|
"hair_color": "silver",
|
||||||
|
"hairstyle": "long hair",
|
||||||
|
"eye_color": "blue",
|
||||||
|
"style": "watercolor",
|
||||||
|
"clothing_keywords": ["white blouse"],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
saved = self.quick_service.save_draft({"draft": draft}, self.profile_service, self.ctx)
|
||||||
|
|
||||||
|
profile = self.profile_service.get_profile(saved["profile"]["profile_id"])
|
||||||
|
self.assertEqual(profile.name, "Nova")
|
||||||
|
self.assertIn("mage student", profile.base_prompt)
|
||||||
|
self.assertIn("silver hair", profile.appearance_prompt)
|
||||||
|
self.assertEqual(profile.style_prompt, "watercolor")
|
||||||
|
self.assertEqual(profile.metadata["source"], "quick_profile")
|
||||||
|
self.assertEqual(profile.metadata["draft_id"], draft["draft_id"])
|
||||||
|
|
||||||
|
def test_save_quick_profile_route_requires_write_permission(self):
|
||||||
|
draft = self.quick_service.create_draft({"questionnaire": {"name": "Nova"}})
|
||||||
|
read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001")
|
||||||
|
|
||||||
|
denied = save_quick_profile_draft(
|
||||||
|
read_only,
|
||||||
|
{"draft": draft},
|
||||||
|
self.quick_service,
|
||||||
|
self.profile_service,
|
||||||
|
)
|
||||||
|
allowed = save_quick_profile_draft(
|
||||||
|
self.ctx,
|
||||||
|
{"draft": draft},
|
||||||
|
self.quick_service,
|
||||||
|
self.profile_service,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertFalse(denied["ok"])
|
||||||
|
self.assertEqual(denied["error_code"], "FORBIDDEN")
|
||||||
|
self.assertTrue(allowed["ok"])
|
||||||
|
self.assertEqual(allowed["data"]["profile"]["name"], "Nova")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
|
|
@ -26,6 +26,7 @@ class RouteRegistryTests(unittest.TestCase):
|
||||||
self.assertIn(("GET", "ui/bootstrap"), keys)
|
self.assertIn(("GET", "ui/bootstrap"), keys)
|
||||||
self.assertIn(("GET", "ui/actions"), keys)
|
self.assertIn(("GET", "ui/actions"), keys)
|
||||||
self.assertIn(("POST", "nodes/{node_id}/generate"), keys)
|
self.assertIn(("POST", "nodes/{node_id}/generate"), keys)
|
||||||
|
self.assertIn(("POST", "profiles/quick-draft/save"), keys)
|
||||||
self.assertIn(("GET", "security/audit-logs"), keys)
|
self.assertIn(("GET", "security/audit-logs"), keys)
|
||||||
self.assertIn(("POST", "api-discovery/probe-provider"), keys)
|
self.assertIn(("POST", "api-discovery/probe-provider"), keys)
|
||||||
self.assertIn(("PUT", "material-categories/{category_id}"), keys)
|
self.assertIn(("PUT", "material-categories/{category_id}"), keys)
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用