The platform for continuously improving AI agents.
import { useEffect, useState } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { Icon } from "@/components/ui/icons";
import { Spinner } from "@/components/ui/spinner";
export type RunPhase =
| "running"
| "done"
| "failed"
| "idle"
| "blocked"
| "cancelled"
| "unmet"
| "exhausted";
/** A run's state and how long it has run, from its recorded start and completion. */
export function RunStatus({
phase,
startedAt,
completedAt,
}: {
phase: RunPhase;
startedAt?: string;
completedAt?: string | null;
}) {
const [clock, setClock] = useState(Date.now);
const reduce = useReducedMotion();
const running = phase === "running";
useEffect(() => {
if (!running) return;
const timer = setInterval(() => setClock(Date.now()), 5000);
return () => clearInterval(timer);
}, [running]);
const end = running ? clock : completedAt ? Date.parse(completedAt) : Number.NaN;
const elapsed = startedAt ? end - Date.parse(startedAt) : Number.NaN;
const minutes = Number.isFinite(elapsed) ? Math.max(0, Math.floor(elapsed / 60000)) : null;
const duration =
minutes === null
? null
: minutes < 1
? "less than a minute"
: minutes < 60
? `${minutes} min`
: `${Math.floor(minutes / 60)} hr${minutes % 60 ? ` ${minutes % 60} min` : ""}`;
const line = running
? minutes === null
? "Running"
: minutes < 1
? "Just started"
: `Running for ${duration}`
: phase === "done"
? duration
? `Done in ${duration}`
: "Done"
: phase === "failed"
? duration
? `Failed after ${duration}`
: "Failed"
: {
blocked: "Blocked",
cancelled: "Cancelled",
exhausted: "Limit reached",
idle: "Not running",
unmet: "Incomplete",
}[phase];
return (
<div
className="inline-flex h-8 w-fit max-w-full min-w-0 items-center rounded-sm border border-border bg-card px-3 text-card-foreground"
data-slot="run-status"
data-state={phase}
>
<span
className="relative flex min-w-0 items-center gap-2 text-xs leading-5 text-muted-foreground"
data-slot="run-status-line"
>
<span className="relative inline-flex size-3.5 flex-none">
<AnimatePresence initial={false} mode="popLayout">
<motion.span
animate={{ opacity: 1, scale: 1 }}
className="inline-flex"
exit={{ opacity: 0, scale: reduce ? 1 : 0.8 }}
initial={{ opacity: 0, scale: reduce ? 1 : 0.8 }}
key={phase}
transition={{ duration: 0.2 }}
>
{running ? (
<Spinner size="sm" />
) : phase === "done" ? (
<Icon.success className="size-3.5 text-success" />
) : phase === "failed" ? (
<Icon.close className="size-3.5 text-destructive" />
) : (
<Icon.bell className="size-3.5" />
)}
</motion.span>
</AnimatePresence>
</span>
<span className="min-w-0 truncate tabular-nums">{line}</span>
</span>
<output aria-live="polite" className="sr-only">
{running ? null : line}
</output>
</div>
);
}