From 15fbd1e001c055f4edf94bd4b2f98ba2932543c6 Mon Sep 17 00:00:00 2001 From: Sycun <165354365+Sycun@users.noreply.github.com> Date: Wed, 18 Nov 2026 21:39:16 +0800 Subject: [PATCH] fix(chat): render token usage in restored timelines --- web/static/js/chat.js | 4 ++- web/static/js/pi-usage-summary-ui.test.cjs | 37 ++++++++++++++++++++++ web/templates/index.html | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/web/static/js/chat.js b/web/static/js/chat.js index 4677bbad..2eaef5a4 100644 --- a/web/static/js/chat.js +++ b/web/static/js/chat.js @@ -4403,6 +4403,8 @@ function renderProcessDetails(messageId, processDetails, options) { itemTitle = agPx + execLine; } else if (eventType === 'agent_reply') { itemTitle = agPx + '💬 ' + (typeof window.t === 'function' ? window.t('chat.agentNameReplyTitle') : '子代理回复'); + } else if (eventType === 'agent_usage_summary') { + itemTitle = agPx + formatPiUsageSummaryTitle(data); } else if (eventType === 'agent_empty_response_continue') { itemTitle = typeof window.t === 'function' ? window.t('chat.piEmptyResponseContinueTitle') @@ -4460,7 +4462,7 @@ function renderProcessDetails(messageId, processDetails, options) { const processDetailId = detail.id || data.processDetailId || ''; const timelineOpts = { title: itemTitle, - message: detail.message || '', + message: eventType === 'agent_usage_summary' ? formatPiUsageSummaryMessage(data) : detail.message || '', data: data, processDetailId: processDetailId, createdAt: detail.createdAt diff --git a/web/static/js/pi-usage-summary-ui.test.cjs b/web/static/js/pi-usage-summary-ui.test.cjs index 6c20ba31..4426af57 100644 --- a/web/static/js/pi-usage-summary-ui.test.cjs +++ b/web/static/js/pi-usage-summary-ui.test.cjs @@ -1,7 +1,9 @@ const fs = require('node:fs'); const test = require('node:test'); const assert = require('node:assert/strict'); +const vm = require('node:vm'); +const chat = fs.readFileSync('web/static/js/chat.js', 'utf8'); const monitor = fs.readFileSync('web/static/js/monitor.js', 'utf8'); const webshell = fs.readFileSync('web/static/js/webshell.js', 'utf8'); const styles = fs.readFileSync('web/static/css/style.css', 'utf8'); @@ -27,6 +29,41 @@ test('Pi token usage summary is visible in WebShell live and restored timelines' assert.match(styles, /html\[data-theme="dark"\] \.webshell-ai-timeline-agent_usage_summary/); }); +test('chat history renders recorded token counts instead of repeating the raw event label', () => { + const formatters = monitor.slice(monitor.indexOf('function formatCompactInteger('), monitor.indexOf('function dispatchAgentPlanTaskEvent(')); + const renderer = chat.slice(chat.indexOf('function renderOneProcessDetail('), chat.indexOf('function finishPrependRender(')); + const rows = []; + const context = vm.createContext({ + window: { t: key => en.chat[key.slice('chat.'.length)] }, + timeline: {}, + prependMode: false, + getCurrentTimeLocale: () => 'en-US', + processDetailAgentPrefix: () => '', + addTimelineItem: (_timeline, type, options) => rows.push({ type, ...options }) + }); + vm.runInContext(formatters + renderer, context); + context.renderOneProcessDetail({ + id: 'usage-1', + eventType: 'agent_usage_summary', + message: 'Pi token usage summary', + data: { modelCalls: 1, promptTokens: 1000, completionTokens: 24, totalTokens: 1024 } + }); + const error = 'Pi worker exited without a result: invalid Pi protocol JSON: unexpected end of JSON input'; + context.renderOneProcessDetail({ id: 'error-1', eventType: 'error', message: error }); + + assert.equal(rows.length, 2); + assert.equal(rows[0].type, 'agent_usage_summary'); + assert.equal(rows[0].title, en.chat.piUsageSummaryTitle + ' · 1,024'); + assert.equal(rows[0].message, [ + en.chat.piUsageModelCalls + ': 1', + en.chat.piUsagePromptTokens + ': 1,000', + en.chat.piUsageCompletionTokens + ': 24', + en.chat.piUsageTotalTokens + ': 1,024' + ].join('\n')); + assert.equal(rows[1].type, 'error'); + assert.equal(rows[1].message, error); +}); + test('Pi token usage summary supports language refresh', () => { assert.match(monitor, /type === 'agent_usage_summary'/); assert.match(monitor, /item\.dataset\.modelCalls/); diff --git a/web/templates/index.html b/web/templates/index.html index dac4ac20..1a658a85 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -7089,7 +7089,7 @@ - +