Queue
Lists the work an agent has left and the work it has finished, in two collapsible groups.
Installation
pnpm dlx shadcn@latest add @silkware/ai-queueTo copy the pattern by hand, install the components it composes:
pnpm dlx shadcn@latest add @silkware/bubble @silkware/button @silkware/message @silkware/traceThen the packages:
pnpm add ai @ai-sdk/react @shadcn/helpers lucide-reactCopy the source into components/ai-elements/queue.tsx. The file also needs @/lib/ai:
"use client"
import { useChat } from "@ai-sdk/react"import type { UIDataTypes, UIMessage } from "ai"import { CircleCheckIcon, CircleDotIcon, CircleIcon, ListChecksIcon, ListTodoIcon,} 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, TraceIcon, TraceLine, TraceTitle,} from "@/components/ui/trace"
type ChatMessage = UIMessage< unknown, UIDataTypes, { writeTodos: { input: { todos: { content: string status: "pending" | "in_progress" | "completed" }[] } output: string } }>
type Todo = Extract< ChatMessage["parts"][number], { type: "tool-writeTodos"; state: "input-available" }>["input"]["todos"][number]
const TODOS = [ "Add a dark variant to the theme tokens", "Read the theme from a cookie", "Add a toggle to the settings page", "Check contrast on the charts",]
function todosAt(current: number) { return TODOS.map( (content, index) => ({ content, status: index < current ? "completed" : index === current ? "in_progress" : "pending", }) satisfies Todo )}
const chat = createChat<ChatMessage>() .user("Add dark mode to the settings page.") .assistant(({ writer }) => { TODOS.forEach((_, index) => { writer .tool("writeTodos", { input: { todos: todosAt(index) } }) .output("Updated the todo list.") writer.sleep(1400) }) writer .tool("writeTodos", { input: { todos: todosAt(TODOS.length) } }) .output("Updated the todo list.") writer.text("Dark mode is in. The toggle is under Settings, Appearance.") })
const transcript = chat.get()const [question] = chat.get(1)const transport = chat.transport({ delayMs: 20 })
function TodoLine({ todo, streaming }: { todo: Todo; streaming: boolean }) { return ( <TraceLine status={ streaming && todo.status === "in_progress" ? "running" : undefined } > <TraceIcon> {todo.status === "completed" ? ( <CircleCheckIcon /> ) : todo.status === "in_progress" ? ( <CircleDotIcon /> ) : ( <CircleIcon /> )} </TraceIcon> <TraceTitle>{todo.content}</TraceTitle> </TraceLine> )}
function TodoQueue({ todos, streaming,}: { todos: Todo[] streaming: boolean}) { const pending = todos.filter((todo) => todo.status !== "completed") const completed = todos.filter((todo) => todo.status === "completed")
return ( <Trace> {pending.length > 0 && ( <TraceCollapsible defaultExpanded> <TraceCollapsibleTrigger> <TraceLine> <TraceIcon> <ListTodoIcon /> </TraceIcon> <TraceTitle>{pending.length} to do</TraceTitle> </TraceLine> </TraceCollapsibleTrigger> <TraceCollapsibleContent> <Trace> {pending.map((todo) => ( <TodoLine key={todo.content} todo={todo} streaming={streaming} /> ))} </Trace> </TraceCollapsibleContent> </TraceCollapsible> )} {completed.length > 0 && ( <TraceCollapsible defaultExpanded> <TraceCollapsibleTrigger> <TraceLine> <TraceIcon> <ListChecksIcon /> </TraceIcon> <TraceTitle>{completed.length} completed</TraceTitle> </TraceLine> </TraceCollapsibleTrigger> <TraceCollapsibleContent> <Trace> {completed.map((todo) => ( <TodoLine key={todo.content} todo={todo} streaming={streaming} /> ))} </Trace> </TraceCollapsibleContent> </TraceCollapsible> )} </Trace> )}
export function QueueDemo() { 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 todos = message.parts .filter((part) => part.type === "tool-writeTodos") .filter((part) => part.state !== "input-streaming") .at(-1)?.input?.todos const text = getMessageText(message)
return ( <Message key={message.id}> <MessageContent className="gap-4"> {todos && <TodoQueue todos={todos} streaming={streaming} />} {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 queue is two groups of lines: what is left, and what is done. Each group is a collapsible with a count in its title.
import { ListChecksIcon, ListTodoIcon } from "lucide-react"
import { Trace, TraceCollapsible, TraceCollapsibleContent, TraceCollapsibleTrigger, TraceIcon, TraceLine, TraceTitle,} from "@/components/ui/trace"<Trace> <TraceCollapsible defaultExpanded> <TraceCollapsibleTrigger> <TraceLine> <TraceIcon> <ListTodoIcon /> </TraceIcon> <TraceTitle>{pending.length} to do</TraceTitle> </TraceLine> </TraceCollapsibleTrigger> <TraceCollapsibleContent> <Trace> {pending.map((todo) => ( <TodoLine key={todo.content} todo={todo} /> ))} </Trace> </TraceCollapsibleContent> </TraceCollapsible></Trace>Render the completed group the same way with ListChecksIcon and the title "N completed". Only render a group that has items; the demo skips an empty group.
Examples
With descriptions
A line that has more to say becomes a collapsible of its own. The title stays short, and the detail opens on request.
Open work
Work still to do for Brevia Manufacturing, with a finished item kept in a collapsed group below it.
AI SDK
The demo does not use a custom part. The agent calls a writeTodos tool each time its list changes, and the tool's input is the whole list. Render the input of the last call that has one:
import type { UIDataTypes, UIMessage } from "ai"
type ChatMessage = UIMessage< unknown, UIDataTypes, { writeTodos: { input: { todos: { content: string; status: "pending" | "in_progress" | "completed" }[] } output: string } }>const todos = message.parts .filter((part) => part.type === "tool-writeTodos") .filter((part) => part.state !== "input-streaming") .at(-1)?.input?.todos
{todos && <TodoQueue todos={todos} streaming={streaming} />}Skipping input-streaming keeps the list from flickering while the model is still writing the arguments, because the input is partial until the call is complete.
On the server, define the tool so the model sends the full list each time, not a diff:
import { tool } from "ai"import { z } from "zod"
const writeTodos = tool({ description: "Replace the todo list with the current state of the work.", inputSchema: z.object({ todos: z.array( z.object({ content: z.string(), status: z.enum(["pending", "in_progress", "completed"]), }) ), }), execute: async () => "Updated the todo list.",})Scripted streams in development
createChat calls the tool and resolves it right away, once per state of the list:
const chat = createChat<ChatMessage>() .user("Add dark mode to the settings page.") .assistant(({ writer }) => { writer .tool("writeTodos", { input: { todos: todosAt(0) } }) .output("Updated the todo list.") writer.sleep(1400) })Accessibility
| Key | Action |
|---|---|
| Enter | Opens or closes the focused group or line. |
| Space | Opens or closes the focused group or line. |
| Tab | Moves focus to the next group or line that can open. |
The composed components provide:
- Real buttons with
aria-expandedfor the groups and for lines with a description. - A visible focus ring on every trigger.
You provide:
- The status of an item in text. A done item sits in the "completed" group, which is its own signal; an in-progress item should say "in progress" or sit first in the list. The check and circle icons are decoration.
- A count in each group title ("3 to do", "2 completed") so a collapsed group still tells the person how much is left.
- A way to learn that the list changed. The list updates in place and is not announced. Mention the finished item in the message text when it matters.
Guidelines
When to use
- For a running list of work that changes while the agent works, where the person wants to see what is left.
- When items finish out of order or get added as the agent learns more.
When not to use
- For a fixed sequence the person approves before it starts. Use Plan.
- For the detailed steps of one piece of work. Use Task.
- For a list people manage themselves, with editing, assigning and due dates. Build it from Item or a Table; a queue here is read-only.
Content
- Write each item as a verb and an object: "Confirm order BM-2041 with Luca".
- Keep the title short. Put the reason or the detail in the description.
- Group by state only: "to do" and "completed". Do not add a third group for the item in progress; mark it in its title.
- Do not remove a completed item. The person may want to see what was done.
Name each item by its action and keep the group counts in the titles.
Don’t add items such as "Do the rest" or "Finalise". The person cannot tell when they are done.
Human in the loop
- The queue shows what the agent will do next, so the person can step in before it gets there. Put the items that need a decision from a person first, and say so: "Ask Giulia to approve the price list".
- Do not let the agent reorder or drop items silently. If the list changes in a way that matters, say it in the message.
- A queue is read-only here. If the person should be able to remove or reorder an item, add controls with an accessible name each, and have the agent respect the change on its next turn.
Composition
| Component | What it does here |
|---|---|
| Trace | Lays out the groups and lines, with the collapsible, chevron and description styling. |
| Message | Places the conversation in turns. |
| Bubble | Wraps the person's own message. |
| Button | The replay control of the demo. |
components/ai-elements/queue.tsx exports QueueDemo, a scripted chat that calls writeTodos five times and renders the last list with TodoQueue and TodoLine. Copy those two functions into your app and replace the scripted chat with useChat against your route.