Skip to content

Checkpoint

A marker after a turn that lets the person return the conversation to that point.

Rename the button component to action.

Renamed Button to Action in components/ui/button.tsx.

Button renamed
Update the docs to match.

Updated the Button page to say Action.

Installation

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

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

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

Then install the packages it imports and copy the pattern into components/ai-elements/checkpoint.tsx:

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

The pattern also imports @/lib/ai, which holds the scripted streams.

components/ai-elements/checkpoint.tsx
"use client"
import * as React from "react"import { useChat } from "@ai-sdk/react"import type { UIMessage } from "ai"
import { createChat, getMessageText } from "@/lib/ai"import { Bubble, BubbleContent } from "@/components/ui/bubble"import { Button } from "@/components/ui/button"import { Marker, MarkerContent } from "@/components/ui/marker"import { Message, MessageContent } from "@/components/ui/message"
type ChatMessage = UIMessage<  unknown,  {    checkpoint: {      label: string    }  }>
type CheckpointPart = Extract<  ChatMessage["parts"][number],  { type: "data-checkpoint" }>
const chat = createChat<ChatMessage>()  .user("Rename the button component to action.")  .assistant(({ writer }) => {    writer.text("Renamed Button to Action in components/ui/button.tsx.")    writer.data({      type: "data-checkpoint",      id: "rename",      data: { label: "Button renamed" },    })  })  .user("Update the docs to match.")  .assistant(({ writer }) => {    writer.text("Updated the Button page to say Action.")  })
const transcript = chat.get()const transport = chat.transport({ delayMs: 20 })
export function CheckpointDemo() {  const { messages, sendMessage, setMessages, status } = useChat({    messages: transcript,    transport,  })  const nextMessage = chat.next(messages)  const busy = status === "submitted" || status === "streaming"
  function restore(messageId: string) {    const index = messages.findIndex((message) => message.id === messageId)
    if (index === -1) {      return    }
    setMessages(messages.slice(0, index + 1))  }
  return (    <div className="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">      {messages.map((message) => {        const checkpoint = message.parts.find(          (part): part is CheckpointPart => part.type === "data-checkpoint"        )        const index = messages.findIndex((item) => item.id === message.id)        const canRestore = index < messages.length - 1
        return (          <React.Fragment key={message.id}>            {message.role === "user" ? (              <Message align="end">                <MessageContent>                  <Bubble align="end" variant="muted">                    <BubbleContent>{getMessageText(message)}</BubbleContent>                  </Bubble>                </MessageContent>              </Message>            ) : (              <Message>                <MessageContent>                  <p>{getMessageText(message)}</p>                </MessageContent>              </Message>            )}            {checkpoint && (              <Marker variant="separator">                <MarkerContent className="inline-flex items-center gap-2">                  {checkpoint.data.label}                  {canRestore && (                    <Button                      size="xs"                      variant="outline"                      aria-label={`Restore to ${checkpoint.data.label}`}                      onPress={() => restore(message.id)}                    >                      Restore                    </Button>                  )}                </MarkerContent>              </Marker>            )}          </React.Fragment>        )      })}      {!busy && nextMessage && (        <Button          variant="outline"          size="sm"          className="mt-auto self-start"          onPress={() => sendMessage(nextMessage)}        >          Continue        </Button>      )}    </div>  )}

Usage

import { Button } from "@/components/ui/button"import { Marker, MarkerContent } from "@/components/ui/marker"
<Marker variant="separator">  <MarkerContent className="inline-flex items-center gap-2">    {checkpoint.data.label}    {canRestore && (      <Button        size="xs"        variant="outline"        aria-label={`Restore to ${checkpoint.data.label}`}        onPress={() => restore(message.id)}      >        Restore      </Button>    )}  </MarkerContent></Marker>

A checkpoint is a Marker with the separator variant: a line across the thread with a label and a button in the middle. Put it after the turn that made the change. The latest checkpoint has nothing after it, so the demo hides its button (canRestore).

Restore keeps the turn that carries the checkpoint and drops every message after it:

function restore(messageId: string) {  const index = messages.findIndex((message) => message.id === messageId)
  if (index === -1) {    return  }
  setMessages(messages.slice(0, index + 1))}

The aria-label includes the checkpoint's label, so each Restore button is distinct for a screen reader.

Examples

Restore with confirmation

Restoring discards the later messages. When they also record approvals or changes, ask first with an Alert Dialog that says what will be removed and what stays.

Import the spring price list.

Imported 214 prices.

Price list imported
Raise steel sheet by 5%.

Raised 12 prices. Giulia approved them.

AI SDK

The AI SDK has no checkpoint type, so use a data part. Declare it on your message type, then send it from the route or script it. The part's id makes it updatable: sending the same id again replaces it.

type ChatMessage = UIMessage<  unknown,  { checkpoint: { label: string } }>
const stream = createUIMessageStream<ChatMessage>({  execute: ({ writer }) => {    writer.write({      type: "data-checkpoint",      id: "rename",      data: { label: "Button renamed" },    })  },})

On the client, find it in the message's parts:

const checkpoint = message.parts.find(  (part): part is CheckpointPart => part.type === "data-checkpoint")

setMessages only changes the client. The server keeps whatever it stored, so restore the stored thread too, or send the shortened messages with the next request and treat that as the thread. Note that restoring the conversation does not undo what the agent did outside it, such as a saved price or a sent email. Say so in the confirmation, or undo those changes yourself.

Offline, createChat scripts a checkpoint with writer.data({ type: "data-checkpoint", id, data }), which is what the demo does. The demo opens with both turns. Press Restore to go back to the first one, then Continue to play the second again.

Accessibility

KeyAction
TabMoves focus to the next Restore button.
EnterPresses Restore.
SpacePresses Restore.

The composed components provide:

  • A real button with a focus ring and the same press behaviour for mouse, touch and keyboard.
  • A separator line that is decorative and does not take focus.

You provide:

  • An aria-label for each Restore button that names its checkpoint, as the pattern does.
  • A message after restoring, in a live region, saying what the thread now ends with.
  • Focus: the restored turns can unmount the button that had focus, so move focus to the field or the last message.

Guidelines

When to use

  • After a turn that changes something the person might want to take back: a rename, an import, an approval.
  • In long threads, so the person can go back without starting over.

When not to use

  • For undoing one action in the product. Use an undo control next to that action.
  • For saving versions of a document. Use the product's history.

Content

  • Label a checkpoint with what changed: "Price list imported", not "Checkpoint 2".
  • Say "Restore", the verb the button performs.
  • In the confirmation, name what is removed and what is not.

Human in the loop

A checkpoint is the person's way to undo. Offer one after every turn that changes data, show it without being asked, and be honest about its reach: a restored thread is not a restored system. When a restore would drop approvals, confirm it.

Do

Say what restoring removes and what stays, before the person confirms.

Don’t

Don’t restore silently on one press when the later turns hold approved changes.

Composition

ComponentWhat it does here
MarkerMarker draws the line and MarkerContent holds the label and the button.
ButtonThe Restore button, and Continue in the demo.
Message and BubbleThe turns around the marker.

The pattern exports one component, CheckpointDemo, with the ChatMessage type that declares the data-checkpoint part.