# Input Request

> Pause an agent mid-run to ask for structured input, say why it needs it and what it does next, and send nothing until the person chooses to.

Source: https://www.honestui.com/docs/ai/input-request

```tsx
"use client"

import * as React from "react"

import { Button } from "@/components/honest-ui/ui/button"
import {
  InputRequest,
  type InputRequestField,
  type InputRequestValues,
} from "@/components/ai/input-request"

const fields: InputRequestField[] = [
  {
    id: "environment",
    type: "select",
    label: "Target environment",
    required: true,
    options: [
      { value: "staging", label: "Staging" },
      { value: "production", label: "Production" },
    ],
  },
  {
    id: "window",
    type: "date",
    label: "Maintenance date",
    help: "The migration locks the orders table for about 10 minutes.",
    required: true,
    min: "2026-11-27",
  },
  {
    id: "batch",
    type: "number",
    label: "Batch size",
    unit: "rows",
    defaultValue: 5000,
    min: 500,
    max: 50000,
    step: 500,
  },
  {
    id: "notify",
    type: "multiselect",
    label: "Who should hear about it",
    options: [
      { value: "oncall", label: "On-call engineer" },
      { value: "support", label: "Support team" },
      { value: "finance", label: "Finance" },
    ],
    defaultValue: ["oncall"],
  },
  { id: "dryRun", type: "boolean", label: "Run a dry run first", help: "Reports row counts without writing.", defaultValue: true },
  { id: "notes", type: "textarea", label: "Anything else I should know", placeholder: "Optional", maxLength: 400 },
]

/** This preview sends nothing. Sending waits a moment, then shows what your `onSubmit` receives. */
export function InputRequestDemo() {
  const [received, setReceived] = React.useState<InputRequestValues | null>(null)
  const [resetKey, setResetKey] = React.useState(0)

  return (
    <div className="flex w-full max-w-lg min-w-0 flex-col items-start gap-[var(--hui-space-4)]">
      <InputRequest
        key={resetKey}
        agent="Migration agent"
        title="Where and when should I run the orders migration?"
        reason="The plan is ready, but I can't pick the environment or a maintenance date on my own."
        nextStep="Back up the orders table, then migrate in batches and report the row counts."
        deadline="The run waits until 5:00 PM"
        fields={fields}
        onSubmit={(values) =>
          new Promise<void>((resolve) =>
            window.setTimeout(() => {
              setReceived(values)
              resolve()
            }, 700)
          )
        }
        onSkip={() => undefined}
        onCancelRun={() => undefined}
      />
      {received && (
        <pre
          aria-label="Values received by onSubmit"
          tabIndex={0}
          className="m-0 w-full overflow-x-auto rounded-[var(--hui-radius-2)] bg-[var(--hui-color-background-base-secondary)] p-[var(--hui-space-4)] font-mono text-[var(--hui-color-foreground-base-primary)] outline-none [font-size:var(--hui-font-size-mono-mini)] [line-height:var(--hui-line-height-regular)] focus-visible:[outline:var(--hui-focus-ring)]"
        >
          {JSON.stringify(received, null, 2)}
        </pre>
      )}
      <Button
        variant="ghost"
        size="sm"
        onClick={() => {
          setReceived(null)
          setResetKey((key) => key + 1)
        }}
      >
        Replay
      </Button>
    </div>
  )
}

```

## Overview [#overview]

Input Request is the form an agent shows when it stops mid-run and needs values it cannot decide on its own. It says what the agent is asking for, why, and what it will do next, then collects typed fields: text, longer text, numbers, a choice, several choices, a date, and yes or no.

Take a migration agent with a finished plan. It cannot choose between staging and production, or pick a maintenance date. It pauses and shows a request. The person fills it in and presses Send, and your `onSubmit` receives the values and resumes the run.

It extends [Approval Card](/docs/ai/approval-card) and shares its look. Use Approval Card for one or more multiple-choice questions asked in turn. Use Input Request when the agent needs typed values in one form.

Input Request does not talk to your agent. It collects the values and passes them to `onSubmit`.

## Anatomy [#anatomy]

<Anatomy title="InputRequest">
  <AnatomyItem name="Header" description="Who is asking, the request, and why the agent needs it" />

  <AnatomyItem name="Fields" description="Each field with its label, Required or Optional, help, and error" />

  <AnatomyItem name="Next step" description="What the agent does with the answers" />

  <AnatomyItem name="Footer" description="The deadline, then Cancel run, Skip, and Send" />

  <AnatomyItem name="Summary" description="What was sent, once the request is answered" />
</Anatomy>

Installing `input-request` brings [Approval Card](/docs/ai/approval-card), [Button](/docs/components/button), [Checkbox](/docs/components/checkbox), [Input](/docs/components/input), [Select](/docs/components/select), [Switch](/docs/components/switch), and [Textarea](/docs/components/textarea) along as registry dependencies.

