AI Plan

The plan of a run: a board whose cards move from to do to done, a winding route the agent face travels, and a draft list you drag to reorder before starting.

Installation

Installation

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

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { typeInto, useAiLoop, useIdleHands } from "@/lib/ai-kit"
import {
  AiPlanBoard,
  AiPlanDraft,
  AiPlanRoute,
  type AiDraftStep,
  type AiMilestone,
  type AiPlanCard,
  type AiPlanColumn,
} from "@/registry/tween/ai-plan"

const realSleep = (ms: number) =>
  new Promise<void>((resolve) => window.setTimeout(resolve, ms))

const CARDS: AiPlanCard[] = [
  {
    id: "read",
    title: "Read the hero section",
    column: "todo",
    agent: "Mira",
    estimate: "2m",
  },
  {
    id: "reveal",
    title: "Reuse text-skew-reveal",
    column: "todo",
    agent: "Mira",
    estimate: "3m",
  },
  {
    id: "write",
    title: "Write the pricing section",
    column: "todo",
    agent: "Mira",
    estimate: "8m",
  },
  {
    id: "tests",
    title: "Write 24 tests",
    column: "todo",
    agent: "Odo",
    tone: "violet",
    shape: "blob",
    estimate: "6m",
  },
  {
    id: "build",
    title: "Rebuild the registry",
    column: "todo",
    agent: "Pim",
    tone: "green",
    shape: "leaf",
    estimate: "2m",
  },
]

function Board() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [cards, setCards] = React.useState<AiPlanCard[]>(() =>
    CARDS.map((card, index) => ({
      ...card,
      column: index === 0 ? "done" : index === 1 ? "doing" : "todo",
    }))
  )
  const move = (id: string, column: AiPlanColumn) =>
    setCards((list) =>
      list.map((card) => (card.id === id ? { ...card, column } : card))
    )

  useAiLoop(
    root,
    async ({ sleep }) => {
      setCards(CARDS)
      await sleep(1100)
      const script: Array<[string, AiPlanColumn, number]> = [
        ["read", "doing", 1100],
        ["read", "done", 500],
        ["reveal", "doing", 700],
        ["tests", "doing", 1300],
        ["reveal", "done", 600],
        ["write", "doing", 1500],
        ["tests", "done", 900],
        ["write", "done", 500],
        ["build", "doing", 1400],
        ["build", "done", 2600],
      ]
      for (const [id, column, hold] of script) {
        move(id, column)
        await sleep(hold)
      }
    },
    { gap: 500, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 580px)" }}
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiPlanBoard cards={cards} onMove={move} />
    </div>
  )
}

const STOPS: AiMilestone[] = [
  {
    id: "read",
    title: "Read the hero",
    short: "Read",
    note: "hero.tsx and the page layout, to see what is there.",
  },
  {
    id: "pick",
    title: "Pick the reveal",
    short: "Pick",
    note: "text-skew-reveal is installed already, so nothing new to add.",
  },
  {
    id: "write",
    title: "Write the section",
    short: "Write",
    note: "Pricing with a monthly and yearly toggle.",
  },
  {
    id: "check",
    title: "Check the types",
    short: "Check",
    note: "pnpm typecheck across 214 files.",
  },
  {
    id: "ship",
    title: "Ship the preview",
    short: "Ship",
    note: "Rebuild the registry and open a preview link.",
  },
]

function Route() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [done, setDone] = React.useState(2)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setDone(0)
      await sleep(1500)
      for (let count = 1; count <= STOPS.length; count++) {
        setDone(count)
        await sleep(count === STOPS.length ? 2600 : 1700)
      }
    },
    { gap: 500, enabled: !hands, still: () => setDone(2) }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 580px)" }}
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiPlanRoute milestones={STOPS} done={done} onSelect={setDone} />
    </div>
  )
}

const DRAFT: AiDraftStep[] = [
  { id: "read", title: "Read the hero section", minutes: 2 },
  { id: "write", title: "Write the pricing section", minutes: 8 },
  { id: "build", title: "Rebuild the registry", minutes: 2 },
  { id: "check", title: "Run the type check", minutes: 3 },
]

