Merge pull request #409 from Sycun/codex/fix-chat-history-token-usage

fix(chat): render token usage in restored timelines
This commit is contained in:
公明 authored and GitHub committed 2026-11-19 09:34:04 +08:00
commit b6f755717b
3 files changed
+41 -2

No files matched your search

+3 -1
View File
@@ -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
@@ -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/);
+1 -1
View File
@@ -7089,7 +7089,7 @@
<script src="/static/js/chat-scroll.js?v=20260815-1-20261113-chat-scroll"></script>
<script src="/static/js/monitor.js?v=20261011-token-usage-pagination-20261027-20261113-chat-scroll"></script>
<script src="/static/js/attack-chain-context.js?v=20261012-summary-2"></script>
<script src="/static/js/chat.js?v=20261016-context-memory-token-usage-pagination-20261027-20261113-chat-scroll"></script>
<script src="/static/js/chat.js?v=20261118-pi-usage-summary"></script>
<script src="/static/js/chat-plan-progress.js?v=20260815-1"></script>
<script src="/static/js/hitl.js?v=20260819-1-pagination-20261027"></script>
<script src="/static/js/tool-guard.js?v=20261010-conflict"></script>