feat: import character documents and filter advanced materials

这个提交存在于:
YokiiroBW 2026-07-23 00:03:33 +08:00
父节点 ad6e21de88
当前提交 ee0e775142
共有 8 个文件被更改,包括 438 次插入 和 4 次删除

查看文件

@ -60,6 +60,7 @@ ROUTES: tuple[RouteSpec, ...] = (
RouteSpec("GET", "profiles", "backend.routes.profile_routes.list_profiles", "read"),
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/from-document", "backend.routes.profile_routes.import_profile_document", "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("PUT", "profiles/{profile_id}", "backend.routes.profile_routes.update_profile", "write"),

查看文件

@ -8,6 +8,7 @@ from backend.routes.common import handle_request
from backend.routes.params import int_or_default, optional_str
from backend.services.event_stream_service import get_default_event_bus
from backend.services.profile_service import ProfileService
from backend.services.profile_document_import_service import ProfileDocumentImportService
from backend.services.quick_profile_service import QuickProfileService
from backend.services.workflow_service import WorkflowService
from core.audit_service import AuditService
@ -105,6 +106,21 @@ def archive_profile(
return delete_profile(ctx, profile_id, service)
def import_profile_document(
ctx: RequestContext,
payload: dict[str, Any],
service: ProfileDocumentImportService | None = None,
) -> dict:
import_service = service or ProfileDocumentImportService()
def action() -> dict:
require_authenticated(ctx)
return import_service.import_document(payload)
return handle_request(ctx, action)
def create_quick_profile_draft(
ctx: RequestContext,
payload: dict[str, Any],

查看文件

@ -0,0 +1,148 @@
"""Turn character documents into editable profile questionnaire drafts."""
from __future__ import annotations
import json
import re
from typing import Any
from backend.dto.common import ApiError, ErrorCode
from backend.services.model_provider_runtime import ModelProviderRuntime
from backend.services.model_provider_service import ModelProviderService
from core.astrbot_provider_bridge import astrbot_structured_text_callable
from database.db_helper import Database, load_config_payload, load_database_settings
from database.repositories.model_provider_repository import ModelProviderRepository
MAX_DOCUMENT_CHARS = 120_000
ALLOWED_EXTENSIONS = {".md", ".markdown", ".txt"}
QUESTIONNAIRE_FIELDS = (
"role_core", "gender_presentation", "age_stage", "species", "body_type",
"distinctive_features", "personality", "appearance", "outfit", "scene", "avoid",
"worldview", "background", "height_build", "body_features", "likes", "dislikes",
"habits", "relationships", "story_hooks", "generation_style", "shot_preference", "mood", "extra_notes",
)
class ProfileDocumentImportService:
def import_document(self, payload: dict[str, Any]) -> dict[str, Any]:
filename = str(payload.get("filename") or "character.txt").strip()
content = str(payload.get("content") or "")
self._validate_document(filename, content)
seed = payload.get("questionnaire") if isinstance(payload.get("questionnaire"), dict) else {}
route = self._resolve_route(payload.get("model_route"))
callable_ = self._resolve_callable(route)
if callable_ is None:
return self._fallback(content, filename, seed, route)
try:
raw = callable_(self._model_request(filename, content, seed))
result = self._normalize_result(raw, seed)
except Exception as exc:
raise ApiError(
ErrorCode.GENERATION_FAILED,
"角色文档识别失败,请检查文本模型配置后重试",
{"reason": str(exc)[:500], "model_route": route or None},
) from exc
return {**result, "filename": filename, "model_route": route or None, "llm_used": True}
def _validate_document(self, filename: str, content: str) -> None:
normalized = filename.lower().split("?")[0]
if not any(normalized.endswith(extension) for extension in ALLOWED_EXTENSIONS):
raise ApiError(ErrorCode.UNSUPPORTED_FILE_TYPE, "角色文档仅支持 Markdown 或 TXT 文件", {"filename": filename})
if not content.strip():
raise ApiError(ErrorCode.VALIDATION_FAILED, "角色文档内容不能为空", {"field": "content"})
if len(content) > MAX_DOCUMENT_CHARS:
raise ApiError(ErrorCode.UPLOAD_TOO_LARGE, "角色文档过大,请控制在 120000 个字符以内", {"max_chars": MAX_DOCUMENT_CHARS})
def _resolve_route(self, requested: Any) -> str:
if str(requested or "").strip():
return str(requested).strip()
config = load_config_payload()
assignments = config.get("model_assignments") if isinstance(config.get("model_assignments"), dict) else {}
for key in ("profile_import", "prompt_planning", "cross_review_primary"):
route = str(assignments.get(key) or "").strip()
if route:
return route
return ""
def _resolve_callable(self, route: str):
if not route:
return None
database = Database(load_database_settings())
runtime = ModelProviderRuntime(ModelProviderService(ModelProviderRepository(database)))
return runtime.structured_text_callable(route) or astrbot_structured_text_callable(route)
def _model_request(self, filename: str, content: str, seed: dict[str, Any]) -> dict[str, Any]:
return {
"purpose": "extract an editable character persona questionnaire from a document",
"filename": filename,
"document": content,
"existing_questionnaire": seed,
"rules": [
"Return one JSON object only.",
"Only extract facts supported by the document; do not invent details.",
"The result is an editable draft, not a sealed profile card.",
"Use empty strings for unknown fields.",
],
"questionnaire_schema": {field: "string; leave empty when unknown" for field in QUESTIONNAIRE_FIELDS},
}
def _normalize_result(self, raw: Any, seed: dict[str, Any]) -> dict[str, Any]:
parsed = _json_object(raw)
source = parsed.get("questionnaire") if isinstance(parsed.get("questionnaire"), dict) else parsed
questionnaire: dict[str, str] = {}
recognized: list[str] = []
for field in QUESTIONNAIRE_FIELDS:
value = _text(source.get(field))
if value:
questionnaire[field] = value[:8000]
recognized.append(field)
elif field in seed:
questionnaire[field] = _text(seed.get(field))[:8000]
return {
"questionnaire": questionnaire,
"summary": _text(parsed.get("summary") or parsed.get("description"))[:4000],
"recognized_fields": recognized,
"unresolved_fields": [field for field in QUESTIONNAIRE_FIELDS if field not in recognized],
"warnings": _string_list(parsed.get("warnings")),
}
def _fallback(self, content: str, filename: str, seed: dict[str, Any], route: str) -> dict[str, Any]:
questionnaire = {field: _text(seed.get(field))[:8000] for field in QUESTIONNAIRE_FIELDS if _text(seed.get(field))}
questionnaire.setdefault("role_core", content.strip()[:1000])
questionnaire.setdefault("extra_notes", content.strip()[:8000])
return {
"filename": filename,
"model_route": route or None,
"llm_used": False,
"questionnaire": questionnaire,
"summary": "未配置可用文本模型,已保留原文供你手动整理。",
"recognized_fields": [field for field in ("role_core", "extra_notes") if field in questionnaire],
"unresolved_fields": [field for field in QUESTIONNAIRE_FIELDS if field not in questionnaire],
"warnings": ["未找到可用文本模型,未执行 LLM 识别。"],
}
def _json_object(raw: Any) -> dict[str, Any]:
if isinstance(raw, dict):
return raw
text = re.sub(r"^```(?:json)?\s*|\s*```$", "", str(raw or "").strip(), flags=re.IGNORECASE).strip()
start, end = text.find("{"), text.rfind("}")
if start >= 0 and end > start:
text = text[start : end + 1]
try:
value = json.loads(re.sub(r",\s*([}\]])", r"\1", text))
except json.JSONDecodeError as exc:
raise ValueError("structured response was not valid JSON") from exc
if not isinstance(value, dict):
raise ValueError("structured response must be an object")
return value
def _text(value: Any) -> str:
return str(value or "").strip()
def _string_list(value: Any) -> list[str]:
values = value.split(",") if isinstance(value, str) else value if isinstance(value, list) else []
return [_text(item)[:500] for item in values if _text(item)]

查看文件

@ -11,6 +11,14 @@ from core.audit_service import AuditService
from core.identity import RequestContext
from database.repositories.profile_repository import ProfileRepository
VISUAL_PROFILE_FIELDS = {
"base_prompt",
"appearance_prompt",
"style_prompt",
"negative_prompt",
"avatar_image_uri",
}
class ProfileService:
def __init__(
@ -98,6 +106,14 @@ class ProfileService:
if "name" in payload and not str(payload["name"]).strip():
raise ApiError(ErrorCode.VALIDATION_FAILED, "角色名称不能为空", {"field": "name"})
update_payload = dict(payload)
if before.profile_category == "profile_card" or bool((before.metadata or {}).get("visual_identity_locked")):
blocked = sorted(VISUAL_PROFILE_FIELDS.intersection(update_payload))
if blocked:
raise ApiError(
ErrorCode.FORBIDDEN,
"sealed profile cards only allow non-visual persona edits",
{"blocked_fields": blocked, "profile_id": profile_id},
)
if "name" in update_payload:
update_payload["name"] = str(update_payload["name"]).strip()
after = self.repository.update(profile_id, update_payload)

查看文件

@ -9,6 +9,7 @@ export const endpoints = {
profile: (profileId: string) => `profiles/${profileId}`,
profileArchive: (profileId: string) => `profiles/${profileId}/archive`,
quickProfileDraft: "profiles/quick-draft",
quickProfileDocumentImport: "profiles/quick-draft/from-document",
quickProfileDraftSave: "profiles/quick-draft/save",
profileWorkflows: (profileId: string) => `profiles/${profileId}/workflows`,
profileCards: (profileId: string) => `profiles/${profileId}/profile-cards`,

查看文件

@ -750,6 +750,9 @@ const showPersonaQuestionnaire = ref(false)
const showQuickProfileDetailCard = ref(false)
const showExternalProfileImport = ref(false)
const externalProfileImportText = ref("")
const externalProfileImportFileName = ref("")
const externalProfileImportBusy = ref(false)
const externalProfileImportError = ref("")
const quickProfileQuestionnaireMode = ref<QuestionnaireMode>("simple")
const quickProfileQuestionnaire = ref<QuickProfileQuestionnaire>(createDefaultQuickProfileQuestionnaire())
const backendProfiles = ref<CharacterProfile[]>([])
@ -7957,6 +7960,8 @@ function openPersonaQuestionnaire() {
function openExternalProfileImport() {
closeAllOverlays("externalProfileImport")
externalProfileImportError.value = ""
externalProfileImportFileName.value = ""
showExternalProfileImport.value = true
void nextTick(() => {
document.querySelector<HTMLTextAreaElement>("[data-profile-import-input]")?.focus()
@ -9516,6 +9521,80 @@ function quickProfileGeneratedColors(index: number): [string, string, string] {
return palettes[index % palettes.length]
}
async function importExternalProfileDocumentFile(event: Event) {
const input = event.target instanceof HTMLInputElement ? event.target : null
const file = input?.files?.[0]
if (!file) return
const filename = file.name
if (!/\.(md|markdown|txt)$/i.test(filename)) {
externalProfileImportError.value = "仅支持 .md、.markdown 或 .txt 文件"
input.value = ""
return
}
externalProfileImportBusy.value = true
externalProfileImportError.value = ""
externalProfileImportFileName.value = filename
try {
const content = await file.text()
const result = await postJson<{
questionnaire?: Record<string, string>
summary?: string
warnings?: string[]
unresolved_fields?: string[]
llm_used?: boolean
}>(endpoints.quickProfileDocumentImport, {
filename,
content,
questionnaire: buildQuestionnairePayload(quickProfileQuestionnaire.value, quickProfileQuestionnaireMode.value),
})
const fieldMap: Record<string, keyof QuickProfileQuestionnaire> = {
role_core: "roleCore",
gender_presentation: "genderPresentation",
age_stage: "ageStage",
species: "species",
body_type: "bodyType",
distinctive_features: "distinctiveFeatures",
personality: "personality",
appearance: "appearance",
outfit: "outfit",
scene: "scene",
avoid: "avoid",
worldview: "worldview",
background: "background",
height_build: "heightBuild",
body_features: "bodyFeatures",
likes: "likes",
dislikes: "dislikes",
habits: "habits",
relationships: "relationships",
story_hooks: "storyHooks",
generation_style: "generationStyle",
shot_preference: "shotPreference",
mood: "mood",
extra_notes: "extraNotes",
}
const imported = { ...quickProfileQuestionnaire.value }
for (const [key, value] of Object.entries(result.questionnaire || {})) {
const target = fieldMap[key]
if (target && typeof value === "string" && value.trim()) imported[target] = value
}
quickProfileQuestionnaire.value = imported
if ((result.unresolved_fields || []).some((field) => ["worldview", "background", "relationships", "story_hooks", "extra_notes"].includes(field))) {
quickProfileQuestionnaireMode.value = "advanced"
}
externalProfileImportText.value = content
const warning = result.warnings?.filter(Boolean).join(";")
externalProfileImportError.value = warning || (result.llm_used === false ? "未配置文本模型,已保留原文,请手动整理。" : "")
closeOverlay("externalProfileImport")
openPersonaQuestionnaire()
} catch (error) {
externalProfileImportError.value = error instanceof Error ? error.message : "文档识别失败"
} finally {
externalProfileImportBusy.value = false
if (input) input.value = ""
}
}
function applyExternalProfileImport() {
const source = externalProfileImportText.value.trim()
if (!source) return
@ -12701,6 +12780,15 @@ function errorMessage(err: unknown) {
</header>
<div class="external-profile-import-body">
<div class="external-profile-import-file-row">
<label class="secondary-file-picker">
<span>从文档导入</span>
<input type="file" accept=".md,.markdown,.txt,text/markdown,text/plain" @change="importExternalProfileDocumentFile" />
</label>
<em v-if="externalProfileImportFileName">{{ externalProfileImportFileName }}</em>
<small v-if="externalProfileImportBusy">正在让文本模型整理角色档案…</small>
<small v-else-if="externalProfileImportError">{{ externalProfileImportError }}</small>
</div>
<label class="external-profile-import-source">
<span>原始文本</span>
<textarea
@ -22002,3 +22090,26 @@ body {
}
}
</style>
.external-profile-import-file-row {
display: flex;
align-items: center;
gap: 10px;
flex-wrap: wrap;
margin-bottom: 12px;
}
.secondary-file-picker {
display: inline-flex;
align-items: center;
gap: 8px;
cursor: pointer;
}
.secondary-file-picker input {
max-width: 240px;
}
.external-profile-import-file-row small {
color: var(--text-muted, #7b8494);
}

查看文件

@ -87,6 +87,10 @@ const acceptConfirmed = ref(false)
const validationReferenceEnabled = ref(true)
const consensusAnalysisStrategy = ref<"text_only" | "vision_assisted">("text_only")
const validationReferenceSourceId = ref("")
const advancedMaterialCategoryFilter = ref("")
const advancedMaterialStatusFilter = ref("")
const advancedMaterialPriorityFilter = ref(0)
const advancedMaterialSearch = ref("")
let pollTimer = 0
const ownedMediaObjectUrls = new Set<string>()
@ -104,6 +108,31 @@ const styleSetUploadEnabled = computed(() => isStyleCategory(selectedUploadCateg
const isVisibleImport = (item: AdvancedMaterialImport) => !["accepted", "archived"].includes(normalizeStatus(item.status))
const sortedImports = computed(() => sortAdvancedImports(imports.value.filter(isVisibleImport)))
const sortedMaterials = computed(() => sortAdvancedMaterials(materials.value))
const advancedMaterialCategoryTabs = computed(() => [
{ id: "", label: "全部", count: sortedMaterials.value.length },
...rootCategories.value.map((category) => ({
id: category.category_id,
label: materialCategoryLabel(category.name),
count: sortedMaterials.value.filter((material) => material.category_id === category.category_id).length,
})),
])
const filteredSortedMaterials = computed(() => {
const keyword = advancedMaterialSearch.value.trim().toLowerCase()
return sortedMaterials.value.filter((material) => {
if (advancedMaterialCategoryFilter.value && material.category_id !== advancedMaterialCategoryFilter.value) return false
if (advancedMaterialStatusFilter.value && normalizeStatus(material.status) !== advancedMaterialStatusFilter.value) return false
if (Number(advancedMaterialPriorityFilter.value) > 0 && Number(material.priority || 0) < Number(advancedMaterialPriorityFilter.value)) return false
if (!keyword) return true
const haystack = [material.display_name, material.prompt, material.negative_prompt, ...material.tags].filter(Boolean).join(" ").toLowerCase()
return haystack.includes(keyword)
})
})
function clearAdvancedMaterialFilters() {
advancedMaterialCategoryFilter.value = ""
advancedMaterialStatusFilter.value = ""
advancedMaterialPriorityFilter.value = 0
advancedMaterialSearch.value = ""
}
const selectedImport = computed(() =>
selectedImportDetail.value?.import_id === selectedImportId.value
? selectedImportDetail.value
@ -1352,16 +1381,29 @@ function errorMessage(err: unknown) {
<header class="advanced-section-header">
<div>
<span>高优先级封存库</span>
<h3>{{ sortedMaterials.length }} 条进阶素材</h3>
<h3>{{ filteredSortedMaterials.length }} 条进阶素材</h3>
<p>源图、验证图和最终提示词被独立封存,不进入普通图库或工作流候选池。</p>
</div>
<button type="button" class="primary" @click="emit('navigate', 'advanced-import')">+ 新建进阶导入</button>
</header>
<div v-if="loading && !sortedMaterials.length" class="advanced-material-skeleton"><span v-for="index in 4" :key="index" /></div>
<div v-else-if="sortedMaterials.length" class="advanced-material-card-grid">
<section class="advanced-material-filter-panel" aria-label="进阶素材筛选">
<div class="advanced-material-filter-tabs" role="tablist" aria-label="素材分类">
<button v-for="tab in advancedMaterialCategoryTabs" :key="tab.id || 'all'" type="button" :class="{ active: advancedMaterialCategoryFilter === tab.id }" @click="advancedMaterialCategoryFilter = tab.id">
{{ tab.label }} <span>{{ tab.count }}</span>
</button>
</div>
<div class="advanced-material-filter-controls">
<label><span>搜索</span><input v-model="advancedMaterialSearch" type="search" placeholder="名称、标签或提示词" /></label>
<label><span>状态</span><select v-model="advancedMaterialStatusFilter"><option value="">全部状态</option><option value="active">有效</option><option value="archived">已归档</option></select></label>
<label><span>最低优先级</span><select v-model.number="advancedMaterialPriorityFilter"><option :value="0">不限</option><option :value="50">≥ 50</option><option :value="80">≥ 80</option><option :value="100">100</option></select></label>
<button type="button" @click="clearAdvancedMaterialFilters">清除筛选</button>
</div>
</section>
<div v-if="loading && !sortedMaterials.length" class="advanced-material-skeleton"><span v-for="index in 4" :key="index" /></div>
<div v-else-if="filteredSortedMaterials.length" class="advanced-material-card-grid">
<button
v-for="material in sortedMaterials"
v-for="material in filteredSortedMaterials"
:key="material.material_id"
type="button"
class="advanced-material-card"
@ -1967,4 +2009,14 @@ button:disabled { cursor: not-allowed; opacity: .52; }
.advanced-style-reference-card img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; background: #111; }
.advanced-style-reference-card small { font-size: 10px; font-weight: 800; }
.advanced-material-filter-panel { display: grid; gap: 10px; margin: 0 0 14px; padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel-muted); }
.advanced-material-filter-tabs { display: flex; flex-wrap: wrap; gap: 8px; }
.advanced-material-filter-tabs button { display: inline-flex; align-items: center; gap: 7px; border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; color: var(--text-soft); background: var(--panel); font-weight: 700; }
.advanced-material-filter-tabs button.active { border-color: var(--primary); color: var(--text); background: color-mix(in srgb, var(--primary) 18%, var(--panel)); }
.advanced-material-filter-tabs span { min-width: 20px; border-radius: 999px; padding: 1px 6px; color: var(--text); background: var(--panel-muted); font-size: 11px; text-align: center; }
.advanced-material-filter-controls { display: grid; grid-template-columns: minmax(180px, 1.5fr) repeat(2, minmax(150px, 1fr)) auto; gap: 10px; align-items: end; }
.advanced-material-filter-controls label { display: grid; gap: 5px; color: var(--text-soft); font-size: 11px; font-weight: 700; }
.advanced-material-filter-controls input, .advanced-material-filter-controls select { min-width: 0; border: 1px solid var(--border); border-radius: 7px; padding: 9px 10px; color: var(--text); background: var(--panel); }
@media (max-width: 820px) { .advanced-material-filter-controls { grid-template-columns: 1fr; } }
</style>

查看文件

@ -0,0 +1,89 @@
from types import SimpleNamespace
import pytest
from backend.dto.common import ApiError, ErrorCode
from backend.services.profile_document_import_service import ProfileDocumentImportService
from backend.services.profile_service import ProfileService
from core.identity import RequestContext
def test_document_import_rejects_unsupported_file() -> None:
with pytest.raises(ApiError) as error:
ProfileDocumentImportService().import_document({"filename": "character.pdf", "content": "x"})
assert error.value.error_code == ErrorCode.UNSUPPORTED_FILE_TYPE
def test_document_import_falls_back_without_model(monkeypatch) -> None:
service = ProfileDocumentImportService()
monkeypatch.setattr(service, "_resolve_route", lambda _requested: "")
result = service.import_document({"filename": "character.md", "content": "# Mika\nA quiet archivist."})
assert result["llm_used"] is False
assert result["questionnaire"]["role_core"].startswith("# Mika")
assert result["questionnaire"]["extra_notes"].startswith("# Mika")
def test_document_import_normalizes_structured_model_result(monkeypatch) -> None:
service = ProfileDocumentImportService()
monkeypatch.setattr(service, "_resolve_route", lambda _requested: "test-route")
monkeypatch.setattr(service, "_resolve_callable", lambda _route: lambda _request: {
"summary": "A careful archivist",
"questionnaire": {
"role_core": "archivist",
"personality": "quiet",
"unknown_field": "ignored",
},
})
result = service.import_document({"filename": "character.txt", "content": "Mika"})
assert result["llm_used"] is True
assert result["questionnaire"] == {"role_core": "archivist", "personality": "quiet"}
assert "unknown_field" not in result["questionnaire"]
assert "appearance" in result["unresolved_fields"]
def test_sealed_profile_rejects_visual_updates() -> None:
profile = SimpleNamespace(profile_category="profile_card", metadata={}, name="Mika", status="active", profile_id="profile-1", owner_user_id="system")
class Repository:
def get(self, _profile_id):
return profile
def update(self, _profile_id, _payload):
raise AssertionError("sealed visual update must not reach repository")
class Audit:
def record(self, **_kwargs):
raise AssertionError("sealed visual update must not be audited")
service = ProfileService(Repository(), Audit())
with pytest.raises(ApiError) as error:
service.update_profile("profile-1", {"appearance_prompt": "new face"}, RequestContext.system("test"))
assert error.value.error_code == ErrorCode.FORBIDDEN
def test_sealed_profile_allows_non_visual_update() -> None:
profile = SimpleNamespace(profile_category="profile_card", metadata={}, name="Mika", status="active", profile_id="profile-1", owner_user_id="system")
updated = SimpleNamespace(profile_category="profile_card", metadata={}, name="Mika", status="active", profile_id="profile-1", owner_user_id="system")
profile.to_dict = lambda: vars(profile)
updated.to_dict = lambda: vars(updated)
class Repository:
def get(self, _profile_id):
return profile
def update(self, _profile_id, payload):
assert payload == {"description": "new backstory"}
return updated
class Audit:
def record(self, **_kwargs):
return None
result = ProfileService(Repository(), Audit()).update_profile(
"profile-1", {"description": "new backstory"}, RequestContext.system("test")
)
assert result is updated