From 0fa98d6a1b24c80732bde8da01e1545cff45defc Mon Sep 17 00:00:00 2001 From: rainbowgits <164521089+rainbowgits@users.noreply.github.com> Date: Wed, 26 Aug 2026 13:37:40 +0300 Subject: [PATCH] fix(desktop): switch model after refresh when it leaves the catalog Refresh Models only updated the catalog cache, so the composer kept showing a model that was no longer in the new group list. Co-authored-by: Cursor --- .../src/app/shell/model-menu-panel.test.tsx | 46 +++++++++++++++ .../src/app/shell/model-menu-panel.tsx | 11 +++- apps/desktop/src/lib/model-options.test.ts | 41 ++++++++++++- apps/desktop/src/lib/model-options.ts | 58 +++++++++++++++++++ 4 files changed, 154 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/app/shell/model-menu-panel.test.tsx b/apps/desktop/src/app/shell/model-menu-panel.test.tsx index 3a7d0ffda7..39e11b6640 100644 --- a/apps/desktop/src/app/shell/model-menu-panel.test.tsx +++ b/apps/desktop/src/app/shell/model-menu-panel.test.tsx @@ -399,4 +399,50 @@ describe('ModelMenuPanel provider collapse', () => { expect($collapsedProviders.get()).toContain('google') expect($collapsedProviders.get()).toContain('deepseek') }) + + it('switches the session model when Refresh Models drops the current pick', async () => { + $currentProvider.set('zhipu') + $currentModel.set('glm-4.5-air') + getGlobalModelOptions + .mockResolvedValueOnce({ + model: 'glm-4.5-air', + provider: 'zhipu', + providers: [{ models: ['glm-4.5-air', 'glm-5-turbo'], name: '智谱2', slug: 'zhipu' }, MOA_PROVIDER] + }) + .mockResolvedValueOnce({ + model: 'glm-4.5-air', + provider: 'zhipu', + providers: [DEEPSEEK_PROVIDER, MOA_PROVIDER] + }) + + const { content, onSelectModel } = renderPanel() + + await content.findByText(/Glm 4\.5 Air/i) + + fireEvent.click(await content.findByText('Refresh Models')) + + await vi.waitFor(() => { + expect(onSelectModel).toHaveBeenCalledWith({ + model: 'deepseek-v4-pro', + provider: 'deepseek', + sessionId: 'runtime-1' + }) + }) + }) + + it('does not switch when Refresh Models still lists the current pick', async () => { + $currentProvider.set('deepseek') + $currentModel.set('deepseek-v4-pro') + getGlobalModelOptions.mockResolvedValue({ providers: MOCK_PROVIDERS }) + + const { content, onSelectModel } = renderPanel() + + await content.findByText(/Deepseek V4 Pro/i) + fireEvent.click(await content.findByText('Refresh Models')) + + await vi.waitFor(() => { + expect(getGlobalModelOptions).toHaveBeenCalledTimes(2) + }) + expect(onSelectModel).not.toHaveBeenCalled() + }) }) diff --git a/apps/desktop/src/app/shell/model-menu-panel.tsx b/apps/desktop/src/app/shell/model-menu-panel.tsx index c65282ffca..7e6fa4ab45 100644 --- a/apps/desktop/src/app/shell/model-menu-panel.tsx +++ b/apps/desktop/src/app/shell/model-menu-panel.tsx @@ -7,7 +7,7 @@ import { Codicon } from '@/components/ui/codicon' import { DropdownMenuItem, dropdownMenuRow } from '@/components/ui/dropdown-menu' import type { HermesGateway } from '@/hermes' import { useI18n } from '@/i18n' -import { modelOptionsQueryKey, requestModelOptions } from '@/lib/model-options' +import { modelOptionsQueryKey, reconcileSelectionAfterCatalogRefresh, requestModelOptions } from '@/lib/model-options' import { currentPickerSelection } from '@/lib/model-status-label' import { DEFAULT_REASONING_EFFORT } from '@/lib/reasoning-effort' import { cn } from '@/lib/utils' @@ -105,6 +105,15 @@ export function ModelMenuPanel({ gateway, onSelectModel, profile = 'default', re }) queryClient.setQueryData(queryKey, next) + + // Group / credential swaps can return a catalog that no longer contains + // the session's current model. The store + currentPickerSelection would + // otherwise keep painting the stale id (it is not in the new list). + const switchTo = reconcileSelectionAfterCatalogRefresh(optionsModel, next.providers) + + if (switchTo) { + await onSelectModel({ ...switchTo, sessionId: activeSessionId || null }) + } } catch { // Network/backend hiccup — fall back to a plain invalidate so the next // open re-fetches (still cached, but no worse than before). diff --git a/apps/desktop/src/lib/model-options.test.ts b/apps/desktop/src/lib/model-options.test.ts index 144013f215..41a634feae 100644 --- a/apps/desktop/src/lib/model-options.test.ts +++ b/apps/desktop/src/lib/model-options.test.ts @@ -2,7 +2,14 @@ import { afterEach, describe, expect, it, vi } from 'vitest' import { getGlobalModelOptions } from '@/hermes' -import { manualPickRemoved, modelOptionsQueryKey, requestModelOptions } from './model-options' +import { + firstSelectableCatalogModel, + manualPickRemoved, + modelOptionsQueryKey, + reconcileSelectionAfterCatalogRefresh, + requestModelOptions, + selectionInCatalog +} from './model-options' const globalOptions = { model: 'hermes-4', provider: 'nous', providers: [] } @@ -212,3 +219,35 @@ describe('manualPickRemoved', () => { expect(manualPickRemoved(providers, '', '')).toBe(false) }) }) + +describe('reconcileSelectionAfterCatalogRefresh', () => { + const zhipu = { name: '智谱2', slug: 'zhipu', models: ['glm-4.5-air', 'glm-5-turbo'] } + const bytea = { + name: '字节A', + slug: 'byteplus', + models: ['deepseek-v4-flash', 'doubao-seed-2.0-pro'] + } + const moa = { name: 'Mixture of Agents', slug: 'moa', models: ['default'] } + + it('switches to the first new-group model when the current pick is gone', () => { + expect(selectionInCatalog([bytea], 'glm-4.5-air')).toBe(false) + expect(firstSelectableCatalogModel([moa, bytea])).toEqual({ + model: 'deepseek-v4-flash', + provider: 'byteplus' + }) + expect(reconcileSelectionAfterCatalogRefresh('glm-4.5-air', [moa, bytea])).toEqual({ + model: 'deepseek-v4-flash', + provider: 'byteplus' + }) + }) + + it('keeps the current pick when it is still in the refreshed catalog', () => { + expect(reconcileSelectionAfterCatalogRefresh('glm-4.5-air', [zhipu, moa])).toBeNull() + }) + + it('does not wipe the pick when the refreshed catalog has no selectable models', () => { + expect(reconcileSelectionAfterCatalogRefresh('glm-4.5-air', [moa])).toBeNull() + expect(reconcileSelectionAfterCatalogRefresh('glm-4.5-air', [])).toBeNull() + expect(reconcileSelectionAfterCatalogRefresh('glm-4.5-air', undefined)).toBeNull() + }) +}) diff --git a/apps/desktop/src/lib/model-options.ts b/apps/desktop/src/lib/model-options.ts index d736402d7b..e2c72cb25a 100644 --- a/apps/desktop/src/lib/model-options.ts +++ b/apps/desktop/src/lib/model-options.ts @@ -34,6 +34,64 @@ export function manualPickRemoved( return !models.includes(model) } +const MOA_PROVIDER_SLUG = 'moa' + +/** True when `model` appears in any provider's live list. Used after Refresh + * Models so a group/catalog swap can tell "still offered" from "gone". */ +export function selectionInCatalog(providers: ModelOptionProvider[] | undefined, model: string): boolean { + if (!providers?.length || !model) { + return false + } + + return providers.some(provider => (provider.models ?? []).includes(model)) +} + +/** First real (non-MoA) catalog row that still has models. */ +export function firstSelectableCatalogModel( + providers: ModelOptionProvider[] | undefined +): { model: string; provider: string } | null { + if (!providers?.length) { + return null + } + + for (const provider of providers) { + if (provider.slug === MOA_PROVIDER_SLUG) { + continue + } + + const model = provider.models?.[0] + + if (model) { + return { model, provider: provider.slug } + } + } + + return null +} + +/** + * After Refresh Models replaces the catalog: keep the current pick when it is + * still listed; otherwise switch to the first available model in the new + * catalog. Returns null when the catalog is empty/unloaded so we never wipe + * a selection on a failed or still-hydrating refresh. + */ +export function reconcileSelectionAfterCatalogRefresh( + currentModel: string, + providers: ModelOptionProvider[] | undefined +): { model: string; provider: string } | null { + const next = firstSelectableCatalogModel(providers) + + if (!next) { + return null + } + + if (selectionInCatalog(providers, currentModel)) { + return null + } + + return next +} + interface ModelOptionsRequest { /** When false, include ambient/unconfigured providers (onboarding/setup * surfaces). Chat pickers default to true so only explicitly configured