Skip to content

Commit 45f9358

Browse files
committed
chore(deadcode): share chat token formatter
1 parent 29ec5b3 commit 45f9358

4 files changed

Lines changed: 29 additions & 42 deletions

File tree

ui/src/ui/chat/context-notice.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { html, nothing } from "lit";
33
import { icons } from "../icons.ts";
44
import type { GatewaySessionRow } from "../types.ts";
5+
import { formatCompactTokenCount } from "./token-format.ts";
56

67
const CONTEXT_NOTICE_RATIO = 0.85;
78
const CONTEXT_COMPACT_RATIO = 0.9;
@@ -77,7 +78,7 @@ export function getContextNoticeViewModel(
7778
if (!warning) {
7879
return {
7980
pct,
80-
detail: `${formatTokensCompact(used)} / ${formatTokensCompact(limit)}`,
81+
detail: `${formatCompactTokenCount(used)} / ${formatCompactTokenCount(limit)}`,
8182
color: "var(--muted)",
8283
bg: "color-mix(in srgb, var(--muted) 8%, transparent)",
8384
warning,
@@ -97,7 +98,7 @@ export function getContextNoticeViewModel(
9798
const bg = `rgba(${r}, ${g}, ${b}, ${bgOpacity})`;
9899
return {
99100
pct,
100-
detail: `${formatTokensCompact(used)} / ${formatTokensCompact(limit)}`,
101+
detail: `${formatCompactTokenCount(used)} / ${formatCompactTokenCount(limit)}`,
101102
color,
102103
bg,
103104
warning,
@@ -175,14 +176,3 @@ export function renderContextNotice(
175176
</div>
176177
`;
177178
}
178-
179-
/** Format token count compactly (e.g. 128000 -> "128k"). */
180-
function formatTokensCompact(n: number): string {
181-
if (n >= 1_000_000) {
182-
return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
183-
}
184-
if (n >= 1_000) {
185-
return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
186-
}
187-
return String(n);
188-
}

ui/src/ui/chat/grouped-render.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { renderCopyAsMarkdownButton } from "./copy-as-markdown.ts";
2525
import { extractThinkingCached, formatReasoningMarkdown } from "./message-extract.ts";
2626
import { isToolResultMessage, normalizeMessage } from "./message-normalizer.ts";
2727
import { normalizeRoleForGrouping } from "./role-normalizer.ts";
28+
import { formatCompactTokenCount } from "./token-format.ts";
2829
import {
2930
extractToolCardsCached,
3031
formatCollapsedToolPreviewText,
@@ -683,17 +684,6 @@ function extractGroupMeta(group: MessageGroup, contextWindow: number | null): Gr
683684
return { input, output, cacheRead, cacheWrite, cost, model, contextPercent };
684685
}
685686

686-
/** Compact token count formatter (e.g. 128000 → "128k"). */
687-
function fmtTokens(n: number): string {
688-
if (n >= 1_000_000) {
689-
return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
690-
}
691-
if (n >= 1_000) {
692-
return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
693-
}
694-
return String(n);
695-
}
696-
697687
function renderMessageMeta(meta: GroupMeta | null) {
698688
if (!meta) {
699689
return nothing;
@@ -703,18 +693,24 @@ function renderMessageMeta(meta: GroupMeta | null) {
703693

704694
// Token counts: ↑input ↓output
705695
if (meta.input) {
706-
parts.push(html`<span class="msg-meta__tokens">${fmtTokens(meta.input)}</span>`);
696+
parts.push(html`<span class="msg-meta__tokens">${formatCompactTokenCount(meta.input)}</span>`);
707697
}
708698
if (meta.output) {
709-
parts.push(html`<span class="msg-meta__tokens">${fmtTokens(meta.output)}</span>`);
699+
parts.push(
700+
html`<span class="msg-meta__tokens">${formatCompactTokenCount(meta.output)}</span>`,
701+
);
710702
}
711703

712704
// Cache: R/W
713705
if (meta.cacheRead) {
714-
parts.push(html`<span class="msg-meta__cache">R${fmtTokens(meta.cacheRead)}</span>`);
706+
parts.push(
707+
html`<span class="msg-meta__cache">R${formatCompactTokenCount(meta.cacheRead)}</span>`,
708+
);
715709
}
716710
if (meta.cacheWrite) {
717-
parts.push(html`<span class="msg-meta__cache">W${fmtTokens(meta.cacheWrite)}</span>`);
711+
parts.push(
712+
html`<span class="msg-meta__cache">W${formatCompactTokenCount(meta.cacheWrite)}</span>`,
713+
);
718714
}
719715

720716
// Cost

ui/src/ui/chat/slash-command-executor.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
} from "../types.ts";
3131
import { generateUUID } from "../uuid.ts";
3232
import { SLASH_COMMANDS } from "./slash-commands.ts";
33+
import { formatCompactTokenCount } from "./token-format.ts";
3334

3435
export type SlashCommandResult = {
3536
/** Markdown-formatted result to display in chat. */
@@ -436,16 +437,16 @@ async function executeUsage(
436437
const totalDisplay =
437438
cumulativeTotal === null
438439
? "n/a"
439-
: `${totalTokensFresh ? "" : "~"}${fmtTokens(cumulativeTotal)}`;
440+
: `${totalTokensFresh ? "" : "~"}${formatCompactTokenCount(cumulativeTotal)}`;
440441

441442
const lines = [
442443
"**Session Usage**",
443-
`Input: **${fmtTokens(input)}** tokens`,
444-
`Output: **${fmtTokens(output)}** tokens`,
444+
`Input: **${formatCompactTokenCount(input)}** tokens`,
445+
`Output: **${formatCompactTokenCount(output)}** tokens`,
445446
`Total: **${totalDisplay}** tokens`,
446447
];
447448
if (pct !== null) {
448-
lines.push(`Context: **${pct}%** of ${fmtTokens(ctx)}`);
449+
lines.push(`Context: **${pct}%** of ${formatCompactTokenCount(ctx)}`);
449450
}
450451
if (session.model) {
451452
lines.push(`Model: \`${session.model}\``);
@@ -785,13 +786,3 @@ async function executeRedirect(
785786
return { content: `Failed to redirect: ${String(err)}` };
786787
}
787788
}
788-
789-
function fmtTokens(n: number): string {
790-
if (n >= 1_000_000) {
791-
return `${(n / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
792-
}
793-
if (n >= 1_000) {
794-
return `${(n / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
795-
}
796-
return String(n);
797-
}

ui/src/ui/chat/token-format.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Chat surfaces share a one-decimal compact token label, e.g. 214500 -> "214.5k".
2+
export function formatCompactTokenCount(tokens: number): string {
3+
if (tokens >= 1_000_000) {
4+
return `${(tokens / 1_000_000).toFixed(1).replace(/\.0$/, "")}M`;
5+
}
6+
if (tokens >= 1_000) {
7+
return `${(tokens / 1_000).toFixed(1).replace(/\.0$/, "")}k`;
8+
}
9+
return String(tokens);
10+
}

0 commit comments

Comments
 (0)