AI Approval

Human-in-the-loop controls: an amber approve or deny card, a hold-to-confirm button for destructive actions, and a multiple-choice question.

Installation

Installation

One command adds ai-approval and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-approval.json
ai-approval-demo
components/ui/ai-approval-demo.tsx
"use client"

import * as React from "react"
import { Clock, FileDiff } from "lucide-react"

import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop } from "@/lib/ai-kit"
import {
  AiApproval,
  AiHoldConfirm,
  AiQuestion,
} from "@/registry/tween/ai-approval"

function Card() {
  const root = React.useRef<HTMLDivElement>(null)
  const [status, setStatus] = React.useState<"ask" | "approved" | "denied">(
    "ask"
  )
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setStatus("ask")
      await sleep(2800)
      setStatus("approved")
      await sleep(2400)
    },
    { gap: 400, enabled: !hands }
  )

  const answer = (next: "approved" | "denied") => {
    setHands(true)
    setStatus(next)
  }

  return (
    <div
      ref={root}
      style={{ display: "grid", gap: 10, width: "min(100%, 380px)" }}
    >
      <AiApproval
        status={status}
        agent={{ name: "Mira" }}
        title={
          <>
            Run <code>pnpm build:registry</code>?
          </>
        }
        facts={[
          {
            icon: <FileDiff size={16} />,
            text: (
              <>
                Rewrites 104 files in <code>public/r</code>
              </>
            ),
          },
          { icon: <Clock size={16} />, text: "Takes about 40 seconds" },
        ]}
        resolution={
          status === "approved"
            ? "Approved by you. Building now."
            : "Denied by you. Nothing ran."
        }
        onApprove={() => answer("approved")}
        onDeny={() => answer("denied")}
        onAlways={() => answer("approved")}
      />
      {hands && status !== "ask" ? (
        <button
          type="button"
          className="aik-btn sm ghost"
          style={{ justifySelf: "start" }}
          onClick={() => setStatus("ask")}
        >
          Ask again
        </button>
      ) : null}
    </div>
  )
}

function Hold() {
  const root = React.useRef<HTMLDivElement>(null)
  const [holding, setHolding] = React.useState(false)
  const [confirmed, setConfirmed] = React.useState(false)
  const [hands, setHands] = React.useState(false)
  const [round, setRound] = React.useState(0)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setConfirmed(false)
      setHolding(false)
      await sleep(1100)
      setHolding(true)
      await sleep(450)
      setHolding(false)
      await sleep(1500)
      setHolding(true)
      await sleep(1250)
      setHolding(false)
      setConfirmed(true)
      await sleep(2200)
    },
    { gap: 400, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", gap: 10, width: "min(100%, 380px)" }}
      onPointerDown={() => {
        if (hands) return
        setHands(true)
        setRound((value) => value + 1)
      }}
      onKeyDown={() => setHands(true)}
    >
      <AiHoldConfirm
        key={round}
        title="Delete 3 preview deployments"
        description="This cannot be undone. Production is not affected."
        label="Hold to delete"
        doneLabel="Deleted"
        doneHint="3 preview deployments deleted"
        holding={hands ? undefined : holding}
        confirmed={hands ? undefined : confirmed}
      />
      {hands ? (
        <button
          type="button"
          className="aik-btn sm ghost"
          style={{ justifySelf: "start" }}
          onClick={() => setRound((value) => value + 1)}
        >
          Reset
        </button>
      ) : null}
    </div>
  )
}

const OPTIONS = [
  { id: "out", label: "Out", description: "Fast start, soft landing" },
  {
    id: "move",
    label: "Move",
    description: "In and out, for a thumb changing place",
  },
  {
    id: "linear",
    label: "Linear",
    description: "Constant speed, for progress only",
  },
]

