feat(acp): show OpenCode and Antigravity questions in HAPI and return the answer (#2309)

* feat(acp): opt-in form elicitation capability advertisement

* feat(acp): route elicitation/create forms to a registered handler

* feat(opencode): map question forms to AskUserQuestion input and back

* feat(opencode): queue question forms as AskUserQuestion requests without auto-answering

* feat(opencode): advertise form elicitation for OpenCode v2

* feat(opencode): present the question tool call as AskUserQuestion

* feat(opencode): cancel pending question forms on abort, turn end, archive and exit

* test(opencode): cover the unshown question result surfacing

* feat(acp): let a form elicitation be declined

A form elicitation could only be accepted or cancelled. ACP also has
decline, the user refusing to answer, and agents may record it
differently from a cancel: Antigravity records a declined question as
"User Skipped". FormElicitationResponse now includes it, and the backend
passes it back to the agent unchanged.

* feat(acp): let the ACP permission handler ask the user a question

An agent's question (Antigravity's ask_question) is not a permission
request: it needs the user's answers, and it must never be answered
automatically. askUser() shows it as a pending request with the given
tool name and input, whatever the permission mode, and resolves with the
user's decision and answers. cancelAll() resolves an open question as
aborted, and an open question counts as a pending request, so callers
that wait for approvals also wait for it. onUserWait now covers these
waits too and no longer takes the request. isUsed() tells whether an id
is open or was already shown, since clients hide a pending request
whose id was completed before.

* feat(agy): answer agy's questions in the question card

On agy acp, agy's ask_question used to arrive as a generic tool card and
plain text. The turn ended, and whatever the user typed next became the
answer.

agy now gets form elicitation advertised, so it asks through
elicitation/create and keeps the turn open. The launcher takes these
forms through the backend's shared form elicitation handler, the same
one OpenCode uses.

- The question is shown as the AskUserQuestion card on agy's own tool
  call, with the options as choices, multi-select and an Other field.
- The user's choice goes back as agy's answer: the chosen option values,
  plus free text under qN_other only when the user typed some, so agy
  records exactly the chosen label.
- Denying declines the question, which agy records as "User Skipped".
  Stop and session end cancel it.
- No permission mode answers a question, always-proceed included.
- Steer is refused while a question is open.
- An approval that carries no usable answer declines instead of sending
  an empty answer.
- A question that arrives after Stop is cancelled.

* fix(opencode): decline question forms that arrive after Stop

cancelAll only swept requests that already existed, so a form delivered
afterwards was queued as a card whose promise never settled. Close the
gate synchronously in cancelAll and reopen it when the next prompt
begins, matching the aborted-turn guard of the agy handler.

* feat(web): let the question card be skipped

Add a Skip button, also bound to Esc inside the card, that denies the
pending question through the existing deny API and clears the draft like
Submit does. A deny already becomes a decline for agy and a cancel for
OpenCode, so the backend is unchanged.

* fix(agy): decline questions that arrive after the turn has ended

* fix(web): keep typed text when Esc is pressed in the question card's text field

* fix(question): hide Other on question forms that do not allow free text

