AI Steps

An agent run you can follow: a live rail whose running step opens to show its output, a track a comet travels from node to node, and a depth stack of step cards.

Installation

Installation

One command adds ai-steps 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/ai-steps.json
ai-steps-demo
components/ui/ai-steps-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop, useIdleHands } from "@/lib/ai-kit"
import {
  AiStepLog,
  AiSteps,
  AiStepStack,
  AiStepTrack,
  type AiStep,
} from "@/registry/tween/ai-steps"

const PLAN = [
  {
    id: "read",
    title: "Read the hero section",
    tool: "read_file",
    ms: 1000,
    duration: "0.8s",
    note: "hero.tsx and the page layout, 148 lines in all.",
  },
  {
    id: "find",
    title: "Find text-skew-reveal",
    tool: "search_registry",
    ms: 1200,
    duration: "1.1s",
    note: "Already in the registry, so no new dependency.",
  },
  {
    id: "write",
    title: "Write the pricing section",
    tool: "write_file",
    ms: 3200,
    duration: "3.2s",
    note: "A monthly and yearly toggle on a 180ms state change.",
  },
  {
    id: "check",
    title: "Run the type check",
    tool: "run_command",
    ms: 1800,
    duration: "1.8s",
    note: "pnpm typecheck across 214 files.",
  },
  {
    id: "build",
    title: "Rebuild the registry",
    tool: "run_command",
    ms: 1400,
    duration: "1.4s",
    note: "Regenerates 104 registry files.",
  },
] as const

const CODE = [
  "export function PricingSection() {",
  "  const [yearly, setYearly] = useState(false)",
  "  return (",
  '    <section className="pricing">',
  "      <Toggle on={yearly} onChange={setYearly} />",
  "      <Plans yearly={yearly} />",
]
const CHECK = ["$ tsc --noEmit", "checked 214 files", "0 errors"]

