feat(web): display reported chat throughput tokens context and cost
This commit is contained in:
+12
-6
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// Native snapshots, not tokenizers, pricing estimates, or accumulated deltas.
|
||||
const count = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0 ? (value === 0 ? 0 : value) : null;
|
||||
const amount = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? (value === 0 ? 0 : value) : null;
|
||||
|
||||
export function normalizeChatUsage(usage) {
|
||||
if (!usage || typeof usage !== "object" || Array.isArray(usage)) return null;
|
||||
const window = count(usage.context_window);
|
||||
const result = {
|
||||
scope: ["session", "turn"].includes(usage.scope) ? usage.scope : "",
|
||||
total_tokens: count(usage.total_tokens),
|
||||
context_tokens: count(usage.context_tokens),
|
||||
context_window: window > 0 ? window : null,
|
||||
cost_usd: amount(usage.cost_usd),
|
||||
output_tokens_per_second: amount(usage.output_tokens_per_second),
|
||||
rate_basis: ["generation", "turn"].includes(usage.rate_basis) ? usage.rate_basis : "",
|
||||
};
|
||||
// Legacy input/output counters do not prove a total that includes cache and
|
||||
// reasoning; legacy zero-valued costs cannot distinguish unknown from free.
|
||||
return result.scope || Object.entries(result).some(([key, value]) => key !== "scope" && key !== "rate_basis" && value !== null) ? result : null;
|
||||
}
|
||||
|
||||
export function chatUsageMetrics(value, locale) {
|
||||
const usage = normalizeChatUsage(value);
|
||||
const integer = new Intl.NumberFormat(locale, { maximumFractionDigits: 0 });
|
||||
const decimal = new Intl.NumberFormat(locale, { maximumFractionDigits: 1 });
|
||||
const dollars = new Intl.NumberFormat(locale, { style: "currency", currency: "USD", minimumFractionDigits: 4, maximumFractionDigits: 6 });
|
||||
const metric = (key, label, number, format, title) => ({ key, label, available: number !== null && number !== undefined,
|
||||
value: number === null || number === undefined ? "Unavailable" : format(number), title });
|
||||
const rateTitle = usage?.rate_basis === "turn"
|
||||
? "Average output tokens per second over the measured turn, including tools and waiting."
|
||||
: usage?.rate_basis === "generation"
|
||||
? "Non-reasoning output tokens per second over the latest native request-to-stream-completion interval, including request latency but excluding tool execution."
|
||||
: "Native output-token rate when reported; never estimated from text length.";
|
||||
const used = usage?.context_tokens ?? null;
|
||||
const limit = usage?.context_window ?? null;
|
||||
const context = used !== null && limit !== null ? `${integer.format(used)} / ${integer.format(limit)}`
|
||||
: limit !== null ? `${integer.format(limit)} limit`
|
||||
: used !== null ? `${integer.format(used)} used` : "Unavailable";
|
||||
return [
|
||||
metric("rate", usage?.rate_basis === "turn" ? "Avg output rate" : "Output rate", usage?.output_tokens_per_second,
|
||||
(number) => `${number > 0 && number < 0.1 ? "<" + decimal.format(0.1) : decimal.format(number)} tok/s`, rateTitle),
|
||||
metric("tokens", usage?.scope === "turn" ? "Turn tokens" : "Total tokens", usage?.total_tokens,
|
||||
(number) => integer.format(number), usage?.scope === "turn" ? "Reported token total for the measured turn. Snapshots are not added together."
|
||||
: "Reported token total across the conversation's model calls, not the current context size. Snapshots are not added together."),
|
||||
{ key: "context", label: "Context", available: used !== null || limit !== null, value: context,
|
||||
title: "Latest reported prompt/context usage / model context-window limit. Harness accounting may differ; cumulative processed tokens are not context usage." },
|
||||
metric("cost", "Reported cost", usage?.cost_usd,
|
||||
(number) => number > 0 && number < 0.000001 ? "<" + dollars.format(0.000001) : dollars.format(number),
|
||||
"USD cost reported by the harness, not a subscription invoice. We do not estimate unreported costs or infer subscription billing."),
|
||||
];
|
||||
}
|
||||
+34
-1
@@ -8,6 +8,7 @@ import { isLocalArtifactLink } from "../artifactNavigation.js";
|
||||
import { formFieldVisible, formFieldValue, formAnswerError, visibleFormAnswers, safeFormURL } from "../chatForms.js";
|
||||
import { readChatStars, writeChatStars } from "../chatStars.js";
|
||||
import { chatModelLabel, chatModelSelectionNote, supportsChatModels } from "../chatModels.js";
|
||||
import { chatUsageMetrics } from "../chatUsage.js";
|
||||
import { describeError } from "../format.js";
|
||||
import { SHELL_OUTPUT_LIMIT, nativeChatMessageId, newChatOperationId } from "../repository.js";
|
||||
import { codeBlockLabel, parseInline, parseMarkdown } from "../markdown.js";
|
||||
@@ -790,6 +791,13 @@ function onChatEvent(frame) {
|
||||
scheduleHistorySync(sessionId, "", {force:true});
|
||||
return;
|
||||
}
|
||||
if (frame.kind === "usage") {
|
||||
const current = state.chatThreads.get(sessionId);
|
||||
const next = applyChatEvent(current, frame);
|
||||
state.chatThreads.set(sessionId, next);
|
||||
if (state.view === "chat" && sessionId === state.chatActiveId) refreshChatUsageMetrics(sessionId, next);
|
||||
return;
|
||||
}
|
||||
markBackgroundChatUnseen(sessionId);
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
const messageId = String(frame?.message_id ?? "");
|
||||
@@ -1384,7 +1392,7 @@ function onChatFrame(message) {
|
||||
if (!frame || !frame.session_id) return;
|
||||
// onChatEvent already folded this session activity with native identity
|
||||
// checks and refreshed the composer. Do not reapply an ignored stale start.
|
||||
if (frame.kind === "compaction") return;
|
||||
if (frame.kind === "compaction" || frame.kind === "usage") return;
|
||||
rt.touchView?.("chat");
|
||||
const sessionId = frame.session_id;
|
||||
const thread = state.chatThreads.get(sessionId);
|
||||
@@ -2462,6 +2470,30 @@ function renderChatActionsMenu(session, duplicateTitles = new Set()) {
|
||||
return wrap;
|
||||
}
|
||||
|
||||
/** Reported usage, with explicit unknowns and no inferred token/cost values. */
|
||||
function renderChatUsageMetrics(thread, sessionId) {
|
||||
const metrics = h("dl", {
|
||||
class: "chat-usage-metrics",
|
||||
id: "chatUsageMetrics",
|
||||
"data-session": sessionId,
|
||||
"aria-label": "Chat usage and performance",
|
||||
});
|
||||
for (const metric of chatUsageMetrics(thread?.usage)) {
|
||||
metrics.append(h("div", {
|
||||
class: "chat-usage-metric",
|
||||
"data-metric": metric.key,
|
||||
"data-available": String(metric.available),
|
||||
title: metric.title,
|
||||
}, h("dt", { text: metric.label }), h("dd", { text: metric.value })));
|
||||
}
|
||||
return metrics;
|
||||
}
|
||||
|
||||
function refreshChatUsageMetrics(sessionId, thread) {
|
||||
const metrics = document.getElementById("chatUsageMetrics");
|
||||
if (metrics?.dataset.session === sessionId) metrics.replaceWith(renderChatUsageMetrics(thread, sessionId));
|
||||
}
|
||||
|
||||
/** The active conversation: identity, context, transcript and its controls. */
|
||||
function renderChatConversation() {
|
||||
const pane = h("div", { class: "detail chat-detail" });
|
||||
@@ -2530,6 +2562,7 @@ function renderChatConversation() {
|
||||
context.append(modelButton);
|
||||
pane.append(context);
|
||||
}
|
||||
pane.append(renderChatUsageMetrics(thread, state.chatActiveId));
|
||||
|
||||
// The background-tasks bar (C11) sits above the transcript, outside the
|
||||
// scroller so the C14 scroll positions are untouched. It is present only while
|
||||
|
||||
Reference in New Issue
Block a user