Agent Router
A router agent sorting incoming requests: a light runs to the agent it picks with its confidence, and requests it is unsure about go to a person.
"use client"
import * as React from "react"
import { CornerDownRight, Mail, UserRound } from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Edge,
elbowPath,
Face,
Mark,
Roll,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
export interface RouterRequest {
id: string
subject: string
from: string
body: string
}
export interface RouterAgent {
id: string
name: string
model: string
vendor: string
tone: AiTone
shape?: AiShape
/** Requests waiting for this agent. */
queue: number
/** The last request it took. */
last?: string
}
export interface AgentRouterProps {
title?: string
request: RouterRequest
/** Reading the request, or done and sent on. */
phase: "reading" | "routed"
/** How sure the router is, 0 to 1. Below `threshold` it goes to a person. */
confidence: number
threshold?: number
/** Where the request went: an agent id or "human". */
route?: string | null
agents: readonly RouterAgent[]
/** Label of the person who takes unsure requests. */
human?: string
routedCount: number
className?: string
}
const CSS = `
.aik-rt{width:min(100%,1040px);container-type:inline-size}
.aik-rt-head{display:flex;align-items:center;gap:16px;padding:0 6px 20px;font-size:15px}
.aik-rt-head b{font-size:16px;font-weight:600;letter-spacing:-.01em}
.aik-rt-head > span{margin-left:auto;display:flex;gap:16px;color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-rt-head > span > span:first-child{color:var(--aik-blue)}
.aik-rt-map{position:relative;display:grid;gap:58px}
.aik-rt-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-rt-port{fill:var(--aik-bg);stroke-width:1.5;transition:stroke var(--aik-d-panel)}
.aik-rt-center{display:grid;justify-items:center}
.aik-rt-mid{position:relative;display:grid;grid-template-columns:1fr minmax(0,400px) 1fr;align-items:center;gap:0}
.aik-rt-box{--aik-r-shell:20px;--aik-rim:5px;width:100%;max-width:400px;position:relative}
.aik-rt-card{display:grid;gap:10px;padding:15px 16px}
.aik-rt-top{display:flex;align-items:center;gap:11px;min-width:0}
.aik-rt-tile{display:grid;place-items:center;width:40px;height:40px;flex:none;border-radius:12px;background:color-mix(in oklab,var(--c,var(--aik-text-3)) 13%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c,var(--aik-text-3)) 26%,transparent);color:var(--aik-text-2)}
.aik-rt-id{display:grid;min-width:0;line-height:19px}
.aik-rt-id b{font-size:15px;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-rt-id span{display:flex;align-items:center;gap:5px;font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-rt-pill{margin-left:auto;display:inline-flex;align-items:center;gap:5px;height:25px;padding:0 9px 0 6px;border-radius:8px;font-size:12.5px;font-weight:500;white-space:nowrap;color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-rt-pill .aik-mark{transform:scale(.72)}
.aik-rt-pill[data-s="done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 14%,transparent)}
.aik-rt-pill[data-s="ask"]{color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent)}
.aik-rt-body{font-size:14.5px;color:var(--aik-text-2);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-rt-req{animation:aik-rt-drop 560ms var(--aik-ease-soft) both}
@keyframes aik-rt-drop{from{opacity:0;transform:translateY(-14px)}}
.aik-rt-conf{display:flex;align-items:center;gap:12px;font:500 13px var(--aik-mono);color:var(--aik-text-2);font-variant-numeric:tabular-nums}
.aik-rt-bar{position:relative;flex:1;height:5px;border-radius:5px;background:var(--aik-line)}
.aik-rt-bar i{position:absolute;inset:0;border-radius:inherit;background:var(--cc);transform-origin:left;transition:transform 900ms var(--aik-ease-move),background var(--aik-d-panel);box-shadow:0 0 10px color-mix(in oklab,var(--cc) 55%,transparent)}
.aik-rt-bar b{position:absolute;top:-4px;bottom:-4px;width:1.5px;border-radius:1px;background:var(--aik-text-3)}
.aik-rt-why{display:flex;align-items:center;gap:6px;font-size:13.5px;color:var(--aik-text-3);min-width:0}
.aik-rt-why span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-rt-human{justify-self:start;margin-left:clamp(24px,6cqi,60px);display:inline-flex;align-items:center;gap:8px;height:36px;padding:0 13px 0 9px;border-radius:11px;background:var(--aik-card);box-shadow:var(--aik-sh-card);font-size:14px;font-weight:500;color:var(--aik-text-2);transition:color var(--aik-d-panel),box-shadow var(--aik-d-panel),background var(--aik-d-panel)}
.aik-rt-human[data-on]{color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 14%,var(--aik-card));box-shadow:0 0 0 1.5px var(--aik-amber),0 10px 26px -10px color-mix(in oklab,var(--aik-amber) 70%,transparent)}
.aik-rt-row{display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));gap:18px}
.aik-rt-agent{transition:transform var(--aik-d-move) var(--aik-ease-soft)}
.aik-rt-agent[data-on]{transform:translateY(-4px)}
.aik-rt-agent[data-on] .aik-rt-card{box-shadow:inset 0 0 0 1.5px color-mix(in oklab,var(--c) 65%,transparent),0 14px 34px -14px color-mix(in oklab,var(--c) 70%,transparent)}
.aik-rt-q{display:flex;align-items:center;gap:12px;font-size:13px;color:var(--aik-text-3);font-variant-numeric:tabular-nums;white-space:nowrap}
.aik-rt-q i{position:relative;flex:1;height:4px;border-radius:4px;background:var(--aik-line);overflow:hidden}
.aik-rt-q i b{position:absolute;inset:0;border-radius:inherit;background:var(--c);transform-origin:left;transition:transform 700ms var(--aik-ease-move)}
@container (max-width:760px){
.aik-rt-row{grid-template-columns:minmax(0,1fr)}
.aik-rt-mid{grid-template-columns:minmax(0,1fr)}
.aik-rt-human{grid-row:2;justify-self:end;margin:10px 0 0}
.aik-rt-mid > span:first-child{display:none}
.aik-rt-svg{display:none}
.aik-rt-map{gap:18px}
.aik-rt-box{max-width:none}
}
@container (max-width:460px){
.aik-rt-req .aik-rt-top{flex-wrap:wrap;row-gap:8px}
.aik-rt-req .aik-rt-id{flex:1 1 170px}
.aik-rt-req .aik-rt-pill{margin-left:51px}
}
@media (prefers-reduced-motion: reduce){.aik-rt-req{animation:none}}
`
/**
* A router agent sorting incoming requests. While it reads, a pulse runs in
* from the request; once it decides, a light runs to the chosen agent with
* the router's confidence, and anything under the threshold goes to a person.
*/
export function AgentRouter({
title = "Support inbox",
request,
phase,
confidence,
threshold = 0.7,
route,
agents,
human = "Human",
routedCount,
className,
}: AgentRouterProps) {
const map = React.useRef<HTMLDivElement>(null)
const els = React.useRef(new Map<string, HTMLElement>())
const [lines, setLines] = React.useState<
Array<{ id: string; d: string; a: [number, number]; b: [number, number] }>
>([])
const box = (node: HTMLElement) => {
let left = 0
let top = 0
let el: HTMLElement | null = node
while (el && el !== map.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(() => {
const req = els.current.get("req")
const router = els.current.get("router")
const person = els.current.get("human")
if (!req || !router) return
const r = box(req)
const o = box(router)
const next: typeof lines = []
const a: [number, number] = [r.left + r.width / 2, r.top + r.height]
const b: [number, number] = [o.left + o.width / 2, o.top]
next.push({ id: "in", d: `M${a[0]} ${a[1]}V${b[1]}`, a, b })
for (const agent of agents) {
const node = els.current.get(agent.id)
if (!node) continue
const t = box(node)
const p: [number, number] = [o.left + o.width / 2, o.top + o.height]
const q: [number, number] = [t.left + t.width / 2, t.top]
next.push({
id: agent.id,
d: elbowPath(p[0], p[1], q[0], q[1], "v", 14),
a: p,
b: q,
})
}
if (person) {
const h = box(person)
const p: [number, number] = [o.left + o.width, o.top + o.height / 2]
const q: [number, number] = [h.left, h.top + h.height / 2]
next.push({ id: "human", d: `M${p[0]} ${p[1]}H${q[0]}`, a: p, b: q })
}
setLines(next)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [agents.length])
React.useLayoutEffect(() => {
measure()
const observer = new ResizeObserver(() => measure())
if (map.current) observer.observe(map.current)
return () => observer.disconnect()
}, [measure])
const register = (id: string) => (node: HTMLElement | null) => {
if (node) els.current.set(id, node)
else els.current.delete(id)
}
const sure = confidence >= threshold
const chosen = phase === "routed" ? route : null
const target = agents.find((a) => a.id === chosen)
const confColor =
phase === "reading"
? "var(--aik-blue)"
: sure
? "var(--aik-green)"
: "var(--aik-amber)"
return (
<div className={cx("aik aik-rt", className)}>
<AiKitStyles />
<AiStyle id="agent-router" css={CSS} />
<div className="aik-rt-head">
<b>{title}</b>
<span>
<span>{phase === "reading" ? "1 routing" : "0 routing"}</span>
<span>
<Roll>{`${routedCount} routed`}</Roll>
</span>
</span>
</div>
<div ref={map} className="aik-rt-map">
<svg className="aik-rt-svg" aria-hidden="true">
{lines.map((line) => {
const agent = agents.find((a) => a.id === line.id)
let state: "idle" | "live" | "done" = "idle"
let tone: AiTone = agent?.tone ?? "blue"
if (line.id === "in") state = phase === "reading" ? "live" : "done"
else if (line.id === "human") {
tone = "amber"
state = chosen === "human" ? "live" : "idle"
} else if (chosen === line.id) state = "live"
return (
<g key={line.id}>
<Edge d={line.d} tone={tone} state={state} duration={1.1} />
{[line.a, line.b].map(([x, y], i) => (
<circle
key={i}
className="aik-rt-port"
cx={x}
cy={y}
r={4}
stroke={
state === "idle"
? "var(--aik-line-3)"
: `var(--aik-f-${tone})`
}
/>
))}
</g>
)
})}
</svg>
<div className="aik-rt-center">
<div ref={register("req")} className="aik-rt-box aik-shell">
<div
key={request.id}
className="aik-surface aik-rt-card aik-rt-req"
>
<div className="aik-rt-top">
<span className="aik-rt-tile">
<Mail size={18} />
</span>
<span className="aik-rt-id">
<b>{request.subject}</b>
<span>{request.from}</span>
</span>
<span
className="aik-rt-pill"
data-s={phase === "reading" ? "run" : sure ? "done" : "ask"}
>
<Mark
status={phase === "reading" ? "run" : sure ? "done" : "ask"}
/>
<Roll>
{phase === "reading"
? "Routing"
: sure
? `To ${target?.name ?? ""}`
: `To ${human}`}
</Roll>
</span>
</div>
<p className="aik-rt-body">{request.body}</p>
</div>
</div>
</div>
<div className="aik-rt-mid">
<span />
<div
ref={register("router")}
className="aik-rt-box aik-shell"
style={{ "--c": "var(--aik-f-blue)" } as React.CSSProperties}
>
<div className="aik-surface aik-rt-card">
<span
className="aik-beam"
data-on={phase === "reading" ? "" : undefined}
/>
<div className="aik-rt-top">
<span className="aik-rt-tile">
<Face
shape="orb"
tone="blue"
size={28}
mood={
phase === "reading" ? "think" : sure ? "happy" : "needs"
}
/>
</span>
<span className="aik-rt-id">
<b>Router</b>
<span>
<BrandIcon name="claude" tint="brand" size={12} />
Claude Haiku
</span>
</span>
</div>
<p className="aik-rt-body" style={{ color: "var(--aik-text)" }}>
<Roll>
{phase === "reading"
? `Reading “${request.subject}”`
: sure
? `Matched to ${target?.name}`
: "Not sure — asking a person"}
</Roll>
</p>
<div
className="aik-rt-conf"
style={{ "--cc": confColor } as React.CSSProperties}
>
<span className="aik-rt-bar">
<i
style={{ transform: `scaleX(${confidence.toFixed(3)})` }}
/>
<b
style={{ left: `${threshold * 100}%` }}
title="Threshold"
/>
</span>
<Roll>{`${Math.round(confidence * 100)}% sure`}</Roll>
</div>
</div>
</div>
<span
ref={register("human")}
className="aik-rt-human"
data-on={chosen === "human" ? "" : undefined}
>
<UserRound size={16} />
{human}
</span>
</div>
<div
className="aik-rt-row"
style={{ "--n": agents.length } as React.CSSProperties}
>
{agents.map((agent) => {
const on = chosen === agent.id
return (
<div
key={agent.id}
ref={register(agent.id)}
className="aik-rt-box aik-shell aik-rt-agent"
data-on={on ? "" : undefined}
style={
{
"--c": `var(--aik-f-${agent.tone})`,
maxWidth: "none",
} as React.CSSProperties
}
>
<div className="aik-surface aik-rt-card">
<div className="aik-rt-top">
<span className="aik-rt-tile">
<Face
shape={agent.shape ?? "squircle"}
tone={agent.tone}
size={26}
mood={on ? "work" : "idle"}
/>
</span>
<span className="aik-rt-id">
<b>{agent.name}</b>
<span>
<BrandIcon name={agent.vendor} tint="brand" size={12} />
{agent.model}
</span>
</span>
<span style={{ marginLeft: "auto" }}>
<Mark status={on ? "run" : "todo"} />
</span>
</div>
<p className="aik-rt-body">
<Roll>{on ? "Picking it up" : "Waiting for work"}</Roll>
</p>
<div className="aik-rt-q">
<i>
<b
style={{
transform: `scaleX(${Math.min(100, 8 + agent.queue * 16) / 100})`,
}}
/>
</i>
<Roll>{`${agent.queue} in queue`}</Roll>
</div>
<div className="aik-rt-why">
<CornerDownRight size={14} />
<span>
<Roll>{agent.last ?? "Nothing yet"}</Roll>
</span>
</div>
</div>
</div>
)
})}
</div>
</div>
</div>
)
}
Installation
One command adds agent-router 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-router.json