Agent Crew
A lead and its sub-agents as a live tree of cards in their own colours: what each one is doing, its context and tools, with hand-offs running along the connectors.
"use client"
import * as React from "react"
import { ChevronDown, CornerDownRight, FolderClosed } from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandIcon,
BrandStack,
cx,
Edge,
elbowPath,
Face,
Mark,
Roll,
type AiMood,
type AiShape,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
export interface CrewAgent {
id: string
name: string
/** The model, shown under the name. */
model?: string
/** Brand mark beside the model, e.g. "claude" or "openai". */
vendor?: string
/** Id of the agent that started this one. The lead has none. */
parent?: string
tone: AiTone
/** Lead and team agents get a face; sub-agents get a pixel mark. */
shape?: AiShape
status: AiStatus
/** What the agent is doing right now. */
doing: string
/** Context used and context window, in tokens. */
used: number
limit: number
/** Tools it may call, as brand names. */
tools?: readonly string[]
/** The last hand-off it received: who sent it and what it says. */
note?: { from: string; text: string }
}
export interface AgentCrewProps {
title: string
agents: readonly CrewAgent[]
/** Ids of the edges a hand-off is travelling along right now (child ids). */
passing?: readonly string[]
/** Ids of team agents whose sub-agents are folded away. */
folded?: readonly string[]
onFold?: (id: string) => void
className?: string
}
const CSS = `
.aik-crew{width:min(100%,1060px);container-type:inline-size}
.aik-crew-head{display:flex;align-items:center;gap:10px;padding:0 6px 22px;font-size:15px}
.aik-crew-head b{font-weight:600;font-size:16px;letter-spacing:-.01em}
.aik-crew-head svg{color:var(--aik-text-3)}
.aik-crew-head .aik-crew-count{margin-left:auto;display:flex;gap:16px;color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-crew-head .aik-crew-count span:first-child{color:var(--aik-blue)}
.aik-crew-tree{position:relative;display:grid;justify-items:center;gap:46px}
.aik-crew-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-crew-port{fill:var(--aik-bg);stroke-width:1.5;transition:stroke var(--aik-d-panel)}
.aik-crew-row{display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));gap:18px;width:100%;align-items:start}
.aik-crew-col{display:grid;gap:14px;align-content:start}
.aik-crew-subs{display:grid;gap:12px;padding-left:34px;transform-origin:top left;transition:opacity 240ms var(--aik-ease-out),translate 320ms var(--aik-ease-move),scale 320ms var(--aik-ease-move),display 320ms allow-discrete}
.aik-crew-subs[data-folded]{display:none;opacity:0;translate:0 -12px;scale:.97}
@starting-style{.aik-crew-subs{opacity:0;translate:0 -12px;scale:.97}}
.aik-crew-subs > div{display:grid;gap:12px;padding:2px}
.aik-crew-node{--aik-r-shell:20px;--aik-rim:5px;width:100%;max-width:340px;position:relative;transition:opacity var(--aik-d-panel),transform var(--aik-d-move) var(--aik-ease-out);animation:aik-crew-in 520ms var(--aik-ease-soft) both;animation-delay:calc(var(--d,0)*70ms)}
.aik-crew-node[data-lead]{max-width:380px}
.aik-crew[data-focus] .aik-crew-node:not([data-lit]){opacity:.38}
.aik-crew-card{display:grid;gap:12px;padding:14px 14px 13px}
.aik-crew-top{display:flex;align-items:center;gap:11px;min-width:0}
.aik-crew-tile{display:grid;place-items:center;width:40px;height:40px;flex:none;border-radius:12px;background:color-mix(in oklab,var(--c) 13%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 26%,transparent)}
.aik-crew-node[data-sub] .aik-crew-tile{width:32px;height:32px;border-radius:10px}
.aik-crew-id{display:grid;min-width:0;line-height:19px}
.aik-crew-id b{font-weight:600;font-size:15px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-crew-id{flex:1 1 auto}
.aik-crew-id span{display:block;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:13px;color:var(--aik-text-3)}
.aik-crew-id span svg{display:inline-block;vertical-align:-1.5px;margin-right:5px}
.aik-crew-node{container-type:inline-size}
@container (max-width:250px){.aik-crew-state .aik-roll{display:none}.aik-crew-state{padding:0 4px}}
.aik-crew-state{margin-left:auto;display:inline-flex;align-items:center;gap:6px;height:26px;padding:0 9px 0 6px;border-radius:9px;font-size:12.5px;font-weight:500;color:var(--aik-text-3);background:var(--aik-raised);white-space:nowrap;transition:background var(--aik-d-state),color var(--aik-d-state)}
.aik-crew-state .aik-mark{transform:scale(.78)}
.aik-crew-state[data-s="run"]{color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-crew-state[data-s="ask"]{color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent)}
.aik-crew-state[data-s="done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 14%,transparent)}
.aik-crew-state[data-s="fail"]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 14%,transparent)}
.aik-crew-doing{font-size:15px;color:var(--aik-text-2);min-height:20px}
.aik-crew-node[data-s="run"] .aik-crew-doing,.aik-crew-node[data-s="ask"] .aik-crew-doing{color:var(--aik-text)}
.aik-crew-meter{display:flex;align-items:center;gap:12px;font:12.5px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-crew-bar{position:relative;flex:1;height:4px;border-radius:4px;background:var(--aik-line);overflow:hidden}
.aik-crew-bar i{position:absolute;inset:0;border-radius:inherit;background:var(--c);transform-origin:left;transform:scaleX(var(--p,0));transition:transform 900ms var(--aik-ease-move)}
.aik-crew-node[data-s="run"] .aik-crew-bar i{opacity:1}
.aik-crew-foot{display:flex;align-items:center;gap:9px;min-height:34px;padding:6px 8px 1px;font-size:13px;color:var(--aik-text-3);min-width:0}
.aik-crew-foot .aik-crew-noteroll{min-width:0;flex:1;display:grid;grid-template-columns:minmax(0,1fr)}
.aik-crew-noteroll .aik-roll-in{min-width:0;overflow:hidden}
.aik-crew-note{display:flex;align-items:center;gap:6px;min-width:0;white-space:nowrap}
.aik-crew-note b{font-weight:500;color:var(--aik-text-2)}
.aik-crew-note span{overflow:hidden;text-overflow:ellipsis}
.aik-crew-fold{display:grid;place-items:center;width:24px;height:24px;margin-left:auto;border-radius:7px;color:var(--aik-text-3);transition:background var(--aik-d-state),transform var(--aik-d-move) var(--aik-ease-out)}
.aik-crew-fold:hover{background:var(--aik-hover);color:var(--aik-text)}
.aik-crew-fold[aria-expanded="false"]{transform:rotate(-90deg)}
.aik-crew-node .aik-beam{--aik-beam-c:var(--c);animation:aik-beam 2.4s linear infinite}
@keyframes aik-crew-in{from{opacity:0;transform:translateY(8px) scale(.985)}}
@container (max-width:760px){
.aik-crew-row{grid-template-columns:minmax(0,1fr);gap:26px}
.aik-crew-tree{justify-items:stretch;gap:26px}
.aik-crew-node,.aik-crew-node[data-lead]{max-width:none}
.aik-crew-col{padding-left:26px}
.aik-crew-head .aik-crew-count span:last-child{display:none}
}
@media (prefers-reduced-motion: reduce){.aik-crew-node{animation:none}}
`
const MOOD: Record<AiStatus, AiMood> = {
todo: "idle",
run: "work",
ask: "needs",
done: "happy",
fail: "oops",
}
const STATE_LABEL: Record<AiStatus, string> = {
todo: "Idle",
run: "Working",
ask: "Needs you",
done: "Done",
fail: "Failed",
}
function tokens(n: number) {
if (n >= 1000) return `${Math.round(n / 1000)}k`
return String(n)
}
/**
* A lead agent and its team as one live tree of cards. Each branch carries its
* own colour through the face, the context bar and the connector, and a
* hand-off travels down the connector as a bright pulse in that colour.
*/
export function AgentCrew({
title,
agents,
passing = [],
folded = [],
onFold,
className,
}: AgentCrewProps) {
const root = React.useRef<HTMLDivElement>(null)
const nodes = React.useRef(new Map<string, HTMLElement>())
const [edges, setEdges] = React.useState<
Array<{
id: string
d: string
tone: AiTone
a: [number, number]
b: [number, number]
}>
>([])
const [focus, setFocus] = React.useState<string | null>(null)
const lead = agents.find((agent) => !agent.parent)
const team = agents.filter((agent) => lead && agent.parent === lead.id)
const subsOf = (id: string) => agents.filter((agent) => agent.parent === id)
const byId = new Map(agents.map((agent) => [agent.id, agent]))
// Which agents light up while one is focused: itself, its ancestors and descendants.
const lit = React.useMemo(() => {
if (!focus) return null
const set = new Set<string>([focus])
let up = byId.get(focus)?.parent
while (up) {
set.add(up)
up = byId.get(up)?.parent
}
const down = (id: string) =>
agents
.filter((a) => a.parent === id)
.forEach((a) => {
set.add(a.id)
down(a.id)
})
down(focus)
return set
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [focus, agents])
const tree = React.useRef<HTMLDivElement>(null)
// Layout boxes relative to the tree, ignoring transforms, so cards that are
// still sliding in (or lifted on hover) never bend the connectors.
const box = (node: HTMLElement) => {
let left = 0
let top = 0
let el: HTMLElement | null = node
while (el && el !== tree.current) {
left += el.offsetLeft
top += el.offsetTop
el = el.offsetParent as HTMLElement | null
}
return { left, top, width: node.offsetWidth, height: node.offsetHeight }
}
const measure = React.useCallback(() => {
if (!tree.current) return
const wide = (root.current?.clientWidth ?? 0) > 760
const next: typeof edges = []
for (const agent of agents) {
if (!agent.parent) continue
const from = nodes.current.get(agent.parent)
const to = nodes.current.get(agent.id)
if (!from || !to || to.offsetParent === null) continue
const f = box(from)
const t = box(to)
// Folded sub-agents keep their box; skip them so no line hangs in the air.
const holder = to.closest<HTMLElement>(".aik-crew-subs")
if (holder?.hasAttribute("data-folded")) continue
const isTeam = byId.get(agent.parent)?.parent === undefined
let a: [number, number]
let b: [number, number]
let d: string
if (isTeam && wide) {
a = [f.left + f.width / 2, f.top + f.height]
b = [t.left + t.width / 2, t.top]
d = elbowPath(a[0], a[1], b[0], b[1], "v", 12)
} else {
a = [f.left + (isTeam ? 10 : 22), f.top + f.height]
b = [t.left, t.top + Math.min(t.height / 2, 30)]
d = `M${a[0]} ${a[1]}V${b[1] - 10}Q${a[0]} ${b[1]} ${a[0] + 10} ${b[1]}H${b[0]}`
}
next.push({ id: agent.id, d, tone: agent.tone, a, b })
}
setEdges(next)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [agents])
React.useLayoutEffect(() => {
measure()
const observer = new ResizeObserver(() => measure())
if (root.current) observer.observe(root.current)
nodes.current.forEach((node) => observer.observe(node))
// Folding animates heights: follow it for the length of the move.
let frame = 0
const until = performance.now() + 520
const follow = () => {
measure()
if (performance.now() < until) frame = requestAnimationFrame(follow)
}
frame = requestAnimationFrame(follow)
return () => {
observer.disconnect()
cancelAnimationFrame(frame)
}
}, [measure, folded])
const register = (id: string) => (node: HTMLElement | null) => {
if (node) nodes.current.set(id, node)
else nodes.current.delete(id)
}
const working = agents.filter((agent) => agent.status === "run").length
const card = (agent: CrewAgent, depth: number) => {
const isSub = depth > 1
const hasSubs = subsOf(agent.id).length > 0
return (
<div
key={agent.id}
ref={register(agent.id)}
className="aik-crew-node aik-shell"
data-s={agent.status}
data-lead={depth === 0 ? "" : undefined}
data-sub={isSub ? "" : undefined}
data-lit={lit?.has(agent.id) ? "" : undefined}
style={
{
"--c": `var(--aik-f-${agent.tone})`,
"--d": depth * 2,
} as React.CSSProperties
}
onPointerEnter={() => setFocus(agent.id)}
onPointerLeave={() => setFocus(null)}
>
<div className="aik-surface aik-crew-card">
<span
className="aik-beam"
data-on={agent.status === "run" ? "" : undefined}
/>
<div className="aik-crew-top">
<span className="aik-crew-tile">
{isSub ? (
<AgentGlyph
seed={agent.id}
tone={agent.tone}
size={16}
live={agent.status === "run"}
/>
) : (
<Face
mood={MOOD[agent.status]}
shape={agent.shape ?? "squircle"}
tone={agent.tone}
size={28}
/>
)}
</span>
<span className="aik-crew-id">
<b>{agent.name}</b>
{agent.model ? (
<span>
{agent.vendor ? (
<BrandIcon name={agent.vendor} tint="brand" size={12} />
) : null}
{agent.model}
</span>
) : null}
</span>
<span className="aik-crew-state" data-s={agent.status}>
<Mark status={agent.status} />
<Roll>{STATE_LABEL[agent.status]}</Roll>
</span>
</div>
<p className="aik-crew-doing">
<Roll>{agent.doing}</Roll>
</p>
<div className="aik-crew-meter">
<span className="aik-crew-bar" aria-hidden="true">
<i
style={
{
"--p": Math.max(
0.02,
Math.min(1, agent.used / agent.limit)
),
} as React.CSSProperties
}
/>
</span>
<span>
{tokens(agent.used)} / {tokens(agent.limit)}
</span>
</div>
</div>
{agent.tools?.length || agent.note || hasSubs ? (
<div className="aik-crew-foot">
{agent.tools?.length ? (
<BrandStack names={agent.tools} size={22} />
) : null}
{agent.note ? (
<span className="aik-roll aik-crew-noteroll">
<span
key={agent.note.from + agent.note.text}
className="aik-roll-in"
>
<CrewNote from={agent.note.from} text={agent.note.text} />
</span>
</span>
) : (
<span style={{ flex: 1 }} />
)}
{hasSubs && onFold ? (
<button
type="button"
className="aik-crew-fold"
aria-expanded={!folded.includes(agent.id)}
aria-label={`${folded.includes(agent.id) ? "Show" : "Hide"} ${agent.name}'s sub-agents`}
onClick={() => onFold(agent.id)}
>
<ChevronDown size={15} />
</button>
) : null}
</div>
) : null}
</div>
)
}
if (!lead) return null
return (
<div
ref={root}
className={cx("aik aik-crew", className)}
data-focus={lit ? "" : undefined}
>
<AiKitStyles />
<AiStyle id="agent-crew" css={CSS} />
<div className="aik-crew-head">
<FolderClosed size={17} aria-hidden="true" />
<b>{title}</b>
<ChevronDown size={15} aria-hidden="true" />
<span className="aik-crew-count">
<span>
<Roll>{`${working} working`}</Roll>
</span>
<span>{agents.length} agents</span>
</span>
</div>
<div ref={tree} className="aik-crew-tree">
<svg className="aik-crew-svg" aria-hidden="true">
{edges.map((edge) => {
const child = byId.get(edge.id)
const live = passing.includes(edge.id)
const on = child && child.status !== "todo"
const dim = lit && !lit.has(edge.id)
return (
<g
key={edge.id}
style={{
opacity: dim ? 0.25 : 1,
transition: "opacity var(--aik-d-panel)",
}}
>
<Edge
d={edge.d}
tone={edge.tone}
state={live ? "live" : on ? "done" : "idle"}
width={1.5}
duration={1.1}
/>
<circle
className="aik-crew-port"
cx={edge.a[0]}
cy={edge.a[1]}
r={3.5}
stroke={
on || live
? `var(--aik-f-${edge.tone})`
: "var(--aik-line-3)"
}
/>
</g>
)
})}
</svg>
{card(lead, 0)}
<div
className="aik-crew-row"
style={{ "--n": team.length } as React.CSSProperties}
>
{team.map((member) => (
<div key={member.id} className="aik-crew-col">
{card(member, 1)}
{subsOf(member.id).length ? (
<div
className="aik-crew-subs"
data-folded={folded.includes(member.id) ? "" : undefined}
>
<div>{subsOf(member.id).map((sub) => card(sub, 2))}</div>
</div>
) : null}
</div>
))}
</div>
</div>
</div>
)
}
/** Renders a hand-off line; exported for custom footers. */
export function CrewNote({ from, text }: { from: string; text: string }) {
return (
<span className="aik-crew-note">
<CornerDownRight size={14} aria-hidden="true" />
<b>{from}</b>
<span>{text}</span>
</span>
)
}
Installation
One command adds agent-crew and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/agent-crew.json