function Draft() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [steps, setSteps] = React.useState(DRAFT)
  const [lift, setLift] = React.useState<{ id: string; to: number }>()
  const [add, setAdd] = React.useState("")
  const [status, setStatus] = React.useState<"draft" | "running">("draft")
  const [at, setAt] = React.useState(0)
  const run = React.useRef(0)
  const serial = React.useRef(0)

  const reorder = (ids: string[]) =>
    setSteps((list) =>
      ids.flatMap((id) => list.filter((step) => step.id === id))
    )
  const append = (title: string) =>
    setSteps((list) => [
      ...list,
      { id: `new-${++serial.current}`, title, minutes: 4 },
    ])

  const go = React.useCallback(
    async (
      sleep: (ms: number) => Promise<void>,
      mine: number,
      count: number
    ) => {
      setStatus("running")
      for (let index = 0; index < count; index++) {
        if (run.current !== mine) return
        setAt(index)
        await sleep(900)
      }
      if (run.current !== mine) return
      setStatus("draft")
    },
    []
  )

  useAiLoop(
    root,
    async ({ sleep }) => {
      const mine = ++run.current
      setStatus("draft")
      setSteps(DRAFT)
      setLift(undefined)
      setAdd("")
      await sleep(1300)
      setLift({ id: "check", to: 3 })
      await sleep(450)
      setLift({ id: "check", to: 2 })
      await sleep(800)
      reorder(["read", "write", "check", "build"])
      setLift(undefined)
      await sleep(900)
      await typeInto(setAdd, "Check contrast in dark mode", sleep, 30)
      await sleep(400)
      append("Check contrast in dark mode")
      setAdd("")
      await sleep(1300)
      await go(sleep, mine, 5)
      await sleep(700)
    },
    { gap: 400, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 540px)" }}
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiPlanDraft
        title="Pricing section"
        steps={steps}
        onReorder={reorder}
        onRemove={(id) =>
          setSteps((list) => list.filter((step) => step.id !== id))
        }
        onAdd={append}
        addValue={add}
        onAddValueChange={setAdd}
        lift={hands ? undefined : lift}
        status={status}
        runningIndex={at}
        onStart={() => {
          const mine = ++run.current
          void go(realSleep, mine, steps.length)
        }}
      />
    </div>
  )
}

export function AiPlanDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Board", node: <Board /> },
        { name: "Route", node: <Route /> },
        { name: "Draft plan", node: <Draft /> },
      ]}
    />
  )
}

Installation

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

Usage

import { AiPlanBoard, AiPlanDraft, AiPlanRoute } from "@/components/ui/ai-plan"
const [steps, setSteps] = useState([
  { id: "read", title: "Read the hero section", minutes: 2 },
  { id: "write", title: "Write the pricing section", minutes: 8 },
])
 
<AiPlanDraft
  title="Pricing section"
  steps={steps}
  onReorder={(ids) =>
    setSteps((list) => ids.flatMap((id) => list.filter((step) => step.id === id)))
  }
  onAdd={(title) => setSteps((list) => [...list, { id: crypto.randomUUID(), title }])}
  onRemove={(id) => setSteps((list) => list.filter((step) => step.id !== id))}
  onStart={start}
/>

Versions

  • AiPlanBoard is a three-column board. Whenever a card's column changes it travels to its new place on a FLIP transform, so the agent's progress reads as movement. Cards also move by hand with their buttons or the left and right arrow keys.
  • AiPlanRoute is a winding route with a node per milestone. The agent's face walks the path as done rises, the stretch behind it draws in, and a card names the current stop.
  • AiPlanDraft is the plan before the run. Drag a row by its handle (it lifts while the others slide) or move it with the up and down arrow keys, add and remove steps, and watch the total follow. Start locks the rows and turns the footer into a progress line.

Props

AiPlanBoard

PropTypeDefaultDescription
cards{ id, title, column, agent?, tone?, shape?, estimate? }[]column is todo, doing or done.
onMove(id: string, column) => voidCalled from a card's buttons or arrow keys.
labels[string, string, string]["To do", "Doing", "Done"]Column headings.

AiPlanRoute

PropTypeDefaultDescription
milestones{ id, title, note?, short? }[]The stops, in order.
donenumberHow many are finished. The face stands at the next.
onSelect(index: number) => voidCalled from a node or the arrow buttons.
tonetone or CSS colour"blue"Face colour.
shapeface shape"blob"Face shape.

AiPlanDraft

PropTypeDefaultDescription
steps{ id, title, minutes? }[]The plan, in order.
onReorder(ids: string[]) => voidCalled with the new order after a drag or key move.
onAdd(title: string) => voidCalled on Enter in the add row. Omit to hide the row.
onRemove(id: string) => voidCalled from a row's remove button.
onStart() => voidCalled from Start.
status"draft" | "running""draft"Running locks the rows and shows progress.
runningIndexnumber0The step in progress while running.
addValuestringControls the add row's text.
onAddValueChange(value: string) => voidCalled as the add row changes.
lift{ id: string; to: number }Holds a row lifted over a slot, for scripted demos.
titlestring"Plan"The heading.