Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/agent/state/StateBadges.tsx
iris 25965341e0 frontend: one tone vocabulary for badges, hints, banners and toasts
Part of the unification order in the UI sweep issue (step b).

A badge named its tones positive/warning/negative, a hint muted/warn/error/ok,
a banner info/warning/error and a toast info/ok/error. They now all take the
shared Tone: neutral, info, ok, warn, error (a badge adds accent for a selected
choice). Warn is amber everywhere (a hint used yellow), info is cyan everywhere
(a toast used purple). A test pins the list against each component's CSS.
2026-11-25 21:47:01 +01:00

115 lines
3.8 KiB
TypeScript

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<string, BadgeTone> = {
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<ReturnType<typeof useSwarmAgentStateStream>["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 (
<>
<Badge
tone={TURN_STATE_TONE[header.turn_state] ?? "neutral"}
value={
<>
{header.turn_state} (
<RelativeTime epochMs={Date.parse(header.turn_state_since)} />)
</>
}
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" && (
<Badge tone="warn" value={header.agent_state} />
)}
{withModel &&
(agent === undefined ? (
<Badge
icon={<ChipIcon />}
value={model}
title={inferenceTitle(
header.resolved_model,
header.effort_source,
false,
)}
ariaLabel={`model ${model}`}
/>
) : (
<BadgePopover
icon={<ChipIcon />}
value={model}
title={inferenceTitle(
header.resolved_model,
header.effort_source,
true,
)}
ariaLabel={`model ${model}`}
popoverLabel={`${agent} inference settings`}
>
<AgentInference agent={agent} />
</BadgePopover>
))}
{ctx !== null && (
<Badge
icon={<GaugeIcon />}
value={fmtTokens(ctx)}
title="tokens used in the current context window"
ariaLabel={`context ${fmtTokens(ctx)} tokens`}
/>
)}
</>
);
}