Skip to content
WebJs
Menu

Confirmation

Asks for approval before a tool runs.

Clean up the build folder.
Thought
The build output lives in dist. Deleting it cannot be undone, so I should ask before I run the command.
Run rm -rf dist?

The command runs in this project.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { bubbleClass, bubbleContentClass } from '#components/ui/bubble.ts';
import { buttonClass } from '#components/ui/button.ts';
import {
  itemClass, itemActionsClass, itemContentClass, itemDescriptionClass,
  itemMediaClass, itemTitleClass,
} from '#components/ui/item.ts';
import { messageClass, messageContentClass } from '#components/ui/message.ts';
import {
  traceClass, traceLineClass, traceIconClass, traceTitleClass,
  traceCollapsibleClass, traceCollapsibleTriggerClass, traceCollapsibleContentClass,
} from '#components/ui/trace.ts';
import { ChevronRightIcon, CircleCheckIcon, TerminalIcon } from '#lib/ui/lucide.ts';

const reasoning =
  'The build output lives in dist. Deleting it cannot be undone, so I should ask before I run the command.';

// The turn waiting on the person: the thinking has settled, and the command
// asks for a yes or no. A pending approval is a prompt, not activity, so it
// sits outside the trace. Deny and Approve are the reader's to wire.
export default function ConfirmationDemo() {
  return html`
    <div class="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
      <div data-slot="message" data-align="end" class=${messageClass()}>
        <div data-slot="message-content" class=${messageContentClass()}>
          <div data-slot="bubble" data-variant="muted" data-align="end" class=${bubbleClass({ variant: 'muted' })}>
            <div data-slot="bubble-content" class=${bubbleContentClass()}>Clean up the build folder.</div>
          </div>
        </div>
      </div>
      <div data-slot="message" data-align="start" class=${messageClass()}>
        <div data-slot="message-content" class=${cn(messageContentClass(), 'gap-4')}>
          <div data-slot="trace" class=${traceClass()}>
            <details data-slot="trace-collapsible" class=${cn(traceCollapsibleClass(), 'group/trace-collapsible')}>
              <summary data-slot="trace-collapsible-trigger" class=${traceCollapsibleTriggerClass()}>
                <span data-slot="trace-line" class=${traceLineClass()}>
                  <span data-slot="trace-icon" aria-hidden="true" class=${traceIconClass()}>${CircleCheckIcon()}</span>
                  <span data-slot="trace-title" class=${traceTitleClass()}>Thought</span>
                </span>
                <span data-slot="trace-icon" aria-hidden="true" class=${cn(traceIconClass(), 'transition-transform group-open/trace-collapsible:rotate-90')}>${ChevronRightIcon()}</span>
              </summary>
              <div data-slot="trace-collapsible-content" class=${cn(traceCollapsibleContentClass(), 'whitespace-pre-wrap')}>${reasoning}</div>
            </details>
          </div>
          <div data-slot="item" data-variant="outline" data-size="sm" class=${itemClass({ variant: 'outline', size: 'sm' })}>
            <div data-slot="item-media" data-variant="icon" class=${itemMediaClass({ variant: 'icon' })}>
              ${TerminalIcon()}
            </div>
            <div data-slot="item-content" class=${itemContentClass()}>
              <div data-slot="item-title" class=${itemTitleClass()}>Run rm -rf dist?</div>
              <p data-slot="item-description" class=${itemDescriptionClass()}>The command runs in this project.</p>
            </div>
            <div data-slot="item-actions" class=${itemActionsClass()}>
              <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${buttonClass({ variant: 'outline', size: 'sm' })}>Deny</button>
              <button type="button" data-slot="button" data-variant="default" data-size="sm" class=${buttonClass({ size: 'sm' })}>Approve</button>
            </div>
          </div>
        </div>
      </div>
      <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${cn(buttonClass({ variant: 'outline', size: 'sm' }), 'mt-auto self-start')}>
        Reset
      </button>
    </div>
  `;
}
View Code

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 @webjsdev/ui add item button

Usage

<div data-slot="item" data-variant="outline" data-size="sm" class=${itemClass({ variant: 'outline', size: 'sm' })}>
  <div data-slot="item-media" data-variant="icon" class=${itemMediaClass({ variant: 'icon' })}>
    ${TerminalIcon()}
  </div>
  <div data-slot="item-content" class=${itemContentClass()}>
    <div data-slot="item-title" class=${itemTitleClass()}>Run ${part.input.command}?</div>
    <p data-slot="item-description" class=${itemDescriptionClass()}>The command runs in this project.</p>
  </div>
  <div data-slot="item-actions" class=${itemActionsClass()}>
    <button type="button" data-slot="button" data-variant="outline" data-size="sm" class=${buttonClass({ variant: 'outline', size: 'sm' })} @click=${() => this.deny()}>
      Deny
    </button>
    <button type="button" data-slot="button" data-variant="default" data-size="sm" class=${buttonClass({ size: 'sm' })} @click=${() => this.approve()}>
      Approve
    </button>
  </div>
</div>

AI SDK

While the part is approval-requested, answer with the approval id. Your component sends the answer back to the 'use server' action that streams the turn (send below is your component's own method that calls it), and the action streams the rest of the turn once every approval has an answer.

async approve(part: ToolPart) {
  await this.send({ approvalId: part.approval.id, approved: true });
}

Approve streams the tool output. Deny streams output-denied. Either way, the part becomes a Tool line. See Tool, and AI SDK for streaming the parts from a server action.

AI SDK Helper

To script an approval, yield the tool part in the approval-requested state from the action and stop. The next call carries the answer, and the action reads it to stream the rest of the turn.

'use server';

export async function* cleanUp(answer?: { approved: boolean }) {
  if (!answer) {
    yield {
      type: 'tool-runCommand',
      toolCallId: 'clean',
      state: 'approval-requested',
      input: { command: 'rm -rf dist' },
      approval: { id: 'clean' },
    };
    return;
  }
  yield {
    type: 'text',
    text: answer.approved ? 'Removed the dist folder.' : 'Okay, I left the dist folder in place.',
  };
}

Migrating from AI SDK Elements

If you used Confirmation from AI SDK Elements, its wrapper lives in components/ai-elements/confirmation.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part:

AI SDK ElementsWebJs UI
ConfirmationAn item with itemClass({ variant: 'outline', size: 'sm' }), in place of an Alert. Keep the approval and its state in your component.
ConfirmationTitleAn item content holding an item title.
ConfirmationActionsAn item actions container, itemActionsClass().
ConfirmationActionA button with buttonClass({ size: 'sm' }).
ConfirmationRequest, ConfirmationAccepted, ConfirmationRejectedA branch on the part's state in your render: the item while it is approval-requested, a trace line once it is answered.