"use client"; import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { contextWindow, sessionCost, turnCost, type ChatSettings, type TurnUsage } from "@/src/core"; import { formatTokens, formatUsd, prettyModel } from "@/components/usage-format"; import { ModelControl, catalogLimits, useModelCatalog } from "@/components/model-picker"; /** * The below-composer stats bar (wave 4 observability): the session's * money/context footprint as one quiet row beneath the composer — * LEFT-aligned: context window + session cost; RIGHT-aligned: model + * thinking level. Clicking the left side opens the per-turn detail popover. * * Pre-chat views (home, /chat/new) render NO left side at all (owner, * wave 7 sweep: the knowledge-context budget slider that used to live * there died with the injection; a "— · —" summary over an empty * popover is pure noise) — the dictation strip's middle simply widens. * * All folds come from core/prices.ts (sessionCost, contextWindow, turnCost) * — no cost math lives here. Absent data renders "—", never zero (the * doctrine): no turns yet → every figure is "—"; turns the price table * can't price count as unpriced, shown in the popover, never folded into * a fake $0. The RIGHT side is the model + thinking control (components/ * model-picker.tsx): one controller for the conversation agents, backed by * pi-ai's catalog; the buttons show the EFFECTIVE model (the record's * settings, else config's pin), while the left-side figures still fold the * turn log's actual billed models. */ export function StatsBar({ turns, dictationSlot, chatId, }: { turns: TurnUsage[]; /** The bound session (per-session models, owner 2026-09-28): present * on the in-session bar — the model control binds THE SESSION'S own * record; absent on the draft surfaces, where it binds the strong * slot (the new-session default). */ chatId?: string; /** The dictation strip (STT wave, 2026-09-17): the row's middle is dead * space between the session data and the model control — the mic * button lives there, and while recording the meter unfurls to fill * whatever width the row has (owner placement). */ dictationSlot?: ReactNode; }) { const [open, setOpen] = useState(false); // Same dropdown discipline as the model control (owner, df4433f treatment, // stats round 1): close on any pointerdown outside the bar or on Escape. const barRef = useRef(null); useEffect(() => { if (!open) return; const onPointerDown = (e: PointerEvent) => { if (barRef.current && !barRef.current.contains(e.target as Node)) setOpen(false); }; const onKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") setOpen(false); }; document.addEventListener("pointerdown", onPointerDown); document.addEventListener("keydown", onKeyDown); return () => { document.removeEventListener("pointerdown", onPointerDown); document.removeEventListener("keydown", onKeyDown); }; }, [open]); // The live-limits join (context-meter amendment, owner 2026-09-23): // contextWindow's denominator resolves ONLY from pi's live catalog — // built client-side over the picker's cached /api/models fetch (the // same fetch ModelControl below rides; module-level dedupe). Catalog // pending or failed → undefined index → limit null → USAGE-ONLY (the // honest-render precedent, never a fake window); the usage-only render // IS the null path. const { catalog } = useModelCatalog(); const limits = useMemo(() => (catalog ? catalogLimits(catalog) : undefined), [catalog]); const ctx = contextWindow({ turns }, limits); const { cost, unpriced } = sessionCost({ turns }); // Honest cost: show dollars only when at least one turn priced; // otherwise "—" (never a fake $0.0000). const pricedCount = turns.length - unpriced; const costLabel = pricedCount > 0 ? formatUsd(cost) : "—"; const lastModel = turns.at(-1)?.model; return (
{/* The summary button + its popover: the popover anchors to this wrapper, CENTERED over the button itself (owner, stats round 2) — not over the whole bar row, which made it line up oddly. The outside-close ref anchors HERE too (owner, round 3): on the whole bar it wrapped the model control, so clicking model/thinking never counted as outside — the asymmetry the owner observed. */} {/* The left side: the session-data summary — only once a session exists (turns). Pre-chat renders nothing (see the header). */} {turns.length > 0 && (
{/* No tooltip — the numbers describe themselves and the popover speaks on click (owner, subtlety round 2026-09-25). */} {open && (
Session stats
Context (last call) {ctx ? (ctx.limit !== null ? `${formatTokens(ctx.usedTokens)} / ${formatTokens(ctx.limit)}` : formatTokens(ctx.usedTokens)) : "—"}
Total cost {costLabel}
{unpriced > 0 && (
Unpriced turns {unpriced}
)}
{turns.length > 0 && ( <>
Turns
    {turns.map((turn, i) => { const c = turnCost(turn); return (
  • #{i + 1} · {prettyModel(turn.model, true)} {turn.steps.length} steps {c === null ? "—" : formatUsd(c)}
  • ); })}
)}
)}
)} {/* The middle slot: dictation (owner, 2026-09-17). flex-1 lets the meter expand into the row's free width; justify-center keeps the lone idle mic button visually centered. */} {dictationSlot && (
{dictationSlot}
)} {/* The model + thinking control — one controller for the conversation agents (prober/teacher); the record's `turns` above still folds the ACTUAL models billed, regardless of what the picker says. */}
); }