- 薄自建 LLM 网关:OpenAI 兼容适配器(DeepSeek) + instructor 结构化输出 + usage_ledger 记账 + 档位路由 - 记忆服务 assemble:确定性选择(显式+主角+近况) + 渲染卡 + 缓存断点(中性文本) - LangGraph 写章节点 + Postgres checkpointer + SSE 归一(token/done/error) - API:立项 + 写章 draft(SSE) + PUT 自动保存 + 提供商凭据(Fernet 加密/测试连接) - 前端:AppShell + 作品库 + 5 步立项向导 + 写作工作台(流式打字机+自动保存) + 设置页 - M1 E2E:真实 DB + mock 网关零 token 走通闭环
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
"""用量记账落库(ARCH §4.8)。
|
||
|
||
`LedgerSink` 为接口,便于测试注入内存替身;生产用 SQLAlchemy 实现写 usage_ledger。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Protocol
|
||
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
from ww_db.models import UsageLedger
|
||
|
||
from .types import Scope, Usage
|
||
|
||
|
||
class LedgerSink(Protocol):
|
||
async def record(self, scope: Scope, usage: Usage) -> None: ...
|
||
|
||
|
||
class SqlAlchemyLedgerSink:
|
||
"""把每次调用写入 usage_ledger(owner_id 取 scope.user_id,单用户 stub)。"""
|
||
|
||
def __init__(self, session: AsyncSession) -> None:
|
||
self._session = session
|
||
|
||
async def record(self, scope: Scope, usage: Usage) -> None:
|
||
row = UsageLedger(
|
||
owner_id=scope.user_id,
|
||
project_id=scope.project_id,
|
||
provider=usage.provider,
|
||
model=usage.model,
|
||
input_tokens=usage.input_tokens,
|
||
output_tokens=usage.output_tokens,
|
||
cache_read=usage.cache_read_tokens,
|
||
cost_minor=usage.cost_minor,
|
||
currency=usage.currency,
|
||
)
|
||
self._session.add(row)
|
||
await self._session.flush()
|