import { Heading } from "@radix-ui/themes"; import type { CacheHistory, CacheRecord } from "../model.js"; import { CacheTrend } from "./cache-trend.js"; import "./cache.css"; function numeric(value: number | null | undefined, suffix = ""): string { return value == null ? "unavailable" : `${value.toLocaleString(undefined, { maximumFractionDigits: 2 })}${suffix}`; } function money(value: number | null | undefined): string { return value == null ? "unavailable" : `$${value.toFixed(6)}`; } function Record({ record, select }: { record: CacheRecord; select(id: string): void }) { const comparison = record.comparison; return (
{record.kind} · {record.status} {record.provider} / {record.model} Hit {numeric(record.hitRatePercent, "%")}
Input tokens
{numeric(record.input)}
Cache read tokens
{numeric(record.cacheRead)}
Cache write tokens
{numeric(record.cacheWrite)}
Prompt tokens
{numeric(record.promptTokens)}
Prompt cost
{money(record.promptCost)}
Estimated savings
{money(record.estimatedSavings)}
Cost source
{record.costSource}
Prefix epoch
{record.epoch}
{record.kind !== "assistant" && (

Auxiliary usage: included in totals, not request comparisons or trend.

)} {comparison ? (
Hit-rate change
{numeric(comparison.hitRateDeltaPercent, " pp")}
Hit-rate loss
{numeric(comparison.hitRateLossPercent, " pp")}
Prompt-token change
{numeric(comparison.promptTokenDelta)}
Cache-read change
{numeric(comparison.cacheReadDelta)}
Eligible tokens (estimate)
{numeric(comparison.eligibleTokens)}
Re-billed tokens (estimate)
{numeric(comparison.rebilledTokens)}
Estimated miss premium
{money(comparison.estimatedMissPremium)}
Request start gap
{numeric(comparison.requestStartGapMs, " ms")}
{comparison.modelChanged &&

Provider or model changed; token counts do not prove prefix reuse.

}
) : (

Comparison unavailable: no adjacent comparable response in this epoch.

)}
); } export function Cache({ report, select }: { report?: CacheHistory; select(id: string): void }) { if (!report) return (
Cache usage

Cache history unavailable in this snapshot.

); return (
Cache usage

Active branch leaf: {report.leafId ?? "none"}

Recorded session usage, not a transport-attempt log. Costs and reusable-prefix metrics are estimates. No streaming preview.

{!report.complete &&

Cache data is incomplete; totals are not a complete branch report.

} {report.unavailable ? (

Cache history unavailable: {report.unavailable}

) : ( <>
Usage records
{report.totals.count}
Weighted hit rate
{numeric(report.totals.hitRatePercent, "%")}
Input tokens
{numeric(report.totals.input)}
Cache read tokens
{numeric(report.totals.cacheRead)}
Cache write tokens
{numeric(report.totals.cacheWrite)}
Prompt tokens
{numeric(report.totals.promptTokens)}
Prompt cost
{money(report.totals.promptCost)}
Estimated savings
{money(report.totals.estimatedSavings)}
Recorded usage

Showing {report.records.length} of {report.totalRecords} records · {report.omittedRecords} older records omitted. Totals include all validated branch records.

{report.records.length === 0 &&

No recorded usage on this branch.

}
{report.records.map((record) => ( ))}
)}
); }