// --------------------------------------------------------------------------- // account/UsagePage.tsx — WAVE 35 · W35-T19 (owner item 13, ruling R9, C6/C7). // // R9: ONE meter for every AI surface. It REPORTS weekly usage against an // allowance and does not cut anyone off this wave; over the allowance the // product still works and the bar is red. // // ⛔⛔ THIS PAGE MAY NOT INVENT A FIGURE, AND THAT IS THE WHOLE TICKET. // Every number here comes off `GET /usage` — the week, the reset date, the // allowance, each surface's tokens and calls. Nothing is derived from a clock, // nothing is estimated, and a surface with no ledger lines shows **0**, not a // blank and not an omission (E-9 guarantees all four surfaces are always // present, so there is no missing-row branch to get wrong). // [[no-unverifiable-aggregates]] // // ⛔ AND `unmeasured` IS RENDERED, NOT SWALLOWED. Those are real calls whose // provider declined to report a token count, so the total beside them is a // FLOOR. A page that hides them presents the floor as a complete figure, which // is the cost-surprise failure one step removed — the same reason // `usage_ledger` books an unmeasured call as unknown rather than as zero. // // ⚠ SURFACE/READ SPLIT, as on Home and Starred: `renderToStaticMarkup` runs no // effect, so a shot of the fetching component photographs only its spinner. // --------------------------------------------------------------------------- import { useCallback, useEffect, useRef, useState } from "react"; import { fmt } from "../ui/fmt"; import { loadUsage } from "./accountApi"; import type { Usage, UsageSurface } from "./accountApi"; import TurnAuditTable from "./TurnAuditTable"; import "./account.css"; /** * The four surfaces C7 names, in the order R9 lists them. * * ⚠ AN UNKNOWN KEY IS HUMANISED, NEVER DROPPED. Dropping it would hide real usage from a meter * whose whole job is to account for it, and printing the raw key would put an internal identifier * on screen. The day a fifth LLM entry point lands (this product added two in one wave) it shows * up here as a readable name with the right number beside it, and somebody can then decide what * to call it. */ const SURFACE_LABEL: Record = { assistant: "Assistant", field_agent: "Field agents", ai_review: "Reviews", automation_draft: "Drafting", }; const ORDER = ["assistant", "field_agent", "ai_review", "automation_draft"]; export function surfaceLabel(key: string): string { const known = SURFACE_LABEL[key]; if (known) return known; const words = key.replace(/[_-]+/g, " ").trim(); return words ? words.charAt(0).toUpperCase() + words.slice(1) : key; } /** C7's order first, then anything the server added, alphabetically. */ export function orderSurfaces(surfaces: UsageSurface[]): UsageSurface[] { const rank = (s: UsageSurface) => { const i = ORDER.indexOf(s.surface); return i === -1 ? ORDER.length : i; }; return [...surfaces].sort( (a, b) => rank(a) - rank(b) || surfaceLabel(a.surface).localeCompare(surfaceLabel(b.surface)) ); } /** * The bar's fill, as a percentage, CLAMPED to 100. * * ⚠ The clamp is what keeps "over the allowance" a COLOUR rather than a bar that overflows its * own track and paints across the page. The number above the bar is unclamped and is where the * overage is actually read. * ⚠ An allowance of 0 (unset) yields 0 rather than a division by zero: no allowance means the * meter has nothing to measure against, and a full red bar would be an assertion nobody made. */ export function barPct(value: number, of: number): number { if (!(of > 0)) return 0; return Math.max(0, Math.min(100, (value / of) * 100)); } export type UsageLoad = | { phase: "loading" } | { phase: "ready"; usage: Usage } | { phase: "failed"; message: string }; function Bar({ pct, over }: { pct: number; over: boolean }) { return (