## Behavior [#behavior]

### Fields [#fields]

Each field comes from a plain object with an `id`, a `type`, and a `label`. `required` marks it as Required; others read Optional. `help` adds a line under the label, and `defaultValue` fills it in. Text fields take `minLength`, `maxLength`, and a `pattern` with its own message. Numbers take `min`, `max`, `step`, and a `unit`. Dates take `min` and `max` as `YYYY-MM-DD`.

### Sending [#sending]

Send checks every field with `validateInputRequest`. If any fail, each shows its message, a line above the fields counts them, and focus moves to the first one. Fixing a field clears its message.

`onSubmit` receives a value for every field, keyed by `id`. Text is trimmed, and empty text, numbers, dates, and choices are `null`. Several choices are an array, and yes or no is a boolean.

If `onSubmit` returns a promise, the form shows "Sending…" and locks its controls until it settles. When it resolves, the form becomes a summary of what was sent. When it rejects, the form keeps every value and offers Send again.

### Skip and cancel [#skip-and-cancel]

Pass `onSkip` to let the run continue without the input, and `onCancelRun` to stop the run. Each button appears only when you handle it, and each replaces the form with a line saying what happened.

### Finished requests [#finished-requests]

Pass `status` to show a request that has already finished, such as when a conversation is reloaded: `"submitted"` with `submittedValues` shows the summary, and `"skipped"`, `"cancelled"`, and `"expired"` show their line. `deadline` adds a line to the footer, such as when the run stops waiting.

## Accessibility [#accessibility]

The form is a group named by the request and described by the reason. Every field has a visible label tied to its control. Help and error lines are attached with `aria-describedby`, and an invalid field is marked with `aria-invalid`. Several choices are a group of checkboxes named by the field's label, and yes or no is a switch.

The count of fields that need attention is an alert. The line that replaces a finished form is a status, and focus moves to the card so it is not lost.

The pulsing dot beside "needs your input" stops with reduced motion.

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;@honestai/input-request&#x22;]" />
  

  
    
      
        Add the items it is built from.
      

      <CliBlock commands="[&#x22;button&#x22;, &#x22;checkbox&#x22;, &#x22;input&#x22;, &#x22;select&#x22;, &#x22;switch&#x22;, &#x22;textarea&#x22;, &#x22;@honestai/approval-card&#x22;]" />

      
        Copy and paste this file into your project.
      

      ### components/ai/input-request.tsx

