Skip to content

Plan

Shows the steps an agent intends to take and how far it has got through them.

Migrate the dialog components to Base UI.
Install @base-ui/reactReplace the Radix importsRename asChild to renderRun the type checker

Migrated Dialog and AlertDialog. The type checker passes.

Installation

pnpm dlx shadcn@latest add @silkware/ai-plan

To copy the pattern by hand, install the components it composes:

pnpm dlx shadcn@latest add @silkware/bubble @silkware/button @silkware/message @silkware/trace

Then the packages:

pnpm add ai @ai-sdk/react @shadcn/helpers lucide-react

Copy the source into components/ai-elements/plan.tsx. The file also needs @/lib/ai:

components/ai-elements/plan.tsx
"use client"
import { useChat } from "@ai-sdk/react"import type { UIMessage } from "ai"import {  CircleCheckIcon,  CircleDotIcon,  CircleIcon,  RouteIcon,} from "lucide-react"
import { createChat, getMessageText } from "@/lib/ai"import { Bubble, BubbleContent } from "@/components/ui/bubble"import { Button } from "@/components/ui/button"import { Message, MessageContent } from "@/components/ui/message"import {  Trace,  TraceCollapsible,  TraceCollapsibleContent,  TraceCollapsibleTrigger,  TraceDescription,  TraceIcon,  TraceLine,  TraceTitle,} from "@/components/ui/trace"
type ChatMessage = UIMessage<  unknown,  {    plan: {      title: string      steps: { title: string; status: "done" | "running" | "pending" }[]    }  }>
type PlanPart = Extract<ChatMessage["parts"][number], { type: "data-plan" }>
const STEPS = [  "Install @base-ui/react",  "Replace the Radix imports",  "Rename asChild to render",  "Run the type checker",]
function planAt(current: number) {  return {    title: "Plan: migrate to Base UI",    steps: STEPS.map((title, index) => ({      title,      status:        index < current ? "done" : index === current ? "running" : "pending",    })),  } satisfies PlanPart["data"]}
const chat = createChat<ChatMessage>()  .user("Migrate the dialog components to Base UI.")  .assistant(({ writer }) => {    STEPS.forEach((_, index) => {      writer        .data({ type: "data-plan", id: "plan", data: planAt(index) })        .sleep(1200)    })    writer.data({ type: "data-plan", id: "plan", data: planAt(STEPS.length) })    writer.text("Migrated Dialog and AlertDialog. The type checker passes.")  })
const transcript = chat.get()const [question] = chat.get(1)const transport = chat.transport({ delayMs: 20 })
function PlanRow({  plan,  streaming,}: {  plan: PlanPart["data"]  streaming: boolean}) {  const done = plan.steps.filter((step) => step.status === "done").length  const running = streaming && done < plan.steps.length
  return (    <TraceCollapsible defaultExpanded>      <TraceCollapsibleTrigger>        <TraceLine status={running ? "running" : undefined}>          <TraceIcon>            <RouteIcon />          </TraceIcon>          <TraceTitle>{plan.title}</TraceTitle>          <TraceDescription>            {done} of {plan.steps.length} done          </TraceDescription>        </TraceLine>      </TraceCollapsibleTrigger>      <TraceCollapsibleContent>        <Trace>          {plan.steps.map((step) => (            <TraceLine              key={step.title}              status={                step.status === "pending"                  ? "pending"                  : streaming && step.status === "running"                    ? "running"                    : undefined              }            >              <TraceIcon>                {step.status === "done" ? (                  <CircleCheckIcon />                ) : step.status === "running" ? (                  <CircleDotIcon />                ) : (                  <CircleIcon />                )}              </TraceIcon>              <TraceTitle>{step.title}</TraceTitle>            </TraceLine>          ))}        </Trace>      </TraceCollapsibleContent>    </TraceCollapsible>  )}
export function PlanDemo() {  const { messages, sendMessage, setMessages, status } = useChat({    messages: transcript,    transport,  })  const busy = status === "submitted" || status === "streaming"
  return (    <div className="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">      {messages.map((message) => {        if (message.role === "user") {          return (            <Message key={message.id} align="end">              <MessageContent>                <Bubble align="end" variant="muted">                  <BubbleContent>{getMessageText(message)}</BubbleContent>                </Bubble>              </MessageContent>            </Message>          )        }
        const streaming = busy && message === messages.at(-1)        const plan = message.parts.find((part) => part.type === "data-plan")        const text = getMessageText(message)
        return (          <Message key={message.id}>            <MessageContent className="gap-4">              {plan && (                <Trace>                  <PlanRow plan={plan.data} streaming={streaming} />                </Trace>              )}              {text && <p>{text}</p>}            </MessageContent>          </Message>        )      })}      {!busy && (        <Button          variant="outline"          size="sm"          className="mt-auto self-start"          onPress={() => {            setMessages([])            sendMessage(question)          }}        >          Replay        </Button>      )}    </div>  )}

Usage

A plan is a collapsible line with a title and a count, and a list of steps inside it.

