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
This commit is contained in:
Brennan Benson
2026-11-08 13:29:33 -08:00
committed by GitHub
parent b5e38ad9c6
commit 2d1298f0d6
4 changed files with 244 additions and 26 deletions
@@ -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(
<TooltipProvider>
<AgentLaunchSetupPanel
entry={own}
mode={mode}
draft={draft}
getDraft={() => draft}
onDraftChange={onDraftChange}
onUse={onUse}
/>
</TooltipProvider>
)
)
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<HTMLInputElement>('[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<HTMLInputElement>('[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<HTMLInputElement>('[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')
})
@@ -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<HTMLDivElement>(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({
<Command
surface="inline"
density="model"
ref={modelList}
{...highlight.command}
onKeyDownCapture={(event) => {
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')}
</CommandItem>
) : 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 (
<CommandItem
key={model.id}
value={model.id}
keywords={[model.label]}
disabled={modelDisabled}
data-current={currentModel === model.id && !modelInArguments}
onSelect={() => editDraft({ kind: 'choice', patch: { model: model.id } })}
data-current={current}
onSelect={() => {
if (!model.unlisted) {
editDraft({ kind: 'choice', patch: { model: model.id } })
}
}}
>
{model.label}
<span className="ml-auto flex size-3.5 shrink-0 items-center justify-center">
{currentModel === model.id && !modelInArguments ? (
<Check className="size-3.5 text-muted-foreground" />
) : null}
{current ? <Check className="size-3.5 text-muted-foreground" /> : null}
</span>
</CommandItem>
))}
)
})}
{!legacy && !modelQuery && legacyCount > 0 ? (
<CommandItem value="__legacy__" disabled={disabled} onSelect={() => setLegacy(true)}>
<span className="flex min-w-0 flex-col">
@@ -274,17 +288,6 @@ export function AgentLaunchSetupPanel({
<ChevronRight className="ml-auto size-3.5 text-muted-foreground" />
</CommandItem>
) : null}
{currentModel && !models.some((model) => model.id === currentModel) ? (
<CommandItem
value={currentModel}
disabled={modelDisabled}
onSelect={() => {}}
data-current="true"
>
{currentModel}
<Check className="ml-auto size-3.5 text-muted-foreground" />
</CommandItem>
) : null}
</CommandGroup>
</CommandList>
</Command>
@@ -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 }
}
@@ -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<HTMLDivElement | null>,
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
}
}
}