```tsx
"use client"

import * as React from "react"
import {
  ArrowRight as ArrowRightIcon,
  CircleAlert as CircleAlertIcon,
  CircleCheck as CircleCheckIcon,
  Clock as ClockIcon,
  LoaderCircle as LoaderCircleIcon,
} from "honestui/icons"

import { cn } from "@/lib/utils"
import {
  ApprovalOptionRow,
  approvalBodyText,
  approvalCardFrame,
  approvalMetaText,
} from "@/components/ai/approval-card"
import { Button } from "@/components/honest-ui/ui/button"
import { Checkbox } from "@/components/honest-ui/ui/checkbox"
import { Input } from "@/components/honest-ui/ui/input"
import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "@/components/honest-ui/ui/select"
import { Switch } from "@/components/honest-ui/ui/switch"
import { Textarea } from "@/components/honest-ui/ui/textarea"

export type InputRequestOption = {
  value: string
  label: string
}

type FieldBase = {
  id: string
  label: string
  /** A line under the label that says what to enter. */
  help?: string
  /** A required field must have a value before the person can send. */
  required?: boolean
}

export type InputRequestField =
  | (FieldBase & {
      type: "text" | "textarea"
      placeholder?: string
      defaultValue?: string
      minLength?: number
      maxLength?: number
      /** A regular expression the whole value must match. */
      pattern?: string
      /** What to say when `pattern` does not match. */
      patternMessage?: string
    })
  | (FieldBase & {
      type: "number"
      placeholder?: string
      defaultValue?: number
      min?: number
      max?: number
      step?: number
      /** A unit shown after the field, such as "GB" or "days". */
      unit?: string
    })
  | (FieldBase & { type: "select"; options: InputRequestOption[]; placeholder?: string; defaultValue?: string })
  | (FieldBase & { type: "multiselect"; options: InputRequestOption[]; defaultValue?: string[] })
  | (FieldBase & { type: "date"; min?: string; max?: string; defaultValue?: string })
  | (FieldBase & { type: "boolean"; defaultValue?: boolean })

export type InputRequestValue = string | number | boolean | string[] | null

/** Every field's value, keyed by field `id`. Empty text and numbers are `null`. */
export type InputRequestValues = Record<string, InputRequestValue>

export type InputRequestStatus = "pending" | "submitted" | "skipped" | "cancelled" | "expired"

export type InputRequestLabels = {
  /** The line above the question, such as "Research agent needs your input". */
  eyebrow: (agent?: string) => string
  /** The same line once the request is finished, such as "Asked by Research agent". */
  eyebrowDone: (agent?: string) => string
  /** Formats numbers in the summary. */
  number: (value: number) => string
  next: string
  send: string
  sending: string
  skip: string
  cancelRun: string
  required: string
  optional: string
  yes: string
  no: string
  selectPlaceholder: string
  /** Shown in the summary for a field left empty. */
  empty: string
  submitted: string
  skipped: string
  cancelled: string
  expired: string
  error: string
  /** Shown above the fields when Send finds problems. */
  fixErrors: (count: number) => string
  errors: {
    required: string
    minLength: (min: number) => string
    maxLength: (max: number) => string
    pattern: string
    number: string
    min: (min: number) => string
    max: (max: number) => string
    date: string
  }
}

export const defaultInputRequestLabels: InputRequestLabels = {
  eyebrow: (agent) => (agent ? `${agent} needs your input` : "The agent needs your input"),
  eyebrowDone: (agent) => (agent ? `Asked by ${agent}` : "Asked by the agent"),
  number: (value) => new Intl.NumberFormat("en-US").format(value),
  next: "Next",
  send: "Send",
  sending: "Sending…",
  skip: "Skip",
  cancelRun: "Cancel run",
  required: "Required",
  optional: "Optional",
  yes: "Yes",
  no: "No",
  selectPlaceholder: "Choose…",
  empty: "Not given",
  submitted: "Sent. The run continues with these answers.",
  skipped: "Skipped. The run continues without this input.",
  cancelled: "Run cancelled.",
  expired: "This request expired before it was answered. The run did not continue.",
  error: "Your answers were not sent. They are still here, so you can send again.",
  fixErrors: (count) => (count === 1 ? "1 field needs attention." : `${count} fields need attention.`),
  errors: {
    required: "This field is required.",
    minLength: (min) => `Enter at least ${min} characters.`,
    maxLength: (max) => `Enter at most ${max} characters.`,
    pattern: "Enter a value in the expected format.",
    number: "Enter a number.",
    min: (min) => `Enter ${min} or more.`,
    max: (max) => `Enter ${max} or less.`,
    date: "Enter a valid date.",
  },
}

/** The values a form starts with: each field's `defaultValue`, or empty. */
export function getInitialInputValues(fields: InputRequestField[]): InputRequestValues {
  const values: InputRequestValues = {}
  for (const field of fields) {
    if (field.type === "multiselect") values[field.id] = field.defaultValue ?? []
    else if (field.type === "boolean") values[field.id] = field.defaultValue ?? false
    else values[field.id] = field.defaultValue ?? null
  }
  return values
}

function isEmpty(value: InputRequestValue | undefined) {
  return (
    value === null ||
    value === undefined ||
    (typeof value === "string" && value.trim() === "") ||
    (Array.isArray(value) && value.length === 0)
  )
}

/**
 * Checks every field and returns a message for each one that fails, keyed by
 * field `id`. An empty object means the values can be sent.
 */
export function validateInputRequest(
  fields: InputRequestField[],
  values: InputRequestValues,
  messages: InputRequestLabels["errors"] = defaultInputRequestLabels.errors
): Record<string, string> {
  const errors: Record<string, string> = {}
  for (const field of fields) {
    const value = values[field.id]
    if (field.type === "boolean") continue
    if (isEmpty(value)) {
      if (field.required) errors[field.id] = messages.required
      continue
    }
    if (field.type === "text" || field.type === "textarea") {
      const text = String(value).trim()
      if (field.minLength !== undefined && text.length < field.minLength) errors[field.id] = messages.minLength(field.minLength)
      else if (field.maxLength !== undefined && text.length > field.maxLength) errors[field.id] = messages.maxLength(field.maxLength)
      else if (field.pattern && !new RegExp(`^(?:${field.pattern})$`).test(text))
        errors[field.id] = field.patternMessage ?? messages.pattern
    } else if (field.type === "number") {
      const number = typeof value === "number" ? value : Number(value)
      if (!Number.isFinite(number)) errors[field.id] = messages.number
      else if (field.min !== undefined && number < field.min) errors[field.id] = messages.min(field.min)
      else if (field.max !== undefined && number > field.max) errors[field.id] = messages.max(field.max)
    } else if (field.type === "date") {
      const text = String(value)
      if (!/^\d{4}-\d{2}-\d{2}$/.test(text) || Number.isNaN(Date.parse(text))) errors[field.id] = messages.date
      else if (field.min && text < field.min) errors[field.id] = messages.date
      else if (field.max && text > field.max) errors[field.id] = messages.date
    }
  }
  return errors
}

/** Values ready to send: text trimmed, empty text and numbers as `null`. */
function normalizeValues(fields: InputRequestField[], values: InputRequestValues): InputRequestValues {
  const result: InputRequestValues = {}
  for (const field of fields) {
    const value = values[field.id]
    if (field.type === "text" || field.type === "textarea" || field.type === "date" || field.type === "select") {
      const text = typeof value === "string" ? value.trim() : ""
      result[field.id] = text === "" ? null : text
    } else if (field.type === "number") {
      result[field.id] = isEmpty(value) ? null : Number(value)
    } else if (field.type === "multiselect") {
      result[field.id] = Array.isArray(value) ? value : []
    } else {
      result[field.id] = Boolean(value)
    }
  }
  return result
}

function displayValue(field: InputRequestField, value: InputRequestValue | undefined, labels: InputRequestLabels) {
  if (field.type === "boolean") return value ? labels.yes : labels.no
  if (isEmpty(value)) return null
  if (field.type === "select") return field.options.find((option) => option.value === value)?.label ?? String(value)
  if (field.type === "multiselect" && Array.isArray(value))
    return value.map((item) => field.options.find((option) => option.value === item)?.label ?? item).join(", ")
  if (field.type === "number") {
    const text = typeof value === "number" ? labels.number(value) : String(value)
    return field.unit ? `${text} ${field.unit}` : text
  }
  return String(value)
}

export type InputRequestProps = Omit<React.ComponentProps<"div">, "children" | "onSubmit" | "title"> & {
  /** What the agent asks for, in its words. */
  title: string
  /** Why the agent needs it. */
  reason?: React.ReactNode
  /** What the agent does once it has the answers. */
  nextStep?: React.ReactNode
  /** The agent's name, used in the line above the question. */
  agent?: string
  fields: InputRequestField[]
  /**
   * Receives the values when the person presses Send. Return a promise to show
   * progress; if it rejects, the form keeps the values and offers Send again.
   */
  onSubmit: (values: InputRequestValues) => void | Promise<void>
  /** Lets the run continue without the input. Without it, no Skip is shown. */
  onSkip?: () => void
  /** Stops the run. Without it, no Cancel run is shown. */
  onCancelRun?: () => void
  /**
   * Shows a finished request. Use "submitted" with `submittedValues` to show
   * what was sent, such as when a conversation is reloaded.
   */
  status?: InputRequestStatus
  submittedValues?: InputRequestValues
  /** A line about the deadline, such as "The run waits until 3:40 PM." */
  deadline?: React.ReactNode
  labels?: Partial<InputRequestLabels>
}

/**
 * A form an agent shows when it pauses mid-run for structured input. It says
 * why the agent asks and what it will do next, checks the fields, and sends
 * nothing until the person presses Send.
 */
function InputRequest({
  title,
  reason,
  nextStep,
  agent,
  fields,
  onSubmit,
  onSkip,
  onCancelRun,
  status: statusProp,
  submittedValues,
  deadline,
  labels: labelsProp,
  className,
  ...props
}: InputRequestProps) {
  const labels = { ...defaultInputRequestLabels, ...labelsProp }
  const [values, setValues] = React.useState<InputRequestValues>(() => getInitialInputValues(fields))
  const [errors, setErrors] = React.useState<Record<string, string>>({})
  const [phase, setPhase] = React.useState<"editing" | "submitting" | "error" | InputRequestStatus>("editing")
  const [sent, setSent] = React.useState<InputRequestValues | null>(null)
  const rootRef = React.useRef<HTMLDivElement>(null)
  const titleId = React.useId()
  const reasonId = React.useId()
  const baseId = React.useId()

  const status: InputRequestStatus | "editing" | "submitting" | "error" =
    statusProp && statusProp !== "pending" ? statusProp : phase
  const isSubmitting = status === "submitting"

  React.useEffect(() => {
    if (phase === "submitted" || phase === "skipped" || phase === "cancelled") rootRef.current?.focus()
  }, [phase])

  const fieldId = (id: string) => `${baseId}-${id}`

  const setValue = (id: string, value: InputRequestValue) => {
    setValues((current) => ({ ...current, [id]: value }))
    if (errors[id])
      setErrors((current) => {
        const next = { ...current }
        delete next[id]
        return next
      })
    if (phase === "error") setPhase("editing")
  }

  const submit = async () => {
    const found = validateInputRequest(fields, values, labels.errors)
    setErrors(found)
    const firstInvalid = fields.find((field) => found[field.id])
    if (firstInvalid) {
      const target = rootRef.current?.querySelector<HTMLElement>(`[data-field-id="${CSS.escape(firstInvalid.id)}"]`)
      target?.focus()
      return
    }
    const normalized = normalizeValues(fields, values)
    setPhase("submitting")
    try {
      await onSubmit(normalized)
      setSent(normalized)
      setPhase("submitted")
    } catch {
      setPhase("error")
    }
  }

  const isOpen = status === "editing" || status === "submitting" || status === "error"
  const header = (
    <div className="flex flex-col gap-[var(--hui-space-2)]">
      <p className={cn("m-0 flex items-center gap-[var(--hui-space-2)] text-[var(--hui-color-foreground-base-secondary)]", approvalMetaText)}>
        <span
          aria-hidden="true"
          className={cn(
            "size-1.5 shrink-0 rounded-full",
            isOpen
              ? "bg-[var(--hui-color-foreground-attention-primary)] motion-safe:animate-pulse"
              : "bg-[color-mix(in_oklch,var(--hui-color-foreground-base-primary)_35%,transparent)]"
          )}
        />
        {isOpen ? labels.eyebrow(agent) : labels.eyebrowDone(agent)}
      </p>
      <p
        id={titleId}
        className="m-0 break-words text-[var(--hui-color-foreground-base-primary)] [font-size:15px] [font-weight:var(--hui-font-weight-medium)] [letter-spacing:-0.01em] [line-height:22px]"
      >
        {title}
      </p>
      {reason && (
        <div id={reasonId} className="m-0 break-words text-[var(--hui-color-foreground-base-secondary)]">
          {reason}
        </div>
      )}
    </div>
  )

  // A finished request: what was sent, or what happened instead.
  if (status === "submitted" || status === "skipped" || status === "cancelled" || status === "expired") {
    const shown = status === "submitted" ? (sent ?? submittedValues) : null
    const Icon = status === "submitted" ? CircleCheckIcon : status === "expired" ? ClockIcon : CircleAlertIcon
    const message =
      status === "submitted"
        ? labels.submitted
        : status === "skipped"
          ? labels.skipped
          : status === "cancelled"
            ? labels.cancelled
            : labels.expired
    return (
      <div
        ref={rootRef}
        tabIndex={-1}
        role="group"
        aria-labelledby={titleId}
        data-slot="input-request"
        data-status={status}
        className={cn(approvalCardFrame, approvalBodyText, className)}
        {...props}
      >
        <div className="flex flex-col gap-[var(--hui-space-4)] p-[var(--hui-space-5)]">
          {header}
          {shown && (
            <dl
              data-slot="input-request-summary"
              className="m-0 grid grid-cols-[minmax(0,2fr)_minmax(0,3fr)] gap-x-[var(--hui-space-4)] gap-y-[var(--hui-space-2)] rounded-[var(--hui-radius-3)] bg-[var(--hui-color-background-base-secondary)] p-[var(--hui-space-4)]"
            >
              {fields.map((field) => {
                const text = displayValue(field, shown[field.id], labels)
                return (
                  <React.Fragment key={field.id}>
                    <dt className="min-w-0 break-words text-[var(--hui-color-foreground-base-secondary)]">{field.label}</dt>
                    <dd
                      className={cn(
                        "m-0 min-w-0 whitespace-pre-wrap break-words tabular-nums",
                        text === null && "text-[var(--hui-color-foreground-base-secondary)]"
                      )}
                    >
                      {text ?? labels.empty}
                    </dd>
                  </React.Fragment>
                )
              })}
            </dl>
          )}
        </div>
        <p
          role="status"
          className={cn(
            "m-0 flex items-center gap-[var(--hui-space-3)] border-t-[0.5px] border-[var(--hui-color-border-base-primary)] px-[var(--hui-space-5)] py-[var(--hui-space-4)] text-[var(--hui-color-foreground-base-secondary)]",
            approvalMetaText
          )}
        >
          <Icon
            strokeWidth={1.5}
            strokeLinecap="round"
            aria-hidden="true"
            className={cn("size-4 shrink-0", status === "submitted" && "text-[var(--hui-color-foreground-success-primary)]")}
          />
          {message}
        </p>
      </div>
    )
  }

  const errorCount = Object.keys(errors).length

  return (
    <div
      ref={rootRef}
      tabIndex={-1}
      role="group"
      aria-labelledby={titleId}
      aria-describedby={reason ? reasonId : undefined}
      data-slot="input-request"
      data-status={status}
      className={cn(approvalCardFrame, approvalBodyText, className)}
      {...props}
    >
      <form
        noValidate
        onSubmit={(event) => {
          event.preventDefault()
          if (!isSubmitting) void submit()
        }}
      >
        <div className="flex flex-col gap-[var(--hui-space-5)] p-[var(--hui-space-5)]">
          {header}

          {errorCount > 0 && (
            <p role="alert" className={cn("m-0 flex items-center gap-[var(--hui-space-2)] text-[var(--hui-color-foreground-danger-primary)]", approvalMetaText)}>
              <CircleAlertIcon strokeWidth={1.5} strokeLinecap="round" aria-hidden="true" className="size-3.5 shrink-0" />
              {labels.fixErrors(errorCount)}
            </p>
          )}

          <div data-slot="input-request-fields" className="flex flex-col gap-[var(--hui-space-5)]">
            {fields.map((field) => (
              <FieldRow
                key={field.id}
                field={field}
                id={fieldId(field.id)}
                value={values[field.id]}
                error={errors[field.id]}
                disabled={isSubmitting}
                labels={labels}
                onChange={(value) => setValue(field.id, value)}
              />
            ))}
          </div>

          {nextStep && (
            <div
              data-slot="input-request-next"
              className={cn(
                "flex items-start gap-[var(--hui-space-3)] rounded-[var(--hui-radius-3)] bg-[var(--hui-color-background-base-secondary)] px-[var(--hui-space-4)] py-[var(--hui-space-3)]",
                approvalMetaText
              )}
            >
              <ArrowRightIcon
                strokeWidth={1.5}
                aria-hidden="true"
                className="mt-[2px] size-3.5 shrink-0 text-[var(--hui-color-foreground-base-secondary)]"
              />
              <p className="m-0 min-w-0 break-words">
                <span className="[font-weight:var(--hui-font-weight-medium)]">{labels.next}: </span>
                <span className="text-[var(--hui-color-foreground-base-secondary)]">{nextStep}</span>
              </p>
            </div>
          )}

          {status === "error" && (
            <p role="alert" className="m-0 flex items-start gap-[var(--hui-space-3)]">
              <CircleAlertIcon
                strokeWidth={1.5}
                strokeLinecap="round"
                aria-hidden="true"
                className="mt-[var(--hui-space-1)] size-4 shrink-0 text-[var(--hui-color-foreground-danger-primary)]"
              />
              <span className="min-w-0 break-words">{labels.error}</span>
            </p>
          )}
        </div>

        <div className="flex flex-wrap items-center justify-end gap-[var(--hui-space-3)] border-t-[0.5px] border-[var(--hui-color-border-base-primary)] px-[var(--hui-space-5)] py-[var(--hui-space-4)]">
          {deadline && (
            <p
              className={cn(
                "m-0 me-auto flex min-w-0 items-center gap-[var(--hui-space-2)] text-[var(--hui-color-foreground-base-secondary)]",
                approvalMetaText
              )}
            >
              <ClockIcon strokeWidth={1.5} aria-hidden="true" className="size-3.5 shrink-0" />
              <span className="min-w-0 tabular-nums">{deadline}</span>
            </p>
          )}
          {onCancelRun && (
            <Button
              type="button"
              variant="ghost"
              size="sm"
              disabled={isSubmitting}
              onClick={() => {
                setPhase("cancelled")
                onCancelRun()
              }}
            >
              {labels.cancelRun}
            </Button>
          )}
          {onSkip && (
            <Button
              type="button"
              variant="secondary"
              size="sm"
              disabled={isSubmitting}
              onClick={() => {
                setPhase("skipped")
                onSkip()
              }}
            >
              {labels.skip}
            </Button>
          )}
          {/* aria-disabled while sending, so the pressed button keeps focus. */}
          <Button
            type="submit"
            size="sm"
            aria-disabled={isSubmitting || undefined}
            className="aria-disabled:cursor-progress"
          >
            {isSubmitting && <LoaderCircleIcon aria-hidden="true" className="size-3.5 motion-safe:animate-spin" />}
            {isSubmitting ? labels.sending : labels.send}
          </Button>
        </div>
      </form>
    </div>
  )
}

function FieldRow({
  field,
  id,
  value,
  error,
  disabled,
  labels,
  onChange,
}: {
  field: InputRequestField
  id: string
  value: InputRequestValue | undefined
  error?: string
  disabled: boolean
  labels: InputRequestLabels
  onChange: (value: InputRequestValue) => void
}) {
  const labelId = `${id}-label`
  const helpId = `${id}-help`
  const errorId = `${id}-error`
  const describedBy = [field.help ? helpId : null, error ? errorId : null].filter(Boolean).join(" ") || undefined
  const invalid = error ? true : undefined
  const common = {
    "data-field-id": field.id,
    "aria-describedby": describedBy,
    "aria-invalid": invalid,
    "aria-required": field.required || undefined,
  } as const

  const label = (
    <div className="flex items-baseline justify-between gap-[var(--hui-space-3)]">
      <label
        id={labelId}
        htmlFor={field.type === "multiselect" || field.type === "select" ? undefined : id}
        className="min-w-0 break-words [font-weight:var(--hui-font-weight-medium)]"
      >
        {field.label}
      </label>
      <span className={cn("shrink-0 text-[var(--hui-color-foreground-base-secondary)]", approvalMetaText)}>
        {field.required ? labels.required : field.type === "boolean" ? null : labels.optional}
      </span>
    </div>
  )

  const help = field.help && (
    <p id={helpId} className={cn("m-0 break-words text-[var(--hui-color-foreground-base-secondary)]", approvalMetaText)}>
      {field.help}
    </p>
  )
  const errorLine = error && (
    <p
      id={errorId}
      className={cn("m-0 flex items-center gap-[var(--hui-space-2)] text-[var(--hui-color-foreground-danger-primary)]", approvalMetaText)}
    >
      <CircleAlertIcon strokeWidth={1.5} strokeLinecap="round" aria-hidden="true" className="size-3.5 shrink-0" />
      {error}
    </p>
  )

  if (field.type === "boolean") {
    return (
      <div data-slot="input-request-field" data-type={field.type} className="flex flex-col gap-[var(--hui-space-2)]">
        <div className="flex items-center justify-between gap-[var(--hui-space-4)]">
          <div className="flex min-w-0 flex-col gap-[var(--hui-space-1)]">
            <label id={labelId} htmlFor={id} className="min-w-0 break-words [font-weight:var(--hui-font-weight-medium)]">
              {field.label}
            </label>
            {help}
          </div>
          <Switch
            id={id}
            {...common}
            aria-labelledby={labelId}
            disabled={disabled}
            checked={Boolean(value)}
            onCheckedChange={(checked) => onChange(Boolean(checked))}
          />
        </div>
        {errorLine}
      </div>
    )
  }

  let control: React.ReactNode
  if (field.type === "textarea") {
    control = (
      <Textarea
        id={id}
        {...common}
        rows={3}
        disabled={disabled}
        placeholder={field.placeholder}
        maxLength={field.maxLength}
        value={typeof value === "string" ? value : ""}
        onChange={(event) => onChange(event.target.value)}
        className="resize-y"
      />
    )
  } else if (field.type === "text") {
    control = (
      <Input
        id={id}
        {...common}
        disabled={disabled}
        placeholder={field.placeholder}
        value={typeof value === "string" ? value : ""}
        onChange={(event) => onChange(event.target.value)}
      />
    )
  } else if (field.type === "number") {
    control = (
      <div className="flex items-center gap-[var(--hui-space-3)]">
        <Input
          id={id}
          {...common}
          type="number"
          inputMode="decimal"
          disabled={disabled}
          placeholder={field.placeholder}
          min={field.min}
          max={field.max}
          step={field.step}
          value={value === null || value === undefined ? "" : String(value)}
          onChange={(event) => onChange(event.target.value === "" ? null : event.target.value)}
          className="max-w-40 tabular-nums"
        />
        {field.unit && <span className="text-[var(--hui-color-foreground-base-secondary)]">{field.unit}</span>}
      </div>
    )
  } else if (field.type === "date") {
    control = (
      <Input
        id={id}
        {...common}
        type="date"
        disabled={disabled}
        min={field.min}
        max={field.max}
        value={typeof value === "string" ? value : ""}
        onChange={(event) => onChange(event.target.value === "" ? null : event.target.value)}
        className="max-w-48 tabular-nums"
      />
    )
  } else if (field.type === "select") {
    control = (
      <Select
        value={typeof value === "string" ? value : null}
        onValueChange={(next) => onChange(next === null || next === undefined ? null : String(next))}
        disabled={disabled}
      >
        <SelectTrigger id={id} {...common} aria-labelledby={labelId} className="w-full">
          {/* The placeholder is real text, not an attribute, so it takes the secondary color to stay readable. */}
          <SelectValue
            placeholder={field.placeholder ?? labels.selectPlaceholder}
            className="data-placeholder:text-[var(--hui-color-foreground-base-secondary)]"
          />
        </SelectTrigger>
        <SelectPopup>
          {field.options.map((option) => (
            <SelectItem key={option.value} value={option.value}>
              {option.label}
            </SelectItem>
          ))}
        </SelectPopup>
      </Select>
    )
  } else if (field.type === "multiselect") {
    const selected = Array.isArray(value) ? value : []
    control = (
      <div
        role="group"
        aria-labelledby={labelId}
        aria-describedby={describedBy}
        className="flex flex-col gap-[var(--hui-space-1)] ps-[var(--hui-space-3)] pe-[var(--hui-space-3)]"
      >
        {field.options.map((option, index) => (
          <ApprovalOptionRow key={option.value} label={option.label}>
            {(optionLabelId) => (
              <Checkbox
                aria-labelledby={optionLabelId}
                data-field-id={index === 0 ? field.id : undefined}
                disabled={disabled}
                checked={selected.includes(option.value)}
                onCheckedChange={(checked) =>
                  onChange(
                    checked
                      ? [...selected, option.value]
                      : selected.filter((item) => item !== option.value)
                  )
                }
              />
            )}
          </ApprovalOptionRow>
        ))}
      </div>
    )
  }

  return (
    <div data-slot="input-request-field" data-type={field.type} className="flex flex-col gap-[var(--hui-space-2)]">
      {label}
      {help}
      {control}
      {errorLine}
    </div>
  )
}

export { InputRequest }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { InputRequest } from "@/components/ai/input-request"

<InputRequest
  agent="Migration agent"
  title="Where and when should I run the orders migration?"
  reason="I can't pick the environment or a maintenance date on my own."
  nextStep="Back up the orders table, then migrate in batches."
  fields={[
    {
      id: "environment",
      type: "select",
      label: "Target environment",
      required: true,
      options: [
        { value: "staging", label: "Staging" },
        { value: "production", label: "Production" },
      ],
    },
    { id: "window", type: "date", label: "Maintenance date", required: true },
  ]}
  onSubmit={(values) => resumeRun(run.id, values)}
  onSkip={() => resumeRun(run.id, null)}
  onCancelRun={() => cancelRun(run.id)}
/>
```

