fix(agent-picker): keep the open setup panel when the form takes its pick

This commit is contained in:
Kelvin Amoaba
2026-11-09 21:10:46 +00:00
parent 0936031b37
commit 6dab67e776
3 changed files with 100 additions and 61 deletions
@@ -1,4 +1,5 @@
// @vitest-environment happy-dom
import { useState } from 'react'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, expect, it, vi } from 'vitest'
import { AgentLaunchPickerMenu } from './AgentLaunchPickerMenu'
@@ -8,57 +9,82 @@ import {
DropdownMenuTrigger
} from '@/components/ui/dropdown-menu'
import { TooltipProvider } from '@/components/ui/tooltip'
import type { AgentPickerEntry } from '../../../../shared/agent-launch/picker/agent-launch-picker'
import type {
AgentPickerDraft,
AgentPickerEntry
} from '../../../../shared/agent-launch/picker/agent-launch-picker'
vi.mock('@/hooks/useShortcutLabel', () => ({ useOptionalShortcutLabel: () => null }))
afterEach(cleanup)
it('applies a model or effort picked in a form setup panel to the form while the panel stays open', () => {
const efforts = [
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' }
]
const entry: AgentPickerEntry = {
agent: 'codex',
label: 'Codex',
defaultSurface: 'terminal',
surfaces: ['chat', 'terminal'],
terminal: {},
structured: {},
catalog: {
origin: 'probe',
fetchedAt: 1,
models: [
{ id: 'astra', label: 'Astra', isDefault: true, efforts },
{ id: 'luna', label: 'Luna', isDefault: false, efforts }
]
},
rows: [{ kind: 'cli', agent: 'codex', chat: true }]
}
const pick = vi.fn()
render(
const efforts = [
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' }
]
const entry: AgentPickerEntry = {
agent: 'codex',
label: 'Codex',
defaultSurface: 'terminal',
surfaces: ['chat', 'terminal'],
terminal: {},
structured: {},
catalog: {
origin: 'probe',
fetchedAt: 1,
models: [
{ id: 'astra', label: 'Astra', isDefault: true, efforts },
{ id: 'luna', label: 'Luna', isDefault: false, efforts }
]
},
rows: [{ kind: 'cli', agent: 'codex', chat: true }]
}
// A form that re-derives each entry's draft from what it saved, as Automations and settings do.
function Form({ query, onPick }: { query: string; onPick: (draft?: AgentPickerDraft) => void }) {
const [saved, setSaved] = useState<AgentPickerDraft['choices']>({})
return (
<TooltipProvider>
<DropdownMenu defaultOpen modal={false}>
<DropdownMenuTrigger>Agent</DropdownMenuTrigger>
<DropdownMenuContent>
<AgentLaunchPickerMenu entries={[entry]} mode="select" runLaunch={pick} />
<AgentLaunchPickerMenu
entries={[entry]}
mode="select"
initialQuery={query}
draftForEntry={() => ({ agent: 'codex', surface: 'terminal', choices: { ...saved } })}
runLaunch={(_row, draft) => {
onPick(draft)
if (draft) {
setSaved(draft.choices)
}
}}
/>
</DropdownMenuContent>
</DropdownMenu>
</TooltipProvider>
)
fireEvent.click(screen.getByRole('button', { name: 'Configure Codex' }))
fireEvent.click(screen.getByRole('option', { name: 'Luna' }))
expect(pick).toHaveBeenLastCalledWith(
{ agent: 'codex', kind: 'cli', executionHostId: undefined },
expect.objectContaining({ choices: expect.objectContaining({ model: 'luna' }) }),
entry
)
fireEvent.click(screen.getByRole('radio', { name: 'High' }))
expect(pick).toHaveBeenLastCalledWith(
expect.anything(),
expect.objectContaining({
choices: expect.objectContaining({ model: 'luna', options: { effort: 'high' } })
}),
entry
)
expect(screen.getByRole('menu', { name: 'Configure Codex' })).not.toBeNull()
})
}
it.each([
['an agent row', ''],
['a search-result row', 'astra low']
])(
'applies each model and effort picked in %s setup to the form while the panel stays put',
(_row, query) => {
const onPick = vi.fn()
render(<Form query={query} onPick={onPick} />)
fireEvent.click(screen.getAllByRole('button', { name: 'Configure Codex' })[0])
const search = screen.getByLabelText('Search models')
fireEvent.click(screen.getByRole('option', { name: 'Luna' }))
expect(onPick).toHaveBeenLastCalledWith(
expect.objectContaining({ choices: expect.objectContaining({ model: 'luna' }) })
)
expect(screen.getByRole('option', { name: 'Luna' }).getAttribute('data-current')).toBe('true')
fireEvent.click(screen.getByRole('radio', { name: 'High' }))
expect(onPick).toHaveBeenLastCalledWith(
expect.objectContaining({
choices: expect.objectContaining({ model: 'luna', options: { effort: 'high' } })
})
)
expect(screen.getByLabelText('Search models')).toBe(search)
}
)
@@ -103,10 +103,11 @@ export function AgentLaunchSetupButton({
terminalBlocked={terminalBlocked}
onDraftChange={(draft, picked) => {
setup.change(draft)
// Why: the form's trigger and Create read its selection, so a pick applies without closing the panel.
const current = picked && mode === 'select' ? setup.getDraft() : undefined
// Why: a launch form's trigger and Create read its selection, so a pick applies without closing the panel; text cards stage until Use.
const current =
picked && mode === 'select' && !entry.textGeneration ? setup.getDraft() : undefined
const selected = current && acceptAgentPickerArguments(entry, current)
if (selected && setup.accept(selected)) {
if (selected && setup.select(selected)) {
onSelect?.(selected)
}
}}
@@ -32,6 +32,8 @@ import { writeLaunchSetupOptions } from '../native-chat/drafts/native-chat-compo
export type AgentPickerSetupIdentity = { model: string; effort?: string }
const appSession: AgentPickerDraftOwner = {}
const sessionDrafts = new WeakMap<AgentPickerDraftOwner, Map<string, RetainedSetup>>()
// Why: a form re-derives its source from each pick; the open panel stays the authority until it closes.
const selectedSetups = new WeakSet<RetainedSetup>()
export function invalidateAgentPickerDrafts(scope: AgentPickerDraftOwner) {
sessionDrafts.set(scope, new Map())
@@ -150,8 +152,9 @@ export function useAgentPickerSetup({
)
useSyncExternalStore(subscribe, () => read()?.draft)
const matches = useCallback(
(record: RetainedSetup) => sameSource(record.source, initial),
[initial]
(record: RetainedSetup) =>
(open && selectedSetups.has(record)) || sameSource(record.source, initial),
[open, initial]
)
const lifetime = useMemo(
() => ({
@@ -260,6 +263,23 @@ export function useAgentPickerSetup({
store(draft)
}
}
const accept = (draft: AgentPickerDraft) => {
if (!getDraft()) {
return false
}
if (mode === 'launch' && !record) {
return true
}
record!.source = draft.recipeEdits ? draft : initial
const accepted = draft.recipeEdits
? {
...draft,
recipeEdits: { ...draft.recipeEdits, changed: false, argumentsAuthored: false }
}
: draft
store(accepted)
return true
}
return {
ready: mode === 'launch' || !!record,
draft: currentDraft(record?.draft ?? seed ?? agentPickerDraft(entry)),
@@ -277,21 +297,13 @@ export function useAgentPickerSetup({
publish()
}
},
accept: (draft: AgentPickerDraft) => {
if (!getDraft()) {
accept,
/** Select mode: accepts a pick the form applies while the panel stays open. */
select: (draft: AgentPickerDraft) => {
if (!accept(draft)) {
return false
}
if (mode === 'launch' && !record) {
return true
}
record!.source = draft.recipeEdits ? draft : initial
const accepted = draft.recipeEdits
? {
...draft,
recipeEdits: { ...draft.recipeEdits, changed: false, argumentsAuthored: false }
}
: draft
store(accepted)
selectedSetups.add(record!)
return true
}
}