142 lines
4.8 KiB
Python
142 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
|
|
)
|
|
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)
|