## Examples [#examples]

### Finished requests [#finished-requests-1]

A request that was answered, as shown after a reload, and one that expired.

```tsx
"use client"

import { InputRequest, type InputRequestField } from "@/components/ai/input-request"

const fields: InputRequestField[] = [
  { id: "budget", type: "number", label: "Monthly budget", unit: "USD", required: true },
  {
    id: "channels",
    type: "multiselect",
    label: "Channels",
    options: [
      { value: "search", label: "Search" },
      { value: "social", label: "Social" },
      { value: "email", label: "Email" },
    ],
  },
  { id: "approve", type: "boolean", label: "Auto-approve small changes" },
]

const noop = () => undefined

/** The finished states: what was sent, as shown after a reload, and a request that expired. */
export function InputRequestStates() {
  return (
    <div className="flex w-full max-w-lg min-w-0 flex-col gap-[var(--hui-space-5)]">
      <InputRequest
        agent="Campaign agent"
        title="What budget should the Q1 campaign use?"
        fields={fields}
        onSubmit={noop}
        status="submitted"
        submittedValues={{ budget: 12000, channels: ["search", "email"], approve: false }}
      />
      <InputRequest
        agent="Campaign agent"
        title="Which audience should the retargeting ads use?"
        fields={[{ id: "audience", type: "text", label: "Audience" }]}
        onSubmit={noop}
        status="expired"
      />
    </div>
  )
}

```

