feat: add profile route handlers
这个提交存在于:
父节点
60b995dc7a
当前提交
b69f631711
共有 5 个文件被更改,包括 292 次插入 和 6 次删除
|
|
@ -13,6 +13,7 @@ Prepared:
|
|||
- `profile_workflows` and `workflow_nodes` persistence and service layer with audit logging.
|
||||
- `generation_images` persistence and service layer with selected image exclusivity and audit logging.
|
||||
- `ui/bootstrap` and `dashboard/summary` pure route handlers ready for AstrBot mounting.
|
||||
- Profile CRUD and quick profile draft pure route handlers ready for AstrBot mounting.
|
||||
- Frontend source skeleton for Plugin Page UI and Standalone Web UI.
|
||||
- Runtime data directories with `.gitkeep` files only.
|
||||
- Generated data, local virtual environments, node modules, and build artifacts are ignored.
|
||||
|
|
@ -40,7 +41,6 @@ C:\Users\Administrator\.cache\codex-runtimes\codex-primary-runtime\dependencies\
|
|||
Follow `imagestudio_codex_hermes_dev_flow_v3.md` and continue with:
|
||||
|
||||
```text
|
||||
S2-3 实现 profiles CRUD routes
|
||||
S2-4 实现 workflows / nodes / images routes
|
||||
S2-5 实现 jobs / settings placeholders
|
||||
S2-6 实现 storage status / media sign placeholders
|
||||
|
|
|
|||
11
README.md
11
README.md
|
|
@ -42,14 +42,15 @@ Implemented foundation:
|
|||
- `profile_workflows` and `workflow_nodes` migrations, repositories, services, and audit-backed create/update.
|
||||
- `generation_images` migration, repository, service, selected-state exclusivity, archive/delete metadata semantics, and audit logging.
|
||||
- `ui/bootstrap` and `dashboard/summary` service/route handlers with v3 response wrapping.
|
||||
- Profile CRUD and quick profile draft route handlers.
|
||||
|
||||
Next tasks:
|
||||
|
||||
1. `S2-3` profiles CRUD routes.
|
||||
2. `S2-4` workflows, nodes, and images routes.
|
||||
3. `S2-5` jobs placeholders and settings current/schema.
|
||||
4. `S2-6` storage status and media sign placeholders.
|
||||
5. `S3` mount Plugin Page UI and Standalone Web UI through AstrBot.
|
||||
1. `S2-4` workflows, nodes, and images routes.
|
||||
2. `S2-5` jobs placeholders and settings current/schema.
|
||||
3. `S2-6` storage status and media sign placeholders.
|
||||
4. `S3` mount Plugin Page UI and Standalone Web UI through AstrBot.
|
||||
5. `S4` single image mock flow and generation task lifecycle.
|
||||
|
||||
## Repository Layout
|
||||
|
||||
|
|
|
|||
129
backend/routes/profile_routes.py
普通文件
129
backend/routes/profile_routes.py
普通文件
|
|
@ -0,0 +1,129 @@
|
|||
"""Profile route handlers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.routes.common import handle_request
|
||||
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 core.security import require_authenticated, require_write
|
||||
from database.db_helper import Database, load_database_settings
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
|
||||
|
||||
def list_profiles(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: ProfileService | None = None,
|
||||
) -> dict:
|
||||
profile_service = service or _create_profile_service()
|
||||
params = query or {}
|
||||
|
||||
def action() -> dict:
|
||||
require_authenticated(ctx)
|
||||
result = profile_service.list_profiles(
|
||||
ctx=ctx,
|
||||
keyword=_optional_str(params.get("keyword")),
|
||||
status=_optional_str(params.get("status")) or "active",
|
||||
page=_int_or_default(params.get("page"), 1),
|
||||
page_size=_int_or_default(params.get("page_size"), 30),
|
||||
)
|
||||
return result.to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def create_profile(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any],
|
||||
service: ProfileService | None = None,
|
||||
) -> dict:
|
||||
profile_service = service or _create_profile_service()
|
||||
|
||||
def action() -> dict:
|
||||
require_write(ctx)
|
||||
return profile_service.create_profile(payload, ctx).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def get_profile(
|
||||
ctx: RequestContext,
|
||||
profile_id: str,
|
||||
service: ProfileService | None = None,
|
||||
) -> dict:
|
||||
profile_service = service or _create_profile_service()
|
||||
|
||||
def action() -> dict:
|
||||
require_authenticated(ctx)
|
||||
return profile_service.get_profile(profile_id).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def update_profile(
|
||||
ctx: RequestContext,
|
||||
profile_id: str,
|
||||
payload: dict[str, Any],
|
||||
service: ProfileService | None = None,
|
||||
) -> dict:
|
||||
profile_service = service or _create_profile_service()
|
||||
|
||||
def action() -> dict:
|
||||
require_write(ctx)
|
||||
return profile_service.update_profile(profile_id, payload, ctx).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def delete_profile(
|
||||
ctx: RequestContext,
|
||||
profile_id: str,
|
||||
service: ProfileService | None = None,
|
||||
) -> dict:
|
||||
profile_service = service or _create_profile_service()
|
||||
|
||||
def action() -> dict:
|
||||
require_write(ctx)
|
||||
return profile_service.delete_profile(profile_id, ctx).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def create_quick_profile_draft(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any],
|
||||
service: QuickProfileService | None = None,
|
||||
) -> dict:
|
||||
quick_service = service or QuickProfileService()
|
||||
|
||||
def action() -> dict:
|
||||
require_authenticated(ctx)
|
||||
return quick_service.create_draft(payload)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _create_profile_service() -> ProfileService:
|
||||
database = Database(load_database_settings())
|
||||
return ProfileService(
|
||||
repository=ProfileRepository(database),
|
||||
audit_service=AuditService(database),
|
||||
)
|
||||
|
||||
|
||||
def _optional_str(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text or None
|
||||
|
||||
|
||||
def _int_or_default(value: Any, default: int) -> int:
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
"""Quick profile draft service.
|
||||
|
||||
P0 uses a deterministic draft builder. A later text-model adapter can replace
|
||||
the prompt expansion internals without changing the API contract.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
|
||||
|
||||
class QuickProfileService:
|
||||
def create_draft(self, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
questionnaire = payload.get("questionnaire")
|
||||
if not isinstance(questionnaire, dict):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"缺少快速人设问卷",
|
||||
{"field": "questionnaire"},
|
||||
)
|
||||
name = str(questionnaire.get("name") or "").strip()
|
||||
if not name:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"角色名称不能为空",
|
||||
{"field": "questionnaire.name"},
|
||||
)
|
||||
|
||||
identity = _text(questionnaire.get("identity"))
|
||||
personality = _join(questionnaire.get("personality"))
|
||||
hair_color = _text(questionnaire.get("hair_color"))
|
||||
hairstyle = _text(questionnaire.get("hairstyle"))
|
||||
eye_color = _text(questionnaire.get("eye_color"))
|
||||
style = _text(questionnaire.get("style"))
|
||||
clothing = _join(questionnaire.get("clothing_keywords"))
|
||||
|
||||
base_parts = [name]
|
||||
if identity:
|
||||
base_parts.append(identity)
|
||||
if personality:
|
||||
base_parts.append(f"personality: {personality}")
|
||||
|
||||
appearance_parts = []
|
||||
if hair_color:
|
||||
appearance_parts.append(f"{hair_color} hair")
|
||||
if hairstyle:
|
||||
appearance_parts.append(hairstyle)
|
||||
if eye_color:
|
||||
appearance_parts.append(f"{eye_color} eyes")
|
||||
if clothing:
|
||||
appearance_parts.append(clothing)
|
||||
|
||||
return {
|
||||
"draft_id": f"draft_{uuid4().hex}",
|
||||
"profile_preview": {
|
||||
"name": name,
|
||||
"base_prompt": ", ".join(base_parts),
|
||||
"appearance_prompt": ", ".join(appearance_parts),
|
||||
"style_prompt": style,
|
||||
"negative_prompt": "low quality, blurry, bad anatomy, extra fingers",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _text(value: Any) -> str:
|
||||
return str(value or "").strip()
|
||||
|
||||
|
||||
def _join(value: Any) -> str:
|
||||
if isinstance(value, list):
|
||||
return ", ".join(str(item).strip() for item in value if str(item).strip())
|
||||
return _text(value)
|
||||
|
|
@ -5,6 +5,14 @@ import unittest
|
|||
from pathlib import Path
|
||||
|
||||
from backend.routes.dashboard_routes import get_dashboard_summary
|
||||
from backend.routes.profile_routes import (
|
||||
create_profile,
|
||||
create_quick_profile_draft,
|
||||
delete_profile,
|
||||
get_profile,
|
||||
list_profiles,
|
||||
update_profile,
|
||||
)
|
||||
from backend.routes.ui_routes import get_ui_bootstrap
|
||||
from backend.services.dashboard_service import DashboardService
|
||||
from backend.services.image_service import ImageService
|
||||
|
|
@ -154,6 +162,79 @@ storage:
|
|||
self.assertEqual(len(data["recent_images"]), 1)
|
||||
self.assertGreaterEqual(data["storage"]["total_bytes"], 8)
|
||||
|
||||
def test_profile_crud_routes_return_wrapped_responses(self):
|
||||
created = create_profile(
|
||||
self.ctx,
|
||||
{
|
||||
"name": "莉娅",
|
||||
"description": "银白发少女",
|
||||
"default_model_route": "image_low",
|
||||
},
|
||||
self.profile_service,
|
||||
)
|
||||
|
||||
self.assertTrue(created["ok"])
|
||||
profile_id = created["data"]["profile_id"]
|
||||
self.assertEqual(created["data"]["name"], "莉娅")
|
||||
|
||||
listed = list_profiles(self.ctx, {"page": "1", "page_size": "20"}, self.profile_service)
|
||||
self.assertTrue(listed["ok"])
|
||||
self.assertEqual(listed["data"]["total"], 1)
|
||||
self.assertEqual(listed["data"]["items"][0]["profile_id"], profile_id)
|
||||
|
||||
detail = get_profile(self.ctx, profile_id, self.profile_service)
|
||||
self.assertTrue(detail["ok"])
|
||||
self.assertEqual(detail["data"]["description"], "银白发少女")
|
||||
|
||||
updated = update_profile(
|
||||
self.ctx,
|
||||
profile_id,
|
||||
{"name": "莉娅 Nova", "style_prompt": "anime impasto"},
|
||||
self.profile_service,
|
||||
)
|
||||
self.assertTrue(updated["ok"])
|
||||
self.assertEqual(updated["data"]["name"], "莉娅 Nova")
|
||||
|
||||
deleted = delete_profile(self.ctx, profile_id, self.profile_service)
|
||||
self.assertTrue(deleted["ok"])
|
||||
self.assertEqual(deleted["data"]["status"], "deleted")
|
||||
|
||||
missing = get_profile(self.ctx, profile_id, self.profile_service)
|
||||
self.assertFalse(missing["ok"])
|
||||
self.assertEqual(missing["error_code"], "RESOURCE_NOT_FOUND")
|
||||
|
||||
def test_profile_write_routes_require_write_permission(self):
|
||||
read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001")
|
||||
|
||||
response = create_profile(read_only, {"name": "莉娅"}, self.profile_service)
|
||||
|
||||
self.assertFalse(response["ok"])
|
||||
self.assertEqual(response["error_code"], "FORBIDDEN")
|
||||
|
||||
def test_quick_profile_draft_route_returns_preview(self):
|
||||
response = create_quick_profile_draft(
|
||||
self.ctx,
|
||||
{
|
||||
"questionnaire": {
|
||||
"name": "莉娅",
|
||||
"identity": "魔法学院学生",
|
||||
"personality": ["温柔", "安静"],
|
||||
"hair_color": "银白色",
|
||||
"hairstyle": "长发",
|
||||
"eye_color": "蓝色",
|
||||
"style": "二次元厚涂",
|
||||
"clothing_keywords": ["白色上衣", "蓝色短裙"],
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
self.assertTrue(response["ok"])
|
||||
self.assertTrue(response["data"]["draft_id"].startswith("draft_"))
|
||||
preview = response["data"]["profile_preview"]
|
||||
self.assertEqual(preview["name"], "莉娅")
|
||||
self.assertIn("魔法学院学生", preview["base_prompt"])
|
||||
self.assertIn("银白色 hair", preview["appearance_prompt"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用