Files
chenxin 144415247e M4 收口:神识用户面与计量闭环——BYOK/配额三段式/usage 落库/Anthropic+Gemini 适配器/神识阁
- 神识 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 标记完成、能力矩阵神识行升已验证
2026-08-27 18:39:17 +08:00

63 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""运行时 Run/Event 模型。"""
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.core.ids import new_event_id, new_run_id
from app.core.time import utc_now
from app.db.base import Base
class Run(Base):
__tablename__ = "runs"
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=new_run_id)
# rewrite/1.0:照心试演(kind=rehearsal)无正式人物,character_id 允许为空;
# 旧 chat_sessions 外键随线性会话模型退役移除
character_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("characters.id"), index=True, nullable=True
)
# Run 归属用户(用量计量与配额裁决维度,透传神识 user_id;空 = 平台调用)
owner_id: Mapped[str] = mapped_column(String(80), nullable=False, default="", index=True)
status: Mapped[str] = mapped_column(String(40), nullable=False, default="queued", index=True)
# run.kind:foreground / background_task / awakening / rehearsal(契约词典 §8.2)
kind: Mapped[str] = mapped_column(
String(40), nullable=False, default="foreground", server_default="foreground"
)
goal: Mapped[str | None] = mapped_column(String(300))
origin: Mapped[str] = mapped_column(String(40), nullable=False, default="chat")
model_hint: Mapped[str | None] = mapped_column(String(120))
started_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
error_code: Mapped[str | None] = mapped_column(String(80))
error_message: Mapped[str | None] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=utc_now, onupdate=utc_now
)
class Event(Base):
__tablename__ = "events"
__table_args__ = (UniqueConstraint("run_id", "sequence", name="uq_events_run_sequence"),)
id: Mapped[str] = mapped_column(String(80), primary_key=True, default=new_event_id)
character_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("characters.id"), index=True, nullable=True
)
run_id: Mapped[str | None] = mapped_column(
String(80), ForeignKey("runs.id"), index=True, nullable=True
)
type: Mapped[str] = mapped_column(String(80), nullable=False, index=True)
source: Mapped[str] = mapped_column(String(40), nullable=False, default="system")
payload_json: Mapped[str] = mapped_column(Text, nullable=False, default="{}")
visibility: Mapped[str] = mapped_column(String(40), nullable=False, default="public")
sensitivity: Mapped[str] = mapped_column(String(40), nullable=False, default="low")
trace_id: Mapped[str | None] = mapped_column(String(80), index=True)
parent_id: Mapped[str | None] = mapped_column(String(80))
sequence: Mapped[int] = mapped_column(Integer, nullable=False)
evidence_ids_json: Mapped[str] = mapped_column(Text, nullable=False, default="[]")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now)