## API reference [#api-reference]

### InputRequest [#inputrequest]

Accepts native div props beyond those listed.

| Prop              | Type                                | Default     | Description                                       |
| ----------------- | ----------------------------------- | ----------- | ------------------------------------------------- |
| `title`           | string                              | Required    | What the agent asks for.                          |
| `fields`          | `InputRequestField[]`               | Required    | The fields to fill in.                            |
| `onSubmit`        | `(values) => void \| Promise<void>` | Required    | Receives the values when the person presses Send. |
| `reason`          | ReactNode                           | None        | Why the agent needs the input.                    |
| `nextStep`        | ReactNode                           | None        | What the agent does with the answers.             |
| `agent`           | string                              | None        | The agent's name, shown above the request.        |
| `onSkip`          | `() => void`                        | None        | Lets the run continue without the input.          |
| `onCancelRun`     | `() => void`                        | None        | Stops the run.                                    |
| `status`          | `InputRequestStatus`                | `"pending"` | Shows a finished request.                         |
| `submittedValues` | `InputRequestValues`                | None        | What was sent, for `status="submitted"`.          |
| `deadline`        | ReactNode                           | None        | A line about when the run stops waiting.          |
| `labels`          | `Partial<InputRequestLabels>`       | English     | Every string and validation message.              |

### Helpers [#helpers]

| Export                                            | Description                                                 |
| ------------------------------------------------- | ----------------------------------------------------------- |
| `validateInputRequest(fields, values, messages?)` | Returns a message for each field that fails, keyed by `id`. |
| `getInitialInputValues(fields)`                   | The values a form starts with.                              |

### Types [#types]

| Export               | Shape                                                                                                |
| -------------------- | ---------------------------------------------------------------------------------------------------- |
| `InputRequestField`  | `id`, `type`, `label`, and optional `help`, `required`, `defaultValue`, plus the options of its type |
| `InputRequestValues` | A value for each field, keyed by `id`                                                                |
| `InputRequestStatus` | `"pending"`, `"submitted"`, `"skipped"`, `"cancelled"`, or `"expired"`                               |
| `InputRequestOption` | `value` and `label`                                                                                  |
