- 客户端解析上游 usage(含 DeepSeek 缓存命中/未命中),跨重试累加 - 新增计价表(三档单价 + 峰谷时段),费用在结算时冻结 - 新增用量上报与本地 outbox 重试;4xx 判定为确定性拒绝,不无限重试 - 面板补充 Token/费用/调用次数与计价快照 - 顺带修复面板内联脚本转义错误(模板字面量吃掉 \n 与 \',面板一直空白)
388 lines
18 KiB
TypeScript
388 lines
18 KiB
TypeScript
/**
|
||
* 管理员路由:日志查询 API + 自包含 HTML 面板。
|
||
*/
|
||
|
||
import type { FastifyInstance } from "fastify";
|
||
import { logStore } from "./store.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">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>lpt-ai · 管理面板</title>
|
||
<style>
|
||
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
|
||
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;background:#f5f8f5;color:#2e3a2e;min-height:100vh}
|
||
header{background:linear-gradient(135deg,#2e7d32,#43a047);color:#fff;padding:16px 24px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:10px}
|
||
header h1{font-size:20px;font-weight:600}
|
||
header .sub{font-size:13px;opacity:.85}
|
||
main{padding:14px 24px 40px;max-width:1400px;margin:0 auto}
|
||
.stats-bar{display:flex;gap:16px;flex-wrap:wrap;margin-bottom:16px}
|
||
.stat-card{flex:1;min-width:130px;background:#fff;border-radius:10px;padding:14px 16px;border:1px solid #dce8dc;box-shadow:0 1px 3px rgba(0,0,0,.04)}
|
||
.stat-card .label{font-size:11px;text-transform:uppercase;color:#6b8a6b;letter-spacing:.4px;margin-bottom:4px}
|
||
.stat-card .value{font-size:24px;font-weight:700}
|
||
.stat-card .value.ok{color:#2e7d32}.stat-card .value.err{color:#c62828}.stat-card .value.neutral{color:#546e7a}
|
||
.toolbar{display:flex;gap:10px;align-items:center;flex-wrap:wrap;margin-bottom:14px}
|
||
.toolbar select,.toolbar button{padding:6px 14px;border-radius:6px;border:1px solid #c8d8c8;background:#fff;font-size:13px;cursor:pointer}
|
||
.toolbar button.danger{color:#c62828;border-color:#ef9a9a}
|
||
.toolbar button.danger:hover{background:#ffebee}
|
||
.toolbar button:hover{background:#f0f4f0}
|
||
.log-table{width:100%;border-collapse:collapse;background:#fff;border-radius:10px;overflow:hidden;border:1px solid #dce8dc;box-shadow:0 1px 3px rgba(0,0,0,.04)}
|
||
.log-table th,.log-table td{padding:10px 14px;text-align:left;font-size:13px;border-bottom:1px solid #eef3ee}
|
||
.log-table th{background:#f7faf7;color:#4a6a4a;font-weight:600;font-size:12px;text-transform:uppercase;letter-spacing:.3px}
|
||
.log-table tr:hover td{background:#f4faf5}
|
||
.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}
|
||
.detail-card pre{white-space:pre-wrap;word-break:break-word;font-size:12px;line-height:1.6;color:#3a4a3a;background:#fff;padding:10px 12px;border-radius:6px;border:1px solid #e8ede8;max-height:200px;overflow-y:auto}
|
||
.detail-card .side{display:grid;grid-template-columns:1fr 1fr;gap:10px}
|
||
@media(max-width:700px){.detail-card .side{grid-template-columns:1fr}}
|
||
.empty{text-align:center;padding:48px 20px;color:#8a9a8a;font-size:14px}
|
||
.hidden{display:none}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
|
||
<header>
|
||
<div><h1>lpt-ai 管理面板</h1><span class="sub">请求日志 & 调试</span></div>
|
||
<span style="font-size:12px;opacity:.7" id="serverInfo"></span>
|
||
</header>
|
||
|
||
<main>
|
||
<div class="stats-bar" id="statsBar"></div>
|
||
|
||
<div class="toolbar">
|
||
<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">
|
||
<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>
|
||
|
||
<script>
|
||
const $ = (s) => document.querySelector(s);
|
||
const $$ = (s) => document.querySelectorAll(s);
|
||
|
||
async function fetchJSON(url, opts) {
|
||
const res = await fetch(url, opts);
|
||
if (!res.ok) throw new Error(res.status+' '+res.statusText);
|
||
return res.json();
|
||
}
|
||
|
||
async function loadLogs() {
|
||
const ep = $('#filterEndpoint').value;
|
||
const url = ep ? '/admin/logs?endpoint='+encodeURIComponent(ep) : '/admin/logs';
|
||
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('');
|
||
|
||
// endpoints dropdown
|
||
const sel = $('#filterEndpoint');
|
||
const cur = sel.value;
|
||
sel.innerHTML = '<option value="">全部端点</option>' + stats.endpoints.map(e=>'<option value="'+e+'"'+(e===cur?' selected':'')+'>'+e+'</option>').join('');
|
||
sel.value = cur;
|
||
|
||
// table
|
||
const tbody = $('#logTable tbody');
|
||
if (logs.length === 0) {
|
||
tbody.innerHTML = '';
|
||
$('#empty').classList.remove('hidden');
|
||
} else {
|
||
$('#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.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('');
|
||
}
|
||
}
|
||
|
||
let loadedDetails = {};
|
||
async function toggleDetail(id, el) {
|
||
const row = $('#detail-'+id);
|
||
if (!row.classList.contains('hidden')) {
|
||
row.classList.add('hidden');
|
||
el.textContent = '▸';
|
||
return;
|
||
}
|
||
row.classList.remove('hidden');
|
||
el.textContent = '▾';
|
||
if (loadedDetails[id]) return;
|
||
|
||
const log = await fetchJSON('/admin/logs/'+id);
|
||
loadedDetails[id] = true;
|
||
const reqStr = JSON.stringify(log.requestBody ?? {}, null, 2);
|
||
const respStr = typeof log.responseBody === 'string' ? log.responseBody : JSON.stringify(log.responseBody ?? {}, null, 2);
|
||
const paramsStr = log.params ? JSON.stringify(log.params, null, 2) : null;
|
||
const errorHtml = log.error ? '<span style="color:#c62828">'+escapeHtml(log.error)+'</span><br><br>' : '';
|
||
const copyId = 'copy-'+id;
|
||
|
||
// 渲染 prompt messages
|
||
const renderMessages = (msgs) => {
|
||
if (!msgs || msgs.length === 0) return '';
|
||
return msgs.map((m,i) => {
|
||
const label = m.role === 'system' ? '🤖 System Prompt' : '👤 User Message';
|
||
const bg = m.role === 'system' ? '#e3f2fd' : '#f5f5f5';
|
||
const border = m.role === 'system' ? '#90caf9' : '#e0e0e0';
|
||
return '<div style="margin-bottom:8px">'+
|
||
'<span style="font-size:11px;font-weight:600;color:'+(m.role==='system'?'#1565c0':'#616161')+'">'+label+'</span>'+
|
||
'<pre style="background:'+bg+';border-color:'+border+';max-height:160px">'+escapeHtml(m.content)+'</pre>'+
|
||
'</div>';
|
||
}).join('');
|
||
};
|
||
|
||
// 构建复制的纯文本
|
||
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 (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 += m.content;
|
||
});
|
||
}
|
||
txt += '\\n\\n--- 原始请求体 ---\\n';
|
||
txt += reqStr;
|
||
txt += '\\n\\n--- 响应体 ---\\n';
|
||
if (log.error) txt += '错误: '+log.error+'\\n';
|
||
txt += respStr;
|
||
return txt;
|
||
};
|
||
|
||
let html = '';
|
||
// 复制按钮
|
||
html += '<div style="display:flex;gap:6px;margin-bottom:10px">'+
|
||
'<button class="copy-btn" onclick="copyLog(\\''+copyId+'\\',this)">📋 复制到剪贴板</button>'+
|
||
'<textarea id="'+copyId+'" style="position:absolute;left:-9999px">'+escapeHtml(buildCopyText())+'</textarea>'+
|
||
'</div>';
|
||
// prompt
|
||
if (log.messages && log.messages.length > 0) {
|
||
html += '<div style="margin-bottom:12px"><h4>'+(log.messages.filter(m=>m.role==='system').length>0?'🧠':'')+' Prompt</h4>'+renderMessages(log.messages)+'</div>';
|
||
}
|
||
// 参数 + 耗时
|
||
html += '<div class="side" style="margin-bottom:12px">';
|
||
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'+(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>';
|
||
html += '</div>';
|
||
row.querySelector('.detail-card').innerHTML = html;
|
||
}
|
||
|
||
async function clearLogs() {
|
||
if (!confirm('确定清除全部请求日志?')) return;
|
||
await fetch('/admin/logs', { method:'DELETE' });
|
||
loadedDetails = {};
|
||
await loadLogs();
|
||
}
|
||
|
||
function escapeHtml(s) {
|
||
if (s == null) return '';
|
||
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
||
}
|
||
|
||
/** 千位分隔;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;
|
||
ta.style.position = 'fixed';
|
||
ta.style.left = '0';
|
||
ta.style.top = '0';
|
||
ta.style.width = '1px';
|
||
ta.style.height = '1px';
|
||
ta.select();
|
||
try { document.execCommand('copy'); btn.textContent = '✅ 已复制'; setTimeout(()=>{btn.textContent='📋 复制到剪贴板';},2000); } catch(e) {}
|
||
ta.style.position = 'absolute';
|
||
ta.style.left = '-9999px';
|
||
}
|
||
|
||
$('#filterEndpoint').addEventListener('change', () => { loadedDetails = {}; loadLogs(); });
|
||
|
||
async function init() {
|
||
try {
|
||
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();
|
||
</script>
|
||
</body>
|
||
</html>`;
|
||
|
||
export async function adminRoutes(app: FastifyInstance): Promise<void> {
|
||
|
||
// 管理面板会展示完整 prompt 与学习内容,与 /ai/* 共用同一个可选令牌(安全-03)
|
||
if (!configuredToken()) {
|
||
app.log.warn(
|
||
"未设置 LPT_AI_TOKEN,/admin 面板与 /ai/* 无访问保护(面板会展示 prompt 与学习内容)",
|
||
);
|
||
}
|
||
|
||
app.addHook("preHandler", async (request, reply) => {
|
||
if (!checkToken(request, reply)) {
|
||
return 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);
|
||
});
|
||
|
||
// —— 日志列表 ——
|
||
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,
|
||
err: summaries.filter((s) => s.status < 200 || s.status >= 300).length,
|
||
avgMs: summaries.length > 0
|
||
? 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);
|
||
if (isNaN(id)) {
|
||
return reply.status(400).send({ error: "无效 ID" });
|
||
}
|
||
const entry = logStore.get(id);
|
||
if (!entry) {
|
||
return reply.status(404).send({ error: "日志不存在" });
|
||
}
|
||
return entry;
|
||
});
|
||
|
||
// —— 清空日志 ——
|
||
app.delete("/admin/logs", async () => {
|
||
logStore.clear();
|
||
return { ok: true };
|
||
});
|
||
}
|