import { CircleCheckIcon, CircleDotIcon, CircleIcon, RouteIcon } from "lucide-react"
import {  Trace,  TraceCollapsible,  TraceCollapsibleContent,  TraceCollapsibleTrigger,  TraceDescription,  TraceIcon,  TraceLine,  TraceTitle,} from "@/components/ui/trace"
<TraceCollapsible defaultExpanded>  <TraceCollapsibleTrigger>    <TraceLine status={running ? "running" : undefined}>      <TraceIcon>        <RouteIcon />      </TraceIcon>      <TraceTitle>{plan.title}</TraceTitle>      <TraceDescription>        {done} of {plan.steps.length} done      </TraceDescription>    </TraceLine>  </TraceCollapsibleTrigger>  <TraceCollapsibleContent>    <Trace>      {plan.steps.map((step) => (        <TraceLine key={step.title} status={step.status === "pending" ? "pending" : undefined}>          <TraceIcon>{iconFor(step.status)}</TraceIcon>          <TraceTitle>{step.title}</TraceTitle>        </TraceLine>      ))}    </Trace>  </TraceCollapsibleContent></TraceCollapsible>

Each step has its own icon: a check for done, a dot for the current step, an empty circle for a step still ahead. Pending steps are also dimmed with status="pending". Keep the count in the line, so the progress reads without the icons.

Examples

With a description

Put a short paragraph above the steps when the plan needs a reason, for example a constraint the person should know before the agent starts.

Store the theme in a cookie so the server renders the right one and the page does not flash on load.

Add a dark variant to the theme tokensRead the theme from a cookie on the serverAdd a toggle to the settings pageCheck contrast on the charts

In progress

Two steps done, one running, one ahead. The collapsible line shimmers while the plan runs and carries the count, and the step ahead is dimmed.

Export the current price listApply the 4% increase to Brevia steel fittingsCheck margins against the supplier quotesAsk Giulia to review the changes

AI SDK

A plan is not a built-in part type. Stream it as a data part, whose type starts with data-, and give it an id. Writing a part again with the same id replaces the earlier one, so the plan updates in place as steps finish instead of piling up.

import type { UIMessage } from "ai"
type ChatMessage = UIMessage<  unknown,  {    plan: {      title: string      steps: { title: string; status: "done" | "running" | "pending" }[]    }  }>

On the server, write the plan from a UI message stream, and update it as the work moves on:

import { createUIMessageStream, createUIMessageStreamResponse } from "ai"
const stream = createUIMessageStream<ChatMessage>({  execute: async ({ writer }) => {    writer.write({ type: "data-plan", id: "plan", data: planAt(0) })    await doStepOne()    writer.write({ type: "data-plan", id: "plan", data: planAt(1) })  },})
return createUIMessageStreamResponse({ stream })

On the client, find the part and render it. While the message is still streaming, a step that is running is live; once the stream ends, a step that is still running did not finish, and the demo does not shimmer it:

const plan = message.parts.find((part) => part.type === "data-plan")
{plan && <PlanRow plan={plan.data} streaming={streaming} />}

A model can also fill a plan through a tool whose input is the list of steps, and render the last such part. The Queue page shows that route.

Scripted streams in development

writer.data in createChat writes the data part, and sleep spaces the updates:

const chat = createChat<ChatMessage>()  .user("Migrate the dialog components to Base UI.")  .assistant(({ writer }) => {    STEPS.forEach((_, index) => {      writer.data({ type: "data-plan", id: "plan", data: planAt(index) }).sleep(1200)    })  })

Accessibility

KeyAction
EnterOpens or closes the list of steps.
SpaceOpens or closes the list of steps.
TabMoves focus to the next control.

The composed components provide:

  • A trigger that is a real button with aria-expanded, and a panel tied to it.
  • A visible focus ring on the trigger.

You provide:

  • The status of each step in text. The icon and the dimming are not enough on their own, so the count ("1 of 4 done") stays in the line, and a step that failed or was skipped says so in its title.
  • An update that a screen reader can notice. A plan that changes in place is not announced. Put the current step in the message text when it matters ("Now checking margins").
  • Step titles that make sense read out one after another.

Guidelines

When to use

  • For work with three or more steps that the person may want to check before or while the agent does them.
  • When the order of the steps matters and the person might want to change it.

When not to use

  • For a single action. Use a Tool line.
  • For a to-do list the agent keeps beside the conversation, with done and open groups. Use Queue.
  • For what the agent is doing right now, in more detail. Use Task.
  • For the agent's reasoning about the plan. Use Reasoning.

Content

  • Title the plan with its goal: "Plan: update the Q1 price list".
  • Write each step as a verb and an object, in the same form, in sentence case: "Check margins against the supplier quotes".
  • Keep it to what the agent will do, in the order it will do it. Seven steps is a plan; twenty is a log.
  • Update the status when it changes, and keep the finished steps in view.
Do

Name each step by the action and its object, and keep the count in the line.

Don’t

Don’t write steps as "Step 1", "Processing" and "Finishing". The person cannot check them against what happens.

Human in the loop

A plan is a promise the person can read before the agent starts.

  • Show the plan before the first step that changes anything, and wait if the plan includes a step that needs approval.
  • Mark the steps that will ask for approval, so the person knows where the agent will stop. Use the Confirmation prompt at that step, not a blanket approval of the whole plan.
  • Let the person change the plan. If the agent adds, drops or reorders a step after the work starts, show that in the plan and say it in the message.
  • Keep the plan after it is finished, with every step marked, so the record shows what the agent did and what it left.

Composition

ComponentWhat it does here
TraceLays out the title line, the steps and the collapsible, and carries the running and pending status.
MessagePlaces the conversation in turns.
BubbleWraps the person's own message.
ButtonThe replay control of the demo.

components/ai-elements/plan.tsx exports PlanDemo, a scripted chat that streams a data-plan part five times (one per step, then once more with every step done) and then the answer text. Copy PlanRow and planAt into your app and replace the scripted chat with useChat against your route.