Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/components/agent-ui/agent-ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe("agent UI accessibility contracts", () => {
expect(toolHtml).toContain("400 ms");
expect(subagentHtml).toContain('aria-live="polite"');
expect(subagentHtml).toContain("Completed");
expect(subagentHtml).toContain("1.3K tokens");
expect(subagentHtml).toContain("1.3k tokens");
});

test("gives artifact preview and download controls distinct accessible names", () => {
Expand Down
7 changes: 1 addition & 6 deletions frontend/components/agent-ui/live-subagent-status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@remixicon/react";
import type { CanonicalChildModel } from "@/components/chat/canonical-children";
import type { ChildStatus } from "@/components/chat/native-events";
import { formatTokens } from "@/utils/format";

export interface LiveSubagentStatusProps {
readonly model: CanonicalChildModel;
Expand Down Expand Up @@ -42,12 +43,6 @@ function StatusIcon({ status }: { readonly status: ChildStatus }) {
return <RiTimeLine className="size-4 text-text-tertiary" aria-hidden />;
}

function formatTokens(tokens: number): string {
return new Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 1 }).format(
tokens,
);
}

export function LiveSubagentStatus({
model,
emptyLabel = "No subagents started.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RiArrowDownSLine, RiArrowRightLine, RiArrowRightSLine } from "@remixico
import { AnimatePresence, motion } from "motion/react";
import { resolveTone } from "@/components/application/charts/chart-card";
import { cx } from "@/utils/cx";
import { formatTokens } from "@/utils/format";
import { EASE_OUT } from "@/lib/motion";

/**
Expand Down Expand Up @@ -117,14 +118,6 @@ const DEFAULT_LIMITS: UsageLimit[] = [
{ label: "Weekly · Pro", used: 0.05, resets: "Resets Tue 3:00 PM" },
];

/** 482_800 → "482.8k", 96_000 → "96k", 1_000_000 → "1M", 314 → "314". */
export function formatTokens(n: number) {
const short = (v: number) => (Number.isInteger(v) ? String(v) : v.toFixed(1));
if (n >= 1_000_000) return `${short(n / 1_000_000)}M`;
if (n >= 1_000) return `${short(n / 1_000)}k`;
return String(n);
}

const EASE = EASE_OUT;

/** Thin rounded track + fill, the same bar the earnings/steps cards use. */
Expand Down
4 changes: 1 addition & 3 deletions frontend/components/application/charts/area-chart-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type ChartRange,
} from "@/components/application/charts/chart-card";
import { cx } from "@/utils/cx";
import { compactNumber } from "@/utils/format";

/**
* Multi-series area chart card on Recharts `AreaChart` - the composition-over-
Expand Down Expand Up @@ -126,9 +127,6 @@ const DEFAULT_RANGES: AreaRange[] = [
},
];

const compactNumber = (n: number) =>
n >= 1000 ? `${Math.round(n / 100) / 10}K`.replace(".0K", "K") : String(Math.round(n));

export function AreaChartCard({
variant = "stacked",
shape = "curved",
Expand Down
4 changes: 1 addition & 3 deletions frontend/components/application/charts/combo-chart-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type ChartRange,
} from "@/components/application/charts/chart-card";
import { cx } from "@/utils/cx";
import { compactNumber } from "@/utils/format";

/**
* Combo chart card on Recharts `ComposedChart`: a bar series on the left axis
Expand Down Expand Up @@ -91,9 +92,6 @@ const DEFAULT_DATA = rowsOf(
[2.4, 2.6, 3.1, 2.9, 3.4, 3.8, 3.6, 4.1, 4.4, 4.2, 4.8, 5.2],
);

const compactNumber = (n: number) =>
n >= 1000 ? `${Math.round(n / 100) / 10}K`.replace(".0K", "K") : String(Math.round(n));

const percent = (n: number) => `${Math.round(n * 10) / 10}%`;

const DEFAULT_BAR: ComboSeries = { key: "sessions", label: "Sessions", format: formatNumber };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type ChartRange,
} from "@/components/application/charts/chart-card";
import { cx } from "@/utils/cx";
import { compactNumber } from "@/utils/format";

/**
* Scatter / bubble chart card on Recharts `ScatterChart` - the correlation
Expand Down Expand Up @@ -146,9 +147,6 @@ const DEFAULT_RANGES: ScatterRange[] = [
{ id: "year", label: "This year", series: scaleSeries(DEFAULT_SERIES, 3.6), delta: -0.019 },
];

const compactNumber = (n: number) =>
n >= 1000 ? `${Math.round(n / 100) / 10}K`.replace(".0K", "K") : String(Math.round(n));

/** Bubble area range, px². Big enough to read, small enough not to collide. */
const BUBBLE_RANGE: [number, number] = [90, 620];
/** Opacity of the other series while a point is hovered. */
Expand Down
8 changes: 3 additions & 5 deletions frontend/components/pro/agent-limits-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AnimatePresence, motion } from "motion/react";
import { useId, useState } from "react";
import { EASE_OUT } from "@/lib/motion";
import { cx } from "@/utils/cx";
import { compactNumber } from "@/utils/format";
import { formatTokens } from "@/utils/format";

export type ContextSegment = {
label: string;
Expand Down Expand Up @@ -60,10 +60,8 @@ export interface AgentLimitsCardProps {
const PALETTE = [6, 1, 5, 4, 3, 8, 7, 2].map((n) => `var(--color-chart-${n})`);
const DEFAULT_LIMITS: UsageLimit[] = [];

/** 314, 96k, 1.3M. */
export function formatTokens(n: number) {
return compactNumber(n).replace("K", "k");
}
/** Kept as an export: the private edition's card exports it too. */
export { formatTokens };

/** A 0-1 share as a whole percent, clamped to 0-100. */
const percent = (share: number) => `${Math.round(Math.min(1, Math.max(0, share)) * 100)}%`;
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/pro/composer-status-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import { RiFolder2Line, RiGitMergeLine, RiInfinityLine } from "@remixicon/react";
import type { ReactNode } from "react";
import { formatTokens } from "@/components/application/agent-limits/agent-limits-card";
import { Dropdown, DropdownPopover, DropdownTrigger } from "@/components/base/dropdown/dropdown";
import { UsageCard } from "@/components/pro/usage-card";
import { type LocatedRun, RunLocation } from "@/components/runners/run-location";
import { spendCapped, spendLabel, type SpendSnapshot } from "@/lib/spend";
import { cx } from "@/utils/cx";
import { formatTokens } from "@/utils/format";

export { formatTokens };

Expand Down
27 changes: 26 additions & 1 deletion frontend/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { relativeTime, relativeTimeShort } from "@/utils/format";
import { compactNumber, formatTokens, relativeTime, relativeTimeShort } from "@/utils/format";

/** A fixed clock, so every bucket is a pure function of the age under test. */
const NOW = Date.parse("2026-09-30T12:00:00.000Z");
Expand Down Expand Up @@ -83,3 +83,28 @@ describe("relativeTime input handling", () => {
expect(old).toBe(new Date(NOW - 400 * DAY).toLocaleDateString());
});
});

/**
* One row per edge of the compact formatters. The 999,950 and 1,234,567 rows are the point:
* a hand-rolled divide-and-round printed them as "1000.0k" and "1234.6K", because rounding
* up never moved the number into the next unit.
*/
const COMPACT: Array<[n: number, number: string, tokens: string]> = [
[0, "0", "0"],
[314, "314", "314"],
[999, "999", "999"],
[1000, "1K", "1k"],
[1500, "1.5K", "1.5k"],
[96_000, "96K", "96k"],
[482_800, "482.8K", "482.8k"],
[999_950, "1M", "1M"],
[1_234_567, "1.2M", "1.2M"],
[1_250_000, "1.3M", "1.3M"],
];

describe("compact numbers", () => {
test.each(COMPACT)("shortens %d for chart axes and token labels", (n, number, tokens) => {
expect(compactNumber(n)).toBe(number);
expect(formatTokens(n)).toBe(tokens);
});
});
9 changes: 8 additions & 1 deletion frontend/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ const compact = new Intl.NumberFormat("en", {
maximumFractionDigits: 1,
});

/** Compact number: 12480 → "12.5K", 1_200_000 → "1.2M". */
/** Compact number: 12480 → "12.5K", 1_200_000 → "1.2M". Rounding carries into the next
* unit, so 999_950 is "1M", never "1000K". Chart axes and usage totals use this. */
export function compactNumber(n: number): string {
return compact.format(n);
}

/** Token count: the compact number with a lowercase thousands mark, as token labels read:
* 314 → "314", 96_000 → "96k", 999_950 → "1M", 1_250_000 → "1.3M". */
export function formatTokens(n: number): string {
return compactNumber(n).replace("K", "k");
}

/** Rough per-run token cost for the clearly-estimated "tokens" headline. */
export const TOKENS_PER_RUN = 48_500;

Expand Down
Loading