feat(web): display reported chat throughput tokens context and cost

This commit is contained in:
2026-10-21 00:27:12 +01:00
parent 910028a75c
commit 0b8595f16e
12 changed files with 314 additions and 12 deletions
+12 -6
View File
@@ -52,6 +52,7 @@
// kind is called or coloured.
import { ChangeLineKind } from "./changes.js";
import { humanBytes } from "./artifacts.js";
import { normalizeChatUsage } from "./chatUsage.js";
/** ChatMessage.Role values (internal/protocol/chat.go). */
export const ROLE_USER = "user";
@@ -1400,6 +1401,7 @@ export function newChatThread(sessionId = "") {
nativeTurnId: "",
runningSince: 0,
compaction: null,
usage: null,
toolStartedAt: {},
lastDeltaAt: 0,
queued: false,
@@ -1609,7 +1611,7 @@ export function applyChatForm(thread, form) {
/**
* Applies one pushed `chat_event` frame to a conversation and returns the next
* thread. Turn frames clear `queued` (compaction does not admit a queued prompt);
* thread. Turn frames clear `queued` (usage/compaction do not admit a queued prompt);
* `user` echoes another
* client's prompt once (never this client's own), `text`/`reasoning` append to the
* part their kind started, `tool` merges field by field, `step` drives the running
@@ -1618,6 +1620,12 @@ export function applyChatForm(thread, form) {
* targets is created on first sight, so a live turn never gains a second bubble.
*/
export function applyChatEvent(thread, frame, now = Date.now()) {
if (frame?.kind === EVENT_USAGE) {
const current = thread ?? newChatThread(str(frame?.session_id));
if (current.sessionId && frame.session_id !== current.sessionId) return current;
const usage = normalizeChatUsage(frame.usage);
return usage ? { ...current, usage } : current;
}
if (frame?.kind === EVENT_COMPACTION) return applyChatCompaction(thread, frame, now);
if (frame?.kind === EVENT_RESYNC) return thread ?? newChatThread(str(frame?.session_id));
if (frame?.kind === EVENT_STEP && frame.step?.status === STEP_ENDED && frame.step?.turn_id && frame.step.turn_id !== thread?.nativeTurnId) return thread ?? newChatThread(str(frame?.session_id));
@@ -1644,8 +1652,6 @@ export function applyChatEvent(thread, frame, now = Date.now()) {
return { ...current, shells: mergeShellFrame(current.shells, frame?.shell) };
case EVENT_FORM:
return applyChatForm(current, frame?.form);
// `usage` is recognised but not folded; it still cleared `queued` above.
case EVENT_USAGE:
default:
return current;
}
@@ -1978,11 +1984,11 @@ export function composerSubmit(thread, selected = 0) {
/**
* The queued flag after one pushed frame for the session: any frame means the
* turn the prompt was waiting behind has moved on, so the queued notice clears
* (mirrors ChatStore.event). Null/compaction frames leave it untouched: context
* maintenance alone does not admit the queued prompt.
* (mirrors ChatStore.event). Null/usage/compaction frames leave it untouched:
* usage reporting or context maintenance alone does not admit the queued prompt.
*/
export function queuedAfterFrame(queued, frame) {
if (frame == null || frame.kind === EVENT_COMPACTION) return queued === true;
if (frame == null || frame.kind === EVENT_COMPACTION || frame.kind === EVENT_USAGE) return queued === true;
return false;
}