From 2d1298f0d6c7fb6f2ffa1cbd31df71c7d1e286d7 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:29:33 -0800 Subject: [PATCH] Open agent setup with the current model highlighted (#39923) * Open agent setup with the current model highlighted * Start setup on the current model only when it is a rendered, non-Arguments row; remount once defaults settle * Return the setup highlight to the current model when a model search is cleared * Scope setup highlight navigation to one event and scroll the restored model into view * Scope setup highlight navigation to the event's phases and reveal the start model when it arrives --- ...entLaunchSetupPanel.current-model.test.tsx | 138 ++++++++++++++++++ .../agent/AgentLaunchSetupPanel.tsx | 53 +++---- .../agent/agent-launch-setup-option-rows.ts | 26 +++- .../agent/use-setup-model-highlight.ts | 53 +++++++ 4 files changed, 244 insertions(+), 26 deletions(-) create mode 100644 src/renderer/src/components/agent/AgentLaunchSetupPanel.current-model.test.tsx create mode 100644 src/renderer/src/components/agent/use-setup-model-highlight.ts diff --git a/src/renderer/src/components/agent/AgentLaunchSetupPanel.current-model.test.tsx b/src/renderer/src/components/agent/AgentLaunchSetupPanel.current-model.test.tsx new file mode 100644 index 00000000000..efacd606b38 --- /dev/null +++ b/src/renderer/src/components/agent/AgentLaunchSetupPanel.current-model.test.tsx @@ -0,0 +1,138 @@ +// @vitest-environment happy-dom +import { act } from 'react' +import { fireEvent } from '@testing-library/react' +import { expect, it, vi } from 'vitest' +import { TooltipProvider } from '@/components/ui/tooltip' +import { + agentPickerDraft, + type AgentPickerEntry +} from '../../../../shared/agent-launch/picker/agent-launch-picker' +import { entry, root } from './agent-launch-setup-panel-test-fixture' +import { AgentLaunchSetupPanel } from './AgentLaunchSetupPanel' + +const opencode: AgentPickerEntry = { + ...entry, + agent: 'opencode', + label: 'OpenCode', + rows: [{ agent: 'opencode', kind: 'cli', executionHostId: 'local' }], + arguments: '--model opencode/gpt-5.3-codex', + terminal: {}, + catalog: { origin: 'unknown' } +} +const listed: AgentPickerEntry = { + ...opencode, + catalog: { + origin: 'probe', + fetchedAt: 1, + models: [ + 'github-copilot/claude-fable-5', + 'github-copilot/claude-opus-5', + 'opencode/gpt-5.3-codex' + ].map((id) => ({ id, label: id, efforts: [], isDefault: false })) + } +} +const draft = agentPickerDraft(opencode) +const onUse = vi.fn(() => true) +const onDraftChange = vi.fn() +const render = (own: AgentPickerEntry, mode?: 'select') => + act(() => + root.render( + + draft} + onDraftChange={onDraftChange} + onUse={onUse} + /> + + ) + ) +const highlighted = () => + document.querySelector('[cmdk-item][data-selected="true"]')?.getAttribute('data-value') + +it('opens on the current model, not the first listed one', () => { + render(listed) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') +}) + +it('keeps the current model highlighted when the catalog arrives and lists it', () => { + render(opencode) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') + render(listed) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') +}) + +it('starts on the resolved model when defaults settle after the panel opens', () => { + render({ ...listed, arguments: undefined, defaultsPending: true }) + render(listed) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') +}) + +it('keeps an Arguments-owned text action on its default instead of the inherited model', () => { + render( + { + ...listed, + arguments: '', + terminal: { model: 'opencode/gpt-5.3-codex' }, + textGeneration: { + defaultLabel: 'Default', + modelInArguments: true, + effortInArguments: false, + modelArgumentsUnrecognized: false, + effortArgumentsUnrecognized: false, + effortInModel: false, + effortByModel: {} + } + }, + 'select' + ) + expect(highlighted()).toBe('__text_action_default__') +}) + +it('returns to the current model after a search is cleared, so Enter uses it', () => { + render(listed, 'select') + const search = document.querySelector('[cmdk-input]')! + fireEvent.change(search, { target: { value: 'opus' } }) + expect(highlighted()).toBe('github-copilot/claude-opus-5') + fireEvent.change(search, { target: { value: '' } }) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') + onUse.mockClear() + onDraftChange.mockClear() + fireEvent.keyDown(search, { key: 'Enter' }) + expect(onUse).toHaveBeenCalledTimes(1) + expect(onDraftChange).not.toHaveBeenCalled() +}) + +it('returns to the current model when a search is cleared without a key, as with Cut', async () => { + render(listed) + const search = document.querySelector('[cmdk-input]')! + fireEvent.change(search, { target: { value: 'opus' } }) + fireEvent.pointerMove(search) + await act(async () => {}) + fireEvent.change(search, { target: { value: '' } }) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') +}) + +it('keeps a hovered model through a rerender, so Enter picks it instead of using the current one', () => { + render(listed, 'select') + const opus = document.querySelector('[cmdk-item][data-value="github-copilot/claude-opus-5"]')! + fireEvent.pointerMove(opus) + render(listed, 'select') + expect(highlighted()).toBe('github-copilot/claude-opus-5') + onUse.mockClear() + onDraftChange.mockClear() + fireEvent.keyDown(document.querySelector('[cmdk-input]')!, { key: 'Enter' }) + expect(onUse).not.toHaveBeenCalled() + expect(onDraftChange).toHaveBeenCalledTimes(1) +}) + +it('returns to the current model after End at the last row and a keyless clear', () => { + render(listed) + const search = document.querySelector('[cmdk-input]')! + fireEvent.keyDown(search, { key: 'End' }) + fireEvent.change(search, { target: { value: 'opus' } }) + fireEvent.change(search, { target: { value: '' } }) + expect(highlighted()).toBe('opencode/gpt-5.3-codex') +}) diff --git a/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx b/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx index aec35bb04c9..7e420a90701 100644 --- a/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx +++ b/src/renderer/src/components/agent/AgentLaunchSetupPanel.tsx @@ -4,9 +4,11 @@ import type { AgentLaunchSetupPanelProps } from './AgentLaunchSetupButton' import { agentLaunchSetupOptionRows, agentLaunchSetupRowGroups, - agentLaunchSetupCatalogState as catalogOf + agentLaunchSetupCatalogState as catalogOf, + agentLaunchSetupModelRows } from './agent-launch-setup-option-rows' import { useRef, useState } from 'react' +import { useSetupModelHighlight } from './use-setup-model-highlight' import { Check, ChevronDown, ChevronLeft, ChevronRight } from 'lucide-react' import { getAgentSessionOptionLaunchCatalog } from '../../../../shared/agent-session/options/agent-session-option-catalog' import { @@ -89,6 +91,15 @@ export function AgentLaunchSetupPanel({ const { hasModels, catalogPending, catalogMessage } = catalogOf(entry, models, currentModel, mode) const modelInArguments = textActionArgumentsOwnModel(entry, draft) const modelDisabled = disabled || defaultsPending || catalogPending + const modelRows = agentLaunchSetupModelRows( + models, + currentModel, + modelQuery, + legacy, + modelInArguments + ) + const modelList = useRef(null) + const highlight = useSetupModelHighlight(modelList, modelRows.start, modelQuery) const options = agentPickerEntryOptions(entry, currentModel, draft.surface) const optionRows = agentLaunchSetupOptionRows({ options, @@ -164,6 +175,7 @@ export function AgentLaunchSetupPanel({ } }} onKeyDown={(event) => { + highlight.endKey() event.stopPropagation() if ( !disabled && @@ -179,6 +191,8 @@ export function AgentLaunchSetupPanel({ { if ( !disabled && @@ -194,6 +208,7 @@ export function AgentLaunchSetupPanel({ } }} onKeyDown={(event) => { + highlight.onKeyDown(event) if (event.key === 'Enter') { event.stopPropagation() } @@ -238,29 +253,28 @@ export function AgentLaunchSetupPanel({ {t('agentPicker.legacyModels', 'Legacy models')} ) : null} - {models - .filter( - (model) => - !model.hidden && - (model.id === currentModel || modelQuery || Boolean(model.isLegacy) === legacy) - ) - .map((model) => ( + {modelRows.rows.map((model) => { + const current = model.unlisted || (currentModel === model.id && !modelInArguments) + return ( editDraft({ kind: 'choice', patch: { model: model.id } })} + data-current={current} + onSelect={() => { + if (!model.unlisted) { + editDraft({ kind: 'choice', patch: { model: model.id } }) + } + }} > {model.label} - {currentModel === model.id && !modelInArguments ? ( - - ) : null} + {current ? : null} - ))} + ) + })} {!legacy && !modelQuery && legacyCount > 0 ? ( setLegacy(true)}> @@ -274,17 +288,6 @@ export function AgentLaunchSetupPanel({ ) : null} - {currentModel && !models.some((model) => model.id === currentModel) ? ( - {}} - data-current="true" - > - {currentModel} - - - ) : null} diff --git a/src/renderer/src/components/agent/agent-launch-setup-option-rows.ts b/src/renderer/src/components/agent/agent-launch-setup-option-rows.ts index 7a9e07ca2d1..ba9b27772d4 100644 --- a/src/renderer/src/components/agent/agent-launch-setup-option-rows.ts +++ b/src/renderer/src/components/agent/agent-launch-setup-option-rows.ts @@ -11,7 +11,8 @@ import type { } from '../../../../shared/agent-launch/picker/agent-launch-picker-catalog' import type { AgentPickerEntry, - AgentPickerDraft + AgentPickerDraft, + AgentPickerModel } from '../../../../shared/agent-launch/picker/agent-launch-picker' import type { agentPickerPanelArguments } from '../../../../shared/agent-launch/picker/agent-launch-picker-arguments' @@ -242,3 +243,26 @@ export function agentLaunchSetupCatalogState( : null return { hasModels, catalogPending, catalogMessage } } + +/** Visible model rows, plus the model the list starts on. */ +export function agentLaunchSetupModelRows( + models: readonly AgentPickerModel[], + currentModel: string | undefined, + query: string, + legacy: boolean, + modelInArguments: boolean +) { + const rows: { id: string; label: string; unlisted?: true }[] = models.filter( + (model) => + !model.hidden && (model.id === currentModel || query || Boolean(model.isLegacy) === legacy) + ) + // Why one keyed list: when the catalog later lists an unlisted current model, its row keeps + // its identity, so the highlight stays on it instead of jumping to the first model. + if (currentModel && !models.some((model) => model.id === currentModel)) { + rows.push({ id: currentModel, label: currentModel, unlisted: true }) + } + // An Arguments-owned text action has no current model row, so it starts on its default as before. + const start = + !modelInArguments && rows.some((row) => row.id === currentModel) ? currentModel : undefined + return { rows, start } +} diff --git a/src/renderer/src/components/agent/use-setup-model-highlight.ts b/src/renderer/src/components/agent/use-setup-model-highlight.ts new file mode 100644 index 00000000000..bd48d53d862 --- /dev/null +++ b/src/renderer/src/components/agent/use-setup-model-highlight.ts @@ -0,0 +1,53 @@ +import { useLayoutEffect, useRef, useState, type KeyboardEvent, type RefObject } from 'react' + +const NAVIGATION_KEYS = new Set(['ArrowUp', 'ArrowDown', 'Home', 'End']) + +/** + * Starts the model list's highlight on `start`, and returns to it when a search is cleared. + * Why the navigation flag: controlled cmdk reports user moves and its own first-item picks through + * the same callback; only its own picks are undone. + */ +export function useSetupModelHighlight( + root: RefObject, + start: string | undefined, + query: string +) { + const [reassert, setReassert] = useState(false) + const navigating = useRef(false) + const value = query ? undefined : start + // Why: cmdk applies `value` only when the prop changes, so after its own pick we release it for + // one render and set it again. A start that arrives or returns is brought into view. + useLayoutEffect(() => { + if (reassert) { + setReassert(false) + } else if (value !== undefined) { + root.current + ?.querySelector(`[cmdk-item][data-value="${CSS.escape(value)}"]`) + ?.scrollIntoView({ block: 'nearest' }) + } + }, [reassert, value, root]) + return { + // The flag spans one event: set before cmdk handles it, cleared once it bubbles past the list. + command: { + value: reassert ? undefined : value, + onValueChange: (next: string) => { + if (!navigating.current && value !== undefined && next !== value) { + setReassert(true) + } + }, + onPointerMoveCapture: () => { + navigating.current = true + }, + onPointerMove: () => { + navigating.current = false + } + }, + onKeyDown: (event: KeyboardEvent) => { + navigating.current = + NAVIGATION_KEYS.has(event.key) || (event.ctrlKey && 'jknp'.includes(event.key)) + }, + endKey: () => { + navigating.current = false + } + } +}