routes/ai.ts: - 移除 /ai/aggregate-report、/ai/generate-mind-map、/ai/compare-recall 三个同步端点,仅保留 /health + 异步任务路由 - 清除不再需要的 import(chat、prompts、LlmError、logStore) admin/routes.ts: - 日志详情增加「复制到剪贴板」按钮,一键复制完整请求/响应/ prompt/参数文本,方便粘贴到外部工具调试
281 lines
12 KiB
TypeScript
281 lines
12 KiB
TypeScript
/**
|
|
* 管理员路由:日志查询 API + 自包含 HTML 面板。
|
|
*/
|
|
|
|
import type { FastifyInstance } from "fastify";
|
|
import { logStore } from "./store.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}
|
|
.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>
|
|
</div>
|
|
|
|
<table class="log-table" id="logTable"><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
|
|
$('#statsBar').innerHTML = [
|
|
{ label:'总请求', value:stats.total, cls:'neutral' },
|
|
{ label:'成功', value:stats.ok, cls:'ok' },
|
|
{ label:'失败', value:stats.err, cls:'err' },
|
|
{ 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';
|
|
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>';
|
|
}).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 (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</pre></div>';
|
|
html += '</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,'"');
|
|
}
|
|
|
|
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) {}
|
|
await loadLogs();
|
|
}
|
|
init();
|
|
</script>
|
|
</body>
|
|
</html>`;
|
|
|
|
export async function adminRoutes(app: FastifyInstance): Promise<void> {
|
|
|
|
// —— 面板页面 ——
|
|
app.get("/admin", async (_request, reply) => {
|
|
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);
|
|
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(),
|
|
};
|
|
return { logs: summaries, stats };
|
|
});
|
|
|
|
// —— 日志详情 ——
|
|
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 };
|
|
});
|
|
}
|