- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Message Scroller
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
- Trace
- Typography
"use client"
import * as React from "react""use client"
import * as React from "react"
import { useChat } from "@ai-sdk/react"
import {
lastAssistantMessageIsCompleteWithApprovalResponses,
type UIDataTypes,
type UIMessage,
} from "ai"
import { CircleCheckIcon, LoaderCircleIcon, TerminalIcon } from "lucide-react"
import { createChat, getMessageText } from "@/lib/ai"
import { Bubble, BubbleContent } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import {
Item,
ItemActions,
ItemContent,
ItemDescription,
ItemMedia,
ItemTitle,
} from "@/components/ui/item"
import { Message, MessageContent } from "@/components/ui/message"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/components/ui/trace"
type ChatMessage = UIMessage<
unknown,
UIDataTypes,
{
runCommand: {
input: { command: string }
output: { stdout: string }
}
}
>
const chat = createChat<ChatMessage>()
.user("Clean up the build folder.")
.sleep(600)
.assistant(({ writer }) => {
writer.reasoning(
"The build output lives in dist. Deleting it cannot be undone, so I should ask before I run the command."
)
writer.sleep(400)
writer.tool("runCommand", {
input: { command: "rm -rf dist" },
needsApproval: true,
output: { stdout: "" },
})
})
.assistant(({ writer, toolCall }) => {
writer.sleep(600)
writer.text(
toolCall?.approved
? "Removed the dist folder."
: "Okay, I left the dist folder in place."
)
})
const initialMessages = chat.get(0)
const transport = chat.transport({ delayMs: 20 })
export function ConfirmationDemo() {
const {
messages,
sendMessage,
setMessages,
status,
addToolApprovalResponse,
} = useChat({
messages: initialMessages,
transport,
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
})
const nextMessage = chat.next(messages)
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>
)
}
return (
<Message key={message.id}>
<MessageContent className="gap-4">
{message.parts.map((part, index) => {
if (part.type === "text") {
return <p key={index}>{part.text}</p>
}
if (part.type === "reasoning") {
return (
<Trace key={index}>
<ReasoningRow
text={part.text}
running={part.state === "streaming"}
/>
</Trace>
)
}
if (part.type !== "tool-runCommand") {
return null
}
// A pending approval is a prompt, not activity, so it sits outside Trace.
if (part.state === "approval-requested") {
return (
<Item key={part.toolCallId} variant="outline" size="sm">
<ItemMedia variant="icon">
<TerminalIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Run {part.input.command}?</ItemTitle>
<ItemDescription>
The command runs in this project.
</ItemDescription>
</ItemContent>
<ItemActions>
<Button
size="sm"
variant="outline"
onClick={() =>
addToolApprovalResponse({
id: part.approval.id,
approved: false,
})
}
>
Deny
</Button>
<Button
size="sm"
onClick={() =>
addToolApprovalResponse({
id: part.approval.id,
approved: true,
})
}
>
Approve
</Button>
</ItemActions>
</Item>
)
}
const denied =
part.state === "output-denied" ||
(part.state === "approval-responded" &&
!part.approval.approved)
return (
<Trace key={part.toolCallId}>
<TraceLine
status={
!denied && part.state !== "output-available"
? "running"
: undefined
}
>
<TraceIcon>
<TerminalIcon />
</TraceIcon>
<TraceTitle>
{denied
? "Denied"
: part.state === "output-available"
? "Ran"
: "Running"}{" "}
{part.input?.command}
</TraceTitle>
</TraceLine>
</Trace>
)
})}
</MessageContent>
</Message>
)
})}
{!busy && (
<Button
variant="outline"
size="sm"
className="mt-auto self-start"
onClick={() =>
nextMessage
? sendMessage(nextMessage)
: setMessages(initialMessages)
}
>
{nextMessage ? "Send message" : "Reset"}
</Button>
)}
</div>
)
}
function ReasoningRow({ text, running }: { text: string; running: boolean }) {
const [open, setOpen] = React.useState<boolean>()
return (
<TraceCollapsible open={open ?? running} onOpenChange={setOpen}>
<TraceCollapsibleTrigger>
<TraceLine status={running ? "running" : undefined}>
<TraceIcon>
{running ? (
<LoaderCircleIcon className="animate-spin" />
) : (
<CircleCheckIcon />
)}
</TraceIcon>
<TraceTitle>{running ? "Thinking" : "Thought"}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent className="whitespace-pre-wrap">
{text}
</TraceCollapsibleContent>
</TraceCollapsible>
)
}
Confirmation asks the person for a yes or no before a tool runs. The question is an Item with Deny and Approve buttons. Once the person answers, the call becomes a Tool line.
Installation#
Confirmation is built with the Item and Button components.
pnpm dlx shadcn@latest add item button
Install the required shadcn/ui dependencies:
pnpm dlx shadcn@latest add item button
Copy and paste the following code into your project.
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Slot } from "radix-ui"
import { Separator } from "@/components/ui/separator"
function ItemGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
role="list"
data-slot="item-group"
className={cn(
"group/item-group flex w-full flex-col gap-4 has-data-[size=sm]:gap-2.5 has-data-[size=xs]:gap-2",
className
)}
{...props}
/>
)
}
function ItemSeparator({
className,
...props
}: React.ComponentProps<typeof Separator>) {
return (
<Separator
data-slot="item-separator"
orientation="horizontal"
className={cn("my-2", className)}
{...props}
/>
)
}
const itemVariants = cva(
"group/item flex w-full flex-wrap items-center rounded-2xl border text-sm transition-colors duration-100 outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 [a]:transition-colors [a]:hover:bg-muted",
{
variants: {
variant: {
default: "border-transparent",
outline: "border-border",
muted: "border-transparent bg-muted/50",
},
size: {
default: "gap-3.5 px-4 py-3.5",
sm: "gap-3.5 px-3.5 py-3",
xs: "gap-2 px-2.5 py-2 in-data-[slot=dropdown-menu-content]:p-0",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
function Item({
className,
variant = "default",
size = "default",
asChild = false,
...props
}: React.ComponentProps<"div"> &
VariantProps<typeof itemVariants> & { asChild?: boolean }) {
const Comp = asChild ? Slot.Root : "div"
return (
<Comp
data-slot="item"
data-variant={variant}
data-size={size}
className={cn(itemVariants({ variant, size, className }))}
{...props}
/>
)
}
const itemMediaVariants = cva(
"flex shrink-0 items-center justify-center gap-2 group-has-data-[slot=item-description]/item:translate-y-0.5 group-has-data-[slot=item-description]/item:self-start [&_svg]:pointer-events-none",
{
variants: {
variant: {
default: "bg-transparent",
icon: "[&_svg:not([class*='size-'])]:size-4",
image:
"size-10 overflow-hidden rounded-xl group-data-[size=sm]/item:size-8 group-data-[size=xs]/item:size-6 group-data-[size=xs]/item:rounded-lg [&_img]:size-full [&_img]:object-cover",
},
},
defaultVariants: {
variant: "default",
},
}
)
function ItemMedia({
className,
variant = "default",
...props
}: React.ComponentProps<"div"> & VariantProps<typeof itemMediaVariants>) {
return (
<div
data-slot="item-media"
data-variant={variant}
className={cn(itemMediaVariants({ variant, className }))}
{...props}
/>
)
}
function ItemContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="item-content"
className={cn(
"flex flex-1 flex-col gap-1 group-data-[size=xs]/item:gap-0.5 [&+[data-slot=item-content]]:flex-none",
className
)}
{...props}
/>
)
}
function ItemTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="item-title"
className={cn(
"line-clamp-1 flex w-fit items-center gap-2 text-sm leading-snug font-medium underline-offset-4",
className
)}
{...props}
/>
)
}
function ItemDescription({ className, ...props }: React.ComponentProps<"p">) {
return (
<p
data-slot="item-description"
className={cn(
"line-clamp-2 text-left text-sm font-normal text-muted-foreground [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
className
)}
{...props}
/>
)
}
function ItemActions({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="item-actions"
className={cn("flex items-center gap-2", className)}
{...props}
/>
)
}
function ItemHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="item-header"
className={cn(
"flex basis-full items-center justify-between gap-2",
className
)}
{...props}
/>
)
}
function ItemFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="item-footer"
className={cn(
"flex basis-full items-center justify-between gap-2",
className
)}
{...props}
/>
)
}
export {
Item,
ItemMedia,
ItemContent,
ItemActions,
ItemGroup,
ItemSeparator,
ItemTitle,
ItemDescription,
ItemHeader,
ItemFooter,
}
Update the import paths to match your project setup.
Usage#
<Item variant="outline" size="sm">
<ItemMedia variant="icon">
<TerminalIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Run {part.input.command}?</ItemTitle>
<ItemDescription>The command runs in this project.</ItemDescription>
</ItemContent>
<ItemActions>
<Button size="sm" variant="outline" onClick={deny}>
Deny
</Button>
<Button size="sm" onClick={approve}>
Approve
</Button>
</ItemActions>
</Item>AI SDK#
While the part is approval-requested, answer with the approval id. Pass sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses so the chat continues once every approval has an answer.
addToolApprovalResponse({
id: part.approval.id,
approved: true,
})useChat({
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
})Approve streams the tool output. Deny streams output-denied. Either way, the part becomes a Tool line.
See Tool.
AI SDK Helper#
To script an approval with createChat, set needsApproval on the tool. The output streams after approval, and the next agent turn reads toolCall.approved.
const chat = createChat()
.user("Clean up the build folder.")
.assistant(({ writer }) => {
writer.tool("runCommand", {
input: { command: "rm -rf dist" },
needsApproval: true,
output: { stdout: "" },
})
})
.assistant(({ writer, toolCall }) => {
writer.text(
toolCall?.approved
? "Removed the dist folder."
: "Okay, I left the dist folder in place."
)
})Migrating from AI SDK Elements#
If you added Confirmation from AI SDK Elements, your project has a components/ai-elements/confirmation.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Item and Button. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/confirmation.tsx from AI SDK Elements to the shadcn/ui Item and Button components.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Install the components: npx shadcn@latest add item button
2. Read https://ui.shadcn.com/docs/ai-elements/radix/confirmation.md and build Confirmation the same way.
3. Rebuild each part:
- Confirmation renders an Item with variant="outline" and size="sm", instead of an Alert. Keep the approval and state props and the context they feed.
- ConfirmationTitle renders an ItemContent with an ItemTitle.
- ConfirmationActions renders an ItemActions.
- ConfirmationAction renders a Button with size="sm".
- Keep ConfirmationRequest, ConfirmationAccepted and ConfirmationRejected as they are.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.