const clock = (ms: number) => {
  const seconds = Math.floor(ms / 1000)
  return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`
}

/** One scripted run. `at` is the index of the running step; PLAN.length means finished. */
function useRun(
  root: React.RefObject<HTMLDivElement | null>,
  hands: boolean,
  onDone?: () => void,
  onStart?: () => void
) {
  const [at, setAt] = React.useState(2)
  const [ms, setMs] = React.useState(4000)
  const [lines, setLines] = React.useState(3)

  useAiLoop(
    root,
    async ({ sleep }) => {
      const wait = async (total: number) => {
        for (let spent = 0; spent < total; spent += 200) {
          await sleep(200)
          setMs((value) => value + 200)
        }
      }
      onStart?.()
      setMs(0)
      setLines(0)
      setAt(0)
      await sleep(500)
      for (let index = 0; index < PLAN.length; index++) {
        setAt(index)
        setLines(0)
        const step = PLAN[index]
        if (!step) continue
        const feed = index === 2 ? CODE.length : index === 3 ? CHECK.length : 0
        if (feed === 0) await wait(step.ms)
        else
          for (let line = 1; line <= feed; line++) {
            setLines(line)
            await wait(step.ms / feed)
          }
      }
      setAt(PLAN.length)
      await sleep(1100)
      onDone?.()
      await sleep(2600)
    },
    {
      gap: 400,
      enabled: !hands,
      still: () => {
        setAt(2)
        setLines(4)
      },
    }
  )

  const steps: AiStep[] = PLAN.map((step, index) => {
    const status = index < at ? "done" : index === at ? "run" : "todo"
    let detail: React.ReactNode = step.note
    if (index === 2)
      detail = (
        <AiStepLog
          title="pricing-section.tsx"
          lines={CODE.slice(0, lines)}
          progress={lines / CODE.length}
        />
      )
    if (index === 3)
      detail = (
        <AiStepLog title="pnpm typecheck" lines={CHECK.slice(0, lines)} />
      )
    return {
      id: step.id,
      title: step.title,
      tool: step.tool,
      status,
      duration: status === "done" ? step.duration : undefined,
      detail,
    }
  })
  return { steps, at, setAt, elapsed: clock(ms) }
}

function Rail() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [open, setOpen] = React.useState(true)
  const { steps, elapsed } = useRun(
    root,
    hands,
    () => setOpen(false),
    () => setOpen(true)
  )
  return (
    <div
      ref={root}
      style={{
        display: "grid",
        width: "min(100%, 560px)",
        minHeight: 380,
        alignContent: "start",
      }}
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiSteps
        steps={steps}
        open={open}
        onOpenChange={setOpen}
        elapsed={elapsed}
        summary={`Worked for ${Number(elapsed.split(":")[1] ?? 0)}s · read 3 files, wrote 1`}
      />
    </div>
  )
}

function Track() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const { steps, setAt, elapsed } = useRun(root, hands)
  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 560px)" }}
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiStepTrack
        steps={steps.map((step, index) => ({
          ...step,
          detail: PLAN[index]?.note,
        }))}
        elapsed={elapsed}
        onSelectStep={setAt}
      />
    </div>
  )
}

function Stack() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const { steps, at, setAt, elapsed } = useRun(root, hands)
  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 540px)" }}
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiStepStack
        label="Pricing section"
        steps={steps.map((step, index) => ({
          ...step,
          detail: PLAN[index]?.note,
        }))}
        elapsed={elapsed}
        onAdvance={() => setAt(at >= PLAN.length ? 0 : at + 1)}
      />
    </div>
  )
}

export function AiStepsDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Live rail", node: <Rail /> },
        { name: "Comet track", node: <Track /> },
        { name: "Card stack", node: <Stack /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-steps.json

Usage

import {
  AiStepLog,
  AiSteps,
  AiStepStack,
  AiStepTrack,
  type AiStep,
} from "@/components/ui/ai-steps"
const steps: AiStep[] = [
  { id: "read", title: "Read the hero section", tool: "read_file", status: "done", duration: "0.8s" },
  {
    id: "write",
    title: "Write the pricing section",
    tool: "write_file",
    status: "run",
    detail: <AiStepLog title="pricing-section.tsx" lines={lines} progress={0.5} />,
  },
  { id: "check", title: "Run the type check", tool: "run_command", status: "todo" },
]
 
const [open, setOpen] = useState(true)
 
<AiSteps
  steps={steps}
  open={open}
  onOpenChange={setOpen}
  elapsed="0:04"
  summary="Worked for 41s · read 3 files, wrote 1"
/>

Each step's status is one of todo, run, ask, done or fail. With the AI SDK, map a tool part's input-streaming and input-available to run, approval-requested to ask, output-available to done and output-error to fail.

Versions

  • AiSteps is the live rail. Marks are typed (a dashed ring, a spinning ring, an amber ring, a drawn check, a cross) and the connector turns green behind finished steps. The running row opens in place to show its detail, and the header carries the run's only clock. When every step is done the header reads your summary; fold the list by setting open to false.
  • AiStepTrack is a horizontal track. A comet travels from node to node in 620ms, each node draws its check on arrival, and the current title rolls above with the face following the run's mood. A string detail shows as a note under the track.
  • AiStepStack is a stack of cards. The front card is the current step; when it finishes it is stamped done, lifts away, and the next card rises. A segmented bar below shows the whole run.

AiStepLog is the small live panel used inside a running row. summarizeSteps(steps) returns one status for a whole run.

Props

AiSteps

PropTypeDefaultDescription
stepsAiStep[]{ id, title, tool?, status, duration?, detail? }.
openbooleanWhether the list shows under the header.
onOpenChange(open: boolean) => voidCalled when the header is pressed.
elapsedstringThe run's clock, already formatted.
summarystringThe header's text once every step is done.

AiStepTrack

PropTypeDefaultDescription
stepsAiStep[]The run. A string detail is the note.
elapsedstringThe run's clock.
onSelectStep(index: number) => voidCalled when a node is pressed.
tonetone or CSS colour"blue"Face colour.
shapeface shape"blob"Face shape.

AiStepStack

PropTypeDefaultDescription
stepsAiStep[]The run. detail is the card's text.
elapsedstringThe run's clock.
labelstring"Run"The heading above the stack.
onAdvance(step: AiStep) => voidCalled when the front card is pressed.

AiStepLog

PropTypeDefaultDescription
titlestringA file name or command.
linesstring[]Lines so far. The last three are shown.
progressnumberFrom 0 to 1, drawn as a line at the foot.