Agent Org Chart
People and the agents they own on one chart: reporting lines between them, and an escalation that climbs the lines to the person who owns the agent.
"use client"
import * as React from "react"
import { ArrowRight } from "lucide-react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
BrandStack,
cx,
Edge,
elbowPath,
Face,
Roll,
type AiShape,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
export interface OrgPerson {
kind: "person"
id: string
name: string
role: string
tone: AiTone
parent?: string
}
export interface OrgAgent {
kind: "agent"
id: string
name: string
model: string
vendor: string
tone: AiTone
shape?: AiShape
/** The person who owns this agent. */
parent: string
status: AiStatus
tools?: readonly string[]
}
export type OrgNode = OrgPerson | OrgAgent
export interface OrgEscalation {
/** The agent that is asking. */
from: string
/** What it asks, shown on the card of whoever holds it now. */
ask: string
/** How many steps up the chain it has climbed (1 = its owner). */
hops: number
}
export interface AgentOrgChartProps {
title: string
nodes: readonly OrgNode[]
escalation?: OrgEscalation | null
onResolve?: () => void
className?: string
}
const CSS = `
.aik-org{width:min(100%,1080px);container-type:inline-size}
.aik-org-head{display:flex;align-items:center;gap:12px;padding:0 4px 26px;font-size:14.5px;color:var(--aik-text-3)}
.aik-org-head b{margin-right:auto;font-size:16px;font-weight:600;color:var(--aik-text)}
.aik-org-wait{display:inline-flex;align-items:center;height:28px;padding:0 10px;border-radius:9px;font-weight:500;color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent);animation:aik-org-pop 360ms var(--aik-ease-out)}
.aik-org-body{position:relative;display:grid;justify-items:center;gap:52px}
.aik-org-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-org-teams{display:grid;grid-template-columns:var(--cols);gap:26px;width:100%;align-items:start}
.aik-org-team{display:grid;justify-items:center;gap:52px}
.aik-org-agents{display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));gap:12px;width:100%}
.aik-org-node{--aik-r-shell:18px;--aik-rim:4px;position:relative;width:100%;max-width:330px;transition:opacity var(--aik-d-panel),box-shadow var(--aik-d-panel)}
.aik-org-node[data-agent]{max-width:none}
.aik-org[data-esc] .aik-org-node:not([data-path]){opacity:.36}
.aik-org-node[data-hold]{box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-amber) 55%,transparent),0 14px 40px -18px color-mix(in oklab,var(--aik-amber) 70%,transparent)}
.aik-org-card{display:flex;align-items:center;gap:12px;padding:13px 14px;min-width:0}
.aik-org-av{position:relative;display:grid;place-items:center;width:42px;height:42px;flex:none;border-radius:50%;font-weight:600;font-size:14.5px;letter-spacing:.02em;color:#fff;background:linear-gradient(140deg,color-mix(in oklab,var(--c) 80%,white),color-mix(in oklab,var(--c) 85%,black));box-shadow:inset 0 0 0 1px rgb(255 255 255 / 18%)}
.aik-org-dot{position:absolute;top:-1px;right:-1px;width:11px;height:11px;border-radius:50%;background:var(--aik-amber-fill);box-shadow:0 0 0 2.5px var(--aik-card);animation:aik-org-pop 360ms var(--aik-ease-out)}
.aik-org-tile{position:relative;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-org-id{display:grid;min-width:0;line-height:19px}
.aik-org-id b{font-size:15px;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-org-id{flex:1 1 auto}
.aik-org-id span{display:block;min-width:0;font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-org-id span svg{display:inline-block;vertical-align:-1.5px;margin-right:5px}
.aik-org-node{min-width:0}
.aik-org-foot .aik-brand-stack{flex:none}
.aik-org-foot{display:flex;align-items:center;gap:8px;min-height:36px;padding:4px 10px 2px;font-size:13px;color:var(--aik-text-2);min-width:0}
.aik-org-foot .aik-roll{min-width:0}
.aik-org-mini{display:inline-flex}
.aik-org-mini > *{margin-right:-5px;box-shadow:0 0 0 2px var(--aik-sunk);border-radius:50%}
.aik-org-mini .aik-org-av{width:20px;height:20px;font-size:8.5px}
.aik-org-mini .aik-face{border-radius:7px}
.aik-org-ask{display:flex;align-items:center;gap:8px;width:100%;min-width:0;color:var(--aik-amber);font-weight:500;animation:aik-org-rise 420ms var(--aik-ease-out) both}
.aik-org-ask span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-org-ask button{margin-left:auto;display:grid;place-items:center;width:26px;height:26px;border-radius:8px;color:var(--aik-amber);transition:background var(--aik-d-state),transform var(--aik-d-press)}
.aik-org-ask button:hover{background:color-mix(in oklab,var(--aik-amber) 16%,transparent)}
.aik-org-ask button:active{transform:scale(.92)}
.aik-org-st{margin-left:auto;font-size:13px;color:var(--aik-text-3);white-space:nowrap}
.aik-org-st[data-s="ask"]{color:var(--aik-amber);padding:2px 7px;border-radius:7px;background:color-mix(in oklab,var(--aik-amber) 14%,transparent)}
.aik-org-st[data-s="run"]{color:var(--aik-text-2)}
@keyframes aik-org-pop{from{transform:scale(.4);opacity:0}}
@keyframes aik-org-rise{from{opacity:0;transform:translateY(6px)}}
@container (max-width:780px){
.aik-org-teams{grid-template-columns:minmax(0,1fr)}
.aik-org-body,.aik-org-team{justify-items:stretch;gap:20px}
.aik-org-node{max-width:none}
.aik-org-team{padding-left:26px}
.aik-org-agents{grid-template-columns:minmax(0,1fr);padding-left:26px}
.aik-org-head span:last-child{display:none}
}
@media (prefers-reduced-motion: reduce){.aik-org-ask,.aik-org-wait,.aik-org-dot{animation:none}}
`
const initials = (name: string) =>
name
.split(/\s+/)
.map((w) => w[0])
.join("")
.slice(0, 2)
.toUpperCase()
function Avatar({ person }: { person: OrgPerson }) {
return (
<span
className="aik-org-av"
style={{ "--c": `var(--aik-f-${person.tone})` } as React.CSSProperties}
>
{initials(person.name)}
</span>
)
}
/**
* People and the agents they own on one org chart. When an agent needs a
* decision, the ask climbs the reporting lines in amber to its owner, and on
* up the chain if nobody answers.
*/
export function AgentOrgChart({
title,
nodes,
escalation,
onResolve,
className,
}: AgentOrgChartProps) {
const root = React.useRef<HTMLDivElement>(null)
const body = React.useRef<HTMLDivElement>(null)
const refs = React.useRef(new Map<string, HTMLElement>())
const [edges, setEdges] = React.useState<
Array<{ id: string; d: string; port: [number, number] }>
>([])
const byId = React.useMemo(
() => new Map(nodes.map((n) => [n.id, n])),
[nodes]
)
const head = nodes.find((n) => n.kind === "person" && !n.parent) as
| OrgPerson
| undefined
const leads = nodes.filter(
(n): n is OrgPerson => n.kind === "person" && n.parent === head?.id
)
const agentsOf = (id: string) =>
nodes.filter((n): n is OrgAgent => n.kind === "agent" && n.parent === id)
const peopleOf = (id: string) =>
nodes.filter((n): n is OrgPerson => n.kind === "person" && n.parent === id)
// The chain from the asking agent up to the top, and who holds the ask now.
const chain: string[] = []
if (escalation) {
let at: string | undefined = escalation.from
while (at) {
chain.push(at)
at = byId.get(at)?.parent
}
}
const hops = escalation ? Math.min(escalation.hops, chain.length - 1) : 0
const holder = escalation ? chain[hops] : undefined
const onPath = new Set(chain.slice(0, hops + 1))
const box = (node: HTMLElement) => {
let x = 0
let y = 0
let el: HTMLElement | null = node
while (el && el !== body.current) {
x += el.offsetLeft
y += el.offsetTop
el = el.offsetParent as HTMLElement | null
}
return { x, y, w: node.offsetWidth, h: node.offsetHeight }
}
const measure = React.useCallback(() => {
if (!body.current) return
const wide = (root.current?.clientWidth ?? 0) > 780
const out: typeof edges = []
for (const node of nodes) {
if (!node.parent) continue
const p = refs.current.get(node.parent)
const c = refs.current.get(node.id)
if (!p || !c) continue
const f = box(p)
const t = box(c)
if (wide) {
const port: [number, number] = [f.x + f.w / 2, f.y + f.h]
out.push({
id: node.id,
d: elbowPath(t.x + t.w / 2, t.y, port[0], port[1], "v", 12),
port,
})
} else {
const port: [number, number] = [f.x + 12, f.y + f.h]
const by = t.y + Math.min(t.h / 2, 30)
out.push({
id: node.id,
d: `M${t.x} ${by}H${port[0] + 10}Q${port[0]} ${by} ${port[0]} ${by - 10}V${port[1]}`,
port,
})
}
}
setEdges(out)
}, [nodes])
React.useLayoutEffect(() => {
measure()
const ro = new ResizeObserver(() => measure())
if (root.current) ro.observe(root.current)
refs.current.forEach((n) => ro.observe(n))
return () => ro.disconnect()
}, [measure])
const reg = (id: string) => (el: HTMLElement | null) => {
if (el) refs.current.set(id, el)
else refs.current.delete(id)
}
const ask = escalation ? byId.get(escalation.from) : undefined
const personCard = (person: OrgPerson) => {
const reports = peopleOf(person.id)
const owned = agentsOf(person.id)
const holds = holder === person.id
return (
<div
ref={reg(person.id)}
className="aik-org-node aik-shell"
data-path={onPath.has(person.id) ? "" : undefined}
data-hold={holds ? "" : undefined}
>
<div className="aik-surface aik-org-card">
<span style={{ position: "relative" }}>
<Avatar person={person} />
{holds ? <span className="aik-org-dot" /> : null}
</span>
<span className="aik-org-id">
<b>{person.name}</b>
<span>{person.role}</span>
</span>
</div>
<div className="aik-org-foot">
{holds && ask && escalation ? (
<span className="aik-org-ask" key={`${person.id}-${hops}`}>
<span>
{ask.name} asks: {escalation.ask}
</span>
<button
type="button"
aria-label={`Answer ${ask.name}`}
onClick={onResolve}
>
<ArrowRight size={15} />
</button>
</span>
) : reports.length ? (
<>
<span className="aik-org-mini">
{reports.map((r) => (
<Avatar key={r.id} person={r} />
))}
</span>
<span style={{ marginLeft: 4 }}>
Leads {reports.length} people
</span>
</>
) : (
<>
<span className="aik-org-mini">
{owned.map((a) => (
<Face
key={a.id}
tone={a.tone}
shape={a.shape ?? "squircle"}
mood="idle"
size={20}
/>
))}
</span>
<span style={{ marginLeft: 4 }}>Owns {owned.length} agents</span>
</>
)}
</div>
</div>
)
}
const agentCard = (agent: OrgAgent) => {
const asking = agent.status === "ask"
return (
<div
key={agent.id}
ref={reg(agent.id)}
className="aik-org-node aik-shell"
data-agent=""
data-path={onPath.has(agent.id) ? "" : undefined}
data-hold={holder === agent.id ? "" : undefined}
>
<div className="aik-surface aik-org-card">
<span
className="aik-org-tile"
style={
{ "--c": `var(--aik-f-${agent.tone})` } as React.CSSProperties
}
>
<Face
tone={agent.tone}
shape={agent.shape ?? "squircle"}
mood={
asking
? "needs"
: agent.status === "run"
? "work"
: agent.status === "done"
? "happy"
: "idle"
}
size={28}
/>
{asking ? <span className="aik-org-dot" /> : null}
</span>
<span className="aik-org-id">
<b>{agent.name}</b>
<span>
<BrandIcon name={agent.vendor} tint="brand" size={12} />
{agent.model}
</span>
</span>
</div>
<div className="aik-org-foot">
{agent.tools?.length ? (
<BrandStack names={agent.tools} size={20} />
) : null}
<span className="aik-org-st" data-s={agent.status}>
<Roll>
{asking
? "Waiting"
: agent.status === "run"
? "Working"
: agent.status === "done"
? "Done"
: "Idle"}
</Roll>
</span>
</div>
</div>
)
}
if (!head) return null
const working = nodes.filter(
(n) => n.kind === "agent" && n.status === "run"
).length
const agentCount = nodes.filter((n) => n.kind === "agent").length
const waiting = nodes.filter(
(n) => n.kind === "agent" && n.status === "ask"
).length
return (
<div
ref={root}
className={cx("aik aik-org", className)}
data-esc={escalation ? "" : undefined}
>
<AiKitStyles />
<AiStyle id="agent-org-chart" css={CSS} />
<div className="aik-org-head">
<b>{title}</b>
{waiting ? (
<span className="aik-org-wait" key={hops}>
{hops > 1 ? "Escalated" : `${waiting} waiting`}
</span>
) : null}
<span>
{nodes.filter((n) => n.kind === "person").length} people ·{" "}
<Roll>{`${working}/${agentCount} working`}</Roll>
</span>
</div>
<div ref={body} className="aik-org-body">
<svg className="aik-org-svg" aria-hidden="true">
{edges.map((e) => {
const i = chain.indexOf(e.id)
const lit = i >= 0 && i < hops
const live = i === hops - 1
return (
<g
key={e.id}
style={{
opacity: escalation && !lit ? 0.55 : 1,
transition: "opacity var(--aik-d-panel)",
}}
>
{lit ? null : (
<path
d={e.d}
fill="none"
stroke="var(--aik-line-3)"
strokeWidth={1.5}
strokeLinecap="round"
/>
)}
<Edge
d={e.d}
tone="amber"
state={live ? "live" : lit ? "done" : "idle"}
width={lit ? 1.75 : 1.5}
duration={1}
/>
<circle
cx={e.port[0]}
cy={e.port[1]}
r={3.5}
fill="var(--aik-bg)"
strokeWidth={1.5}
stroke={lit ? "var(--aik-f-amber)" : "var(--aik-line-3)"}
/>
</g>
)
})}
</svg>
{personCard(head)}
<div
className="aik-org-teams"
style={
{
"--cols": leads
.map((l) => `${Math.max(2, agentsOf(l.id).length)}fr`)
.join(" "),
} as React.CSSProperties
}
>
{leads.map((lead) => (
<div key={lead.id} className="aik-org-team">
{personCard(lead)}
<div
className="aik-org-agents"
style={
{ "--n": agentsOf(lead.id).length } as React.CSSProperties
}
>
{agentsOf(lead.id).map(agentCard)}
</div>
</div>
))}
</div>
</div>
</div>
)
}
Installation
One command adds agent-org-chart 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-org-chart.json