fix: Token统计准确性修复 + 仪表盘增强
1. 修复eval_count只记最后一轮的bug:改为每轮累加 2. 新增prompt_eval_count(输入token)统计 3. 修复total_duration使用Ollama实际推理时间而非墙钟时间 4. 数据库messages表新增prompt_eval_count列(含兼容迁移) 5. 仪表盘显示输入/输出token分项统计 6. 柱状图堆叠显示输入(紫色)+输出(橙色)比例 7. 明细表格新增输入/输出/合计三列
This commit is contained in:
@@ -143,6 +143,7 @@ export async function initDatabase(): Promise<SQL.Database> {
|
||||
tool_calls TEXT,
|
||||
tool_name TEXT,
|
||||
eval_count INTEGER,
|
||||
prompt_eval_count INTEGER,
|
||||
total_duration INTEGER,
|
||||
created_at INTEGER NOT NULL,
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
|
||||
@@ -221,6 +222,9 @@ export async function initDatabase(): Promise<SQL.Database> {
|
||||
CREATE INDEX IF NOT EXISTS idx_skills_updated ON skills(updated_at DESC);
|
||||
`);
|
||||
|
||||
// 兼容迁移:为已有 messages 表补充 prompt_eval_count 列(新库已有,旧库需要 ALTER)
|
||||
try { db.run('ALTER TABLE messages ADD COLUMN prompt_eval_count INTEGER'); } catch { /* 列已存在,忽略 */ }
|
||||
|
||||
// 尝试创建 FTS5 全文搜索(可选,sql.js 默认 WASM 可能不包含 FTS5)
|
||||
try {
|
||||
db.run(`
|
||||
@@ -265,6 +269,7 @@ export interface MessageRow {
|
||||
tool_calls: string | null;
|
||||
tool_name: string | null;
|
||||
eval_count: number | null;
|
||||
prompt_eval_count: number | null;
|
||||
total_duration: number | null;
|
||||
created_at: number;
|
||||
}
|
||||
@@ -364,10 +369,10 @@ export function clearAllSessions(): void {
|
||||
|
||||
export function saveMessage(msg: MessageRow): string {
|
||||
const d = getDb();
|
||||
runExec(d, `INSERT OR REPLACE INTO messages (id, session_id, role, content, thinking, images, tool_calls, tool_name, eval_count, total_duration, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
runExec(d, `INSERT OR REPLACE INTO messages (id, session_id, role, content, thinking, images, tool_calls, tool_name, eval_count, prompt_eval_count, total_duration, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[msg.id, msg.session_id, msg.role, msg.content, msg.thinking, msg.images,
|
||||
msg.tool_calls, msg.tool_name, msg.eval_count, msg.total_duration, msg.created_at]
|
||||
msg.tool_calls, msg.tool_name, msg.eval_count, msg.prompt_eval_count, msg.total_duration, msg.created_at]
|
||||
);
|
||||
persist();
|
||||
return msg.id;
|
||||
|
||||
@@ -349,6 +349,7 @@ async function handleRetry(): Promise<void> {
|
||||
...(retryThinkContent && { think: retryThinkContent }),
|
||||
...(toolRecords?.length && { toolCalls: toolRecords }),
|
||||
...(loopStats?.eval_count && { eval_count: loopStats.eval_count }),
|
||||
...(loopStats?.prompt_eval_count && { prompt_eval_count: loopStats.prompt_eval_count }),
|
||||
...(loopStats?.total_duration && { total_duration: loopStats.total_duration }),
|
||||
};
|
||||
state.update(KEYS.CURRENT_SESSION, (s: ChatSession | null) => ({
|
||||
@@ -362,6 +363,7 @@ async function handleRetry(): Promise<void> {
|
||||
...(retryThinkContent && { think: retryThinkContent }),
|
||||
...(toolRecords?.length && { toolCalls: toolRecords }),
|
||||
...(loopStats?.eval_count && { eval_count: loopStats.eval_count }),
|
||||
...(loopStats?.prompt_eval_count && { prompt_eval_count: loopStats.prompt_eval_count }),
|
||||
...(loopStats?.total_duration && { total_duration: loopStats.total_duration }),
|
||||
};
|
||||
state.update(KEYS.CURRENT_SESSION, (s: ChatSession | null) => ({
|
||||
@@ -1056,7 +1058,8 @@ async function sendMessageWithAgentLoop(text: string, currentSession: ChatSessio
|
||||
...(thinkContent && { think: thinkContent }),
|
||||
...(toolRecords?.length && { toolCalls: toolRecords }),
|
||||
...(loopStats?.eval_count && { eval_count: loopStats.eval_count }),
|
||||
...(lastIterDuration > 0 && { total_duration: lastIterDuration }),
|
||||
...(loopStats?.prompt_eval_count && { prompt_eval_count: loopStats.prompt_eval_count }),
|
||||
...(loopStats?.total_duration && { total_duration: loopStats.total_duration }),
|
||||
};
|
||||
state.update(KEYS.CURRENT_SESSION, (session: ChatSession | null) => ({
|
||||
...session,
|
||||
@@ -1064,14 +1067,7 @@ async function sendMessageWithAgentLoop(text: string, currentSession: ChatSessio
|
||||
updatedAt: Date.now()
|
||||
}));
|
||||
}
|
||||
const perIterStats = {
|
||||
eval_count: loopStats?.eval_count,
|
||||
total_duration: (() => {
|
||||
const dur = (Date.now() - lastIterationStartTime) * 1e6;
|
||||
return dur > 0 ? dur : undefined;
|
||||
})(),
|
||||
};
|
||||
updateLastAssistantMessage(assistantContent, thinkContent || null, perIterStats);
|
||||
updateLastAssistantMessage(assistantContent, thinkContent || null, loopStats || null);
|
||||
} else {
|
||||
// 单迭代模式:正常保存
|
||||
const assistantMsg: ChatMessage = {
|
||||
@@ -1081,6 +1077,7 @@ async function sendMessageWithAgentLoop(text: string, currentSession: ChatSessio
|
||||
...(thinkContent && { think: thinkContent }),
|
||||
...(toolRecords?.length && { toolCalls: toolRecords }),
|
||||
...(loopStats?.eval_count && { eval_count: loopStats.eval_count }),
|
||||
...(loopStats?.prompt_eval_count && { prompt_eval_count: loopStats.prompt_eval_count }),
|
||||
...(loopStats?.total_duration && { total_duration: loopStats.total_duration }),
|
||||
};
|
||||
state.update(KEYS.CURRENT_SESSION, (session: ChatSession | null) => ({
|
||||
|
||||
@@ -52,6 +52,7 @@ interface RoundData {
|
||||
round: number;
|
||||
role: string;
|
||||
eval_count: number;
|
||||
prompt_eval_count: number;
|
||||
total_duration: number;
|
||||
timestamp: number;
|
||||
model?: string;
|
||||
@@ -61,12 +62,13 @@ function collectRoundData(messages: ChatMessage[]): RoundData[] {
|
||||
const rounds: RoundData[] = [];
|
||||
let roundNum = 0;
|
||||
for (const msg of messages) {
|
||||
if (msg.role === 'assistant' && (msg.eval_count || msg.total_duration)) {
|
||||
if (msg.role === 'assistant' && (msg.eval_count || msg.prompt_eval_count || msg.total_duration)) {
|
||||
roundNum++;
|
||||
rounds.push({
|
||||
round: roundNum,
|
||||
role: msg.role,
|
||||
eval_count: msg.eval_count || 0,
|
||||
prompt_eval_count: msg.prompt_eval_count || 0,
|
||||
total_duration: msg.total_duration || 0,
|
||||
timestamp: msg.timestamp,
|
||||
model: msg.model,
|
||||
@@ -101,7 +103,9 @@ function renderDashboard(): void {
|
||||
const aiMsgs = messages.filter(m => m.role === 'assistant');
|
||||
const rounds = collectRoundData(messages);
|
||||
|
||||
const totalTokens = rounds.reduce((sum, r) => sum + r.eval_count, 0);
|
||||
const totalOutputTokens = rounds.reduce((sum, r) => sum + r.eval_count, 0);
|
||||
const totalInputTokens = rounds.reduce((sum, r) => sum + r.prompt_eval_count, 0);
|
||||
const totalTokens = totalInputTokens + totalOutputTokens;
|
||||
const totalDurationNs = rounds.reduce((sum, r) => sum + r.total_duration, 0);
|
||||
const avgTokens = rounds.length > 0 ? Math.round(totalTokens / rounds.length) : 0;
|
||||
|
||||
@@ -112,6 +116,7 @@ function renderDashboard(): void {
|
||||
<div class="td-stat-icon">🔥</div>
|
||||
<div class="td-stat-value">${formatTokenCount(totalTokens)}</div>
|
||||
<div class="td-stat-label">总 Token 消耗</div>
|
||||
<div class="td-stat-detail">输入 ${formatTokenCount(totalInputTokens)} / 输出 ${formatTokenCount(totalOutputTokens)}</div>
|
||||
</div>
|
||||
<div class="td-stat-card">
|
||||
<div class="td-stat-icon">💬</div>
|
||||
@@ -135,15 +140,19 @@ function renderDashboard(): void {
|
||||
if (rounds.length === 0) {
|
||||
chartEl.innerHTML = '<div class="td-empty">暂无数据,发送消息后开始统计</div>';
|
||||
} else {
|
||||
const maxTokens = Math.max(...rounds.map(r => r.eval_count), 1);
|
||||
const maxTokens = Math.max(...rounds.map(r => r.eval_count + r.prompt_eval_count), 1);
|
||||
const barsHtml = rounds.map(r => {
|
||||
const heightPct = Math.max((r.eval_count / maxTokens) * 100, 2);
|
||||
const tooltip = `第${r.round}轮 · ${formatTokenCount(r.eval_count)} tokens · ${formatDuration(r.total_duration)}`;
|
||||
const total = r.eval_count + r.prompt_eval_count;
|
||||
const heightPct = Math.max((total / maxTokens) * 100, 2);
|
||||
const inputPct = total > 0 ? (r.prompt_eval_count / total) * 100 : 0;
|
||||
const tooltip = `第${r.round}轮 · 输入 ${formatTokenCount(r.prompt_eval_count)} + 输出 ${formatTokenCount(r.eval_count)} = ${formatTokenCount(total)} tokens · ${formatDuration(r.total_duration)}`;
|
||||
return `
|
||||
<div class="td-bar-col" title="${tooltip}">
|
||||
<div class="td-bar-value">${formatTokenCount(r.eval_count)}</div>
|
||||
<div class="td-bar-value">${formatTokenCount(total)}</div>
|
||||
<div class="td-bar" style="height:${heightPct}%;">
|
||||
<div class="td-bar-fill"></div>
|
||||
<div class="td-bar-fill">
|
||||
<div class="td-bar-input" style="height:${inputPct}%;"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="td-bar-label">R${r.round}</div>
|
||||
</div>
|
||||
@@ -151,6 +160,10 @@ function renderDashboard(): void {
|
||||
}).join('');
|
||||
|
||||
chartEl.innerHTML = `
|
||||
<div class="td-chart-legend">
|
||||
<span class="td-legend-item"><span class="td-legend-dot td-legend-output"></span>输出 Token</span>
|
||||
<span class="td-legend-item"><span class="td-legend-dot td-legend-input"></span>输入 Token</span>
|
||||
</div>
|
||||
<div class="td-chart-container">
|
||||
<div class="td-chart-y-axis">
|
||||
<span>${formatTokenCount(maxTokens)}</span>
|
||||
@@ -165,18 +178,23 @@ function renderDashboard(): void {
|
||||
// C. 明细表格
|
||||
const tableEl = dashboardModalEl.querySelector('#tdTableBody')!;
|
||||
if (rounds.length === 0) {
|
||||
tableEl.innerHTML = '<tr><td colspan="5" class="td-empty-row">暂无数据</td></tr>';
|
||||
tableEl.innerHTML = '<tr><td colspan="6" class="td-empty-row">暂无数据</td></tr>';
|
||||
} else {
|
||||
// 按时间倒序
|
||||
const sorted = [...rounds].reverse();
|
||||
tableEl.innerHTML = sorted.map(r => `
|
||||
tableEl.innerHTML = sorted.map(r => {
|
||||
const total = r.eval_count + r.prompt_eval_count;
|
||||
return `
|
||||
<tr>
|
||||
<td>R${r.round}</td>
|
||||
<td><span class="td-role-badge">🤖 AI</span>${r.model ? ` <span class="td-model-tag">${r.model}</span>` : ''}</td>
|
||||
<td class="td-num">${formatTokenCount(r.prompt_eval_count)}</td>
|
||||
<td class="td-num">${formatTokenCount(r.eval_count)}</td>
|
||||
<td class="td-num td-num-total">${formatTokenCount(total)}</td>
|
||||
<td class="td-num">${formatDuration(r.total_duration)}</td>
|
||||
<td class="td-time">${r.timestamp ? formatTime(r.timestamp) : '-'}</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ export class ChatDB {
|
||||
tool_calls: msg.toolCalls?.length ? JSON.stringify(msg.toolCalls) : null,
|
||||
tool_name: null,
|
||||
eval_count: msg.eval_count || null,
|
||||
prompt_eval_count: msg.prompt_eval_count || null,
|
||||
total_duration: msg.total_duration || null,
|
||||
created_at: msg.timestamp
|
||||
};
|
||||
@@ -112,6 +113,7 @@ export class ChatDB {
|
||||
think: r.thinking || undefined,
|
||||
images: r.images ? JSON.parse(r.images) : undefined,
|
||||
eval_count: r.eval_count || undefined,
|
||||
prompt_eval_count: r.prompt_eval_count || undefined,
|
||||
total_duration: r.total_duration || undefined,
|
||||
toolCalls: r.tool_calls ? JSON.parse(r.tool_calls) : undefined
|
||||
}));
|
||||
@@ -170,6 +172,7 @@ export class ChatDB {
|
||||
images: m.images?.length ? JSON.stringify(m.images) : null,
|
||||
tool_calls: m.toolCalls?.length ? JSON.stringify(m.toolCalls) : null,
|
||||
tool_name: null, eval_count: m.eval_count || null,
|
||||
prompt_eval_count: m.prompt_eval_count || null,
|
||||
total_duration: m.total_duration || null, created_at: m.timestamp
|
||||
}))),
|
||||
memories: [],
|
||||
|
||||
@@ -668,7 +668,9 @@
|
||||
<tr>
|
||||
<th>轮次</th>
|
||||
<th>角色</th>
|
||||
<th>Token 数</th>
|
||||
<th>输入</th>
|
||||
<th>输出</th>
|
||||
<th>合计</th>
|
||||
<th>耗时</th>
|
||||
<th>时间</th>
|
||||
</tr>
|
||||
|
||||
@@ -356,7 +356,7 @@ export interface AgentCallbacks {
|
||||
onToolCallStart: (call: ToolCall) => void;
|
||||
onToolCallResult: (name: string, result: ToolResult, call: ToolCall) => void;
|
||||
onToolCallError: (name: string, error: string, call: ToolCall) => void;
|
||||
onDone: (finalContent: string, toolRecords?: ToolCallRecord[], stats?: { eval_count?: number; total_duration?: number }) => void;
|
||||
onDone: (finalContent: string, toolRecords?: ToolCallRecord[], stats?: { eval_count?: number; prompt_eval_count?: number; total_duration?: number }) => void;
|
||||
onConfirmTool: (call: ToolCall) => Promise<boolean>;
|
||||
/** Agent Loop 新迭代开始(前一轮工具执行完毕,下一轮流式输出即将开始) */
|
||||
onNewIteration?: (toolCalls?: ToolCall[]) => void;
|
||||
@@ -510,16 +510,24 @@ export async function runAgentLoop(
|
||||
const allToolRecords: ToolCallRecord[] = [];
|
||||
const loopStartTime = Date.now();
|
||||
let content = '';
|
||||
/** 每轮累计 token 统计 */
|
||||
let totalEvalCount = 0;
|
||||
let totalPromptEvalCount = 0;
|
||||
let totalInferenceNs = 0;
|
||||
/** 当前轮的 Ollama 统计(流式最后一个 chunk 赋值) */
|
||||
let loopEvalCount = 0;
|
||||
let loopPromptEvalCount = 0;
|
||||
let loopInferenceNs = 0;
|
||||
/** 跨轮去重:仅跟踪成功的工具调用(失败的允许重试) */
|
||||
let prevLoopSuccessKeys: string[] = [];
|
||||
/** 保存上一轮的工具调用,供 onNewIteration 使用 */
|
||||
let prevToolCalls: ToolCall[] = [];
|
||||
|
||||
const makeStats = () => {
|
||||
const totalDuration = (Date.now() - loopStartTime) * 1e6;
|
||||
return { eval_count: totalEvalCount || undefined, total_duration: totalDuration };
|
||||
};
|
||||
const makeStats = () => ({
|
||||
eval_count: totalEvalCount || undefined,
|
||||
prompt_eval_count: totalPromptEvalCount || undefined,
|
||||
total_duration: totalInferenceNs || undefined,
|
||||
});
|
||||
|
||||
while (loopCount < maxLoops) {
|
||||
loopCount++;
|
||||
@@ -581,7 +589,9 @@ export async function runAgentLoop(
|
||||
content += chunk.message.content;
|
||||
callbacks.onContent(content);
|
||||
}
|
||||
if (chunk.eval_count) { totalEvalCount = chunk.eval_count; state.set('_currentEvalCount', totalEvalCount); }
|
||||
if (chunk.eval_count) { loopEvalCount = chunk.eval_count; }
|
||||
if (chunk.prompt_eval_count) { loopPromptEvalCount = chunk.prompt_eval_count; }
|
||||
if (chunk.total_duration) { loopInferenceNs = chunk.total_duration; }
|
||||
if (chunk.message?.tool_calls?.length) {
|
||||
for (const tc of chunk.message.tool_calls) {
|
||||
if (tc.function?.name) {
|
||||
@@ -603,6 +613,16 @@ export async function runAgentLoop(
|
||||
},
|
||||
abortController
|
||||
);
|
||||
|
||||
// 本轮流式结束,累加 token 统计
|
||||
totalEvalCount += loopEvalCount;
|
||||
totalPromptEvalCount += loopPromptEvalCount;
|
||||
totalInferenceNs += loopInferenceNs;
|
||||
state.set('_currentEvalCount', totalEvalCount);
|
||||
// 重置本轮计数器(下一轮重新从 chunk 收集)
|
||||
loopEvalCount = 0;
|
||||
loopPromptEvalCount = 0;
|
||||
loopInferenceNs = 0;
|
||||
} catch (err) {
|
||||
if (abortController.signal.aborted) {
|
||||
logInfo('流式调用已中止');
|
||||
|
||||
@@ -3516,6 +3516,13 @@ html, body {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.td-stat-detail {
|
||||
font-size: 10px;
|
||||
color: var(--text-tertiary);
|
||||
margin-top: 2px;
|
||||
font-family: var(--font-mono);
|
||||
}
|
||||
|
||||
/* 区块标题 */
|
||||
.td-section {
|
||||
margin-bottom: 20px;
|
||||
@@ -3601,6 +3608,43 @@ html, body {
|
||||
background: linear-gradient(180deg, #E8734A 0%, #F0976E 100%);
|
||||
border-radius: 4px 4px 0 0;
|
||||
transition: height 0.3s ease;
|
||||
display: flex;
|
||||
flex-direction: column-reverse;
|
||||
}
|
||||
|
||||
.td-bar-input {
|
||||
width: 100%;
|
||||
background: linear-gradient(180deg, #9B7ED8 0%, #B99BE5 100%);
|
||||
border-radius: 4px 4px 0 0;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.td-chart-legend {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
margin-bottom: 10px;
|
||||
font-size: 11px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.td-legend-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.td-legend-dot {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.td-legend-output {
|
||||
background: #E8734A;
|
||||
}
|
||||
|
||||
.td-legend-input {
|
||||
background: #9B7ED8;
|
||||
}
|
||||
|
||||
.td-bar-label {
|
||||
@@ -3676,6 +3720,11 @@ html, body {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.td-num-total {
|
||||
font-weight: 700;
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.td-time {
|
||||
font-size: 11px;
|
||||
color: var(--text-secondary);
|
||||
|
||||
Vendored
+2
@@ -36,6 +36,7 @@ export interface OllamaStreamChunk {
|
||||
};
|
||||
done?: boolean;
|
||||
eval_count?: number;
|
||||
prompt_eval_count?: number;
|
||||
total_duration?: number;
|
||||
}
|
||||
|
||||
@@ -99,6 +100,7 @@ export interface ChatMessage {
|
||||
model?: string;
|
||||
think?: string;
|
||||
eval_count?: number;
|
||||
prompt_eval_count?: number;
|
||||
total_duration?: number;
|
||||
images?: string[];
|
||||
files?: ChatFile[];
|
||||
|
||||
Reference in New Issue
Block a user