- 形仪域 domains/xingyi:form_anchors(稳定辨识特征/年龄表现/许可禁忌/可变范围——生成内容不可静默覆盖锚点);forms(主仪/化仪/文字形仪,candidate→adopt 定相治理:未定相不进呈现位、主仪唯一、新主仪自动退役旧主仪);voice_profiles(三来源,授权克隆必填用途与传播范围,revoke-grant 撤回立即生效,assert-usable 合成前置校验拒绝撤回/过期) - ASR:元炁 SenseVoice /v1/audio/transcriptions 透传(Whisper 兼容 multipart);YUANQI_BASE_URL 未配置时 503 不降级不伪造——多模态关闭纪律 - 前端形仪工坊(人物详情页):真形锚点编辑(特征逐行解析)/候选登记定相废弃/声音档案建立与授权撤回;头像呈现位改走定相主仪(forms/primary);xingyi api 库 + mock + 组件 4 用例 - 退役:旧 avatar 模块(services/endpoints/avatar、avatar_dir 配置、avatar_url 字段、前端直传组件与测试) - 验收(test_m7_xingyi 三用例):未定相不成为正式形仪(含主仪唯一与旧主仪 retired 断言);克隆授权可撤回(缺用途拒/撤回即拒/过期同理/预设不受约束);关闭全部多模态后文字相伴完整可用(ASR 503 时镜树缘契百工照常——回归) - 四套件:后端 376、前端 203、BFF 4、神识 5 包全绿(2 个预存环境失败不变);迁移 008 净库+±1 干净
267 lines
8.5 KiB
Python
267 lines
8.5 KiB
Python
"""ORM 到 API schema 的转换。"""
|
|
|
|
from datetime import UTC, timedelta
|
|
from typing import Any
|
|
|
|
from app.core.time import to_iso, utc_now
|
|
from app.models.candidates import MemoryCandidate, ProfileCandidate
|
|
from app.models.character import Character
|
|
from app.models.governance import (
|
|
AgentRosterEntry,
|
|
GenerationPreferences,
|
|
Goal,
|
|
PerceptionChannel,
|
|
Plan,
|
|
)
|
|
from app.models.imports import CharacterImportJob, ImportItem
|
|
from app.models.runtime import Event, Run
|
|
from app.models.safety import AuditEvent
|
|
from app.repositories.json_utils import loads_dict, loads_list
|
|
from app.schemas.admin import (
|
|
AgentRosterResponse,
|
|
AuditEntry,
|
|
GoalNode,
|
|
PerceptionChannelResponse,
|
|
PlanSummaryResponse,
|
|
)
|
|
from app.schemas.candidates import MemoryCandidateResponse, ProfileCandidateResponse
|
|
from app.schemas.character import CharacterResponse, CharacterSummaryResponse
|
|
from app.schemas.imports import ImportItemResponse, ImportJobResponse
|
|
from app.schemas.preferences import GenerationPreferencesResponse
|
|
from app.schemas.runtime import EventResponse, RunResponse
|
|
|
|
|
|
|
|
def character_to_response(character: Character) -> CharacterResponse:
|
|
return CharacterResponse(
|
|
id=character.id,
|
|
name=character.name,
|
|
display_name=character.display_name,
|
|
status=character.status,
|
|
language=character.language,
|
|
profile=loads_dict(character.profile_json),
|
|
model_policy=loads_dict(character.model_policy_json),
|
|
privacy_level=character.privacy_level,
|
|
owner_scope=character.owner_scope,
|
|
is_system_template=character.is_system_template,
|
|
source_template_id=character.source_template_id,
|
|
created_at=to_iso(character.created_at) or "",
|
|
updated_at=to_iso(character.updated_at) or "",
|
|
)
|
|
|
|
|
|
def character_to_summary(
|
|
character: Character, *, memory_count: int, personality_version: int
|
|
) -> CharacterSummaryResponse:
|
|
"""人物列表项(api-spec §6);memory_count / personality_version 由调用方查得。"""
|
|
return CharacterSummaryResponse(
|
|
id=character.id,
|
|
name=character.name,
|
|
display_name=character.display_name,
|
|
status=character.status,
|
|
memory_count=memory_count,
|
|
personality_version=personality_version,
|
|
activated_at=to_iso(character.activated_at),
|
|
)
|
|
|
|
|
|
|
|
|
|
def run_to_response(run: Run) -> RunResponse:
|
|
return RunResponse(
|
|
id=run.id,
|
|
character_id=run.character_id,
|
|
status=run.status,
|
|
kind=run.kind,
|
|
goal=run.goal,
|
|
origin=run.origin,
|
|
model_hint=run.model_hint,
|
|
started_at=to_iso(run.started_at),
|
|
completed_at=to_iso(run.completed_at),
|
|
error_code=run.error_code,
|
|
error_message=run.error_message,
|
|
created_at=to_iso(run.created_at) or "",
|
|
updated_at=to_iso(run.updated_at) or "",
|
|
)
|
|
|
|
|
|
def event_to_response(event: Event) -> EventResponse:
|
|
payload: dict[str, Any] = loads_dict(event.payload_json)
|
|
return EventResponse(
|
|
event_id=event.id,
|
|
run_id=event.run_id,
|
|
character_id=event.character_id,
|
|
type=event.type,
|
|
sequence=event.sequence,
|
|
visibility=event.visibility,
|
|
payload=payload,
|
|
created_at=to_iso(event.created_at) or "",
|
|
trace_id=event.trace_id,
|
|
)
|
|
|
|
|
|
# —— 司命台治理对象 ——
|
|
|
|
|
|
def goal_to_response(goal: Goal) -> GoalNode:
|
|
return GoalNode(
|
|
id=goal.id,
|
|
character_id=goal.character_id,
|
|
title=goal.title,
|
|
description=goal.description,
|
|
level=goal.level,
|
|
type=goal.type,
|
|
status=goal.status,
|
|
priority=goal.priority,
|
|
progress=goal.progress,
|
|
constraints=loads_list(goal.constraints_json),
|
|
parent_id=goal.parent_id,
|
|
success_criteria=loads_list(goal.success_criteria_json),
|
|
updated_at=to_iso(goal.updated_at),
|
|
)
|
|
|
|
|
|
def perception_channel_to_response(channel: PerceptionChannel) -> PerceptionChannelResponse:
|
|
return PerceptionChannelResponse(
|
|
channel=channel.channel,
|
|
name=channel.name,
|
|
description=channel.description,
|
|
enabled=channel.enabled,
|
|
consent_status=channel.consent_status,
|
|
privacy_level=channel.privacy_level,
|
|
risk_level=channel.risk_level,
|
|
last_active_at=to_iso(channel.last_active_at),
|
|
)
|
|
|
|
|
|
|
|
|
|
def plan_to_response(plan: Plan) -> PlanSummaryResponse:
|
|
return PlanSummaryResponse(
|
|
id=plan.id,
|
|
goal_id=plan.goal_id or "",
|
|
title=plan.title,
|
|
status=plan.status,
|
|
assumptions=loads_list(plan.assumptions_json),
|
|
risks=loads_list(plan.risks_json),
|
|
required_tools=loads_list(plan.required_tools_json),
|
|
success_criteria=loads_list(plan.success_criteria_json),
|
|
step_count=plan.step_count,
|
|
completed_steps=plan.completed_steps,
|
|
updated_at=to_iso(plan.updated_at),
|
|
)
|
|
|
|
|
|
_ACTIVE_WINDOW = timedelta(hours=1)
|
|
|
|
|
|
def agent_to_response(agent: AgentRosterEntry) -> AgentRosterResponse:
|
|
"""presence 为派生呈现态:启用且最近一小时内活跃 → active,否则 idle。"""
|
|
presence = "idle"
|
|
if agent.enabled and agent.last_active_at:
|
|
last = agent.last_active_at
|
|
if last.tzinfo is None:
|
|
last = last.replace(tzinfo=UTC)
|
|
if utc_now() - last <= _ACTIVE_WINDOW:
|
|
presence = "active"
|
|
return AgentRosterResponse(
|
|
role=agent.role,
|
|
name=agent.name,
|
|
responsibility=agent.responsibility,
|
|
enabled=agent.enabled,
|
|
presence=presence,
|
|
last_active_at=to_iso(agent.last_active_at),
|
|
)
|
|
|
|
|
|
def generation_preferences_to_response(
|
|
prefs: GenerationPreferences,
|
|
) -> GenerationPreferencesResponse:
|
|
return GenerationPreferencesResponse(
|
|
default_model_hint=prefs.default_model_hint,
|
|
temperature=prefs.temperature,
|
|
top_p=prefs.top_p,
|
|
max_output_tokens=prefs.max_output_tokens,
|
|
streaming=prefs.streaming,
|
|
)
|
|
|
|
|
|
def audit_to_response(event: AuditEvent) -> AuditEntry:
|
|
summary = event.reason or event.input_summary or event.output_summary or event.action
|
|
return AuditEntry(
|
|
audit_id=event.id,
|
|
pivot=event.pivot or "memory",
|
|
actor=event.actor,
|
|
action=event.action,
|
|
subject=event.resource_id,
|
|
decision=event.decision,
|
|
risk_level=event.risk_level,
|
|
summary=summary,
|
|
trace_id=event.trace_id,
|
|
created_at=to_iso(event.created_at),
|
|
)
|
|
|
|
|
|
def import_job_to_response(job: CharacterImportJob) -> ImportJobResponse:
|
|
return ImportJobResponse(
|
|
id=job.id,
|
|
character_id=job.character_id,
|
|
material_type=job.material_type,
|
|
source_type=job.source_type,
|
|
title=job.title,
|
|
status=job.status,
|
|
format=job.format,
|
|
media_type=job.media_type,
|
|
mime_type=job.mime_type,
|
|
item_count=job.item_count,
|
|
consent_status=job.consent_status,
|
|
usage_scope=job.usage_scope,
|
|
error_code=job.error_code,
|
|
error_message=job.error_message,
|
|
created_at=to_iso(job.created_at) or "",
|
|
updated_at=to_iso(job.updated_at) or "",
|
|
)
|
|
|
|
|
|
def import_item_to_response(item: ImportItem) -> ImportItemResponse:
|
|
return ImportItemResponse(
|
|
id=item.id,
|
|
import_job_id=item.import_job_id,
|
|
item_type=item.item_type,
|
|
role=item.role,
|
|
sequence=item.sequence,
|
|
content=item.content,
|
|
metadata=loads_dict(item.metadata_json),
|
|
created_at=to_iso(item.created_at) or "",
|
|
)
|
|
|
|
|
|
def profile_candidate_to_response(candidate: ProfileCandidate) -> ProfileCandidateResponse:
|
|
return ProfileCandidateResponse(
|
|
id=candidate.id,
|
|
character_id=candidate.character_id,
|
|
import_job_id=candidate.import_job_id,
|
|
field=candidate.field,
|
|
value=candidate.value,
|
|
confidence=candidate.confidence,
|
|
evidence_ids=loads_list(candidate.evidence_ids_json),
|
|
status=candidate.status,
|
|
reason=candidate.reason,
|
|
reviewed_at=to_iso(candidate.reviewed_at),
|
|
created_at=to_iso(candidate.created_at) or "",
|
|
)
|
|
|
|
|
|
|
|
|
|
def memory_candidate_to_response(candidate): # noqa: ANN001 — 旧导入候选(M6 退役)
|
|
from app.schemas.candidates import MemoryCandidateResponse
|
|
|
|
return MemoryCandidateResponse.model_validate(candidate, from_attributes=True)
|
|
|
|
|
|
def profile_candidate_to_response(candidate): # noqa: ANN001 — 旧导入候选(M6 退役)
|
|
from app.schemas.candidates import ProfileCandidateResponse
|
|
|
|
return ProfileCandidateResponse.model_validate(candidate, from_attributes=True)
|