Agent Handoff
A task passed down a relay of agents: at each hand-off, what went across to the next one and what got lost on the way.
"use client"
import * as React from "react"
import { ArrowRight } from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Edge,
elbowPath,
Face,
Mark,
Roll,
type AiMood,
type AiShape,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
export interface HandoffAgent {
id: string
name: string
model: string
/** Brand mark for the model's maker. */
vendor: string
tone: AiTone
shape?: AiShape
status: AiStatus
/** Spend so far, preformatted, e.g. "$1.31". */
cost: string
/** What this agent is for. */
role: string
/** The task in its hands, if any. */
task?: { brand: string; title: string; state: string }
}
export interface Handoff {
from: string
to: string
/** The work being handed over. */
title: string
brand: string
/** What made it across. */
carried: readonly string[]
/** What got dropped on the way. */
lost?: readonly string[]
state: "waiting" | "passing" | "landed"
}
export interface AgentHandoffProps {
agents: readonly HandoffAgent[]
handoffs: readonly Handoff[]
className?: string
}
const CSS = `
.aik-ho{width:min(100%,1060px);container-type:inline-size}
.aik-ho-wrap{position:relative;display:grid;gap:64px}
.aik-ho-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-ho-port{fill:var(--aik-bg);stroke-width:1.5;transition:stroke var(--aik-d-panel)}
.aik-ho-agents{display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));gap:18px}
.aik-ho-agent{--aik-r-shell:22px;--aik-rim:5px;position:relative;transition:transform var(--aik-d-move) var(--aik-ease-out)}
.aik-ho-agent[data-s="run"]{transform:translateY(-3px)}
.aik-ho-card{display:grid;gap:12px;padding:16px}
.aik-ho-top{display:flex;align-items:center;gap:11px;min-width:0}
.aik-ho-tile{display:grid;place-items:center;width:42px;height:42px;flex:none;border-radius:13px;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-ho-id{display:grid;min-width:0;line-height:19px}
.aik-ho-id b{font-size:15.5px;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-ho-id span{display:flex;align-items:center;gap:5px;font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden}
.aik-ho-right{margin-left:auto;flex:none;display:flex;align-items:center;gap:8px;font:500 13px var(--aik-mono);color:var(--aik-text-2);font-variant-numeric:tabular-nums}
.aik-ho-state{display:inline-flex;align-items:center;gap:5px;height:24px;padding:0 8px 0 5px;border-radius:8px;font:500 12px var(--aik-sans);color:var(--aik-text-3);background:var(--aik-raised);transition:background var(--aik-d-state),color var(--aik-d-state)}
.aik-ho-state .aik-mark{transform:scale(.72)}
.aik-ho-state[data-s="run"]{color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-ho-state[data-s="done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 14%,transparent)}
.aik-ho-state[data-s="ask"]{color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent)}
.aik-ho-role{font-size:14.5px;line-height:21px;color:var(--aik-text-2);min-height:42px}
.aik-ho-task{display:flex;align-items:center;gap:9px;height:44px;padding:0 12px;border-radius:12px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);font-size:14px;min-width:0}
.aik-ho-task b{font-weight:500;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;min-width:0}
.aik-ho-task span{margin-left:auto;color:var(--aik-text-3);white-space:nowrap;flex:none}
.aik-ho-task[data-empty]{color:var(--aik-text-3)}
.aik-ho-agent .aik-beam{--aik-beam-c:var(--c)}
.aik-ho-packs{display:grid;align-items:start;grid-template-columns:repeat(var(--cols),minmax(0,1fr));gap:18px}
.aik-ho-pack{--aik-r-shell:20px;--aik-rim:5px;grid-column:var(--at) / span 2;justify-self:center;width:min(100%,440px);transition:opacity var(--aik-d-panel),transform var(--aik-d-move) var(--aik-ease-soft)}
.aik-ho-pack[data-state="waiting"]{opacity:.42;transform:translateY(6px)}
.aik-ho-ph{display:flex;align-items:center;gap:8px;height:36px;padding:0 10px;font-size:14px;color:var(--aik-text-2)}
.aik-ho-ph svg{color:var(--aik-text-3)}
.aik-ho-ph em{margin-left:auto;display:grid;place-items:center;min-width:24px;height:22px;border-radius:7px;background:var(--aik-raised);font:500 12px var(--aik-mono);font-style:normal}
.aik-ho-body{display:grid;gap:10px;padding:14px 16px}
.aik-ho-body > div:first-child{display:flex;align-items:center;gap:9px;font-size:15px;font-weight:500}
.aik-ho-items{display:flex;flex-wrap:wrap;gap:6px}
.aik-ho-items span{display:inline-flex;align-items:center;gap:5px;height:26px;padding:0 9px;border-radius:8px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);font-size:13px;color:var(--aik-text-2);animation:aik-ho-item 420ms var(--aik-ease-soft) both;animation-delay:calc(var(--i)*110ms)}
.aik-ho-items span[data-lost]{color:var(--aik-red);text-decoration:line-through;background:color-mix(in oklab,var(--aik-red) 9%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-red) 22%,transparent);animation:aik-ho-lost 620ms var(--aik-ease-out) both;animation-delay:calc(var(--i)*110ms)}
@keyframes aik-ho-item{from{opacity:0;transform:translateY(6px) scale(.96)}}
@keyframes aik-ho-lost{0%{opacity:0;transform:translateX(0)}35%{opacity:1;transform:translateX(-3px)}55%{transform:translateX(3px)}75%{transform:translateX(-1px)}}
.aik-ho-verdict{display:flex;align-items:center;gap:8px;font-size:13.5px;color:var(--aik-text-2)}
.aik-ho-verdict i{width:7px;height:7px;border-radius:50%;background:var(--aik-green)}
.aik-ho-verdict[data-lost] i{background:var(--aik-red)}
.aik-ho-pack[data-state="waiting"] .aik-ho-verdict i{background:var(--aik-line-3)}
.aik-ho-pack[data-state="passing"] .aik-ho-verdict i{background:var(--aik-blue);animation:aik-glyph 1s ease-in-out infinite}
.aik-ho-skel{position:relative;overflow:hidden;height:26px;border-radius:8px;background:var(--aik-sunk)}
.aik-ho-skel::after{content:"";position:absolute;inset:0;background:linear-gradient(90deg,transparent,var(--aik-raised),transparent);transform:translateX(-100%);animation:aik-ho-sh 1.4s var(--aik-ease-move) infinite}
@keyframes aik-ho-sh{to{transform:translateX(100%)}}
@container (max-width:760px){
.aik-ho-agents,.aik-ho-packs{grid-template-columns:minmax(0,1fr)}
.aik-ho-pack{grid-column:auto;width:100%}
.aik-ho-svg{display:none}
.aik-ho-wrap{gap:22px}
.aik-ho-role{min-height:0}
}
@container (max-width:440px){
.aik-ho-right{flex-direction:column-reverse;align-items:flex-end;gap:3px}
}
@media (prefers-reduced-motion: reduce){.aik-ho-items span,.aik-ho-skel::after{animation:none}}
`
const MOOD: Record<AiStatus, AiMood> = {
todo: "idle",
run: "work",
ask: "needs",
done: "happy",
fail: "oops",
}
const LABEL: Record<AiStatus, string> = {
todo: "Idle",
run: "Working",
ask: "Needs you",
done: "Done",
fail: "Failed",
}
/**
* A task passed down a relay of agents. Each hand-off sits between the two
* agents it joins and shows what went across and what got lost; a pulse runs
* down the connector while it is passing.
*/
export function AgentHandoff({
agents,
handoffs,
className,
}: AgentHandoffProps) {
const wrap = React.useRef<HTMLDivElement>(null)
const els = React.useRef(new Map<string, HTMLElement>())
const [lines, setLines] = React.useState<
Array<{
key: string
d: string
a: [number, number]
b: [number, number]
tone: AiTone
hand: number
}>
>([])
const byId = new Map(agents.map((a) => [a.id, a]))
const box = (node: HTMLElement) => {
let left = 0
let top = 0
let el: HTMLElement | null = node
while (el && el !== wrap.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 (!wrap.current) return
const next: typeof lines = []
handoffs.forEach((hand, i) => {
const pack = els.current.get(`pack-${i}`)
if (!pack) return
const p = box(pack)
;[hand.from, hand.to].forEach((id, side) => {
const node = els.current.get(id)
const agent = byId.get(id)
if (!node || !agent) return
const c = box(node)
const a: [number, number] = [c.left + c.width / 2, c.top + c.height]
const b: [number, number] = [
p.left + p.width * (side === 0 ? 0.24 : 0.76),
p.top,
]
next.push({
key: `${i}-${side}`,
d: elbowPath(a[0], a[1], b[0], b[1], "v", 14),
a,
b,
tone: agent.tone,
hand: i,
})
})
})
setLines(next)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [handoffs, agents])
React.useLayoutEffect(() => {
measure()
const observer = new ResizeObserver(() => measure())
if (wrap.current) observer.observe(wrap.current)
return () => observer.disconnect()
}, [measure])
const register = (id: string) => (node: HTMLElement | null) => {
if (node) els.current.set(id, node)
else els.current.delete(id)
}
// Hand-off i sits under the gap between agent i and agent i + 1.
const cols = agents.length * 2
return (
<div className={cx("aik aik-ho", className)}>
<AiKitStyles />
<AiStyle id="agent-handoff" css={CSS} />
<div ref={wrap} className="aik-ho-wrap">
<svg className="aik-ho-svg" aria-hidden="true">
{lines.map((line) => {
const hand = handoffs[line.hand]
const state =
hand.state === "passing"
? "live"
: hand.state === "landed"
? "done"
: "idle"
return (
<g key={line.key}>
<Edge
d={line.d}
tone={line.tone}
state={state}
duration={1.2}
/>
{[line.a, line.b].map(([x, y], i) => (
<circle
key={i}
className="aik-ho-port"
cx={x}
cy={y}
r={4}
stroke={
state === "idle"
? "var(--aik-line-3)"
: `var(--aik-f-${line.tone})`
}
/>
))}
</g>
)
})}
</svg>
<div
className="aik-ho-agents"
style={{ "--n": agents.length } as React.CSSProperties}
>
{agents.map((agent) => (
<div
key={agent.id}
ref={register(agent.id)}
className="aik-ho-agent aik-shell"
data-s={agent.status}
style={
{ "--c": `var(--aik-f-${agent.tone})` } as React.CSSProperties
}
>
<div className="aik-surface aik-ho-card">
<span
className="aik-beam"
data-on={agent.status === "run" ? "" : undefined}
/>
<div className="aik-ho-top">
<span className="aik-ho-tile">
<Face
mood={MOOD[agent.status]}
shape={agent.shape ?? "squircle"}
tone={agent.tone}
size={30}
/>
</span>
<span className="aik-ho-id">
<b>{agent.name}</b>
<span>
<BrandIcon name={agent.vendor} tint="brand" size={12} />
{agent.model}
</span>
</span>
<span className="aik-ho-right">
<Roll>{agent.cost}</Roll>
<span className="aik-ho-state" data-s={agent.status}>
<Mark status={agent.status} />
<Roll>{LABEL[agent.status]}</Roll>
</span>
</span>
</div>
<p className="aik-ho-role">{agent.role}</p>
{agent.task ? (
<div className="aik-ho-task">
<BrandIcon name={agent.task.brand} tint="brand" size={16} />
<b>{agent.task.title}</b>
<span>
<Roll>{agent.task.state}</Roll>
</span>
</div>
) : (
<div className="aik-ho-task" data-empty="">
Ready for a task
</div>
)}
</div>
</div>
))}
</div>
<div
className="aik-ho-packs"
style={{ "--cols": cols } as React.CSSProperties}
>
{handoffs.map((hand, i) => {
const from = byId.get(hand.from)
const to = byId.get(hand.to)
const lost = hand.lost ?? []
return (
<div
key={`${hand.from}-${hand.to}`}
ref={register(`pack-${i}`)}
className="aik-ho-pack aik-shell"
data-state={hand.state}
style={{ "--at": i * 2 + 2 } as React.CSSProperties}
>
<div className="aik-ho-ph">
{from ? (
<Face
mood="idle"
shape={from.shape}
tone={from.tone}
size={18}
/>
) : null}
{from?.name}
<ArrowRight size={14} aria-hidden="true" />
{to ? (
<Face
mood="idle"
shape={to.shape}
tone={to.tone}
size={18}
/>
) : null}
{to?.name}
<em>
{hand.state === "landed"
? hand.carried.length + lost.length
: "–"}
</em>
</div>
<div className="aik-surface aik-ho-body">
<div>
<BrandIcon name={hand.brand} tint="brand" size={17} />
{hand.title}
</div>
{hand.state === "waiting" ? (
<div className="aik-ho-skel" style={{ width: "70%" }} />
) : (
<div className="aik-ho-items" key={hand.state}>
{hand.carried.map((item, k) => (
<span
key={item}
style={{ "--i": k } as React.CSSProperties}
>
{item}
</span>
))}
{hand.state === "landed"
? lost.map((item, k) => (
<span
key={item}
data-lost=""
style={
{
"--i": hand.carried.length + k,
} as React.CSSProperties
}
>
{item}
</span>
))
: null}
</div>
)}
<div
className="aik-ho-verdict"
data-lost={
lost.length && hand.state === "landed" ? "" : undefined
}
>
<i />
<Roll>
{hand.state === "waiting"
? "Not handed over yet"
: hand.state === "passing"
? "Handing over…"
: lost.length
? `${lost.length} thing${lost.length > 1 ? "s" : ""} lost on the way`
: "Everything made it"}
</Roll>
</div>
</div>
</div>
)
})}
</div>
</div>
</div>
)
}
Installation
One command adds agent-handoff 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-handoff.json