import * as React from "react"; import { api } from "../api/client"; import type { TokenUsageRecord } from "../api/client"; type SortKey = | "timestamp" | "story_id" | "agent_name" | "model" | "total_cost_usd"; type SortDir = "asc" | "desc"; function formatCost(usd: number): string { if (usd === 0) return "$0.00"; if (usd < 0.001) return `$${usd.toFixed(6)}`; if (usd < 0.01) return `$${usd.toFixed(4)}`; return `$${usd.toFixed(3)}`; } function formatTokens(n: number): string { if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`; return String(n); } function formatTimestamp(iso: string): string { const d = new Date(iso); const year = d.getFullYear(); const month = String(d.getMonth() + 1).padStart(2, "0"); const day = String(d.getDate()).padStart(2, "0"); const h = String(d.getHours()).padStart(2, "0"); const m = String(d.getMinutes()).padStart(2, "0"); return `${year}-${month}-${day} ${h}:${m}`; } /** Infer an agent type from the agent name. */ function agentType(agentName: string): string { const lower = agentName.toLowerCase(); if (lower.startsWith("coder")) return "coder"; if (lower.startsWith("qa")) return "qa"; if (lower.startsWith("mergemaster") || lower.startsWith("merge")) return "mergemaster"; return "other"; } interface SortHeaderProps { label: string; sortKey: SortKey; current: SortKey; dir: SortDir; onSort: (key: SortKey) => void; align?: "left" | "right"; } function SortHeader({ label, sortKey, current, dir, onSort, align = "left", }: SortHeaderProps) { const active = current === sortKey; return (
Loading...
)} {error &&{error}
} {!loading && !error && records.length === 0 && (No token usage records found.
)} {!loading && !error && records.length > 0 && (| Input | Cache+ | Cache↩ | Output | |||||
|---|---|---|---|---|---|---|---|---|
| {formatTimestamp(r.timestamp)} | {r.story_id} | {r.agent_name} | {r.model ?? "—"} | {formatTokens(r.input_tokens)} | {formatTokens(r.cache_creation_input_tokens)} | {formatTokens(r.cache_read_input_tokens)} | {formatTokens(r.output_tokens)} | {formatCost(r.total_cost_usd)} |