Skip to content

The agent that grows with you

fix(desktop): support multi-select clarifications

Thomas Hudspith-Tatham6cbe5a35b63b
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
index cc987e9559a..e4eae4f7cc9 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
@@ -199,7 +199,13 @@ describe('useComposerSubmit with a clarify parked on the session', () => {
const parkClarify = (sessionId: string) => {
$clarifyRequests.set({
- [sessionId]: { requestId: `req-${sessionId}`, question: 'which one?', choices: ['a', 'b'], sessionId }
+ [sessionId]: {
+ requestId: `req-${sessionId}`,
+ question: 'which one?',
+ choices: ['a', 'b'],
+ multiSelect: false,
+ sessionId
+ }
})
$gateway.set({ request: gatewayRequest } as unknown as ReturnType<typeof $gateway.get>)
}
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx
index de0819561df..306c460a560 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/clarify-hydration.test.tsx
@@ -5,7 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { ClientSessionState } from '@/app/types'
import { createClientSessionState } from '@/lib/chat-runtime'
-import { clearClarifyRequest } from '@/store/clarify'
+import { $clarifyRequests, clearClarifyRequest } from '@/store/clarify'
import { onScrollToBottomRequest } from '@/store/thread-scroll'
import type { RpcEvent } from '@/types/hermes'
@@ -123,6 +123,32 @@ describe('clarify.request stream hydration', () => {
expect(scrollToBottom).not.toHaveBeenCalled()
})
+ it('preserves multi-select through the store and hydrated tool row', async () => {
+ await mountStream()
+
+ clarifyRequest({
+ choices: ['read', 'write'],
+ multi_select: true,
+ question: 'Which permissions?',
+ request_id: 'req-multi'
+ })
+
+ expect($clarifyRequests.get()[SID]?.multiSelect).toBe(true)
+
+ const part = clarifyParts()[0]
+ expect(part?.type).toBe('tool-call')
+
+ if (part?.type !== 'tool-call') {
+ throw new Error('Expected a hydrated clarify tool call')
+ }
+
+ expect(part.args).toMatchObject({
+ choices: ['read', 'write'],
+ multi_select: true,
+ question: 'Which permissions?'
+ })
+ })
+
it('merges with the real tool.start row even though its id differs from the request id', async () => {
await mountStream()
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
index 19cae174633..0ba4b2a0db6 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event.ts
@@ -957,6 +957,7 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
const question = typeof payload?.question === 'string' ? payload.question : ''
const rawChoices = payload?.choices
const choices = normalizeChoices(rawChoices)
+ const multiSelect = payload?.multi_select === true
if (requestId && question) {
if (rawChoices != null && choices.length === 0) {
@@ -967,6 +968,7 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
requestId,
question,
choices: choices.length > 0 ? choices : null,
+ multiSelect,
sessionId: sessionId ?? null
})
@@ -978,7 +980,15 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
// choices. Upsert a stable pending clarify tool row from the request
// itself so the prompt stays answerable; a real tool.start/complete
// with the same request id merges rather than duplicates.
- upsertToolCall(sessionId, { args: { choices, question }, name: 'clarify', tool_id: requestId }, 'running')
+ upsertToolCall(
+ sessionId,
+ {
+ args: { choices, ...(multiSelect ? { multi_select: true } : {}), question },
+ name: 'clarify',
+ tool_id: requestId
+ },
+ 'running'
+ )
// The transcript only renders the active session, so a background
// clarify is otherwise invisible (the row just keeps spinning like
diff --git a/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx b/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
index 0b80ad68d4d..4d33a176c26 100644
--- a/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
+++ b/apps/desktop/src/components/assistant-ui/clarify-tool.test.tsx
@@ -71,13 +71,14 @@ function liveClarifyProps(choices = ['staging', 'production']): ToolCallMessageP
}
}
-function renderLiveClarify() {
+function renderLiveClarify({ multiSelect = false }: { multiSelect?: boolean } = {}) {
const request = vi.fn().mockResolvedValue({ ok: true })
$activeSessionId.set('session-1')
$gateway.set({ request } as never)
setClarifyRequest({
choices: ['staging', 'production'],
+ multiSelect,
question: 'Which deployment target?',
requestId: 'request-1',
sessionId: 'session-1'
@@ -87,6 +88,57 @@ function renderLiveClarify() {
return request
}
+describe('ClarifyTool choice selection', () => {
+ it('selects independently, deselects and submits multi-select choices as a JSON array', async () => {
+ const request = renderLiveClarify({ multiSelect: true })
+ const staging = screen.getByRole('button', { name: /staging/ })
+ const production = screen.getByRole('button', { name: /production/ })
+
+ fireEvent.click(staging)
+ fireEvent.click(production)
+ expect(staging.getAttribute('aria-pressed')).toBe('true')
+ expect(production.getAttribute('aria-pressed')).toBe('true')
+
+ fireEvent.keyDown(window, { key: 'ArrowDown' })
+ expect(staging.getAttribute('aria-pressed')).toBe('true')
+ expect(production.getAttribute('aria-pressed')).toBe('true')
+
+ fireEvent.click(staging)
+ expect(staging.getAttribute('aria-pressed')).toBe('false')
+ fireEvent.click(staging)
+
+ fireEvent.click(screen.getByRole('button', { name: /Continue/ }))
+
+ await waitFor(() => {
+ expect(request).toHaveBeenCalledWith('clarify.respond', {
+ answer: JSON.stringify(['production', 'staging']),
+ request_id: 'request-1'
+ })
+ })
+ })
+
+ it('keeps single-select replacement and plain-string submission', async () => {
+ const request = renderLiveClarify()
+ const staging = screen.getByRole('button', { name: /staging/ })
+ const production = screen.getByRole('button', { name: /production/ })
+
+ fireEvent.click(staging)
+ fireEvent.click(production)
+
+ expect(staging.getAttribute('aria-pressed')).toBe('false')
+ expect(production.getAttribute('aria-pressed')).toBe('true')
+
+ fireEvent.click(screen.getByRole('button', { name: /Continue/ }))
+
+ await waitFor(() => {
+ expect(request).toHaveBeenCalledWith('clarify.respond', {
+ answer: 'production',
+ request_id: 'request-1'
+ })
+ })
+ })
+})
+
describe('readClarifyResult', () => {
it('reads question + user_response from the tool JSON payload', () => {
expect(
@@ -348,6 +400,7 @@ describe('ClarifyTool pending marker', () => {
$gateway.set({ request: vi.fn().mockResolvedValue({ ok: true }) } as never)
setClarifyRequest({
choices: null,
+ multiSelect: false,
question: 'Anything else?',
requestId: 'request-1',
sessionId: 'session-1'
diff --git a/apps/desktop/src/components/assistant-ui/clarify-tool.tsx b/apps/desktop/src/components/assistant-ui/clarify-tool.tsx
index f970c6d5727..255729de780 100644
--- a/apps/desktop/src/components/assistant-ui/clarify-tool.tsx
+++ b/apps/desktop/src/components/assistant-ui/clarify-tool.tsx
@@ -42,6 +42,7 @@ import { parseMaybeObject } from './tool/fallback-model/format'
interface ClarifyArgs {
question?: string
choices?: string[] | null
+ multiSelect?: boolean
}
interface ClarifyResult {
@@ -73,7 +74,8 @@ function readClarifyArgs(args: unknown): ClarifyArgs {
return {
question,
- choices: choices.length > 0 ? choices : null
+ choices: choices.length > 0 ? choices : null,
+ multiSelect: row.multi_select === true
}
}
@@ -167,7 +169,7 @@ function ChoiceButton({
disabled,
keyShortcuts,
onClick,
- selected = false,
+ selected,
title
}: {
active?: boolean
@@ -192,6 +194,7 @@ function ChoiceButton({
<button
aria-current={active || undefined}
aria-keyshortcuts={keyShortcuts}
+ aria-pressed={selected}
className={cn(
OPTION_ROW_CLASS,
'text-(--ui-text-secondary) hover:bg-(--chrome-action-hover) hover:text-(--ui-text-primary)',
@@ -204,7 +207,7 @@ function ChoiceButton({
onClick={onClick}
type="button"
>
- <KeyBadge char={char} preview={active} selected={selected} />
+ <KeyBadge char={char} preview={active} selected={Boolean(selected)} />
<span className="flex-1 wrap-anywhere">
<ChoiceLabel choice={choice} />
</span>
@@ -335,10 +338,11 @@ function ClarifyToolPending({ args }: ToolCallMessagePartProps) {
)
const hasChoices = choices.length > 0
+ const multiSelect = hasChoices && Boolean(matchingRequest?.multiSelect ?? fromArgs.multiSelect)
const [draft, setDraft] = useState('')
const [submitting, setSubmitting] = useState(false)
- const [selectedChoice, setSelectedChoice] = useState<string | null>(null)
+ const [selectedChoices, setSelectedChoices] = useState<string[]>([])
// The keyboard cursor. Indices 0..choices.length-1 are the options; the
// trailing index (=== choices.length) is the "Other" free-text row.
const [activeIndex, setActiveIndex] = useState(0)
@@ -388,14 +392,30 @@ function ClarifyToolPending({ args }: ToolCallMessagePartProps) {
// The answer is whichever input is active: a picked choice, or typed text.
// Picking a choice no longer fires immediately — it selects, then the user
// confirms with Continue (or Enter from the field).
- const pendingAnswer = selectedChoice ?? (trimmedDraft || null)
- const selectChoice = useCallback((choice: string, index: number) => {
- // Picking a choice and typing are mutually exclusive answers.
- setDraft('')
- setSelectedChoice(choice)
- setActiveIndex(index)
- }, [])
+ const selectedAnswer = multiSelect
+ ? selectedChoices.length > 0
+ ? JSON.stringify(selectedChoices)
+ : null
+ : (selectedChoices[0] ?? null)
+
+ const pendingAnswer = selectedAnswer ?? (trimmedDraft || null)
+
+ const selectChoice = useCallback(
+ (choice: string, index: number) => {
+ // Picking a choice and typing are mutually exclusive answers.
+ setDraft('')
+ setSelectedChoices(selected => {
+ if (!multiSelect) {
+ return [choice]
+ }
+
+ return selected.includes(choice) ? selected.filter(value => value !== choice) : [...selected, choice]
+ })
+ setActiveIndex(index)
+ },
+ [multiSelect]
+ )
// Keep the cursor in range when the choice set changes (never past "Other").
useEffect(() => {
@@ -406,26 +426,25 @@ function ClarifyToolPending({ args }: ToolCallMessagePartProps) {
(delta: number) => {
const itemCount = choices.length + 1
- // Arrow navigation is a move, not a pick — clear any staged answer so the
- // cursor and the selection can't disagree.
+ // Arrow navigation is a move, not a pick. Multi-select keeps staged
+ // choices while the cursor moves so the user can build a set; the
+ // single-select path retains its existing clear-on-navigation behaviour.
setDraft('')
- setSelectedChoice(null)
+
+ if (!multiSelect) {
+ setSelectedChoices([])
+ }
+
setActiveIndex(index => (index + delta + itemCount) % itemCount)
},
- [choices.length]
+ [choices.length, multiSelect]
)
const submitAnswer = useCallback(() => {
- if (selectedChoice !== null) {
- void respond(selectedChoice)
-
- return
- }
-
- if (trimmedDraft) {
- void respond(trimmedDraft)
+ if (pendingAnswer) {
+ void respond(pendingAnswer)
}
- }, [respond, selectedChoice, trimmedDraft])
+ }, [pendingAnswer, respond])
const activateActive = useCallback(() => {
// A staged answer (picked choice or typed text) wins — confirm it.
@@ -562,7 +581,7 @@ function ClarifyToolPending({ args }: ToolCallMessagePartProps) {
// Typing is its own answer — drop any picked choice so the two inputs can't
// both look selected.
if (value.trim()) {
- setSelectedChoice(null)
+ setSelectedChoices([])
}
}
@@ -600,7 +619,7 @@ function ClarifyToolPending({ args }: ToolCallMessagePartProps) {
key={`${index}-${choice}`}
keyShortcuts={`${letterFor(index)} ${index + 1}`}
onClick={() => selectChoice(choice, index)}
- selected={selectedChoice === choice}
+ selected={selectedChoices.includes(choice)}
/>
))}
<label
@@ -624,7 +643,7 @@ function ClarifyToolPending({ args }: ToolCallMessagePartProps) {
onBlur={() => setOtherFocused(false)}
onChange={event => onDraftChange(event.target.value)}
onFocus={() => {
- setSelectedChoice(null)
+ setSelectedChoices([])
setActiveIndex(choices.length)
setOtherFocused(true)
}}
diff --git a/apps/desktop/src/lib/chat-messages.ts b/apps/desktop/src/lib/chat-messages.ts
index f2d1f03c6bc..fa98550a006 100644
--- a/apps/desktop/src/lib/chat-messages.ts
+++ b/apps/desktop/src/lib/chat-messages.ts
@@ -73,6 +73,7 @@ export type GatewayEventPayload = {
request_id?: string
question?: string
choices?: string[] | null
+ multi_select?: boolean
// mcp.setup.request (setup_mcp tool — inline MCP consent card)
server?: string
action?: string
diff --git a/apps/desktop/src/store/clarify.test.ts b/apps/desktop/src/store/clarify.test.ts
index aec413b5b36..072fd0c3f90 100644
--- a/apps/desktop/src/store/clarify.test.ts
+++ b/apps/desktop/src/store/clarify.test.ts
@@ -18,6 +18,7 @@ function clarify(sessionId: string | null, requestId: string): ClarifyRequest {
requestId,
question: `question-${requestId}`,
choices: null,
+ multiSelect: false,
sessionId
}
}
diff --git a/apps/desktop/src/store/clarify.ts b/apps/desktop/src/store/clarify.ts
index 6dfc275e758..2bf13d516de 100644
--- a/apps/desktop/src/store/clarify.ts
+++ b/apps/desktop/src/store/clarify.ts
@@ -7,6 +7,7 @@ export interface ClarifyRequest {
requestId: string
question: string
choices: string[] | null
+ multiSelect: boolean
sessionId: string | null
}
diff --git a/apps/desktop/src/store/prompts.test.ts b/apps/desktop/src/store/prompts.test.ts
index 1ca6052ce15..284f1ed4e5b 100644
--- a/apps/desktop/src/store/prompts.test.ts
+++ b/apps/desktop/src/store/prompts.test.ts
@@ -201,7 +201,7 @@ describe('$activeSessionAwaitingInput', () => {
clearApprovalRequest('s1')
expect($activeSessionAwaitingInput.get()).toBe(false)
- setClarifyRequest({ choices: null, question: 'q', requestId: 'c1', sessionId: 's1' })
+ setClarifyRequest({ choices: null, multiSelect: false, question: 'q', requestId: 'c1', sessionId: 's1' })
expect($activeSessionAwaitingInput.get()).toBe(true)
})