Agent Metro Map
A run drawn as a metro map: each agent a coloured line, each step a station, interchanges where work changes hands, and a train riding every line.
"use client"
import * as React from "react"
import {
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Roll,
toneColor,
type AiTone,
} from "@/lib/ai-kit"
export interface MetroStation {
label: string
x: number
y: number
/** Where the label sits. */
at?: "top" | "bottom" | "left" | "right"
/** Nudge the label off a line that passes too close. */
nudge?: [number, number]
}
export interface MetroLine {
id: string
/** The agent that rides this line. */
agent: string
vendor?: string
tone: AiTone
/** Station ids in order. A station on several lines is an interchange. */
stops: readonly string[]
}
export interface AgentMetroMapProps {
title: string
stations: Readonly<Record<string, MetroStation>>
lines: readonly MetroLine[]
/** Where each line's train is, as a stop index (fractions ride between stops). Leave a line out to keep its train in the depot. */
trains: Readonly<Record<string, number>>
/** A line to pick out; the others fade back. */
focus?: string | null
onFocus?: (id: string | null) => void
className?: string
}
const W = 990
const H = 520
const CSS = `
.aik-mm{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,1040px);container-type:inline-size}
.aik-mm-head{display:flex;align-items:center;flex-wrap:wrap;gap:10px 16px;padding:8px 14px 14px}
.aik-mm-head b{font-size:16px;font-weight:600}
.aik-mm-head > span{margin-left:auto;font-size:14px;color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-mm-card{display:grid;gap:8px;padding:14px 14px 8px}
.aik-mm-legend{display:flex;flex-wrap:wrap;gap:6px}
.aik-mm-chip{display:inline-flex;align-items:center;gap:8px;height:34px;padding:0 10px;border-radius:11px;font-size:13.5px;font-weight:500;color:var(--aik-text-2);background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);transition:background var(--aik-d-state),color var(--aik-d-state),opacity var(--aik-d-state),transform var(--aik-d-press)}
.aik-mm-chip:active{transform:scale(.96)}
.aik-mm-chip i{width:14px;height:3px;border-radius:2px;background:var(--c)}
.aik-mm-chip[aria-pressed="true"]{color:var(--aik-text);background:var(--aik-raised);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 55%,transparent)}
.aik-mm[data-focus] .aik-mm-chip[aria-pressed="false"]{opacity:.55}
.aik-mm-chip[data-moving] i{animation:aik-mm-blink 1s ease-in-out infinite}
.aik-mm-svg{display:block;width:100%;height:auto;overflow:visible}
.aik-mm-line{fill:none;stroke-linecap:round;stroke-linejoin:round;transition:opacity var(--aik-d-panel)}
.aik-mm-ride{fill:none;stroke-linecap:round;stroke-linejoin:round}
.aik-mm-g{transition:opacity var(--aik-d-panel)}
.aik-mm[data-focus] .aik-mm-g:not([data-on]){opacity:.18}
.aik-mm-stop{fill:var(--aik-card);stroke-width:2.5;transition:fill var(--aik-d-panel),stroke var(--aik-d-panel)}
.aik-mm-x{fill:var(--aik-card);stroke:var(--aik-text-2);stroke-width:2.2}
.aik-mm-ping{fill:none;stroke-width:2;animation:aik-mm-ping 1.1s var(--aik-ease-out) both;transform-box:fill-box;transform-origin:center}
.aik-mm-lbl{font:500 13px var(--aik-sans);fill:var(--aik-text-3);transition:fill var(--aik-d-panel)}
.aik-mm-lbl[data-on]{fill:var(--aik-text)}
.aik-mm-train rect{filter:drop-shadow(0 0 6px var(--c))}
@keyframes aik-mm-ping{from{opacity:1;transform:scale(.6)}to{opacity:0;transform:scale(2.4)}}
@keyframes aik-mm-blink{50%{opacity:.35}}
@container (max-width:600px){.aik-mm-lbl:not([data-x]){display:none}.aik-mm-chip{height:30px;font-size:12.5px}}
@media (prefers-reduced-motion: reduce){.aik-mm-ping,.aik-mm-chip i{animation:none!important}}
`
function lengths(points: Array<{ x: number; y: number }>) {
const out = [0]
for (let i = 1; i < points.length; i++) {
out.push(
out[i - 1] +
Math.hypot(points[i].x - points[i - 1].x, points[i].y - points[i - 1].y)
)
}
return out
}
/** Point and heading at a fractional stop index along a polyline. */
function pointAt(points: Array<{ x: number; y: number }>, at: number) {
const i = Math.max(0, Math.min(points.length - 1, Math.floor(at)))
const j = Math.min(points.length - 1, i + 1)
const t = Math.max(0, Math.min(1, at - i))
const a = points[i]
const b = points[j]
const angle = i === j ? 0 : (Math.atan2(b.y - a.y, b.x - a.x) * 180) / Math.PI
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t, angle }
}
/** A train that glides along its line to `at`, by distance, easing in and out. */
function Train({
points,
at,
tone,
}: {
points: Array<{ x: number; y: number }>
at: number
tone: AiTone
}) {
const [shown, setShown] = React.useState(at)
const from = React.useRef(at)
React.useEffect(() => {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
setShown(at)
return
}
const start = performance.now()
const a = from.current
const span = Math.abs(at - a)
const dur = Math.min(1600, 500 + span * 520)
let frame = 0
const step = (now: number) => {
const t = Math.min(1, (now - start) / dur)
const e = t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2
const v = a + (at - a) * e
from.current = v
setShown(v)
if (t < 1) frame = requestAnimationFrame(step)
}
frame = requestAnimationFrame(step)
return () => cancelAnimationFrame(frame)
}, [at])
const p = pointAt(points, shown)
return (
<g
className="aik-mm-train"
transform={`translate(${p.x} ${p.y}) rotate(${p.angle})`}
style={{ "--c": toneColor(tone) } as React.CSSProperties}
>
<rect
x={-12}
y={-5.5}
width={24}
height={11}
rx={5.5}
fill={toneColor(tone)}
/>
<rect
x={4}
y={-2.5}
width={4}
height={5}
rx={1.5}
fill="var(--aik-card)"
opacity={0.9}
/>
</g>
)
}
/**
* A multi-agent run as a metro map: each agent rides its own coloured line,
* every step is a station, and lines meet at interchanges where work is
* handed over. A train glides along each line as the agent works.
*/
export function AgentMetroMap({
title,
stations,
lines,
trains,
focus,
onFocus,
className,
}: AgentMetroMapProps) {
const counts = new Map<string, number>()
lines.forEach((l) =>
l.stops.forEach((s) => counts.set(s, (counts.get(s) ?? 0) + 1))
)
// Stations a train sits on right now (to ping), and stations already passed per line.
const at = new Set<string>()
lines.forEach((l) => {
const v = trains[l.id]
if (v !== undefined && Math.abs(v - Math.round(v)) < 0.001)
at.add(l.stops[Math.round(v)])
})
const total = lines.reduce((sum, l) => sum + l.stops.length, 0)
const done = lines.reduce(
(sum, l) =>
sum + (trains[l.id] !== undefined ? Math.floor(trains[l.id]) + 1 : 0),
0
)
return (
<div
className={cx("aik aik-mm aik-shell", className)}
data-focus={focus ? "" : undefined}
>
<AiKitStyles />
<AiStyle id="agent-metro-map" css={CSS} />
<div className="aik-mm-head">
<b>{title}</b>
<span>
<Roll>{`${done} of ${total} stops`}</Roll>
</span>
</div>
<div className="aik-surface aik-mm-card">
<div className="aik-mm-legend" role="group" aria-label="Lines">
{lines.map((line) => {
const v = trains[line.id]
const moving = v !== undefined && v < line.stops.length - 1
return (
<button
key={line.id}
type="button"
className="aik-mm-chip"
aria-pressed={focus === line.id}
data-moving={moving ? "" : undefined}
style={{ "--c": toneColor(line.tone) } as React.CSSProperties}
onClick={() => onFocus?.(focus === line.id ? null : line.id)}
>
<i aria-hidden="true" />
{line.agent}
{line.vendor ? (
<BrandIcon name={line.vendor} tint="brand" size={13} />
) : null}
</button>
)
})}
</div>
<svg
className="aik-mm-svg"
viewBox={`0 0 ${W} ${H}`}
role="img"
aria-label={`${title}: ${lines.map((l) => l.agent).join(", ")}`}
>
{lines.map((line) => {
const pts = line.stops.map((s) => stations[s])
const d = pts
.map((p, i) => `${i ? "L" : "M"}${p.x} ${p.y}`)
.join("")
const len = lengths(pts)
const v = trains[line.id]
const ridden =
v === undefined
? 0
: (() => {
const i = Math.min(len.length - 1, Math.floor(v))
const j = Math.min(len.length - 1, i + 1)
return len[i] + (len[j] - len[i]) * (v - i)
})()
const c = toneColor(line.tone)
return (
<g
key={line.id}
className="aik-mm-g"
data-on={focus === line.id ? "" : undefined}
>
<path
d={d}
className="aik-mm-line"
stroke={c}
strokeOpacity={0.28}
strokeWidth={6}
/>
<path
d={d}
className="aik-mm-ride"
stroke={c}
strokeWidth={6}
strokeDasharray={`${ridden} ${len[len.length - 1] + 10}`}
style={{
transition: "stroke-dasharray 1.2s var(--aik-ease-move)",
}}
/>
{line.stops.map((s, i) => {
if ((counts.get(s) ?? 0) > 1) return null
const p = stations[s]
const passed = v !== undefined && i <= v
return (
<circle
key={s}
cx={p.x}
cy={p.y}
r={7}
className="aik-mm-stop"
stroke={c}
fill={passed ? c : undefined}
/>
)
})}
</g>
)
})}
{Object.entries(stations).map(([id, s]) => {
const x = (counts.get(id) ?? 0) > 1
const pos = s.at ?? "top"
const dx = pos === "left" ? -16 : pos === "right" ? 16 : 0
const dy =
pos === "top"
? x
? -20
: -18
: pos === "bottom"
? x
? 30
: 28
: 4
const anchor =
pos === "left" ? "end" : pos === "right" ? "start" : "middle"
const on = at.has(id)
const tone = lines.find(
(l) =>
l.stops.includes(id) &&
trains[l.id] !== undefined &&
Math.round(trains[l.id]) === l.stops.indexOf(id)
)?.tone
return (
<g key={id}>
{x ? (
<rect
x={s.x - 13}
y={s.y - 9}
width={26}
height={18}
rx={9}
className="aik-mm-x"
/>
) : null}
{on && tone ? (
<circle
key={`${id}-${trains[lines.find((l) => l.tone === tone)!.id]}`}
cx={s.x}
cy={s.y}
r={10}
className="aik-mm-ping"
stroke={toneColor(tone)}
/>
) : null}
<text
x={s.x + dx + (s.nudge?.[0] ?? 0)}
y={s.y + dy + (s.nudge?.[1] ?? 0)}
textAnchor={anchor}
className="aik-mm-lbl"
data-on={on ? "" : undefined}
data-x={x ? "" : undefined}
>
{s.label}
</text>
</g>
)
})}
{lines.map((line) =>
trains[line.id] !== undefined ? (
<g
key={line.id}
className="aik-mm-g"
data-on={focus === line.id ? "" : undefined}
>
<Train
points={line.stops.map((s) => stations[s])}
at={trains[line.id]}
tone={line.tone}
/>
</g>
) : null
)}
</svg>
</div>
</div>
)
}
Installation
One command adds agent-metro-map 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-metro-map.json