function Question() {
  const root = React.useRef<HTMLDivElement>(null)
  const [row, setRow] = React.useState(0)
  const [value, setValue] = React.useState<string | null>(null)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setValue(null)
      setRow(0)
      await sleep(1200)
      for (const next of [1, 2, 1]) {
        setRow(next)
        await sleep(620)
      }
      await sleep(300)
      setValue("move")
      await sleep(2400)
    },
    { gap: 400, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 380px)" }}
      onPointerDown={() => {
        if (hands) return
        setHands(true)
        setValue(null)
      }}
      onKeyDown={() => setHands(true)}
    >
      <AiQuestion
        agent={{ name: "Juno", tone: "amber", shape: "arch" }}
        question="Which easing should the pricing toggle use?"
        options={OPTIONS}
        value={value}
        highlight={hands ? undefined : row}
        onValueChange={setValue}
        onSkip={() => setValue(null)}
      />
      {hands && value ? (
        <button
          type="button"
          className="aik-btn sm ghost"
          style={{ justifySelf: "start", marginTop: 6 }}
          onClick={() => setValue(null)}
        >
          Ask again
        </button>
      ) : null}
    </div>
  )
}

export function AiApprovalDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Card", node: <Card /> },
        { name: "Hold to confirm", node: <Hold /> },
        { name: "Question", node: <Question /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-approval.json

Usage

import {
  AiApproval,
  AiHoldConfirm,
  AiQuestion,
} from "@/components/ui/ai-approval"
const [status, setStatus] = useState<"ask" | "approved" | "denied">("ask")
 
<AiApproval
  status={status}
  agent={{ name: "Mira" }}
  title={<>Run <code>pnpm build:registry</code>?</>}
  facts={[{ text: "Rewrites 104 files in public/r" }]}
  onApprove={() => setStatus("approved")}
  onDeny={() => setStatus("denied")}
/>
 
<AiHoldConfirm
  title="Delete 3 preview deployments"
  description="This cannot be undone."
  label="Hold to delete"
  onConfirm={deletePreviews}
/>
 
<AiQuestion
  question="Which easing should the pricing toggle use?"
  options={[{ id: "out", label: "Out" }, { id: "move", label: "Move" }]}
  value={choice}
  onValueChange={setChoice}
/>

With the AI SDK, show AiApproval for a tool part in the approval-requested state and answer it from onApprove and onDeny.

Versions

  • AiApproval is the one amber surface on screen. It says what will run and what it changes. With the card focused, Enter approves and Escape denies. Once answered it settles into a neutral card that keeps the decision on record.
  • AiHoldConfirm is for actions you cannot take back. The button fills while the pointer, Space or Enter is held and drains the moment you let go. onConfirm fires only when the fill completes.
  • AiQuestion is for a choice, not permission. Arrow keys move the highlight, number keys or Enter pick, and the other options fade back.

Props

AiApproval

PropTypeDefaultDescription
status"ask" | "approved" | "denied"ask is the amber card.
titleReactNodeWhat the agent wants to do.
facts{ icon?, text }[][]What it will change, one line each.
agent{ name, tone?, shape? }Who is asking.
onApprove() => voidApprove button and Enter.
onDeny() => voidDeny button and Escape.
onAlways() => voidShows "Always allow" when set.
resolutionstringThe line kept on the card once answered.

AiHoldConfirm

PropTypeDefaultDescription
titlestringThe action.
descriptionstringWhat it costs.
labelstring"Hold to confirm"Button text before the hold.
doneLabelstring"Confirmed"Button text once confirmed.
holdMsnumber1200How long the press must last.
onConfirm() => voidFires once, when the fill completes.
confirmedbooleanControls the confirmed state.
doneHintstring"Done."The line under the button once confirmed.

AiQuestion

PropTypeDefaultDescription
questionstringWhat the agent is asking.
options{ id, label, description? }[]Up to nine options.
valuestring | nullThe chosen id, or null while open.
onValueChange(id: string) => voidCalled with the chosen id.
onSkip() => voidShows a Skip button when set.
agent{ name, tone?, shape? }Who is asking.
highlightnumberControls the highlighted row.