feat: 采集 LLM token 用量并上报 lpt-be

- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加
- 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结
- 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试
- 面板补充 Token/费用/调用次数与计价快照
- 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
This commit is contained in:
2026-10-10 18:46:01 +08:00
parent fd1193a66f
commit 935f4781a4
24 changed files with 2632 additions and 65 deletions
+113 -20
View File
@@ -4,7 +4,8 @@
import type { FastifyInstance } from "fastify";
import { logStore } from "./store.js";
import { checkToken, configuredToken } from "../utils/auth.js";
import { checkToken, configuredToken, queryTokenMatches, TOKEN_COOKIE } from "../utils/auth.js";
import { fetchUpstreamBalance } from "./balance.js";
const PANEL_HTML = `<!DOCTYPE html>
<html lang="zh-CN">
@@ -36,6 +37,8 @@ main{padding:14px 24px 40px;max-width:1400px;margin:0 auto}
.log-table .row-expand{max-width:20px;text-align:center;cursor:pointer;user-select:none;color:#6b8a6b;font-weight:700}
.log-table .status-badge{padding:2px 8px;border-radius:12px;font-size:11px;font-weight:600}
.status-ok{background:#e8f5e9;color:#2e7d32}.status-err{background:#ffebee;color:#c62828}
.muted{color:#9aa89a;font-size:12px}
.unreported{color:#ef6c00;font-size:12px}
.detail-row td{padding:0;border-bottom:2px solid #c8e6c9}
.detail-card{margin:8px 14px 12px;padding:12px 16px;background:#fafdfa;border-radius:8px;border:1px solid #dce8dc;max-height:460px;overflow-y:auto}
.detail-card h4{font-size:13px;color:#4a6a4a;margin-bottom:6px}
@@ -60,9 +63,17 @@ main{padding:14px 24px 40px;max-width:1400px;margin:0 auto}
<select id="filterEndpoint"><option value="">全部端点</option></select>
<button id="btnRefresh" onclick="loadLogs()">刷新</button>
<button class="danger" id="btnClear" onclick="clearLogs()">清除全部</button>
<span id="balanceBar" class="muted" style="margin-left:auto"></span>
</div>
<table class="log-table" id="logTable"><tbody></tbody></table>
<table class="log-table" id="logTable">
<thead><tr>
<th></th><th>时间</th><th>端点</th><th>状态</th>
<th>Token 输入/输出</th><th>缓存命中</th><th>费用(USD)</th>
<th>耗时</th><th>调用次数</th><th>模型</th>
</tr></thead>
<tbody></tbody>
</table>
<div class="empty" id="empty">暂无请求记录</div>
</main>
@@ -82,10 +93,16 @@ async function loadLogs() {
const { logs, stats } = await fetchJSON(url);
// stats
const hitRate = stats.cacheHitRate == null ? '—' : (stats.cacheHitRate*100).toFixed(1)+'%';
$('#statsBar').innerHTML = [
{ label:'总请求', value:stats.total, cls:'neutral' },
{ label:'LLM 调用', value:stats.llmCalls, cls:'neutral' },
{ label:'成功', value:stats.ok, cls:'ok' },
{ label:'失败', value:stats.err, cls:'err' },
{ label:'总 Token', value:formatNumber(stats.totalTokens), cls:'neutral' },
{ label:'缓存命中率', value:hitRate, cls:'neutral' },
{ label:'估算费用', value:'$'+stats.costUsd.toFixed(6), cls:'neutral' },
{ label:'未上报用量', value:stats.unreportedUsageCalls, cls:(stats.unreportedUsageCalls>0?'err':'neutral') },
{ label:'平均耗时', value:stats.avgMs+'ms', cls:(stats.avgMs>60000?'err':'neutral') },
].map(s=>'<div class="stat-card"><div class="label">'+s.label+'</div><div class="value '+s.cls+'">'+s.value+'</div></div>').join('');
@@ -104,15 +121,27 @@ async function loadLogs() {
$('#empty').classList.add('hidden');
tbody.innerHTML = logs.map(l => {
const statusCls = l.status >= 200 && l.status < 300 ? 'status-ok' : 'status-err';
const u = l.usage;
const tokens = !l.isLlmCall ? '<span class="muted">不适用</span>'
: (u && u.promptTokens != null
? formatNumber(u.promptTokens)+' / '+formatNumber(u.completionTokens)
: '<span class="unreported">未上报</span>');
const cache = !l.isLlmCall ? '<span class="muted">—</span>'
: (u && u.cacheHitTokens != null ? formatNumber(u.cacheHitTokens) : '<span class="unreported">未上报</span>');
const cost = l.costUsd == null ? '<span class="unreported">无法计价</span>'
: '$'+l.costUsd.toFixed(6)+(l.pricing && l.pricing.peak ? '' : ' <span class="muted">谷</span>');
return '<tr class="log-row" data-id="'+l.id+'">'+
'<td class="row-expand" onclick="toggleDetail('+l.id+',this)">▸</td>'+
'<td>'+escapeHtml(l.timestamp.replace('T',' ').slice(0,19))+'</td>'+
'<td>'+escapeHtml(l.endpoint)+'</td>'+
'<td><span class="status-badge '+statusCls+'">'+l.status+'</span></td>'+
'<td>'+(l.error ? '<span style="color:#c62828;font-size:12px">'+escapeHtml(l.error.slice(0,40))+'</span>' : '—')+'</td>'+
'<td>'+l.durationMs+'ms</td>'+
'<td style="font-size:12px;color:#6b8a6b">'+escapeHtml(l.model)+'</td>'+
'</tr><tr class="detail-row hidden" id="detail-'+l.id+'"><td colspan="7"><div class="detail-card"><span style="color:#8a9a8a;font-size:13px">加载中…</span></div></td></tr>';
'<td><span class="status-badge '+statusCls+'"'+(l.error ? ' title="'+escapeHtml(l.error)+'"' : '')+'>'+l.status+'</span></td>'+
'<td>'+tokens+'</td>'+
'<td>'+cache+'</td>'+
'<td>'+cost+'</td>'+
'<td>'+(l.durationMs)+'ms</td>'+
'<td>'+(l.attempts == null ? '—' : l.attempts)+'</td>'+
'<td style="font-size:12px;color:#6b8a6b">'+escapeHtml(l.model || '—')+'</td>'+
'</tr><tr class="detail-row hidden" id="detail-'+l.id+'"><td colspan="10"><div class="detail-card"><span style="color:#8a9a8a;font-size:13px">加载中…</span></div></td></tr>';
}).join('');
}
}
@@ -153,22 +182,26 @@ async function toggleDetail(id, el) {
// 构建复制的纯文本
const buildCopyText = () => {
let txt = '=== lpt-ai 请求日志 ===\n';
txt += '端点: '+(log.endpoint||'')+'\n';
txt += '模型: '+(log.model||'')+'\n';
txt += '耗时: '+log.durationMs+'ms\n';
txt += '状态: '+log.status+'\n';
if (paramsStr) txt += '参数: '+paramsStr+'\n';
let txt = '=== lpt-ai 请求日志 ===\\n';
txt += '端点: '+(log.endpoint||'')+'\\n';
txt += '模型: '+(log.model||'')+'\\n';
txt += '耗时: '+log.durationMs+'ms\\n';
txt += '状态: '+log.status+'\\n';
if (log.attempts != null) txt += '调用次数: '+log.attempts+'\\n';
if (log.usage) txt += '用量: '+JSON.stringify(log.usage)+'\\n';
if (log.usageSource) txt += '用量来源: '+log.usageSource+'\\n';
if (log.pricing) txt += '计价: '+(log.pricing.peak?'峰时':'谷时')+' 版本 '+log.pricing.version+' 费用 '+(log.costUsd==null?'无法计价':'$'+log.costUsd)+'\\n';
if (paramsStr) txt += '参数: '+paramsStr+'\\n';
if (log.messages) {
log.messages.forEach((m,i) => {
txt += '\n--- '+(m.role==='system'?'System Prompt':'User Message')+' ---\n';
txt += '\\n--- '+(m.role==='system'?'System Prompt':'User Message')+' ---\\n';
txt += m.content;
});
}
txt += '\n\n--- 原始请求体 ---\n';
txt += '\\n\\n--- 原始请求体 ---\\n';
txt += reqStr;
txt += '\n\n--- 响应体 ---\n';
if (log.error) txt += '错误: '+log.error+'\n';
txt += '\\n\\n--- 响应体 ---\\n';
if (log.error) txt += '错误: '+log.error+'\\n';
txt += respStr;
return txt;
};
@@ -176,7 +209,7 @@ async function toggleDetail(id, el) {
let html = '';
// 复制按钮
html += '<div style="display:flex;gap:6px;margin-bottom:10px">'+
'<button class="copy-btn" onclick="copyLog(\''+copyId+'\',this)">📋 复制到剪贴板</button>'+
'<button class="copy-btn" onclick="copyLog(\\''+copyId+'\\',this)">📋 复制到剪贴板</button>'+
'<textarea id="'+copyId+'" style="position:absolute;left:-9999px">'+escapeHtml(buildCopyText())+'</textarea>'+
'</div>';
// prompt
@@ -188,8 +221,26 @@ async function toggleDetail(id, el) {
if (paramsStr) {
html += '<div><h4>⚙️ 调用参数</h4><pre style="background:#fff3e0;border-color:#ffe0b2;font-size:12px">'+escapeHtml(paramsStr)+'</pre></div>';
}
html += '<div><h4>⏱️ 耗时</h4><pre style="font-size:12px">'+log.durationMs+'ms</pre></div>';
html += '<div><h4>⏱️ 耗时</h4><pre style="font-size:12px">'+log.durationMs+'ms'+(log.attempts != null ? '('+log.attempts+' 次调用)' : '')+'</pre></div>';
html += '</div>';
// 用量与计价:必须标清「未上报」与「无法计价」,不能用 0 冒充
html += '<div style="margin-bottom:12px"><h4>🔢 Token 用量</h4>';
if (log.isLlmCall === false) {
html += '<pre style="font-size:12px">本次请求不过 LLM,无用量</pre>';
} else if (!log.usage || log.usage.promptTokens == null) {
html += '<pre style="background:#fff3e0;border-color:#ffe0b2;font-size:12px">上游未上报用量(usageSource: '+escapeHtml(log.usageSource||'missing')+')</pre>';
} else {
html += '<pre style="font-size:12px">'+escapeHtml(JSON.stringify(log.usage, null, 2))+'</pre>';
}
html += '</div>';
if (log.pricing) {
html += '<div style="margin-bottom:12px"><h4>💰 计价快照</h4><pre style="background:#e8f5e9;border-color:#c8e6c9;font-size:12px">'+escapeHtml(
'版本: '+log.pricing.version+'\\n'+
'时段: '+(log.pricing.peak ? '峰时' : '谷时')+'\\n'+
'费用: '+(log.costUsd == null ? '无法计价(用量未上报)' : '$'+log.costUsd.toFixed(8))+'\\n'+
'生效单价/1M: 命中 '+log.pricing.effectivePrices.cacheHitPerM+' | 未命中 '+log.pricing.effectivePrices.cacheMissPerM+' | 输出 '+log.pricing.effectivePrices.outputPerM
)+'</pre></div>';
}
html += '<div class="side">';
html += '<div><h4>📤 原始请求体</h4><pre>'+escapeHtml(reqStr)+'</pre></div>';
html += '<div><h4>📥 响应体</h4><pre>'+errorHtml+escapeHtml(respStr)+'</pre></div>';
@@ -209,6 +260,12 @@ function escapeHtml(s) {
return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
}
/** 千位分隔;null/undefined 统一显示为 0,调用方负责区分「未上报」 */
function formatNumber(value) {
if (value == null) return '0';
return Number(value).toLocaleString('en-US');
}
function copyLog(id, btn) {
const ta = document.getElementById(id);
if (!ta) return;
@@ -230,6 +287,14 @@ async function init() {
const h = await fetchJSON('/health');
$('#serverInfo').textContent = '模型: '+h.model+' | API: '+(h.llmAvailable?'已配置':'未配置');
} catch(e) {}
try {
const b = await fetchJSON('/admin/usage/balance');
$('#balanceBar').textContent = b.configured
? '上游余额: '+b.currency+' '+b.totalBalance+'(赠送 '+b.grantedBalance+' / 充值 '+b.toppedUpBalance+')'
: '上游余额: 未配置或查询失败';
} catch(e) {
$('#balanceBar').textContent = '上游余额: 查询失败';
}
await loadLogs();
}
init();
@@ -253,7 +318,15 @@ export async function adminRoutes(app: FastifyInstance): Promise<void> {
});
// —— 面板页面 ——
app.get("/admin", async (_request, reply) => {
app.get<{ Querystring: { token?: string } }>("/admin", async (request, reply) => {
// 用 `?token=` 打开面板时换发一个 HttpOnly cookie:
// 面板 HTML 与它后续的 fetch 是两个独立请求,只在 URL 上带令牌会让面板永远空白
if (queryTokenMatches(request)) {
reply.header(
"set-cookie",
`${TOKEN_COOKIE}=${encodeURIComponent(configuredToken())}; Path=/admin; HttpOnly; SameSite=Strict`,
);
}
return reply.type("text/html; charset=utf-8").send(PANEL_HTML);
});
@@ -261,6 +334,13 @@ export async function adminRoutes(app: FastifyInstance): Promise<void> {
app.get<{ Querystring: { endpoint?: string } }>("/admin/logs", async (request) => {
const endpoint = request.query.endpoint || undefined;
const summaries = logStore.list(endpoint);
// 用量统计只算真正消耗 LLM 的记录;/fetch-title 这类请求混进来会把分母撑大
const llm = summaries.filter((s) => s.isLlmCall);
const sum = (pick: (s: (typeof llm)[number]) => number | null | undefined): number =>
llm.reduce((total, item) => total + (pick(item) ?? 0), 0);
const cacheHitTokens = sum((s) => s.usage?.cacheHitTokens);
const cacheMissTokens = sum((s) => s.usage?.cacheMissTokens);
const cacheTotal = cacheHitTokens + cacheMissTokens;
const stats = {
total: summaries.length,
ok: summaries.filter((s) => s.status >= 200 && s.status < 300).length,
@@ -269,10 +349,23 @@ export async function adminRoutes(app: FastifyInstance): Promise<void> {
? Math.round(summaries.reduce((a, s) => a + s.durationMs, 0) / summaries.length)
: 0,
endpoints: logStore.endpoints(),
llmCalls: llm.length,
promptTokens: sum((s) => s.usage?.promptTokens),
completionTokens: sum((s) => s.usage?.completionTokens),
totalTokens: sum((s) => s.usage?.totalTokens),
cacheHitTokens,
cacheMissTokens,
// 分母为 0 时返回 null,面板显示「—」而不是 0%
cacheHitRate: cacheTotal > 0 ? cacheHitTokens / cacheTotal : null,
costUsd: llm.reduce((total, item) => total + (item.costUsd ?? 0), 0),
unreportedUsageCalls: llm.filter((s) => s.usage?.promptTokens == null).length,
};
return { logs: summaries, stats };
});
// —— 上游余额(对账)——
app.get("/admin/usage/balance", async () => fetchUpstreamBalance());
// —— 日志详情 ——
app.get<{ Params: { id: string } }>("/admin/logs/:id", async (request, reply) => {
const id = parseInt(request.params.id, 10);