Decision Pipeline
One request through a chain of small typed model calls (extract, check, infer, route), each value travelling down the line to the next, with what the run cost.
"use client"
import * as React from "react"
import { Pause, Play } from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Edge,
elbowPath,
Mark,
Roll,
type AiStatus,
} from "@/lib/ai-kit"
export interface PipelineStep {
id: string
title: string
/** What kind of call it is: Extraction, Fact check, Inference… */
kind: string
icon: React.ReactNode
status: AiStatus
/** For the input step: the message that started the run. */
message?: { subject: string; body: string }
/** Label and value rows. The first (the question) always shows; the rest
* are answers and show once the step is done, as a shimmer until then. */
rows?: ReadonlyArray<readonly [string, string | undefined]>
ms?: number
cost?: number
}
export interface PipelineHop {
/** Id of the step the value leaves from. */
from: string
to: string
/** The typed value carried along the line, e.g. `amount: 38`. */
value?: string
/** idle: nothing yet, live: the value is travelling, done: it arrived. */
state: "idle" | "live" | "done"
}
export interface DecisionPipelineProps {
title: string
steps: readonly PipelineStep[]
hops: readonly PipelineHop[]
/** Steps per row; the chain wraps and turns down at the end of a row. */
perRow?: number
paused?: boolean
onPause?: () => void
className?: string
}
const CSS = `
.aik-dp{width:min(100%,1080px);container-type:inline-size}
.aik-dp-head{display:flex;align-items:center;gap:14px;flex-wrap:wrap;padding:0 4px 22px}
.aik-dp-head b{font-size:17px;font-weight:600;letter-spacing:-.01em}
.aik-dp-stats{margin-left:auto;display:flex;align-items:center;gap:16px;font:500 13px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-dp-stats em{font-style:normal;color:var(--aik-text)}
.aik-dp-tree{--gap:clamp(104px,12cqi,140px);position:relative;display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));column-gap:var(--gap);row-gap:64px}
.aik-dp-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-dp-port{fill:var(--aik-bg);stroke-width:1.6;transition:stroke var(--aik-d-panel)}
.aik-dp-node{--aik-r-shell:22px;--aik-rim:5px;position:relative;transition:opacity var(--aik-d-panel);animation:aik-dp-in 520ms var(--aik-ease-soft) both;animation-delay:calc(var(--i)*70ms)}
.aik-dp-node[data-s="todo"]{opacity:.55}
.aik-dp-card{display:grid;gap:12px;padding:14px;overflow:hidden}
.aik-dp-card::before{content:"";position:absolute;inset:0;border-radius:inherit;background:radial-gradient(120% 90% at 100% 0%,color-mix(in oklab,var(--aik-blue) 22%,transparent),transparent 60%);opacity:0;transition:opacity var(--aik-d-panel);pointer-events:none}
.aik-dp-node[data-s="run"] .aik-dp-card::before{opacity:1}
.aik-dp-node .aik-beam{animation:aik-beam 2.2s linear infinite}
.aik-dp-top{position:relative;display:flex;align-items:center;gap:12px;min-width:0}
.aik-dp-ico{display:grid;place-items:center;width:40px;height:40px;flex:none;border-radius:12px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line-2);color:var(--aik-text)}
.aik-dp-id{display:grid;line-height:20px;min-width:0}
.aik-dp-id b{font-weight:600;font-size:15.5px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-dp-id span{font-size:13.5px;color:var(--aik-text-3)}
.aik-dp-top > :last-child{margin-left:auto}
.aik-dp-pause{display:grid;place-items:center;width:28px;height:28px;border-radius:8px;color:var(--aik-text-2);transition:background var(--aik-d-state)}
.aik-dp-pause:hover{background:var(--aik-hover);color:var(--aik-text)}
.aik-dp-box{position:relative;display:grid;gap:10px;padding:12px 14px;border-radius:13px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);font-size:14px;line-height:20px}
.aik-dp-box b{font-weight:600;font-size:14.5px}
.aik-dp-box p{color:var(--aik-text-2);display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
.aik-dp-row{display:grid;grid-template-columns:62px minmax(0,1fr);gap:10px;align-items:center;min-height:22px}
.aik-dp-row > span:first-child{color:var(--aik-text-3)}
.aik-dp-row .aik-roll{color:var(--aik-text);font-weight:500;min-width:0}
.aik-dp-row .aik-roll > *{white-space:normal}
.aik-dp-skel{display:block;height:9px;width:72%;border-radius:5px;background:linear-gradient(90deg,var(--aik-line) 0 40%,var(--aik-line-2) 50%,var(--aik-line) 60% 100%) 0 0/300% 100%;animation:aik-dp-shim 1.6s linear infinite}
.aik-dp-node[data-s="todo"] .aik-dp-skel{animation:none}
.aik-dp-foot{position:relative;display:flex;justify-content:space-between;font:500 12.5px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums;min-height:18px;padding:0 2px}
.aik-dp-chip{position:absolute;left:0;top:0;z-index:2;height:24px;padding:0 9px;border-radius:7px;font:500 12px var(--aik-mono);white-space:nowrap;max-width:calc(var(--gap) - 12px);overflow:hidden;text-overflow:ellipsis;display:block;line-height:24px;color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 16%,var(--aik-bg));box-shadow:0 0 0 1px color-mix(in oklab,var(--aik-blue) 30%,transparent),0 6px 16px -6px color-mix(in oklab,var(--aik-blue) 50%,transparent);pointer-events:none;will-change:transform}
.aik-dp-chip[data-state="done"]{box-shadow:0 0 0 1px color-mix(in oklab,var(--aik-blue) 22%,transparent)}
@keyframes aik-dp-in{from{opacity:0;transform:translateY(8px)}}
@keyframes aik-dp-shim{to{background-position:-150% 0}}
@container (max-width:780px){.aik-dp-tree{grid-template-columns:minmax(0,1fr)!important;row-gap:54px}.aik-dp-stats span:nth-child(3){display:none}}
@media (prefers-reduced-motion: reduce){.aik-dp-node{animation:none}.aik-dp-skel{animation:none}}
`
interface Geo {
key: string
d: string
a: [number, number]
b: [number, number]
}
function offsetBox(node: HTMLElement, stop: HTMLElement | null) {
let left = 0
let top = 0
let el: HTMLElement | null = node
while (el && el !== stop) {
left += el.offsetLeft
top += el.offsetTop
el = el.offsetParent as HTMLElement | null
}
return { left, top, width: node.offsetWidth, height: node.offsetHeight }
}
/** Point at a fraction of a rendered SVG path, nudged above the line. */
function pointsAlong(path: SVGPathElement, upto: number, count: number) {
const total = path.getTotalLength()
return Array.from({ length: count + 1 }, (_, i) => {
const p = path.getPointAtLength((total * upto * i) / count)
return [p.x, p.y] as const
})
}
/**
* One request through a chain of small, typed model calls. Each step shows
* the question it asks and its typed answer, and each answer rides down the
* line to the next step as a chip, then parks on the line it travelled.
*/
export function DecisionPipeline({
title,
steps,
hops,
perRow = 3,
paused,
onPause,
className,
}: DecisionPipelineProps) {
const root = React.useRef<HTMLDivElement>(null)
const tree = React.useRef<HTMLDivElement>(null)
const nodes = React.useRef(new Map<string, HTMLElement>())
const chips = React.useRef(new Map<string, HTMLElement>())
const [geo, setGeo] = React.useState<Geo[]>([])
const [narrow, setNarrow] = React.useState(false)
const measure = React.useCallback(() => {
if (!tree.current || !root.current) return
const isNarrow = root.current.clientWidth <= 780
setNarrow(isNarrow)
const next: Geo[] = []
for (const hop of hops) {
const f = nodes.current.get(hop.from)
const t = nodes.current.get(hop.to)
if (!f || !t) continue
const A = offsetBox(f, tree.current)
const B = offsetBox(t, tree.current)
let a: [number, number]
let b: [number, number]
let d: string
const sameRow = !isNarrow && Math.abs(A.top - B.top) < 4
if (sameRow) {
a = [A.left + A.width, A.top + 76]
b = [B.left, B.top + 76]
d = `M${a[0]} ${a[1]}H${b[0]}`
} else {
a = [A.left + A.width / 2, A.top + A.height]
b = [B.left + B.width / 2, B.top]
d = elbowPath(a[0], a[1], b[0], b[1], "v", 14)
}
next.push({ key: `${hop.from}>${hop.to}`, d, a, b })
}
setGeo(next)
}, [hops])
React.useLayoutEffect(() => {
measure()
const observer = new ResizeObserver(() => measure())
if (root.current) observer.observe(root.current)
nodes.current.forEach((node) => observer.observe(node))
return () => observer.disconnect()
}, [measure])
// Chips: a live hop sends its value riding from the start of the line to the middle.
const rode = React.useRef(new Set<string>())
React.useLayoutEffect(() => {
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches
for (const hop of hops) {
const key = `${hop.from}>${hop.to}`
const chip = chips.current.get(key)
const path = root.current?.querySelector<SVGPathElement>(
`path[data-hop="${CSS_ESC(key)}"]`
)
if (!chip || !path) continue
const pts = pointsAlong(path, 0.5, 18)
const end = pts[pts.length - 1]
const at = (p: readonly [number, number]) =>
`translate(${p[0]}px, ${p[1] - 17}px) translate(-50%, -50%)`
chip.style.transform = at(end)
// Only chips on short links between neighbours are capped to the gap;
// the long line between rows has room for the whole value.
chip.style.maxWidth = path.getTotalLength() > 260 ? "none" : ""
if (hop.state === "live" && !rode.current.has(key) && !reduce) {
rode.current.add(key)
chip.animate(
pts.map((p, i) => ({ transform: at(p), opacity: i === 0 ? 0 : 1 })),
{ duration: 900, easing: "cubic-bezier(.65,0,.15,1)" }
)
}
if (hop.state === "idle") rode.current.delete(key)
}
}, [hops, geo])
const done = steps.filter((s) => s.status === "done")
const ms = done.reduce((sum, s) => sum + (s.ms ?? 0), 0)
const cost = done.reduce((sum, s) => sum + (s.cost ?? 0), 0)
const calls = steps.filter((s) => s.rows).length
const callsDone = done.filter((s) => s.rows).length
return (
<div ref={root} className={cx("aik aik-dp", className)}>
<AiKitStyles />
<AiStyle id="decision-pipeline" css={CSS} />
<div className="aik-dp-head">
<b>{title}</b>
<span className="aik-dp-stats">
<span>
<em>
<Roll>{String(callsDone)}</Roll>
</em>
/{calls} calls
</span>
<span>
<em>
<Roll>{ms.toFixed(2)}</Roll>
</em>{" "}
ms
</span>
<span>
<em>
<Roll>{`$${cost.toFixed(5)}`}</Roll>
</em>
</span>
</span>
</div>
<div
ref={tree}
className="aik-dp-tree"
style={{ "--n": perRow } as React.CSSProperties}
>
<svg className="aik-dp-svg" aria-hidden="true">
{geo.map((g) => {
const hop = hops.find((h) => `${h.from}>${h.to}` === g.key)
const state = hop?.state ?? "idle"
return (
<g key={g.key}>
<path data-hop={g.key} d={g.d} fill="none" stroke="none" />
<Edge
d={g.d}
tone="blue"
state={
state === "live"
? "live"
: state === "done"
? "done"
: "idle"
}
duration={0.9}
/>
{[g.a, g.b].map((p, i) => (
<circle
key={i}
className="aik-dp-port"
cx={p[0]}
cy={p[1]}
r={4}
stroke={
state === "idle" ? "var(--aik-line-3)" : "var(--aik-blue)"
}
/>
))}
</g>
)
})}
</svg>
{hops.map((hop) =>
hop.value && hop.state !== "idle" ? (
<span
key={`${hop.from}>${hop.to}`}
ref={(el) => {
const key = `${hop.from}>${hop.to}`
if (el) chips.current.set(key, el)
else chips.current.delete(key)
}}
className="aik-dp-chip"
title={hop.value}
data-state={hop.state}
style={narrow ? { display: "none" } : undefined}
>
{hop.value}
</span>
) : null
)}
{steps.map((step, i) => (
<div
key={step.id}
ref={(el) => {
if (el) nodes.current.set(step.id, el)
else nodes.current.delete(step.id)
}}
className="aik-dp-node aik-shell"
data-s={step.status}
style={{ "--i": i } as React.CSSProperties}
>
<div className="aik-surface aik-dp-card">
<span
className="aik-beam"
data-on={step.status === "run" ? "" : undefined}
/>
<div className="aik-dp-top">
<span className="aik-dp-ico">{step.icon}</span>
<span className="aik-dp-id">
<b>{step.title}</b>
<span>{step.kind}</span>
</span>
{step.message && onPause ? (
<button
type="button"
className="aik-dp-pause"
onClick={onPause}
aria-label={paused ? "Play" : "Pause"}
>
{paused ? <Play size={15} /> : <Pause size={15} />}
</button>
) : (
<Mark status={step.status} />
)}
</div>
<div className="aik-dp-box">
{step.message ? (
<>
<b>{step.message.subject}</b>
<p>{step.message.body}</p>
</>
) : (
step.rows?.map(([label, value], r) => (
<div key={label} className="aik-dp-row">
<span>{label}</span>
{r === 0 ? (
<span>{value}</span>
) : value && step.status === "done" ? (
<Roll>{value}</Roll>
) : (
<span className="aik-dp-skel" />
)}
</div>
))
)}
</div>
<div className="aik-dp-foot">
<span>
{step.status === "done" && step.ms !== undefined
? `${step.ms.toFixed(2)} ms`
: "–"}
</span>
<span>
{step.status === "done" && step.cost !== undefined
? `$${step.cost.toFixed(5)}`
: ""}
</span>
</div>
</div>
</div>
))}
</div>
</div>
)
}
function CSS_ESC(value: string) {
return value.replace(/["\\]/g, "\\$&")
}
Installation
One command adds decision-pipeline 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/decision-pipeline.json