refactor(web): keep token precision in formatTokens; restore UserRow comment

Review follow-up:
- formatTokens keeps one decimal and drops a trailing ".0" instead of
  dropping the decimal from 10k up, so dashboard axis ticks keep their
  .5 steps (12.5M, not 13M) and tooltips show 123.5k, not 123k. From
  999,950 it switches to "M" so values never read "1000k".
- transcript.tsx: the UserRow doc comment sat above fmtBytes on main and
  was removed with it; it is back above UserRow.
- src/lib/format.test.mjs covers the shared formatters under plain
  `node --test` (the "@/i18n" import is stubbed with a fixed locale).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sisantaChhatoi
2026-11-26 18:52:18 +05:30
co-authored by Claude Opus 5.5
parent a183871493
commit 6bcf46de52
3 changed files with 68 additions and 3 deletions
+5
View File
@@ -587,6 +587,11 @@ function parseUserBody(body: string): { text: string; attachments: MsgAttachment
return { text: body, attachments: [] };
}
// UserRow renders a right-aligned chat bubble: either a human turn (the message you
// sent the main agent) or, in a worker session, the LLM-generated intent that leads
// it (intent=true) — same bubble, but a target icon instead of the human avatar.
// summary is a truncated first line, so the full message is pulled from the detail
// and shown in full (bubble is whitespace-pre-wrap, so long/multi-line text wraps).
function UserRow({ step, intent, getDetail }: { step: Activity; intent?: boolean; getDetail: (seq: number) => Promise<string> }) {
const Icon = intent ? CrosshairIcon : UserIcon;
const [ref, inView] = useInView();
+58
View File
@@ -0,0 +1,58 @@
import assert from "node:assert/strict";
import { register } from "node:module";
import test from "node:test";
// format.ts reads the UI locale from "@/i18n", which needs the browser and the bundler.
// Replace it with a fixed locale so these tests run under plain `node --test`.
register(
`data:text/javascript,${encodeURIComponent(`
export async function resolve(specifier, context, next) {
if (specifier === "@/i18n") {
return { url: "data:text/javascript,export const intlLocale = 'en-US';", shortCircuit: true };
}
return next(specifier, context);
}
`)}`,
);
const { formatBytes, formatDurationMs, formatShortDateTime, formatTokens } = await import("./format.ts");
test("formatBytes picks the unit and keeps one decimal above bytes", () => {
assert.equal(formatBytes(512), "512 B");
assert.equal(formatBytes(1536), "1.5 KB");
assert.equal(formatBytes(1024 ** 2), "1.0 MB");
assert.equal(formatBytes(2 * 1024 ** 3), "2.0 GB");
});
test("formatBytes renders missing, zero and invalid sizes as the empty value", () => {
for (const value of [0, -5, Number.NaN, null, undefined]) assert.equal(formatBytes(value), "0 B");
assert.equal(formatBytes(null, { empty: "—" }), "—");
});
test("formatTokens keeps one decimal and drops a trailing .0", () => {
assert.equal(formatTokens(950), "950");
assert.equal(formatTokens(1234), "1.2k");
assert.equal(formatTokens(25_000), "25k");
assert.equal(formatTokens(123_456), "123.5k");
assert.equal(formatTokens(12_500_000), "12.5M");
assert.equal(formatTokens(50_000_000), "50M");
});
test("formatTokens switches to M where k would round up to 1000k", () => {
assert.equal(formatTokens(999_949), "999.9k");
assert.equal(formatTokens(999_950), "1M");
});
test("formatDurationMs renders seconds, minutes and hours", () => {
assert.equal(formatDurationMs(42_000), "42s");
assert.equal(formatDurationMs(185_000), "3m05s");
assert.equal(formatDurationMs(7_620_000), "2h07m");
});
test("formatShortDateTime honours the seconds, year and empty options", () => {
const ts = "2026-11-26T14:03:22";
assert.match(formatShortDateTime(ts), /^11\/26, 0?2:03:22\s?PM$/);
assert.match(formatShortDateTime(ts, { seconds: false }), /^11\/26, 0?2:03\s?PM$/);
assert.match(formatShortDateTime(ts, { year: true, seconds: false }), /^11\/26\/26, 0?2:03\s?PM$/);
assert.equal(formatShortDateTime("", { empty: "—" }), "—");
assert.equal(formatShortDateTime(undefined), "");
});
+5 -3
View File
@@ -17,10 +17,12 @@ export function formatBytes(bytes: number | null | undefined, { empty = "0 B" }:
return unit === 0 ? `${value} B` : `${value.toFixed(1)} ${BYTE_UNITS[unit]}`;
}
/** Compact token count: 950, 1.2k, 25k, 1.5M, 12M (one decimal only below 10k / 10M). */
/** Compact token count: 950, 1.2k, 25k, 123.5k, 12.5M (one decimal, a trailing ".0" dropped). */
export function formatTokens(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`;
if (n >= 1000) return `${(n / 1000).toFixed(n >= 10_000 ? 0 : 1)}k`;
const oneDecimal = (v: number) => v.toFixed(1).replace(/\.0$/, "");
// From 999,950 the "k" value would round up to "1000k", so switch to "M" there.
if (n >= 999_950) return `${oneDecimal(n / 1_000_000)}M`;
if (n >= 1000) return `${oneDecimal(n / 1000)}k`;
return String(n);
}