Agent Timing
A run as a timing diagram: each agent's signal steps up while it thinks or calls a tool, with hand-offs dropping straight from one row to the next under a moving playhead.
"use client"
import * as React from "react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
cx,
Roll,
type AiTone,
} from "@/lib/ai-kit"
export interface TimingRow {
id: string
name: string
tone: AiTone
}
export interface TimingSpan {
row: string
/** Seconds on the run's clock. */
start: number
end: number
/** "think" steps the signal up halfway, "tool" all the way. */
kind: "think" | "tool"
label: string
}
export interface TimingHandoff {
from: string
to: string
/** Seconds on the run's clock. */
at: number
}
export interface AgentTimingProps {
title: string
rows: readonly TimingRow[]
spans: readonly TimingSpan[]
handoffs?: readonly TimingHandoff[]
/** Length of the run in seconds; the axis runs to here. */
length: number
/** The playhead, in seconds. Signals are drawn up to it. */
time: number
/** Called while the visitor drags along the axis. */
onScrub?: (seconds: number) => void
className?: string
}
const ROW = 70
const W = 1000
const CSS = `
.aik-tm{width:min(100%,1060px);container-type:inline-size}
.aik-tm-card{--aik-r-shell:24px;--aik-rim:5px}
.aik-tm-in{padding:20px 24px 16px}
.aik-tm-head{display:flex;align-items:center;gap:18px;padding-bottom:18px;font-size:15px}
.aik-tm-head b{font-weight:600;font-size:16px}
.aik-tm-head span{margin-left:auto;color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-tm-head span b{font-size:15px;color:var(--aik-text);font-weight:500}
.aik-tm-head strong{font:500 15px var(--aik-mono);font-variant-numeric:tabular-nums}
.aik-tm-grid{display:grid;grid-template-columns:200px minmax(0,1fr)}
.aik-tm-who{display:flex;align-items:center;gap:12px;height:${ROW}px;min-width:0}
.aik-tm-who > span{display:grid;line-height:19px;min-width:0}
.aik-tm-who b{font-weight:600;font-size:15px}
.aik-tm-who small{font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-tm-who[data-on] small{color:var(--c)}
.aik-tm-plot{position:relative;touch-action:none;cursor:ew-resize}
.aik-tm-svg{display:block;width:100%;overflow:visible}
.aik-tm-svg line.base{stroke:var(--aik-line-2);stroke-width:1;vector-effect:non-scaling-stroke}
.aik-tm-svg line.grid{stroke:var(--aik-line);stroke-width:1;vector-effect:non-scaling-stroke}
.aik-tm-svg path.sig{fill:none;stroke-width:2;stroke-linejoin:round;vector-effect:non-scaling-stroke}
.aik-tm-svg path.area{stroke:none}
.aik-tm-svg path.hand{fill:none;stroke:var(--aik-text-3);stroke-width:1.25;stroke-dasharray:3 4;vector-effect:non-scaling-stroke}
.aik-tm-svg line.head{stroke:var(--aik-blue);stroke-width:1.5;vector-effect:non-scaling-stroke}
.aik-tm-lab{position:absolute;translate:0 -50%;padding:0 8px;font:500 13.5px var(--aik-sans);color:color-mix(in oklab,var(--c) 75%,var(--aik-text));white-space:nowrap;overflow:hidden;text-overflow:ellipsis;pointer-events:none;animation:aik-tm-in var(--aik-d-enter) var(--aik-ease-out) both}
.aik-tm-dot{position:absolute;width:9px;height:9px;margin:-4.5px 0 0 -4.5px;border-radius:50%;background:var(--c);box-shadow:0 0 0 4px color-mix(in oklab,var(--c) 22%,transparent),0 0 14px var(--c);pointer-events:none}
.aik-tm-port{position:absolute;width:9px;height:9px;margin:-4.5px 0 0 -4.5px;border-radius:50%;background:var(--aik-card);box-shadow:inset 0 0 0 1.5px var(--c);pointer-events:none}
.aik-tm-axis{position:relative;height:34px;grid-column:2}
.aik-tm-axis span{position:absolute;top:10px;translate:-50% 0;font:12.5px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-tm-axis span:first-child{translate:0 0}
.aik-tm-axis em{position:absolute;top:6px;translate:-50% 0;height:24px;padding:0 7px;display:inline-flex;align-items:center;border-radius:6px;font:500 12.5px var(--aik-mono);font-style:normal;color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 16%,var(--aik-card));z-index:1}
@keyframes aik-tm-in{from{opacity:0;transform:translateY(4px)}}
@container (max-width:620px){
.aik-tm-grid{grid-template-columns:46px minmax(0,1fr)}
.aik-tm-who > span:not(.aik-glyph){display:none}
.aik-tm-in{padding:16px 14px 12px}
.aik-tm-lab{display:none}
.aik-tm-axis span:nth-child(even){display:none}
}
@media (prefers-reduced-motion:reduce){.aik-tm-lab{animation:none}}
`
function clock(s: number) {
const v = Math.max(0, Math.round(s))
return `${Math.floor(v / 60)}:${String(v % 60).padStart(2, "0")}`
}
/**
* A run as a timing diagram: one signal per agent that steps up halfway while
* it thinks and all the way while it calls a tool, drawn up to a playhead,
* with hand-offs dropping from one row to the next. Drag along the plot to scrub.
*/
export function AgentTiming({
title,
rows,
spans,
handoffs = [],
length,
time,
onScrub,
className,
}: AgentTimingProps) {
const plot = React.useRef<HTMLDivElement>(null)
const H = rows.length * ROW
const x = (s: number) => (Math.min(s, length) / length) * W
const pct = (s: number) => (Math.min(s, length) / length) * 100
const low = (i: number) => i * ROW + ROW - 12
const mid = (i: number) => i * ROW + ROW / 2 + 2
const high = (i: number) => i * ROW + 14
const signal = (row: TimingRow, i: number) => {
const mine = spans
.filter((s) => s.row === row.id && s.start < time)
.sort((a, b) => a.start - b.start)
const first = handoffs.find((h) => h.to === row.id && h.at <= time)
const open = first ? first.at : row.id === rows[0]?.id ? 0 : null
if (open === null) return null
const end = x(time)
let d = `M${x(open)} ${low(i)}`
for (const s of mine) {
const a = x(s.start)
const b = Math.min(x(s.end), end)
const y = s.kind === "tool" ? high(i) : mid(i)
d += `H${a}L${a + 4} ${y}H${Math.max(a + 4, b - 4)}`
d += b < end ? `L${b} ${low(i)}` : ""
}
const last = mine.at(-1)
if (!last || last.end <= time) d += `H${end}`
const area = `${d}V${low(i)}H${x(open)}Z`
return { d, area }
}
const active = (row: TimingRow) =>
spans.find((s) => s.row === row.id && s.start <= time && s.end > time)
const toolCalls = spans.filter(
(s) => s.kind === "tool" && s.start <= time
).length
const scrub = (event: React.PointerEvent) => {
if (!onScrub || !plot.current) return
const box = plot.current.getBoundingClientRect()
const t = ((event.clientX - box.left) / box.width) * length
onScrub(Math.max(0, Math.min(length, t)))
}
const ticks = Array.from(
{ length: Math.floor(length / 10) + 1 },
(_, i) => i * 10
)
return (
<div className={cx("aik aik-tm", className)}>
<AiKitStyles />
<AiStyle id="agent-timing" css={CSS} />
<div className="aik-shell aik-tm-card">
<div className="aik-surface aik-tm-in">
<div className="aik-tm-head">
<b>{title}</b>
<span>
<b>
<Roll>{String(toolCalls)}</Roll>
</b>{" "}
tool call{toolCalls === 1 ? "" : "s"}
</span>
<strong>{clock(time)}</strong>
</div>
<div className="aik-tm-grid">
<div>
{rows.map((row) => {
const now = active(row)
return (
<div
key={row.id}
className="aik-tm-who"
data-on={now ? "" : undefined}
style={
{
"--c": `var(--aik-f-${row.tone})`,
} as React.CSSProperties
}
>
<AgentGlyph
seed={row.id}
tone={row.tone}
size={22}
live={!!now}
/>
<span>
<b>{row.name}</b>
<small>
<Roll>{now ? now.label : "Idle"}</Roll>
</small>
</span>
</div>
)
})}
</div>
<div
ref={plot}
className="aik-tm-plot"
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId)
scrub(e)
}}
onPointerMove={(e) => {
if (e.buttons) scrub(e)
}}
role="slider"
tabIndex={0}
aria-label="Playhead"
aria-valuemin={0}
aria-valuemax={length}
aria-valuenow={Math.round(time)}
aria-valuetext={clock(time)}
onKeyDown={(e) => {
if (e.key === "ArrowRight")
onScrub?.(Math.min(length, time + 1))
if (e.key === "ArrowLeft") onScrub?.(Math.max(0, time - 1))
}}
>
<svg
className="aik-tm-svg"
viewBox={`0 0 ${W} ${H}`}
preserveAspectRatio="none"
style={{ height: H }}
aria-hidden="true"
>
<defs>
{rows.map((row) => (
<linearGradient
key={row.id}
id={`aik-tm-g-${row.id}`}
x1="0"
x2="0"
y1="0"
y2="1"
>
<stop
offset="0"
stopColor={`var(--aik-f-${row.tone})`}
stopOpacity=".26"
/>
<stop
offset="1"
stopColor={`var(--aik-f-${row.tone})`}
stopOpacity="0"
/>
</linearGradient>
))}
</defs>
{ticks.map((t) => (
<line
key={t}
className="grid"
x1={x(t)}
x2={x(t)}
y1={0}
y2={H}
/>
))}
{rows.map((row, i) => (
<line
key={row.id}
className="base"
x1={0}
x2={W}
y1={low(i)}
y2={low(i)}
/>
))}
{handoffs
.filter((h) => h.at <= time)
.map((h) => {
const a = rows.findIndex((r) => r.id === h.from)
const b = rows.findIndex((r) => r.id === h.to)
return (
<path
key={h.from + h.to + h.at}
className="hand"
d={`M${x(h.at)} ${low(a)}V${low(b)}`}
/>
)
})}
{rows.map((row, i) => {
const sig = signal(row, i)
if (!sig) return null
return (
<g key={row.id}>
<path
className="area"
d={sig.area}
fill={`url(#aik-tm-g-${row.id})`}
/>
<path
className="sig"
d={sig.d}
stroke={`var(--aik-f-${row.tone})`}
/>
</g>
)
})}
<line
className="head"
x1={x(time)}
x2={x(time)}
y1={0}
y2={H}
/>
</svg>
{spans
.filter((s) => s.start < time && x(s.end) - x(s.start) > 50)
.map((s) => {
const i = rows.findIndex((r) => r.id === s.row)
const tone = rows[i]?.tone ?? "blue"
const y = s.kind === "tool" ? high(i) : mid(i)
const top = (y + (low(i) - y) / 2) / H
// A label stops where the next span in its row begins, so a
// thinking label never runs under the tool call after it.
const next = spans
.filter(
(o) =>
o.row === s.row && o.start > s.start && o.start < time
)
.reduce((m, o) => Math.min(m, o.start), Infinity)
const w = pct(Math.min(s.end, time, next)) - pct(s.start)
return (
<span
key={s.row + s.start}
className="aik-tm-lab"
style={
{
left: `${pct(s.start)}%`,
top: `${top * 100}%`,
maxWidth: `${w}%`,
"--c": `var(--aik-f-${tone})`,
} as React.CSSProperties
}
>
{s.label}
</span>
)
})}
{handoffs
.filter((h) => h.at <= time)
.flatMap((h) =>
[h.from, h.to].map((id) => {
const i = rows.findIndex((r) => r.id === id)
return (
<span
key={h.at + id}
className="aik-tm-port"
style={
{
left: `${pct(h.at)}%`,
top: `${(low(i) / H) * 100}%`,
"--c": `var(--aik-f-${rows[i]?.tone})`,
} as React.CSSProperties
}
/>
)
})
)}
{rows.map((row, i) => {
const now = active(row)
if (!now) return null
const y = now.kind === "tool" ? high(i) : mid(i)
return (
<span
key={row.id}
className="aik-tm-dot"
style={
{
left: `${pct(time)}%`,
top: `${(y / H) * 100}%`,
"--c": `var(--aik-f-${row.tone})`,
} as React.CSSProperties
}
/>
)
})}
</div>
<div className="aik-tm-axis" aria-hidden="true">
{ticks.map((t) => (
<span
key={t}
style={{
left: `${pct(t)}%`,
opacity: Math.abs(t - time) < length * 0.05 ? 0 : 1,
}}
>
{clock(t)}
</span>
))}
<em style={{ left: `${pct(time)}%` }}>{clock(time)}</em>
</div>
</div>
</div>
</div>
</div>
)
}
Installation
One command adds agent-timing 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-timing.json