Case Model
An open case worked by agents and people as a case plan: stages, tasks that start once their entry condition holds, optional tasks someone has to plan, and milestones.
"use client"
import * as React from "react"
import {
Check,
FileText,
Flag,
FolderClosed,
Pause,
Play,
RotateCcw,
} from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Face,
Roll,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
export type CaseTaskStatus = "done" | "run" | "wait" | "optional"
export interface CaseTask {
id: string
title: string
/** Who works it: an agent's name, a person's name and role, or a service. */
owner: string
kind: "agent" | "person" | "service"
tone: AiTone
/** Brand mark for a service task, e.g. "stripe". */
brand?: string
/** Face shape for a person. */
shape?: AiShape
status: CaseTaskStatus
/** True once the entry criterion holds (its diamond lights). */
ready: boolean
/** Sub-steps done and total, e.g. [1, 2]. */
progress?: [number, number]
seconds?: number
}
export interface CaseStage {
id: string
label: string
tasks: readonly CaseTask[]
milestone: { label: string; reached: boolean }
}
export interface CaseModelProps {
cases: ReadonlyArray<{ id: string; label: string; tone: AiTone }>
active: string
onCase?: (id: string) => void
documents?: readonly string[]
stages: readonly CaseStage[]
paused?: boolean
onPause?: () => void
onRestart?: () => void
/** Plan an optional task so it joins the case. */
onPlan?: (task: string) => void
className?: string
}
const CSS = `
.aik-cm{width:min(100%,1080px);container-type:inline-size}
.aik-cm-top{display:flex;align-items:flex-end;gap:12px;flex-wrap:wrap}
.aik-cm-tabs{display:flex;gap:0;min-width:0;overflow-x:auto;scrollbar-width:none}
.aik-cm-tab{position:relative;display:inline-flex;align-items:center;gap:10px;height:50px;padding:0 18px 0 10px;border-radius:16px 16px 0 0;color:var(--aik-text-3);font-size:15px;font-weight:500;white-space:nowrap;transition:background var(--aik-d-state),color var(--aik-d-state)}
.aik-cm-tab[aria-selected="true"]{background:var(--aik-sunk);color:var(--aik-text);box-shadow:inset 0 1px 0 var(--aik-line),inset 1px 0 0 var(--aik-line),inset -1px 0 0 var(--aik-line)}
.aik-cm-tab > span{display:grid;place-items:center;width:30px;height:30px;border-radius:9px;color:var(--c);background:color-mix(in oklab,var(--c) 15%,transparent)}
.aik-cm-tools{margin-left:auto;display:flex;align-items:center;gap:6px;padding-bottom:8px;flex-wrap:wrap}
.aik-cm-doc{display:inline-flex;align-items:center;gap:6px;height:30px;padding:0 10px;border-radius:9px;background:var(--aik-raised);color:var(--aik-text-2);font-size:13.5px}
.aik-cm-doc svg{color:var(--aik-blue)}
.aik-cm-body{position:relative;display:grid;grid-template-columns:repeat(var(--n),minmax(0,1fr));gap:64px;padding:22px;border-radius:0 22px 22px 22px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line)}
.aik-cm-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none}
.aik-cm-svg path{fill:none;stroke-width:1.5;stroke-dasharray:3 4;transition:stroke var(--aik-d-panel)}
.aik-cm-svg path[data-on]{animation:aik-cm-flow 900ms linear infinite}
.aik-cm-stage{position:relative;display:grid;align-content:start;gap:0;padding:14px 14px 16px;border-radius:18px;border:1.5px dashed var(--aik-line-3);background:color-mix(in oklab,var(--aik-panel) 70%,transparent);transition:border-color var(--aik-d-panel)}
.aik-cm-stage[data-on]{border-color:color-mix(in oklab,var(--aik-blue) 55%,transparent)}
.aik-cm-tag{justify-self:start;display:inline-flex;align-items:center;height:26px;padding:0 9px;margin-bottom:6px;border-radius:8px;background:var(--aik-raised);color:var(--aik-text-2);font-size:13px;font-weight:500}
.aik-cm-step{position:relative;display:grid;justify-items:center;padding-top:30px}
.aik-cm-step::before{content:"";position:absolute;top:0;bottom:calc(100% - 30px);left:50%;border-left:1.5px dashed var(--aik-line-3);transition:border-color var(--aik-d-panel)}
.aik-cm-step[data-ready]::before{border-left-color:var(--aik-blue)}
.aik-cm-dia{position:absolute;top:9px;left:50%;width:13px;height:13px;margin-left:-6.5px;rotate:45deg;border-radius:3px;background:var(--aik-panel);box-shadow:inset 0 0 0 1.5px var(--aik-line-3);transition:background var(--aik-d-panel),box-shadow var(--aik-d-panel);z-index:1}
.aik-cm-step[data-ready] .aik-cm-dia{background:var(--aik-blue);box-shadow:0 0 0 4px color-mix(in oklab,var(--aik-blue) 22%,transparent),0 0 14px var(--aik-blue);animation:aik-cm-pop 520ms var(--aik-ease-out)}
.aik-cm-task{width:100%;display:grid;gap:12px;padding:12px;border-radius:15px;background:var(--aik-card);box-shadow:var(--aik-sh-card);transition:box-shadow var(--aik-d-panel),opacity var(--aik-d-panel)}
.aik-cm-task[data-s="run"]{box-shadow:0 0 0 1.5px color-mix(in oklab,var(--aik-blue) 70%,transparent),0 12px 30px -14px color-mix(in oklab,var(--aik-blue) 55%,transparent)}
.aik-cm-task[data-s="optional"]{background:transparent;box-shadow:inset 0 0 0 1.5px var(--aik-line-2);outline:1.5px dashed var(--aik-line-3);outline-offset:-1.5px}
.aik-cm-id{display:flex;align-items:center;gap:11px;min-width:0;line-height:19px}
.aik-cm-id > span:last-child{display:grid;min-width:0}
.aik-cm-id b{font-size:15px;font-weight:500;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-cm-id small{font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-cm-tile{display:grid;place-items:center;width:38px;height:38px;flex:none;border-radius:11px;background:color-mix(in oklab,var(--c) 14%,var(--aik-card));box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 25%,transparent)}
.aik-cm-foot{display:flex;align-items:center;gap:8px;min-height:32px;padding:4px 4px 4px 6px;border-radius:10px;background:var(--aik-panel);font-size:13px;color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-cm-st{display:inline-flex;align-items:center;gap:5px;height:24px;padding:0 8px;border-radius:7px;font-weight:500;color:var(--aik-text-2);background:var(--aik-raised)}
.aik-cm-st[data-s="done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 14%,transparent)}
.aik-cm-st[data-s="run"]{color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-cm-st[data-s="optional"]{background:transparent;box-shadow:inset 0 0 0 1px var(--aik-line-3)}
.aik-cm-right{margin-left:auto;display:inline-flex;align-items:center;gap:6px}
.aik-cm-ring{width:15px;height:15px;border-radius:50%;background:conic-gradient(var(--aik-blue) calc(var(--p)*1turn),var(--aik-line-2) 0);-webkit-mask:radial-gradient(circle,transparent 4.6px,#000 5px);mask:radial-gradient(circle,transparent 4.6px,#000 5px);transition:--p var(--aik-d-move)}
.aik-cm-ms{position:relative;width:100%;display:flex;align-items:center;gap:10px;height:46px;padding:0 12px;border-radius:23px;background:var(--aik-card);box-shadow:var(--aik-sh-card);font-size:14.5px;color:var(--aik-text-2);transition:box-shadow var(--aik-d-panel),color var(--aik-d-panel)}
.aik-cm-ms > span{display:grid;place-items:center;width:26px;height:26px;border-radius:50%;background:var(--aik-raised);color:var(--aik-text-3);transition:background var(--aik-d-panel),color var(--aik-d-panel)}
.aik-cm-ms[data-on]{color:var(--aik-text);box-shadow:0 0 0 1.5px color-mix(in oklab,var(--aik-green) 60%,transparent)}
.aik-cm-ms[data-on] > span{background:var(--aik-green);color:var(--aik-on-fill);animation:aik-cm-pop 520ms var(--aik-ease-out)}
@keyframes aik-cm-pop{40%{scale:1.35}}
@keyframes aik-cm-flow{to{stroke-dashoffset:-14}}
@property --p{syntax:"<number>";inherits:false;initial-value:0}
@container (max-width:820px){
.aik-cm-body{grid-template-columns:minmax(0,1fr);gap:20px;border-radius:0 0 22px 22px;padding:14px}
.aik-cm-svg{display:none}
.aik-cm-tools{margin-left:0;width:100%;order:-1}
}
@media (prefers-reduced-motion: reduce){.aik-cm-svg path{animation:none!important}.aik-cm-dia,.aik-cm-ms > span{animation:none!important}}
`
const LABEL: Record<CaseTaskStatus, string> = {
done: "Done",
run: "Working",
wait: "Waiting",
optional: "Optional",
}
function boxIn(node: HTMLElement, root: HTMLElement) {
let left = 0
let top = 0
let el: HTMLElement | null = node
while (el && el !== root) {
left += el.offsetLeft
top += el.offsetTop
el = el.offsetParent as HTMLElement | null
}
return { left, top, width: node.offsetWidth, height: node.offsetHeight }
}
/**
* An open case worked by agents and people, as a case plan. Stages hold tasks;
* each task waits behind a diamond (its entry criterion) that lights when the
* criterion holds, milestones close a stage and hand on to the next, and
* optional tasks wait for someone to plan them.
*/
export function CaseModel({
cases,
active,
onCase,
documents = [],
stages,
paused,
onPause,
onRestart,
onPlan,
className,
}: CaseModelProps) {
const body = React.useRef<HTMLDivElement>(null)
const firsts = React.useRef(new Map<string, HTMLElement>())
const ends = React.useRef(new Map<string, HTMLElement>())
const [links, setLinks] = React.useState<
Array<{ id: string; d: string; on: boolean }>
>([])
const shape = stages.map((s) => s.id + s.tasks.length).join()
const lit = stages.map((s) => (s.milestone.reached ? 1 : 0)).join()
const measure = React.useCallback(() => {
const root = body.current
if (!root) return
const next: typeof links = []
stages.forEach((stage, i) => {
const after = stages[i + 1]
if (!after) return
const end = ends.current.get(stage.id)
const first = firsts.current.get(after.id)
if (!end || !first) return
const a = boxIn(end, root)
const b = boxIn(first, root)
const x1 = a.left + a.width
const y1 = a.top + a.height / 2
const x2 = b.left + b.width / 2 - 7
const y2 = b.top + 15
const mx = x1 + 30
next.push({
id: stage.id,
d: `M${x1} ${y1}H${mx - 10}Q${mx} ${y1} ${mx} ${y1 - 10}V${y2 + 10}Q${mx} ${y2} ${mx + 10} ${y2}H${x2}`,
on: stage.milestone.reached,
})
})
setLinks(next)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [shape, lit])
React.useLayoutEffect(() => {
measure()
const observer = new ResizeObserver(measure)
if (body.current) observer.observe(body.current)
return () => observer.disconnect()
}, [measure])
return (
<div className={cx("aik aik-cm", className)}>
<AiKitStyles />
<AiStyle id="case-model" css={CSS} />
<div className="aik-cm-top">
<div className="aik-cm-tabs" role="tablist" aria-label="Cases">
{cases.map((c) => (
<button
key={c.id}
type="button"
role="tab"
className="aik-cm-tab"
aria-selected={c.id === active}
onClick={() => onCase?.(c.id)}
style={{ "--c": `var(--aik-f-${c.tone})` } as React.CSSProperties}
>
<span>
<FolderClosed size={16} aria-hidden="true" />
</span>
{c.label}
</button>
))}
</div>
<div className="aik-cm-tools">
{documents.map((doc) => (
<span key={doc} className="aik-cm-doc">
<FileText size={14} aria-hidden="true" />
{doc}
</span>
))}
<button
type="button"
className="aik-btn ghost icon sm"
aria-label={paused ? "Resume" : "Pause"}
onClick={onPause}
>
{paused ? <Play size={14} /> : <Pause size={14} />}
</button>
<button
type="button"
className="aik-btn ghost icon sm"
aria-label="Restart"
onClick={onRestart}
>
<RotateCcw size={14} />
</button>
</div>
</div>
<div
ref={body}
className="aik-cm-body"
style={{ "--n": stages.length } as React.CSSProperties}
>
<svg className="aik-cm-svg" aria-hidden="true">
{links.map((link) => (
<path
key={link.id}
d={link.d}
data-on={link.on ? "" : undefined}
stroke={link.on ? "var(--aik-blue)" : "var(--aik-line-3)"}
/>
))}
</svg>
{stages.map((stage) => {
const busy = stage.tasks.some((t) => t.status === "run")
return (
<section
key={stage.id}
className="aik-cm-stage"
data-on={busy ? "" : undefined}
aria-label={stage.label}
>
<span className="aik-cm-tag">{stage.label}</span>
{stage.tasks.map((task, i) => (
<div
key={task.id}
className="aik-cm-step"
data-ready={task.ready ? "" : undefined}
ref={
i === 0
? (node) => {
if (node) firsts.current.set(stage.id, node)
else firsts.current.delete(stage.id)
}
: undefined
}
>
<i className="aik-cm-dia" aria-hidden="true" />
<div
className="aik-cm-task"
data-s={task.status}
style={
{
"--c": `var(--aik-f-${task.tone})`,
} as React.CSSProperties
}
>
<div className="aik-cm-id">
<span className="aik-cm-tile">
{task.kind === "service" && task.brand ? (
<BrandIcon name={task.brand} tint="brand" size={20} />
) : task.kind === "person" ? (
<Face
mood={
task.status === "run"
? "work"
: task.status === "done"
? "happy"
: "idle"
}
shape={task.shape ?? "orb"}
tone={task.tone}
size={28}
/>
) : (
<AgentGlyph
seed={task.id}
tone={task.tone}
size={17}
live={task.status === "run"}
/>
)}
</span>
<span>
<b>{task.title}</b>
<small>{task.owner}</small>
</span>
</div>
<div className="aik-cm-foot">
<span className="aik-cm-st" data-s={task.status}>
{task.status === "done" ? (
<Check size={13} aria-hidden="true" />
) : null}
<Roll>{LABEL[task.status]}</Roll>
</span>
<span className="aik-cm-right">
{task.status === "optional" ? (
onPlan ? (
<button
type="button"
className="aik-btn line sm"
onClick={() => onPlan(task.id)}
>
Plan it
</button>
) : (
"Not planned"
)
) : task.progress ? (
<>
<i
className="aik-cm-ring"
style={
{
"--p": task.progress[0] / task.progress[1],
} as React.CSSProperties
}
aria-hidden="true"
/>
<Roll>{`${task.progress[0]}/${task.progress[1]}`}</Roll>
</>
) : task.seconds !== undefined ? (
<Roll>{`${task.seconds}s`}</Roll>
) : null}
</span>
</div>
</div>
</div>
))}
<div
className="aik-cm-step"
data-ready={stage.milestone.reached ? "" : undefined}
>
<i className="aik-cm-dia" aria-hidden="true" />
<div
className="aik-cm-ms"
data-on={stage.milestone.reached ? "" : undefined}
ref={(node) => {
if (node) ends.current.set(stage.id, node)
else ends.current.delete(stage.id)
}}
>
<span>
{stage.milestone.reached ? (
<Check size={14} aria-hidden="true" />
) : (
<Flag size={13} aria-hidden="true" />
)}
</span>
{stage.milestone.label}
</div>
</div>
</section>
)
})}
</div>
</div>
)
}
Installation
One command adds case-model 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/case-model.json