import { Badge, type BadgeTone } from "@hive/shared/badge.js"; import { ChipIcon, GaugeIcon } from "@hive/shared/icons.js"; import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js"; import { AgentInference } from "../detail/AgentInference.js"; import { inferenceTitle } from "./badge-titles.js"; import { RelativeTime } from "../../ui/relative-time/RelativeTime.js"; import { useSwarmAgentStateStream, type TokenUsage, } from "../terminal/useSwarmAgentStateStream.js"; // The turn-state vocabulary is glyphless text, so "idle" reads the same // wherever a turn state shows. const TURN_STATE_TONE: Record = { idle: "neutral", thinking: "accent", compacting: "warn", }; export function tokenTotal(u: TokenUsage | null): number | null { if (!u) return null; // Input plus both cache buckets; `output_tokens` is deliberately left // out, as this badge has no breakdown to put it in. return ( (u.input_tokens ?? 0) + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0) ); } function fmtTokens(n: number): string { if (n >= 1_000_000) return (n / 1_000_000).toFixed(1) + "M"; if (n >= 1_000) return Math.round(n / 1000) + "k"; return String(n); } // Turn state, agent state, the model with its effort, and the context size, as // the badges beside the connection one. Siblings only, no wrapper: they sit in // the caller's row. The model and the effort are one badge, "opus (high)", and // there is no cost badge: the cost is a metric in Grafana. export function StateBadges({ header, ctx, withModel = true, agent, }: { header: NonNullable["header"]>; ctx: number | null; // Off where the model is shown beside them, as in the details table. withModel?: boolean; // The agent whose settings the model badge opens in a popover when clicked. // Omitted: the badge only displays. agent?: string; }) { const model = `${header.model} (${header.effort})`; return ( <> {header.turn_state} ( ) } title="how long the agent has been in this state" /> {/* Only the non-boring case gets a badge — a healthy running agent (`agent_state: "up"`) doesn't need one for the obvious default. In practice this only ever fires for "paused": `hive-agent` can only honestly publish "up" or "paused" on this subject (see `AgentStateMsg::agent_state`'s own doc), never "offline"/"destroyed" — a stopped or destroyed agent publishes nothing at all. */} {header.agent_state !== "up" && ( )} {withModel && (agent === undefined ? ( } value={model} title={inferenceTitle( header.resolved_model, header.effort_source, false, )} ariaLabel={`model ${model}`} /> ) : ( } value={model} title={inferenceTitle( header.resolved_model, header.effort_source, true, )} ariaLabel={`model ${model}`} popoverLabel={`${agent} inference settings`} > ))} {ctx !== null && ( } value={fmtTokens(ctx)} title="tokens used in the current context window" ariaLabel={`context ${fmtTokens(ctx)} tokens`} /> )} ); }