Agent Graph
Agents and the tools they call as a living network: a pulse runs along the link for every call and its answer, and an agent turns amber when it needs you.
"use client"
import * as React from "react"
import { Network, Search, UserRound } from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Edge,
elbowPath,
Face,
Roll,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
export type GraphNode =
| { id: string; kind: "you"; label: string; x: number; y: number }
| {
id: string
kind: "agent"
label: string
model: string
vendor?: string
tone: AiTone
shape?: AiShape
/** "needs" turns the agent amber and lights its line to you. */
state?: "idle" | "work" | "needs"
x: number
y: number
lead?: boolean
}
| {
id: string
kind: "tool"
label: string
brand?: string
x: number
y: number
}
export interface GraphCall {
/** Unique id for this call. */
key: string
from: string
to: string
/** "answer" runs the pulse back from `to` to `from`. */
dir?: "call" | "answer"
}
export interface AgentGraphProps {
nodes: readonly GraphNode[]
links: ReadonlyArray<readonly [string, string]>
calls?: readonly GraphCall[]
callsPerMinute?: number
cost?: number
/** Answer an agent that needs you. */
onAnswer?: (id: string) => void
className?: string
}
// Node positions are in this coordinate space; the stage keeps its aspect.
const W = 960
const H = 560
const CSS = `
.aik-gr{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,1040px);container-type:inline-size}
.aik-gr-head{display:flex;align-items:center;flex-wrap:wrap;gap:8px 18px;padding:8px 14px 14px;font-size:14.5px;color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-gr-head b{display:flex;align-items:center;gap:9px;margin-right:auto;font-size:16px;font-weight:600;color:var(--aik-text)}
.aik-gr-head b svg{color:var(--aik-blue)}
.aik-gr-head strong{color:var(--aik-text);font-weight:600}
.aik-gr-stage{position:relative;aspect-ratio:${W}/${H};overflow:hidden;background-image:radial-gradient(var(--aik-line) 1px,transparent 1.2px);background-size:22px 22px}
.aik-gr-scroll{border-radius:calc(var(--aik-r-shell,28px) - var(--aik-rim,6px));overflow-x:auto;overscroll-behavior-x:contain;scrollbar-width:none}
.aik-gr-scroll::-webkit-scrollbar{display:none}
@container (max-width:640px){.aik-gr-stage{min-width:640px}.aik-gr-scroll{-webkit-mask:linear-gradient(90deg,transparent,#000 9% 91%,transparent);mask:linear-gradient(90deg,transparent,#000 9% 91%,transparent)}}
.aik-gr-svg{position:absolute;inset:0;width:100%;height:100%}
.aik-gr-node{position:absolute;display:grid;justify-items:center;translate:-50% -50%;transition:opacity var(--aik-d-panel)}
.aik-gr[data-focus] .aik-gr-node:not([data-lit]){opacity:.3}
.aik-gr-tile{position:relative;display:grid;place-items:center;width:clamp(36px,6.2cqw,56px);height:clamp(36px,6.2cqw,56px);border-radius:16px;background:var(--aik-card);box-shadow:var(--aik-sh-card);transition:box-shadow var(--aik-d-panel),transform var(--aik-d-move) var(--aik-ease-out)}
.aik-gr-node[data-kind="tool"] .aik-gr-tile{width:clamp(30px,4.6cqw,44px);height:clamp(30px,4.6cqw,44px);border-radius:13px}
.aik-gr-node[data-lead] .aik-gr-tile{width:clamp(46px,8cqw,74px);height:clamp(46px,8cqw,74px);border-radius:22px}
.aik-gr-node[data-kind="you"] .aik-gr-tile{background:var(--aik-pri);color:var(--aik-pri-ink)}
.aik-gr-halo{position:absolute;inset:-60%;border-radius:50%;background:radial-gradient(closest-side,color-mix(in oklab,var(--c) 30%,transparent),transparent);opacity:0;transition:opacity 600ms;pointer-events:none;z-index:-1}
.aik-gr-node[data-state="work"] .aik-gr-halo{opacity:.9;animation:aik-gr-breathe 2.4s ease-in-out infinite}
.aik-gr-node[data-state="needs"] .aik-gr-tile{box-shadow:0 0 0 1.5px var(--aik-amber),0 0 0 6px color-mix(in oklab,var(--aik-amber) 18%,transparent);transform:scale(1.06)}
.aik-gr-node[data-state="needs"] .aik-gr-halo{--c:var(--aik-amber);opacity:1}
.aik-gr-node[data-state="needs"] .aik-gr-tile::after{content:"";position:absolute;inset:-8px;border-radius:inherit;border:1.5px solid var(--aik-amber);animation:aik-gr-ping 1.6s var(--aik-ease-out) infinite}
.aik-gr-label{position:absolute;top:calc(100% + 6px);left:50%;translate:-50% 0;display:grid;justify-items:center;line-height:17px;text-align:center;white-space:nowrap}
.aik-gr-label b{font-size:clamp(11.5px,1.5cqw,14.5px);font-weight:500}
.aik-gr-label span{font-size:clamp(10.5px,1.3cqw,13px);color:var(--aik-text-3)}
.aik-gr-node[data-state="needs"] .aik-gr-label span{color:var(--aik-amber)}
.aik-gr-node button.aik-gr-tile{cursor:pointer}
.aik-gr-badge{position:absolute;top:-5px;right:-5px;width:12px;height:12px;border-radius:50%;background:var(--aik-amber-fill);box-shadow:0 0 0 2.5px var(--aik-panel)}
@keyframes aik-gr-breathe{50%{opacity:.5}}
@keyframes aik-gr-ping{from{opacity:.9;transform:scale(.9)}to{opacity:0;transform:scale(1.35)}}
@container (max-width:560px){.aik-gr-label span{display:none}.aik-gr-head > span:nth-of-type(2){display:none}}
@media (prefers-reduced-motion: reduce){.aik-gr-halo,.aik-gr-tile::after{animation:none!important}}
`
const pathFor = (a: { x: number; y: number }, b: { x: number; y: number }) =>
Math.abs(a.x - b.x) >= Math.abs(a.y - b.y)
? elbowPath(a.x, a.y, b.x, b.y, "h", 16)
: elbowPath(a.x, a.y, b.x, b.y, "v", 16)
/**
* Agents and the tools they call as a living network. Every call sends a
* pulse along its link and every answer sends one back; an agent that needs
* you turns amber and its line to you lights up.
*/
export function AgentGraph({
nodes,
links,
calls = [],
callsPerMinute,
cost,
onAnswer,
className,
}: AgentGraphProps) {
const [focus, setFocus] = React.useState<string | null>(null)
// On narrow screens the network scrolls sideways; start it centred on the
// orchestrator instead of the left edge.
const scroller = React.useRef<HTMLDivElement>(null)
React.useLayoutEffect(() => {
const el = scroller.current
if (!el) return
const centre = () => {
el.scrollLeft = (el.scrollWidth - el.clientWidth) / 2
}
centre()
const ro = new ResizeObserver(centre)
ro.observe(el)
return () => ro.disconnect()
}, [])
const byId = React.useMemo(
() => new Map(nodes.map((n) => [n.id, n])),
[nodes]
)
const you = nodes.find((n) => n.kind === "you")
const needy = nodes.filter((n) => n.kind === "agent" && n.state === "needs")
const lit = React.useMemo(() => {
if (!focus) return null
const set = new Set([focus])
links.forEach(([a, b]) => {
if (a === focus) set.add(b)
if (b === focus) set.add(a)
})
return set
}, [focus, links])
const toneOf = (id: string): AiTone => {
const n = byId.get(id)
return n && n.kind === "agent" ? n.tone : "blue"
}
const allLinks: Array<readonly [string, string]> = [
...links,
...(you
? needy
.filter(
(n) =>
!links.some(
([a, b]) =>
(a === you.id && b === n.id) || (b === you.id && a === n.id)
)
)
.map((n) => [you.id, n.id] as const)
: []),
]
return (
<div
className={cx("aik aik-gr aik-shell", className)}
data-focus={lit ? "" : undefined}
>
<AiKitStyles />
<AiStyle id="agent-graph" css={CSS} />
<div className="aik-gr-head">
<b>
<Network size={17} aria-hidden="true" />
Agent graph
</b>
<span>{nodes.filter((n) => n.kind === "agent").length} agents</span>
<span>{nodes.filter((n) => n.kind === "tool").length} tools</span>
{callsPerMinute !== undefined ? (
<span>
<Roll>{String(callsPerMinute)}</Roll> calls/min
</span>
) : null}
{cost !== undefined ? (
<strong>
<Roll>{`$${cost.toFixed(2)}`}</Roll>
</strong>
) : null}
</div>
{/* On narrow screens the network keeps its spacing and scrolls sideways. */}
<div ref={scroller} className="aik-gr-scroll">
<div className="aik-surface aik-gr-stage">
<svg
className="aik-gr-svg"
viewBox={`0 0 ${W} ${H}`}
preserveAspectRatio="none"
aria-hidden="true"
>
{allLinks.map(([a, b]) => {
const A = byId.get(a)
const B = byId.get(b)
if (!A || !B) return null
const toYou =
(A.kind === "you" &&
B.kind === "agent" &&
B.state === "needs") ||
(B.kind === "you" && A.kind === "agent" && A.state === "needs")
const dim =
lit &&
!(lit.has(a) && lit.has(b) && (a === focus || b === focus))
return (
<g
key={`${a}-${b}`}
style={{
opacity: dim ? 0.2 : 1,
transition: "opacity var(--aik-d-panel)",
}}
>
<Edge
d={
toYou && A.kind !== "you"
? pathFor(A, B)
: toYou
? pathFor(B, A)
: pathFor(A, B)
}
tone={toYou ? "amber" : toneOf(A.kind === "agent" ? a : b)}
state={toYou ? "live" : "idle"}
width={toYou ? 1.75 : 1.25}
duration={1}
/>
</g>
)
})}
{calls.map((call) => {
const A = byId.get(call.from)
const B = byId.get(call.to)
if (!A || !B) return null
const d = call.dir === "answer" ? pathFor(B, A) : pathFor(A, B)
return (
<g key={call.key} className="aik-gr-call">
<Edge
d={d}
tone={toneOf(A.kind === "agent" ? call.from : call.to)}
state="live"
width={1.75}
duration={0.9}
/>
</g>
)
})}
</svg>
{nodes.map((node) => {
const style = {
left: `${(node.x / W) * 100}%`,
top: `${(node.y / H) * 100}%`,
"--c":
node.kind === "agent"
? `var(--aik-f-${node.tone})`
: "var(--aik-blue)",
} as React.CSSProperties
const hover = {
onPointerEnter: () => setFocus(node.id),
onPointerLeave: () => setFocus(null),
}
if (node.kind === "agent") {
const needs = node.state === "needs"
const Tile = needs && onAnswer ? "button" : "span"
return (
<div
key={node.id}
className="aik-gr-node"
data-kind="agent"
data-lead={node.lead ? "" : undefined}
data-state={node.state ?? "idle"}
data-lit={lit?.has(node.id) ? "" : undefined}
style={style}
{...hover}
>
<Tile
className="aik-gr-tile"
{...(Tile === "button"
? {
type: "button" as const,
onClick: () => onAnswer?.(node.id),
"aria-label": `Answer ${node.label}`,
}
: {})}
>
<span className="aik-gr-halo" />
<Face
tone={needs ? "amber" : node.tone}
shape={node.shape ?? "squircle"}
mood={
needs
? "needs"
: node.state === "work"
? "work"
: "idle"
}
size={node.lead ? 44 : 32}
/>
</Tile>
<span className="aik-gr-label">
<b>{node.label}</b>
<span>{needs ? "Needs you" : node.model}</span>
</span>
</div>
)
}
return (
<div
key={node.id}
className="aik-gr-node"
data-kind={node.kind}
data-lit={lit?.has(node.id) ? "" : undefined}
style={style}
{...hover}
>
<span className="aik-gr-tile">
{node.kind === "you" ? (
<UserRound size={20} aria-hidden="true" />
) : node.brand ? (
<BrandIcon name={node.brand} tint="brand" size={20} />
) : (
<Search
size={18}
aria-hidden="true"
style={{ color: "var(--aik-blue)" }}
/>
)}
{node.kind === "you" && needy.length ? (
<span className="aik-gr-badge" />
) : null}
</span>
<span className="aik-gr-label">
<b>{node.label}</b>
</span>
</div>
)
})}
</div>
</div>
</div>
)
}
Installation
One command adds agent-graph 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-graph.json