- 神识 Go:store 双实现(PG 自动建表+60s 就绪重试 / 内存回退)承载 usage_logs·byok_keys·quota_balances;BYOK 密钥库(AES-GCM 加密、API 脱敏、撤回即失效、主密钥未配置 fail-closed);配额三段式(预扣 max_tokens 钳位 1M→结算实际 token→全败幂等退款,HJ-2004);计量归属(IR user_id→usage_logs,/me/usage 用户维度聚合+配额总览,/admin/usage/sample 对账抽样);Anthropic/Gemini 出站适配器(SSE 解析、system 提升、用量映射);stub 端点补名修复计量空 endpoint - IR 契约:user_id/byok_key_id 双端同步(Go types.go + Py types.py) - 灵台:runs.owner_id(迁移 005,server_default 兼容存量)经 kernel 全链透传为 IR user_id;shenshi_client 扩展用户面方法(X-Hunjing-User-Id 代理,Echo 桩带内存实现);/me/byok-keys 登记·列表·撤回与 /me/usage 端点(4 用例) - 前端神识阁(/models 重建):偏好(生成参数表单)/自带密钥(登记加密保存、脱敏列表、撤回二次确认)/用量(聚合表+配额进度条)三区;me API 库+mock+5 用例;侧栏恢复入口 - 配置与部署:.env.example 增 SHENSHI_DATABASE_URL/SHENSHI_BYOK_MASTER_KEY/SHENSHI_USER_BUDGET_TOKENS;compose 神识容器接自有库(init-shenshi.sql 预建 hunjing_shenshi,与灵台不共库共表) - 证据:Go 34 用例(BYOK 独占/配额拒绝/结算退款/归属聚合)、后端 532、前端 210、BFF 4 全绿;跨进程 E2E 四场景(灵台 HttpShenshiClient→真 Go 神识:登记脱敏/BYOK 独占不可达整体失败/用量归属聚合/撤回即失效)通过;rewrite-plan M4 标记完成、能力矩阵神识行升已验证
143 lines
4.8 KiB
Python
143 lines
4.8 KiB
Python
"""灵胚 API(镜生域,rewrite/1.0 M1)。对应 api-contracts.md §3.2。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
from pydantic import BaseModel, Field
|
|
|
|
from app.api.v1.deps import get_current_user
|
|
from app.domains import jingsheng
|
|
from app.kernel import Kernel
|
|
from app.schemas.auth import CurrentUser
|
|
|
|
router = APIRouter(prefix="/embryo-drafts", tags=["jingsheng"])
|
|
|
|
|
|
class EmbryoCreate(BaseModel):
|
|
source: str = Field(default="original", description="original/adopted/imported/serendipity")
|
|
name: str = Field(min_length=1, max_length=120)
|
|
description: str | None = Field(default=None, max_length=8000)
|
|
identity_baseline: dict = Field(default_factory=dict, description="道体锚点草稿")
|
|
|
|
|
|
class EmbryoPatch(BaseModel):
|
|
name: str | None = Field(default=None, min_length=1, max_length=120)
|
|
description: str | None = Field(default=None, max_length=8000)
|
|
identity_baseline: dict | None = None
|
|
|
|
|
|
class RehearseRequest(BaseModel):
|
|
prompt: str = Field(min_length=1, max_length=8000)
|
|
|
|
|
|
def _embryo_view(embryo) -> dict: # noqa: ANN001 — ORM 行
|
|
return {
|
|
"id": embryo.id,
|
|
"status": embryo.status,
|
|
"source": embryo.source,
|
|
"name": embryo.name,
|
|
"description": embryo.description,
|
|
"identity_baseline": json.loads(embryo.identity_baseline_json or "{}"),
|
|
"rehearsals": json.loads(embryo.rehearsals_json or "[]"),
|
|
"created_at": embryo.created_at.isoformat(),
|
|
"updated_at": embryo.updated_at.isoformat(),
|
|
}
|
|
|
|
|
|
@router.get("")
|
|
async def list_embryo_drafts(user: CurrentUser = Depends(get_current_user)) -> list[dict]:
|
|
return [_embryo_view(e) for e in await jingsheng.list_embryos(user.owner_scope)]
|
|
|
|
|
|
@router.post("", status_code=201)
|
|
async def create_embryo_draft(
|
|
payload: EmbryoCreate, user: CurrentUser = Depends(get_current_user)
|
|
) -> dict:
|
|
embryo = await jingsheng.create_embryo(
|
|
owner_scope=user.owner_scope,
|
|
source=payload.source,
|
|
name=payload.name,
|
|
description=payload.description,
|
|
identity_baseline=payload.identity_baseline,
|
|
)
|
|
return _embryo_view(embryo)
|
|
|
|
|
|
@router.get("/{embryo_id}")
|
|
async def get_embryo_draft(
|
|
embryo_id: str, user: CurrentUser = Depends(get_current_user)
|
|
) -> dict:
|
|
return _embryo_view(await jingsheng.get_embryo(embryo_id, user.owner_scope))
|
|
|
|
|
|
@router.patch("/{embryo_id}")
|
|
async def patch_embryo_draft(
|
|
embryo_id: str, payload: EmbryoPatch, user: CurrentUser = Depends(get_current_user)
|
|
) -> dict:
|
|
embryo = await jingsheng.patch_embryo(
|
|
embryo_id,
|
|
user.owner_scope,
|
|
name=payload.name,
|
|
description=payload.description,
|
|
identity_baseline=payload.identity_baseline,
|
|
)
|
|
return _embryo_view(embryo)
|
|
|
|
|
|
@router.post("/{embryo_id}/rehearse")
|
|
async def rehearse_embryo(
|
|
embryo_id: str,
|
|
payload: RehearseRequest,
|
|
request: Request,
|
|
user: CurrentUser = Depends(get_current_user),
|
|
) -> dict:
|
|
"""照心试演:幻境 Run,不产生人物经历;事件流经 /runs/{run_id}/stream。"""
|
|
embryo = await jingsheng.get_embryo(embryo_id, user.owner_scope)
|
|
if embryo.status == "activated":
|
|
from app.core.errors import AppError
|
|
|
|
raise AppError("HJ-1103", "灵胚已启镜,无需再试演。", status_code=409)
|
|
kernel: Kernel | None = getattr(request.app.state, "kernel", None)
|
|
if kernel is None:
|
|
from app.core.errors import AppError
|
|
|
|
raise AppError("service_unavailable", "Kernel 运行时未就绪。", status_code=503)
|
|
|
|
baseline = json.loads(embryo.identity_baseline_json or "{}")
|
|
system_contract = (
|
|
f"你是一位尚未降生的灵胚「{embryo.name}」,正在幻境中试演。"
|
|
f"身份锚点:{json.dumps(baseline, ensure_ascii=False)}"
|
|
)
|
|
run = await kernel.start_rehearsal_run(
|
|
embryo_id=embryo_id, prompt=payload.prompt, system_contract=system_contract,
|
|
owner_id=user.owner_scope,
|
|
)
|
|
await jingsheng.record_rehearsal(
|
|
embryo_id, user.owner_scope, run_id=run.id, prompt=payload.prompt
|
|
)
|
|
return {"run_id": run.id, "kind": "rehearsal"}
|
|
|
|
|
|
@router.post("/{embryo_id}/activate")
|
|
async def activate_embryo_draft(
|
|
embryo_id: str, user: CurrentUser = Depends(get_current_user)
|
|
) -> dict:
|
|
"""启镜:签发正式人物 ID、魂籍、执镜关系与命台。"""
|
|
character = await jingsheng.activate_embryo(embryo_id, user.owner_scope)
|
|
return {
|
|
"character_id": character.id,
|
|
"name": character.name,
|
|
"status": character.status,
|
|
"source": character.source,
|
|
"identity_record_version": 1,
|
|
}
|
|
|
|
|
|
@router.post("/{embryo_id}/abandon", status_code=204)
|
|
async def abandon_embryo_draft(
|
|
embryo_id: str, user: CurrentUser = Depends(get_current_user)
|
|
) -> None:
|
|
await jingsheng.abandon_embryo(embryo_id, user.owner_scope)
|