feat(logs): 实时日志展开显示单笔 token 用量与缓存命中率

- calllog 新增 peek_tokens(rid):只读不消耗,供实时日志展示(落库仍由 _row pop)
- AccessLog.add 把 token 挂到条目 tokens 字段
- 实时日志展开新增「Token 用量」块:输入/输出/缓存命中/推理 + 命中率(cached÷prompt)
- 调用日志详情弹窗补同样一行 token(数据取自 tok_* 列)
- 版本 2.9.3 → 2.9.4
This commit is contained in:
jidekin committed 2026-10-23 15:19:39 +08:00
1 parent b54dc303f9
commit 6457a92314
8 files changed
+90 -1

No files matched your search

+11
View File
@@ -618,6 +618,17 @@ def note_tokens(rid: str, prompt: int, completion: int,
break
def peek_tokens(rid: str) -> dict[str, int] | None:
"""按 rid 偷看 token 用量(**不消耗**)—— 实时日志页展开时展示用。
真正落库时由 `_row()` 负责 pop 清空,这里只读,避免两边抢同一份数据。
"""
if not store.enabled or not rid:
return None
with _tok_lock:
return TOKENS_BY_RID.get(str(rid))
def on_access(entry: dict[str, Any]) -> None:
"""`AccessLog.add()` 调用:拿到访问日志条目;若响应元数据已就绪则立即落库。"""
if not store.enabled:
+1 -1
View File
@@ -355,7 +355,7 @@ _docs_on = config.server.docs_enabled # 安全默认关(report: /docs、/ope
# 2026-09-30: 2.6.1 → 2.7.0(按老板要求整个删除「火山方舟 Coding Plan」供应商:配置/模型/代码/文档/前端下拉/环境变量全清)。
# 2026-09-30: 2.7.0 → 2.7.1(界面提示白话化:熔断器→供应商自动保护、引擎→运行环境、slug→标识;删 120 个未引用死键)。
# ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。
VERSION = "2.9.3"
VERSION = "2.9.4"
app = FastAPI(
title="API Gateway",
+9
View File
@@ -273,6 +273,15 @@ class AccessLog:
snip = BODY_BY_RID.pop(rid, None)
if snip:
entry["body"] = snip
# 2026-10-23:把这次请求的 token 用量挂到日志条目上(实时页展开即可看到
# prompt/completion/cached/reasoning 与缓存命中率)。
try:
from app import calllog as _calllog
_tk = _calllog.peek_tokens(str(rid))
if _tk:
entry["tokens"] = dict(_tk)
except Exception: # noqa: BLE001
pass
with self._lock:
self._buf.append(entry)
# 调用日志落地(2026-10-23):交给 app/calllog.py —— 它与请求中间件的响应元数据