From 2e6c9e70dbda2196a87591bf06450bd6043ca19f Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:38:32 -0800 Subject: [PATCH] Fix the speed button's unknown, Flex and chat-options Escape cases (#40173) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix the speed button's unknown, Flex and composer Escape cases Follow-up to #40052 from its review: - An unknown speed names no tier instead of claiming Standard. - Escape in the speed list inside the chat options popover closes only the list, not the whole popover. - Only Codex's Fast tier gets the on/off chip, bolt and pressed fill; Flex and other tiers are picked from the list. The Fast test is shared with the main process. - The list trigger takes the chip's pressed fill from the shared variant, and the list has an accessible name. * Show every agent's speed as the same Speed button in every picker Claude's and Cursor's Fast mode now use the ⚡ Fast button under a Speed label in the chat options popover and the select-mode pickers too, instead of a Fast mode switch, matching Codex: one button for two modes, a list for more. (cherry picked from commit 3ad9b7c44f8ba1803ae3ab0c2f7c11e018ec25c1) --- .../catalog/codex-structured-service-tier.ts | 5 +- .../NativeChatSessionOptionPickers.tsx | 8 ++- .../controls/NativeChatSessionOptionRows.tsx | 25 ++++---- .../NativeChatSessionSpeedButton.test.tsx | 61 +++++++++++++++++++ .../controls/NativeChatSessionSpeedButton.tsx | 44 ++++++------- src/renderer/src/components/ui/toggle.tsx | 3 +- ...ructured-agent-session-discovered-model.ts | 7 +++ 7 files changed, 113 insertions(+), 40 deletions(-) create mode 100644 src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.test.tsx diff --git a/src/main/codex/structured/catalog/codex-structured-service-tier.ts b/src/main/codex/structured/catalog/codex-structured-service-tier.ts index 2124fce21f2..ff04c99f7f2 100644 --- a/src/main/codex/structured/catalog/codex-structured-service-tier.ts +++ b/src/main/codex/structured/catalog/codex-structured-service-tier.ts @@ -8,6 +8,7 @@ import type { CodexOpenedThread } from '../threads/codex-structured-thread-open' import type { CodexSession } from '../session-lifecycle/codex-structured-session-state' import { codexSessionCapabilities } from './codex-structured-catalog-entry' import { effectiveServiceTier } from '../../../../shared/agent-session/options/structured-agent-session-effective-tier' +import { isFastServiceTier } from '../../../../shared/agent-session/options/structured-agent-session-discovered-model' export const CODEX_DEFAULT_SERVICE_TIER = 'default' @@ -41,9 +42,7 @@ export function readCodexServiceTiers( export function codexFastServiceTier( model: Pick | undefined ): string | undefined { - return model?.serviceTiers?.find( - (tier) => tier.label.toLowerCase() === 'fast' || tier.value.toLowerCase() === 'fast' - )?.value + return model?.serviceTiers?.find(isFastServiceTier)?.value } /** Nonstandard tiers other than Fast have no legacy toggle value. */ diff --git a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.tsx b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.tsx index d39bcebf399..6a753a83b16 100644 --- a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.tsx +++ b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionPickers.tsx @@ -261,8 +261,14 @@ function SessionOptionPopover(props: { onInteractOutside={focusReturn.onInteractOutside} onCloseAutoFocus={focusReturn.onCloseAutoFocus} // Close through a level tooltip’s first Escape; let Radix handle search Escape after IME. + // Escape from a portaled child (the speed list) closes only that child. onKeyDown={(event) => { - if (event.key === 'Escape' && !(event.target instanceof HTMLInputElement)) { + if ( + event.key === 'Escape' && + !(event.target instanceof HTMLInputElement) && + event.target instanceof Node && + event.currentTarget.contains(event.target) + ) { setOpen(false) } }} diff --git a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionRows.tsx b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionRows.tsx index 7a78a28dc85..3c46c7fa97c 100644 --- a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionRows.tsx +++ b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionOptionRows.tsx @@ -125,8 +125,10 @@ function OptionRow(props: { ) } - if (descriptor.kind.type === 'boolean' && layout === 'inline' && descriptor.id === 'fastMode') { - return ( + // Every agent's speed is one button: Fast on/off for two modes, a list for more. + const speed = speedButtonTiers(descriptor) + const speedButton = + descriptor.kind.type === 'boolean' && descriptor.id === 'fastMode' ? ( {say('fast')} - ) - } - const speed = speedButtonTiers(descriptor) - if (speed && descriptor.kind.type === 'select') { - const button = ( + ) : speed && descriptor.kind.type === 'select' ? ( - ) + ) : null + if (speedButton) { return layout === 'inline' ? ( - button + speedButton ) : (
- {label} - {button} + {say('speed')} + {speedButton}
) } diff --git a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.test.tsx b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.test.tsx new file mode 100644 index 00000000000..5bf08723ca7 --- /dev/null +++ b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.test.tsx @@ -0,0 +1,61 @@ +// @vitest-environment happy-dom +import { act } from 'react' +import { createRoot } from 'react-dom/client' +import { fireEvent, getAllByRole, getByRole } from '@testing-library/react' +import { afterEach, expect, it } from 'vitest' +import { NativeChatSessionSpeedButton } from './NativeChatSessionSpeedButton' + +const STANDARD = { value: 'default', label: 'Standard' } +const TIERS = [ + { value: 'priority', label: 'Fast' }, + { value: 'ultrafast', label: 'Ultrafast' } +] + +let unmount: (() => void) | undefined +afterEach(() => { + act(() => unmount?.()) + document.body.innerHTML = '' +}) + +function renderSpeed( + current: string | undefined, + tiers: { value: string; label: string }[] = TIERS +): HTMLElement { + const host = document.body.appendChild(document.createElement('div')) + const root = createRoot(host) + act(() => + root.render( + {}} + /> + ) + ) + unmount = () => root.unmount() + return getByRole(host, 'button') +} + +function checkedTiers(trigger: HTMLElement): string[] { + act(() => { + fireEvent.keyDown(trigger, { key: 'Enter' }) + }) + return getAllByRole(document.body, 'menuitemradio') + .filter((item) => item.getAttribute('aria-checked') === 'true') + .map((item) => item.textContent ?? '') +} + +it('names no tier when the current speed is unknown', () => { + const trigger = renderSpeed(undefined) + expect(trigger.getAttribute('aria-label')).toBe('Speed') + expect(checkedTiers(trigger)).toEqual([]) +}) + +it('opens a list for a lone Flex tier instead of a Fast toggle', () => { + const trigger = renderSpeed('flex', [{ value: 'flex', label: 'flex' }]) + expect(trigger.getAttribute('aria-pressed')).toBeNull() + expect(trigger.getAttribute('aria-label')).toBe('Speed: flex') +}) diff --git a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.tsx b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.tsx index 44922b9bd5f..c4cdcdba29f 100644 --- a/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.tsx +++ b/src/renderer/src/components/native-chat/session/options/controls/NativeChatSessionSpeedButton.tsx @@ -1,5 +1,4 @@ import { ChevronDown, Zap } from 'lucide-react' -import { cn } from '@/lib/utils' import { Toggle, toggleVariants } from '@/components/ui/toggle' import { DropdownMenu, @@ -14,37 +13,41 @@ import type { SessionOptionValue } from '../../../../../../../shared/native-chat/input/native-chat-session-options' import { nativeChatSessionChoiceLabel } from '../../../../../../../shared/native-chat/input/native-chat-session-option-labels' +import { isFastServiceTier } from '../../../../../../../shared/agent-session/options/structured-agent-session-discovered-model' import { sayNativeChatSessionOptionTranslated as say } from './native-chat-session-option-labels-text' const STANDARD_TIER = 'default' -/** Standard plus the faster tiers, or null when the select is not a speed choice with any. */ +/** Standard plus the other tiers, or null when the select is not a speed choice with any. */ export function speedButtonTiers( descriptor: SessionOptionDescriptor -): { standard: SessionOptionSelectChoice; faster: SessionOptionSelectChoice[] } | null { +): { standard: SessionOptionSelectChoice; tiers: SessionOptionSelectChoice[] } | null { if (descriptor.id !== 'serviceTier' || descriptor.kind.type !== 'select') { return null } const standard = descriptor.kind.choices.find((choice) => choice.value === STANDARD_TIER) - const faster = descriptor.kind.choices.filter((choice) => choice.value !== STANDARD_TIER) - return standard && faster.length > 0 ? { standard, faster } : null + const tiers = descriptor.kind.choices.filter((choice) => choice.value !== STANDARD_TIER) + return standard && tiers.length > 0 ? { standard, tiers } : null } -/** One faster tier toggles like Claude's Fast; several open a list instead of a segmented switcher. */ +/** A lone Fast tier toggles like Claude's Fast; anything else opens a list instead of a segmented switcher. */ export function NativeChatSessionSpeedButton(props: { label: string standard: SessionOptionSelectChoice - faster: SessionOptionSelectChoice[] + tiers: SessionOptionSelectChoice[] current: string | undefined disabled: boolean onSet: (value: SessionOptionValue) => void }): React.JSX.Element { - const { label, standard, faster, current, disabled, onSet } = props - const picked = faster.find((choice) => choice.value === current) + const { label, standard, tiers, current, disabled, onSet } = props + const picked = tiers.find((choice) => choice.value === current) + // An unknown tier names no choice, rather than claiming Standard. + const shown = picked ?? (current === standard.value ? standard : undefined) const nameOf = (choice: SessionOptionSelectChoice) => nativeChatSessionChoiceLabel(choice, say, 'serviceTier') - if (faster.length === 1) { - const only = faster[0] + // Only Fast takes the toggle and bolt; Flex and other tiers are picked from the list. + if (tiers.length === 1 && isFastServiceTier(tiers[0])) { + const only = tiers[0] return ( - - {[standard, ...faster].map((choice) => ( + + {[standard, ...tiers].map((choice) => ( {nameOf(choice)} diff --git a/src/renderer/src/components/ui/toggle.tsx b/src/renderer/src/components/ui/toggle.tsx index da07ea14713..4ffbb9e4346 100644 --- a/src/renderer/src/components/ui/toggle.tsx +++ b/src/renderer/src/components/ui/toggle.tsx @@ -16,7 +16,8 @@ const toggleVariants = cva( segmented: 'bg-transparent text-muted-foreground transition-[color,background-color,box-shadow] hover:bg-foreground/6 hover:text-foreground data-[state=on]:bg-foreground/12 data-[state=on]:text-foreground dark:hover:bg-accent/50 dark:data-[state=on]:bg-accent', // An outlined segmented toggle that stands alone; pressed trades its border for the fill. - chip: 'border border-input bg-transparent text-muted-foreground transition-[color,background-color,box-shadow] hover:bg-foreground/6 hover:text-foreground data-[state=on]:border-transparent data-[state=on]:bg-foreground/12 data-[state=on]:text-foreground dark:hover:bg-accent/50 dark:data-[state=on]:bg-accent' + // data-pressed lets a menu trigger, whose data-state names open, show the same pressed fill. + chip: 'border border-input bg-transparent text-muted-foreground transition-[color,background-color,box-shadow] hover:bg-foreground/6 hover:text-foreground data-[pressed=true]:border-transparent data-[pressed=true]:bg-foreground/12 data-[pressed=true]:text-foreground data-[state=on]:border-transparent data-[state=on]:bg-foreground/12 data-[state=on]:text-foreground dark:hover:bg-accent/50 dark:data-[pressed=true]:bg-accent dark:data-[state=on]:bg-accent' }, size: { default: 'h-9 min-w-9 px-2', diff --git a/src/shared/agent-session/options/structured-agent-session-discovered-model.ts b/src/shared/agent-session/options/structured-agent-session-discovered-model.ts index c71721c2e72..afb40cdfdfc 100644 --- a/src/shared/agent-session/options/structured-agent-session-discovered-model.ts +++ b/src/shared/agent-session/options/structured-agent-session-discovered-model.ts @@ -42,6 +42,13 @@ function fastModeOption(): CatalogOption { } } +/** The tier Codex names Fast, by label or id; the only one the single Fast toggle stands for. */ +export function isFastServiceTier( + tier: Pick +): boolean { + return tier.label.toLowerCase() === 'fast' || tier.value.toLowerCase() === 'fast' +} + /** Standard plus the tiers the model lists, valued by tier id; one choice in place of Fast. */ export function serviceTierOption(tiers: readonly AgentSessionOptionChoice[]): CatalogOption { return {