Guardrail Funnel
The checks an agent's traffic passes through, as a funnel: input check, redaction, tool allowlist, output check, with what each stage stopped and the calls it caught.
"use client"
import * as React from "react"
import {
CircleCheck,
EyeOff,
Inbox,
MessageSquareWarning,
ShieldCheck,
Wrench,
} from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandTile,
cx,
Roll,
type AiTone,
} from "@/lib/ai-kit"
export type GuardrailKind = "in" | "input" | "pii" | "tools" | "output" | "out"
export interface GuardrailCatch {
id: string
/** Brand of the service the call was aimed at, if any. */
brand?: string
/** The call or text that was stopped, with the offending part in `[[…]]`. */
code: string
reason: string
ago: string
/** Label of the override button, e.g. "Allow once". Omit for no override. */
action?: string
}
export interface GuardrailStage {
id: string
kind: GuardrailKind
label: string
/** Second line, e.g. "1,605 blocked". */
note: string
/** Requests still flowing after this stage. */
passed: number
tone: AiTone
caught?: readonly GuardrailCatch[]
}
/** A request falling through the funnel; `stop` is the stage index that catches it. */
export interface GuardrailDrop {
id: string
stop?: number
}
export interface GuardrailFunnelProps {
agent: string
stages: readonly GuardrailStage[]
range: string
ranges?: readonly string[]
onRange?: (range: string) => void
/** The stage whose catches are listed below. */
active?: string
onActive?: (id: string) => void
drops?: readonly GuardrailDrop[]
onAction?: (stage: string, item: string) => void
className?: string
}
const ICON: Record<GuardrailKind, React.ComponentType<{ size?: number }>> = {
in: Inbox,
input: ShieldCheck,
pii: EyeOff,
tools: Wrench,
output: MessageSquareWarning,
out: CircleCheck,
}
const ROW = 70
const CSS = `
.aik-gf{--aik-r-shell:26px;--aik-rim:6px;width:min(100%,860px);container-type:inline-size}
.aik-gf-card{padding:20px 22px 18px}
.aik-gf-head{display:flex;align-items:center;gap:12px;flex-wrap:wrap;padding-bottom:16px}
.aik-gf-head b{font-size:16px;font-weight:600}
.aik-gf-agent{display:inline-flex;align-items:center;gap:7px;color:var(--aik-text-2);font-size:14px}
.aik-gf-ranges{margin-left:auto;display:flex;align-items:center;gap:2px}
.aik-gf-live{display:inline-flex;align-items:center;gap:7px;margin-right:8px;color:var(--aik-text-2);font-size:13.5px}
.aik-gf-live i{width:7px;height:7px;border-radius:50%;background:var(--aik-green);animation:aik-gf-blink 1.6s ease-in-out infinite}
.aik-gf-ranges button{height:30px;padding:0 11px;border-radius:9px;color:var(--aik-text-3);font-size:13.5px;font-weight:500;transition:background var(--aik-d-state),color var(--aik-d-state)}
.aik-gf-ranges button[aria-pressed="true"]{background:var(--aik-raised);color:var(--aik-text)}
.aik-gf-rows{position:relative;display:grid}
.aik-gf-row{position:relative;display:grid;grid-template-columns:minmax(0,200px) minmax(0,1fr) 90px;align-items:center;gap:18px;height:${ROW}px;padding:0 12px;border-radius:14px;text-align:left;width:100%;transition:background var(--aik-d-state)}
.aik-gf-row:hover{background:var(--aik-hover)}
.aik-gf-row[aria-pressed="true"]{background:var(--aik-raised)}
.aik-gf-lab{display:grid;grid-template-columns:22px minmax(0,1fr);column-gap:10px;line-height:20px}
.aik-gf-lab svg{grid-row:span 2;margin-top:1px;color:var(--c)}
.aik-gf-lab b{font-size:15px;font-weight:500;white-space:nowrap;color:var(--aik-text)}
.aik-gf-lab span{font-size:13px;color:var(--aik-text-3);white-space:nowrap}
.aik-gf-track{position:relative;height:34px;display:grid;place-items:center}
.aik-gf-bar,.aik-gf-wing{position:absolute;left:0;right:0;top:2px;height:30px;transition:clip-path 800ms var(--aik-ease-move),opacity var(--aik-d-panel)}
.aik-gf-bar{background:color-mix(in oklab,var(--c) 30%,var(--aik-raised))}
.aik-gf-unused-i{position:absolute;inset:0;border-radius:inherit;background:color-mix(in oklab,var(--c) 30%,var(--aik-raised));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 55%,transparent)}
.aik-gf-wing{background:color-mix(in oklab,var(--c) 22%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 40%,transparent)}
.aik-gf-row[data-kind="out"] .aik-gf-unused-i{background:linear-gradient(90deg,color-mix(in oklab,var(--aik-green) 34%,var(--aik-raised)),color-mix(in oklab,var(--aik-green) 18%,var(--aik-raised)))}
.aik-gf-num{display:grid;justify-items:end;font-variant-numeric:tabular-nums;line-height:20px}
.aik-gf-num b{font-size:15px;font-weight:600;color:var(--aik-text)}
.aik-gf-num span{font-size:13px;color:var(--aik-text-3)}
.aik-gf-drops{position:absolute;inset:0;pointer-events:none}
.aik-gf-drop{position:absolute;left:calc(50% + var(--x,0px));top:${ROW / 2}px;width:8px;height:8px;margin:-4px;border-radius:50%;background:var(--aik-blue);box-shadow:0 0 10px var(--aik-blue);animation:aik-gf-fall var(--t) var(--aik-ease-soft) forwards,aik-gf-land 600ms var(--t) var(--aik-ease-out) forwards}
@keyframes aik-gf-land{to{transform:translateY(var(--y)) scale(.3);opacity:0;background:var(--aik-green);box-shadow:0 0 12px var(--aik-green)}}
.aik-gf-drop[data-stop]{animation:aik-gf-fall var(--t) var(--aik-ease-soft) forwards,aik-gf-caught 500ms var(--t) var(--aik-ease-out) forwards}
@keyframes aik-gf-fall{from{transform:translateY(-${ROW / 2}px);opacity:0}15%{opacity:1}to{transform:translateY(var(--y))}}
@keyframes aik-gf-caught{0%{background:var(--aik-red);box-shadow:0 0 0 0 color-mix(in oklab,var(--aik-red) 70%,transparent)}100%{background:var(--aik-red);box-shadow:0 0 0 12px transparent;opacity:0;transform:translateY(var(--y)) scale(.4)}}
@keyframes aik-gf-blink{50%{opacity:.35}}
.aik-gf-detail{margin-top:14px;padding-top:16px;box-shadow:inset 0 1px 0 var(--aik-line)}
.aik-gf-dh{display:flex;align-items:baseline;gap:10px;margin-bottom:12px}
.aik-gf-dh b{font-size:16px;font-weight:600}
.aik-gf-dh span{font-size:13.5px;color:var(--aik-text-3)}
.aik-gf-list{display:grid;gap:8px}
.aik-gf-item{display:flex;align-items:center;gap:14px;padding:12px 14px;border-radius:14px;background:var(--aik-panel);box-shadow:inset 0 0 0 1px var(--aik-line);animation:aik-gf-in 420ms var(--aik-ease-out) both;animation-delay:calc(var(--i)*60ms)}
.aik-gf-item > div{display:grid;gap:3px;min-width:0;flex:1}
.aik-gf-item code{font-size:13px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-gf-item mark{padding:1px 4px;border-radius:5px;background:color-mix(in oklab,var(--aik-amber) 22%,transparent);color:var(--aik-amber)}
.aik-gf-item small{font-size:13px;color:var(--aik-text-3)}
.aik-gf-item small em{font-style:normal;margin-left:8px;opacity:.75}
.aik-gf-allowed{color:var(--aik-green);font-size:13px;font-weight:500}
.aik-gf-empty{padding:16px;color:var(--aik-text-3);font-size:14px}
@keyframes aik-gf-in{from{opacity:0;transform:translateY(6px)}}
@container (max-width:560px){
.aik-gf-card{padding:16px 12px}
.aik-gf-row{grid-template-columns:minmax(0,1fr) 70px;grid-template-rows:auto 14px;height:auto;padding:10px;row-gap:6px}
.aik-gf-track{grid-column:1/-1;grid-row:2;height:14px}
.aik-gf-bar,.aik-gf-wing{height:12px;top:1px}
.aik-gf-drops{display:none}
.aik-gf-ranges{margin-left:0}
}
@media (prefers-reduced-motion: reduce){.aik-gf-drops{display:none}.aik-gf-live i{animation:none}}
`
function codeParts(code: string) {
return code
.split(/(\[\[[^\]]+\]\])/)
.map((part, i) =>
part.startsWith("[[") ? (
<mark key={i}>{part.slice(2, -2)}</mark>
) : (
<React.Fragment key={i}>{part}</React.Fragment>
)
)
}
/**
* The guardrails an agent's traffic passes through, as a funnel. Every stage
* narrows the bar by what it stopped (the stopped part shown as wings), live
* requests drop through and pop red where they are caught, and a stage opens
* the calls it caught with an override.
*/
export function GuardrailFunnel({
agent,
stages,
range,
ranges = ["1h", "24h", "7d"],
onRange,
active,
onActive,
drops = [],
onAction,
className,
}: GuardrailFunnelProps) {
const total = stages[0]?.passed || 1
const open = stages.find((stage) => stage.id === active)
const [allowed, setAllowed] = React.useState<string[]>([])
return (
<div className={cx("aik aik-gf aik-shell", className)}>
<AiKitStyles />
<AiStyle id="guardrail-funnel" css={CSS} />
<div className="aik-surface aik-gf-card">
<div className="aik-gf-head">
<b>Guardrails</b>
<span className="aik-gf-agent">
<AgentGlyph seed="support" tone="blue" size={15} live />
{agent}
</span>
<div className="aik-gf-ranges" role="group" aria-label="Time range">
<span className="aik-gf-live">
<i aria-hidden="true" />
Live
</span>
{ranges.map((r) => (
<button
key={r}
type="button"
aria-pressed={r === range}
onClick={() => onRange?.(r)}
>
{r}
</button>
))}
</div>
</div>
<div className="aik-gf-rows">
{stages.map((stage, index) => {
const Icon = ICON[stage.kind]
const prev = index === 0 ? total : stages[index - 1].passed
const width = (stage.passed / total) * 100
const prevWidth = (prev / total) * 100
const wing = Math.max(0, (prevWidth - width) / 2)
return (
<button
key={stage.id}
type="button"
className="aik-gf-row"
data-kind={stage.kind}
aria-pressed={stage.id === active}
onClick={() => onActive?.(stage.id)}
style={
{ "--c": `var(--aik-f-${stage.tone})` } as React.CSSProperties
}
>
<span className="aik-gf-lab">
<Icon size={18} />
<b>{stage.label}</b>
<span>
<Roll>{stage.note}</Roll>
</span>
</span>
<span className="aik-gf-track" aria-hidden="true">
{/* Full-width layers clipped to size: clip-path animates
on the compositor and keeps the corner radius. */}
<span
className="aik-gf-wing"
style={{
clipPath: `inset(0 ${(100 - prevWidth) / 2}% round 9px)`,
opacity: wing > 0.05 ? 1 : 0,
}}
/>
<span
className="aik-gf-bar"
style={{
clipPath: `inset(0 ${(100 - prevWidth) / 2 + wing}% round 9px)`,
}}
/>
</span>
<span className="aik-gf-num">
<b>
<Roll>{stage.passed.toLocaleString("en-US")}</Roll>
</b>
<span>
<Roll>{`${((stage.passed / total) * 100).toFixed(1)}%`}</Roll>
</span>
</span>
</button>
)
})}
<div className="aik-gf-drops" aria-hidden="true">
{drops.map((drop, i) => {
const last = drop.stop ?? stages.length - 1
return (
<span
key={drop.id}
className="aik-gf-drop"
data-stop={drop.stop !== undefined ? "" : undefined}
style={
{
"--y": `${last * ROW}px`,
"--t": `${380 + last * 260}ms`,
"--x": `${((i * 37) % 60) - 30}px`,
} as React.CSSProperties
}
/>
)
})}
</div>
</div>
{open ? (
<div className="aik-gf-detail">
<div className="aik-gf-dh">
<b>{open.label}</b>
<span>
{open.caught?.length
? `${open.caught.length} caught in the last ${range}`
: "Nothing stopped here"}
</span>
</div>
<div className="aik-gf-list" key={open.id}>
{open.caught?.length ? (
open.caught.map((item, i) => (
<div
key={item.id}
className="aik-gf-item"
style={{ "--i": i } as React.CSSProperties}
>
<BrandTile name={item.brand ?? "postgresql"} size={34} />
<div>
<code>{codeParts(item.code)}</code>
<small>
{item.reason}
<em>{item.ago}</em>
</small>
</div>
{item.action ? (
allowed.includes(item.id) ? (
<span className="aik-gf-allowed">Allowed once</span>
) : (
<button
type="button"
className="aik-btn line sm"
onClick={() => {
setAllowed((list) => [...list, item.id])
onAction?.(open.id, item.id)
}}
>
{item.action}
</button>
)
) : null}
</div>
))
) : (
<p className="aik-gf-empty">
Everything that reached this stage went through.
</p>
)}
</div>
</div>
) : null}
</div>
</div>
)
}
Installation
One command adds guardrail-funnel 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/guardrail-funnel.json