From 6dab67e77612b4d4571bd2d19244da207eefa586 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 20:48:41 +0000 Subject: [PATCH] fix(agent-picker): keep the open setup panel when the form takes its pick --- .../AgentLaunchPickerMenu.select.test.tsx | 112 +++++++++++------- .../agent/AgentLaunchSetupButton.tsx | 7 +- .../agent/agent-launch-picker-drafts.ts | 42 ++++--- 3 files changed, 100 insertions(+), 61 deletions(-) diff --git a/src/renderer/src/components/agent/AgentLaunchPickerMenu.select.test.tsx b/src/renderer/src/components/agent/AgentLaunchPickerMenu.select.test.tsx index 28d39a33133..4033673eb46 100644 --- a/src/renderer/src/components/agent/AgentLaunchPickerMenu.select.test.tsx +++ b/src/renderer/src/components/agent/AgentLaunchPickerMenu.select.test.tsx @@ -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({}) + return ( Agent - + ({ agent: 'codex', surface: 'terminal', choices: { ...saved } })} + runLaunch={(_row, draft) => { + onPick(draft) + if (draft) { + setSaved(draft.choices) + } + }} + /> ) - 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(
) + 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) + } +) diff --git a/src/renderer/src/components/agent/AgentLaunchSetupButton.tsx b/src/renderer/src/components/agent/AgentLaunchSetupButton.tsx index 1224add69f7..054e809a532 100644 --- a/src/renderer/src/components/agent/AgentLaunchSetupButton.tsx +++ b/src/renderer/src/components/agent/AgentLaunchSetupButton.tsx @@ -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) } }} diff --git a/src/renderer/src/components/agent/agent-launch-picker-drafts.ts b/src/renderer/src/components/agent/agent-launch-picker-drafts.ts index 96d828773dd..f15b1b2df1d 100644 --- a/src/renderer/src/components/agent/agent-launch-picker-drafts.ts +++ b/src/renderer/src/components/agent/agent-launch-picker-drafts.ts @@ -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>() +// Why: a form re-derives its source from each pick; the open panel stays the authority until it closes. +const selectedSetups = new WeakSet() 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 } }