Forms without qN_custom (OpenCode) or qN_other (agy) take no typed answer,
yet the card always offered Other, and a typed answer was dropped so the
result became a cancel. Carry an allowFreeText flag through the tool-call
input and the synthesized question input, and hide Other on the card when
it is false. Inputs without the flag keep showing Other.
This commit is contained in:
Junmo Kim
2026-11-09 12:06:28 +08:00
committed by GitHub
parent 1feac0a1be
commit 7de3fdc405
31 changed files with 2413 additions and 38 deletions
@@ -13,20 +13,25 @@ vi.mock('@/lib/use-translation', () => ({
import { AskUserQuestionFooter } from './AskUserQuestionFooter'
function makeTool(id: string): ChatToolCall {
function makeTool(id: string, allowFreeText?: boolean): ChatToolCall {
return {
id, name: 'AskUserQuestion', state: 'pending',
input: { questions: [{ question: 'Pick one', options: [{ label: 'Alpha', description: null }, { label: 'Beta', description: null }], multiSelect: false }] },
input: { questions: [{ question: 'Pick one', options: [{ label: 'Alpha', description: null }, { label: 'Beta', description: null }], multiSelect: false, ...(allowFreeText === undefined ? {} : { allowFreeText }) }] },
createdAt: 1, startedAt: null, completedAt: null, execStartedAt: null, execCompletedAt: null, description: null,
permission: { id: 'permission-1', status: 'pending' }
} as unknown as ChatToolCall
}
function renderFooter(sessionId: string, tool: ChatToolCall, approvePermission = vi.fn().mockResolvedValue(undefined)) {
function renderFooter(
sessionId: string,
tool: ChatToolCall,
approvePermission = vi.fn().mockResolvedValue(undefined),
denyPermission = vi.fn().mockResolvedValue(undefined)
) {
return {
...render(
<AskUserQuestionFooter
api={{ approvePermission } as unknown as ApiClient}
api={{ approvePermission, denyPermission } as unknown as ApiClient}
sessionId={sessionId}
tool={tool}
disabled={false}
@@ -34,6 +39,7 @@ function renderFooter(sessionId: string, tool: ChatToolCall, approvePermission =
/>
),
approvePermission,
denyPermission,
}
}
@@ -118,4 +124,69 @@ describe('AskUserQuestionFooter draft persistence (hapi#1734)', () => {
rerender(<SessionKeyedWrapper sessionId="session-A" tool={tool} />)
expect(screen.getByRole('radio', { name: /Alpha/ })).toHaveAttribute('aria-checked', 'false')
})
it('denies the question once on Skip and clears the draft', async () => {
const tool = makeTool('tool-1')
const { approvePermission, denyPermission, rerender } = renderFooter('session-A', tool)
fireEvent.click(screen.getByRole('radio', { name: /Alpha/ }))
fireEvent.click(screen.getByRole('button', { name: 'tool.skip' }))
await vi.waitFor(() => expect(denyPermission).toHaveBeenCalledTimes(1))
expect(denyPermission).toHaveBeenCalledWith('session-A', 'permission-1')
expect(approvePermission).not.toHaveBeenCalled()
rerender(<SessionKeyedWrapper sessionId="session-A" tool={tool} />)
expect(screen.getByRole('radio', { name: /Alpha/ })).toHaveAttribute('aria-checked', 'false')
})
it('skips on Escape from inside the card', async () => {
const { denyPermission } = renderFooter('session-A', makeTool('tool-1'))
fireEvent.keyDown(screen.getByRole('radio', { name: /Alpha/ }), { key: 'Escape' })
await vi.waitFor(() => expect(denyPermission).toHaveBeenCalledTimes(1))
})
it('only leaves the Other field on Escape, keeping the typed text', async () => {
const { denyPermission } = renderFooter('session-A', makeTool('tool-1'))
fireEvent.click(screen.getByRole('radio', { name: /tool\.other/ }))
const field = screen.getByPlaceholderText('tool.askUserQuestion.otherPlaceholder') as HTMLTextAreaElement
field.focus()
fireEvent.change(field, { target: { value: 'my answer' } })
fireEvent.keyDown(field, { key: 'Escape' })
await new Promise((resolve) => setTimeout(resolve, 50))
expect(denyPermission).not.toHaveBeenCalled()
expect(field.value).toBe('my answer')
expect(document.activeElement).not.toBe(field)
})
it('hides Other when the question does not allow free text, and submits an option as accept', async () => {
const { approvePermission } = renderFooter('session-A', makeTool('tool-1', false))
expect(screen.queryByRole('radio', { name: /tool\.other/ })).toBeNull()
fireEvent.click(screen.getByRole('radio', { name: /Beta/ }))
fireEvent.click(screen.getByRole('button', { name: 'tool.submit' }))
await vi.waitFor(() => expect(approvePermission).toHaveBeenCalledWith('session-A', 'permission-1', { answers: { '0': ['Beta'] } }))
})
it('still shows Other when free text is allowed or the flag is absent', () => {
const allowed = renderFooter('session-A', makeTool('tool-1', true))
expect(screen.getByRole('radio', { name: /tool\.other/ })).toBeTruthy()
allowed.unmount()
renderFooter('session-B', makeTool('tool-2'))
expect(screen.getByRole('radio', { name: /tool\.other/ })).toBeTruthy()
})
it('is not shown once the question is no longer pending', () => {
const tool = makeTool('tool-1')
const resolved = { ...tool, permission: { id: 'permission-1', status: 'denied' } } as unknown as ChatToolCall
renderFooter('session-A', resolved)
expect(screen.queryByRole('button', { name: 'tool.skip' })).toBeNull()
})
})
@@ -244,6 +244,15 @@ export function AskUserQuestionFooter(props: {
setLoading(false)
}
// Dismisses the question without answering it (agy's "esc Skip"): the
// backend maps the denial to a decline / cancel for the agent.
const skip = async () => {
if (loading || props.disabled) return
setLoading(true)
await run(() => props.api.denyPermission(props.sessionId, permission.id), 'success')
setLoading(false)
}
const next = () => {
if (questions.length === 0) return
const a = validateQuestion(clampedStep)
@@ -330,7 +339,20 @@ export function AskUserQuestionFooter(props: {
}
return (
<div className="rounded-2xl border border-[var(--app-border)] bg-[var(--app-bg)] p-3">
<div
className="rounded-2xl border border-[var(--app-border)] bg-[var(--app-bg)] p-3"
onKeyDown={(e) => {
if (e.key !== 'Escape' || e.defaultPrevented || e.nativeEvent.isComposing) return
e.preventDefault()
// Esc in a text field only leaves it, so typed text isn't discarded.
const target = e.target
if (target instanceof HTMLTextAreaElement || target instanceof HTMLInputElement) {
target.blur()
return
}
void skip()
}}
>
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<div className="flex items-center gap-2">
@@ -401,16 +423,18 @@ export function AskUserQuestionFooter(props: {
)
})}
<OptionRow
checked={otherSelectedByQuestion[clampedStep] ?? false}
mode={mode}
disabled={props.disabled || loading}
title={t('tool.other')}
description={t('tool.otherDescription')}
onClick={() => toggleOther(clampedStep)}
/>
{questions[clampedStep].allowFreeText ? (
<OptionRow
checked={otherSelectedByQuestion[clampedStep] ?? false}
mode={mode}
disabled={props.disabled || loading}
title={t('tool.other')}
description={t('tool.otherDescription')}
onClick={() => toggleOther(clampedStep)}
/>
) : null}
{(otherSelectedByQuestion[clampedStep] ?? false) ? (
{questions[clampedStep].allowFreeText && (otherSelectedByQuestion[clampedStep] ?? false) ? (
<textarea
value={otherTextByQuestion[clampedStep] ?? ''}
onChange={(e) => updateOtherText(clampedStep, e.target.value)}
@@ -440,6 +464,16 @@ export function AskUserQuestionFooter(props: {
</div>
<div className="flex items-center gap-2">
<Button
type="button"
variant="outline"
size="sm"
disabled={props.disabled || loading}
onClick={skip}
className={questionNavButtonClassName}
>
{t('tool.skip')}
</Button>
{questions.length > 1 && clampedStep < questions.length - 1 ? (
<Button
type="button"
@@ -14,6 +14,8 @@ export type AskUserQuestionQuestion = {
question: string
options: AskUserQuestionOption[]
multiSelect: boolean
/** False when the agent's form takes no typed answer; absent (older messages) means allowed. */
allowFreeText: boolean
}
export type AskUserQuestionQuestionInfo = {
@@ -57,7 +59,8 @@ export function parseAskUserQuestionInput(input: unknown): { questions: AskUserQ
header: header.length > 0 ? header : null,
question,
options,
multiSelect
multiSelect,
allowFreeText: raw.allowFreeText !== false
})
}
@@ -55,7 +55,8 @@ export function parseCursorAskQuestionInput(input: unknown): { questions: AskUse
header: header.length > 0 ? header : (requestTitle.length > 0 ? requestTitle : null),
question,
options,
multiSelect
multiSelect,
allowFreeText: true
})
}