Agent status
One glyph for what an agent is doing. Its strokes travel between idle, thinking, working, waiting, done and failed, so a change of state reads as one motion, never an icon swap.
npx shadcn@latest add https://captain-ui.vercel.app/r/agent-status.json- AtlasEditing billing/invoice.tsWorking
- JunoWants to run pnpm db:resetWaiting for you
- RookPlanning tests for #482Thinking
- VegaDeploy health check timed outFailed
"use client";
// Captain UI · Agent status, MIT. One glyph that morphs between idle, thinking, working, waiting, done and failed: the strokes themselves move, nothing swaps.
import { AnimatePresence, animate, motion, useAnimationFrame, useMotionValue, useReducedMotion, useTransform } from "motion/react";
import { useEffect, useRef } from "react";
import { EASE, cx } from "@/registry/captain/ui/captain";
export type AgentState = "idle" | "thinking" | "working" | "waiting" | "done" | "failed";
type P = [number, number];
/** Three strokes, each M + two cubics = 7 points. Every state uses the same shape of data, so any state can morph into any other. */
type Glyph = [P[], P[], P[]];
const line = (a: P, b: P): P[] => {
const at = (t: number): P => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
return [a, at(1 / 6), at(2 / 6), at(3 / 6), at(4 / 6), at(5 / 6), b];
};
const dot = (p: P): P[] => Array.from({ length: 7 }, () => [p[0], p[1]] as P);
const K = 0.5523 * 7;
// Arc pieces of a circle r=7 around (12,12).
const TOP: P[] = [[5, 12], [5, 12 - K], [12 - K, 5], [12, 5], [12 + K, 5], [19, 12 - K], [19, 12]];
const QUARTER: P[] = [[19, 12], [19, 12 + K], [12 + K, 19], [12, 19], [12, 19], [12, 19], [12, 19]];
const GLYPHS: Record<AgentState, Glyph> = {
idle: [dot([12, 12]), dot([12, 12]), dot([12, 12])],
thinking: [dot([6.5, 12]), dot([12, 12]), dot([17.5, 12])],
working: [TOP, QUARTER, dot([12, 19])],
waiting: [line([9, 7.5], [9, 16.5]), line([15, 7.5], [15, 16.5]), dot([9, 16.5])],
done: [line([5.5, 12.5], [9.75, 16.75]), line([9.75, 16.75], [18.5, 7.25]), dot([9.75, 16.75])],
failed: [line([7, 7], [17, 17]), line([17, 7], [7, 17]), dot([12, 12])],
};
const TONE: Record<AgentState, string> = {
idle: "text-ink-3",
thinking: "text-ink-2",
working: "text-accent",
waiting: "text-warning",
done: "text-success",
failed: "text-danger",
};
const LABEL: Record<AgentState, string> = {
idle: "Idle",
thinking: "Thinking",
working: "Working",
waiting: "Waiting for you",
done: "Done",
failed: "Failed",
};
const flat = (g: Glyph) => g.flat().flat();
function toD(n: number[]) {
let d = "";
for (let s = 0; s < 3; s++) {
const o = s * 14;
const v = (i: number) => (n[o + i] ?? 0).toFixed(2);
d += `M${v(0)} ${v(1)}C${v(2)} ${v(3)} ${v(4)} ${v(5)} ${v(6)} ${v(7)}C${v(8)} ${v(9)} ${v(10)} ${v(11)} ${v(12)} ${v(13)}`;
}
return d;
}
type AgentStatusProps = {
state: AgentState;
/** Override the label; pass null to hide it (then aria-label still says the state). */
label?: string | null;
size?: number;
className?: string;
};
export function AgentStatus({ state, label, size = 18, className }: AgentStatusProps) {
const reduce = useReducedMotion();
const from = useRef(flat(GLYPHS[state]));
const cur = useRef(flat(GLYPHS[state]));
const to = useRef(flat(GLYPHS[state]));
const t = useMotionValue(1);
const wave = useMotionValue(0);
const spin = useMotionValue(0);
const d = useTransform([t, wave], ([p, w]: number[]) => {
const a = from.current;
const b = to.current;
const n = b.map((x, i) => (a[i] ?? x) + (x - (a[i] ?? x)) * (p as number));
// Thinking: the three dots rise one after another.
if (w) for (let s = 0; s < 3; s++) for (let i = 1; i < 14; i += 2) n[s * 14 + i]! -= Math.max(0, Math.sin((w as number) - s * 0.9)) * 2.2;
cur.current = n;
return toD(n);
});
useEffect(() => {
// Retarget from wherever the strokes are now, so a change mid-morph never jumps.
from.current = cur.current;
to.current = flat(GLYPHS[state]);
t.jump(0);
const a = animate(t, 1, reduce ? { duration: 0 } : { type: "spring", stiffness: 380, damping: 30, mass: 0.8 });
return () => a.stop();
}, [state, reduce, t]);
// Leaving "working", finish the turn instead of snapping back.
useEffect(() => {
if (state === "working" || spin.get() === 0) return;
const a = animate(spin, Math.ceil(spin.get() / 360) * 360, { duration: 0.35, ease: EASE, onComplete: () => spin.jump(0) });
return () => a.stop();
}, [state, spin]);
useAnimationFrame((time, delta) => {
if (reduce) return;
if (state === "working") spin.set(spin.get() + delta * 0.36);
wave.set(state === "thinking" ? (time / 220) % (Math.PI * 2) : 0);
});
const text = label === undefined ? LABEL[state] : label;
return (
<span role="status" aria-label={text ?? LABEL[state]} className={cx("inline-flex items-center gap-2", className)}>
<motion.svg width={size} height={size} viewBox="0 0 24 24" aria-hidden="true" className={cx("shrink-0 overflow-visible transition-colors duration-200", TONE[state])} style={{ rotate: spin }}>
<motion.path d={d} fill="none" stroke="currentColor" strokeWidth={2.25} strokeLinecap="round" strokeLinejoin="round" />
</motion.svg>
{text !== null ? (
<span className="relative inline-grid text-[13.5px] text-ink-2">
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={text}
className="col-start-1 row-start-1 whitespace-nowrap"
initial={{ opacity: 0, y: reduce ? 0 : 5 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: reduce ? 0 : -5 }}
transition={{ duration: 0.2, ease: EASE }}
>
{text}
</motion.span>
</AnimatePresence>
</span>
) : null}
</span>
);
}Usage
import { AgentStatus } from "@/components/ui/agent-status"
<AgentStatus state={run.state} />Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| state | "idle" | "thinking" | "working" | "waiting" | "done" | "failed" | Change it any time, even mid-morph: the strokes retarget from where they are. | |
| label | string | null | Overrides the quiet label. null hides it; the state is still announced. | |
| size | number | 18 | Glyph size in px. |
Accessibility
- role=status with the state as its accessible name, so changes are announced politely.
- Colour is never the only signal: every state has its own shape.
- Under reduced motion the shape changes at once and nothing spins or bobs.