Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(assistant-ui): support question-style tool approvals and text answers

Steven Enamakel57e669c03b32
diff --git a/app/src/components/assistant-ui/elements/tool-fallback.tsx b/app/src/components/assistant-ui/elements/tool-fallback.tsx
index 9965fc2d78..c6627c9cf9 100644
--- a/app/src/components/assistant-ui/elements/tool-fallback.tsx
+++ b/app/src/components/assistant-ui/elements/tool-fallback.tsx
@@ -336,15 +336,23 @@ const APPROVAL_OPTION_DEFAULT_LABELS: Record<string, string> = {
'reject-always': 'Always deny',
};
+const isKnownKind = (kind: string) => Object.hasOwn(APPROVAL_OPTION_DEFAULT_LABELS, kind);
+
const isAllowKind = (kind: string) => kind === 'allow-once' || kind === 'allow-always';
const approvalOptionLabel = (option: ToolApprovalOption) =>
option.label ??
- (Object.hasOwn(APPROVAL_OPTION_DEFAULT_LABELS, option.kind)
- ? APPROVAL_OPTION_DEFAULT_LABELS[option.kind]
- : undefined) ??
+ (isKnownKind(option.kind) ? APPROVAL_OPTION_DEFAULT_LABELS[option.kind] : undefined) ??
option.id;
+/**
+ * A request that declares how it wants to be presented is asking a question,
+ * not gating an action, so a refusal is not one of the answers it accepts
+ * unless the request declares itself dismissible.
+ */
+const isQuestion = (approval: ToolCallMessagePart['approval']) =>
+ approval?.display === 'select' || approval?.display === 'text';
+
const offersInterruptAction = (
status: ToolCallMessagePartStatus | undefined,
approval: ToolCallMessagePart['approval'],
@@ -372,41 +380,80 @@ function ToolFallbackApproval({
approval?: ToolCallMessagePart['approval'];
}) {
const [submitted, setSubmitted] = useState(false);
+ const voiceActive = useAuiState(s => s.thread.voice !== undefined);
+ const locked = submitted || voiceActive;
const [confirmingId, setConfirmingId] = useState<string | null>(null);
+ const [answer, setAnswer] = useState('');
+ const [error, setError] = useState<string | null>(null);
if (approval != null && (approval.approved !== undefined || approval.resolution !== undefined))
return null;
if (!offersInterruptAction(status, approval, interrupt)) return null;
- // Custom (`_`-prefixed) kinds cannot be resolved to a boolean by the kit;
- // hosts using custom kinds render their own bar. A declared option list is
- // a host constraint: the kit never adds an approval path beyond it, but
- // always preserves a refusal path.
+ // A declared option list is a host constraint: the kit never adds an
+ // approval path beyond it, and preserves a refusal path only where the
+ // request is an action the user may refuse.
const declaredOptions = respondToApproval ? approval?.options : undefined;
- const options = declaredOptions?.filter(o =>
- Object.hasOwn(APPROVAL_OPTION_DEFAULT_LABELS, o.kind)
- );
+ const acceptsText =
+ approval != null && respondToApproval != null && toolApprovalAcceptsText(approval);
+
+ // A refused response leaves the request open, so the controls come back
+ // rather than staying spent on a decision the runtime never recorded.
+ const submit = (send: () => Promise<void> | void) => {
+ setSubmitted(true);
+ setError(null);
+ void (async () => {
+ try {
+ await send();
+ } catch (sendError) {
+ setSubmitted(false);
+ setError(sendError instanceof Error ? sendError.message : String(sendError));
+ }
+ })();
+ };
+
+ const typedNote = () => (answer.trim() ? { text: answer } : {});
const respond = (approved: boolean) => {
- if (submitted) return;
+ if (locked) return;
if (approval != null && approval.approved === undefined && respondToApproval) {
- respondToApproval({ approved });
+ submit(() => respondToApproval({ approved, ...typedNote() }));
} else if (interrupt) {
- resume?.({ approved });
+ submit(() => resume?.({ approved }));
} else if (status?.type === 'requires-action' && status.reason === 'interrupt') {
return;
} else {
- addResult?.(approved ? APPROVED_RESULT : DENIED_RESULT);
+ submit(() => addResult?.(approved ? APPROVED_RESULT : DENIED_RESULT));
}
- setSubmitted(true);
};
const respondWithOption = (option: ToolApprovalOption) => {
- if (submitted) return;
- respondToApproval?.({ optionId: option.id });
- setSubmitted(true);
+ if (locked) return;
setConfirmingId(null);
+ // A custom kind has no decision class for the runtime to derive, and
+ // responding without one throws; picking a declared option is an answer,
+ // so it resolves as approved.
+ submit(() =>
+ respondToApproval?.(
+ isKnownKind(option.kind)
+ ? { optionId: option.id, ...typedNote() }
+ : { optionId: option.id, approved: true, ...typedNote() }
+ )
+ );
+ };
+
+ // The kit does not validate an answer the request never constrained: a host
+ // that cannot record an empty one rejects it, which reopens the controls.
+ const submitAnswer = () => {
+ if (locked) return;
+ submit(() => respondToApproval?.({ text: answer }));
+ };
+
+ // A dismissal is no answer at all, so a typed draft does not travel with it.
+ const dismiss = () => {
+ if (locked) return;
+ submit(() => respondToApproval?.({ approved: false }));
};
const handleOption = (option: ToolApprovalOption) => {
@@ -417,7 +464,51 @@ function ToolFallbackApproval({
}
};
- const confirming = confirmingId != null ? options?.find(o => o.id === confirmingId) : undefined;
+ const confirming =
+ confirmingId != null ? declaredOptions?.find(o => o.id === confirmingId) : undefined;
+
+ const question = isQuestion(approval);
+ const dismissible = question && respondToApproval != null && approval?.dismissible === true;
+
+ const dismissButton = dismissible ? (
+ <Button size="sm" variant="outline" className={pressable} onClick={dismiss} disabled={locked}>
+ Dismiss
+ </Button>
+ ) : null;
+
+ const promptText = approval?.prompt ? (
+ <p className="aui-tool-fallback-approval-prompt text-foreground whitespace-pre-line">
+ {approval.prompt}
+ </p>
+ ) : null;
+
+ const errorText = error ? (
+ <p
+ role="alert"
+ className="aui-tool-fallback-approval-error text-destructive text-xs whitespace-pre-line">
+ {error}
+ </p>
+ ) : null;
+
+ const answerField = acceptsText ? (
+ <div className="aui-tool-fallback-approval-answer flex flex-col items-start gap-2">
+ <Textarea
+ value={answer}
+ onChange={event => setAnswer(event.target.value)}
+ disabled={locked}
+ aria-label={question ? (approval?.prompt ?? 'Answer') : 'Note'}
+ placeholder={question ? 'Type your answer' : 'Add a note to your decision'}
+ />
+ {question && (
+ <div className="flex items-center gap-2">
+ <Button size="sm" className={pressable} onClick={submitAnswer} disabled={locked}>
+ Send
+ </Button>
+ {dismissButton}
+ </div>
+ )}
+ </div>
+ ) : null;
if (confirming) {
const confirmMeta = typeof confirming.confirm === 'object' ? confirming.confirm : undefined;
@@ -431,7 +522,7 @@ function ToolFallbackApproval({
{confirmMeta?.title ?? `${approvalOptionLabel(confirming)}?`}
</p>
{confirmDescription && (
- <p className="aui-tool-fallback-approval-confirm-description text-muted-foreground">
+ <p className="aui-tool-fallback-approval-confirm-description text-muted-foreground whitespace-pre-line">
{confirmDescription}
</p>
)}
@@ -451,7 +542,7 @@ function ToolFallbackApproval({
size="sm"
className={pressable}
onClick={() => respondWithOption(confirming)}
- disabled={submitted}>
+ disabled={locked}>
Confirm
</Button>
<Button
@@ -459,7 +550,7 @@ function ToolFallbackApproval({
variant="outline"
className={pressable}
onClick={() => setConfirmingId(null)}
- disabled={submitted}>
+ disabled={locked}>
Back
</Button>
</div>
@@ -468,37 +559,59 @@ function ToolFallbackApproval({
}
if (declaredOptions && declaredOptions.length > 0) {
- const allowOptions = options?.filter(o => isAllowKind(o.kind)) ?? [];
- const rejectOptions = options?.filter(o => !isAllowKind(o.kind)) ?? [];
+ const allowOptions = declaredOptions.filter(o => isAllowKind(o.kind));
+ const customOptions = declaredOptions.filter(o => !isKnownKind(o.kind));
+ const rejectOptions = declaredOptions.filter(o => isKnownKind(o.kind) && !isAllowKind(o.kind));
return (
<div
data-slot="tool-fallback-approval"
- className={cn(
- 'aui-tool-fallback-approval flex flex-wrap items-center gap-2 pt-1',
- className
- )}
+ className={cn('aui-tool-fallback-approval flex flex-col gap-2 pt-1', className)}
{...props}>
- {[...allowOptions, ...rejectOptions].map(option => (
- <Button
- key={option.id}
- size="sm"
- variant={option === allowOptions[0] ? 'default' : 'outline'}
- className={pressable}
- onClick={() => handleOption(option)}
- disabled={submitted}>
- {approvalOptionLabel(option)}
- </Button>
- ))}
- {rejectOptions.length === 0 && (
- <Button
- size="sm"
- variant="outline"
- className={pressable}
- onClick={() => respond(false)}
- disabled={submitted}>
- Deny
- </Button>
+ {promptText}
+ <div className="flex flex-wrap items-center gap-2">
+ {[...allowOptions, ...customOptions, ...rejectOptions].map(option => (
+ <Button
+ key={option.id}
+ size="sm"
+ variant={option === allowOptions[0] ? 'default' : 'outline'}
+ className={pressable}
+ onClick={() => handleOption(option)}
+ disabled={locked}>
+ {approvalOptionLabel(option)}
+ </Button>
+ ))}
+ {rejectOptions.length === 0 && !question && (
+ <Button
+ size="sm"
+ variant="outline"
+ className={pressable}
+ onClick={() => respond(false)}
+ disabled={locked}>
+ Deny
+ </Button>
+ )}
+ {!acceptsText && dismissButton}
+ </div>
+ {answerField}
+ {errorText}
+ </div>
+ );
+ }
+
+ // A question carries no decision to fabricate, so it renders only what the
+ // request declared, even when that leaves nothing to act on here.
+ if (question) {
+ return (
+ <div
+ data-slot="tool-fallback-approval"
+ className={cn('aui-tool-fallback-approval flex flex-col gap-2 pt-1', className)}
+ {...props}>
+ {promptText}
+ {answerField}
+ {!acceptsText && dismissButton && (
+ <div className="flex items-center gap-2">{dismissButton}</div>
)}
+ {errorText}
</div>
);
}
@@ -506,19 +619,24 @@ function ToolFallbackApproval({
return (
<div
data-slot="tool-fallback-approval"
- className={cn('aui-tool-fallback-approval flex items-center gap-2 pt-1', className)}
+ className={cn('aui-tool-fallback-approval flex flex-col gap-2 pt-1', className)}
{...props}>
- <Button size="sm" className={pressable} onClick={() => respond(true)} disabled={submitted}>
- Allow
- </Button>
- <Button
- size="sm"
- variant="outline"
- className={pressable}
- onClick={() => respond(false)}
- disabled={submitted}>
- Deny
- </Button>
+ {promptText}
+ <div className="flex items-center gap-2">
+ <Button size="sm" className={pressable} onClick={() => respond(true)} disabled={locked}>
+ Allow
+ </Button>
+ <Button
+ size="sm"
+ variant="outline"
+ className={pressable}
+ onClick={() => respond(false)}
+ disabled={locked}>
+ Deny
+ </Button>
+ </div>
+ {answerField}
+ {errorText}
</div>
);
}