Skip to content

Commit ddedab1

Browse files
committed
fix(settings): preserve configured LiteLLM model ID in model picker
The LiteLLM case in useSelectedModel validated the configured model ID against the fetched /models list and silently substituted the hardcoded default (claude-3-7-sonnet-20250219) whenever the configured ID was absent. LiteLLM is a proxy that fronts arbitrary models and aliases, so a configured ID is the user's explicit selection even when it is not in the fetched list (custom aliases, incomplete or stale listings, renamed deployments). On the settings screen the picker reverted to the default after every selection, making the model ID appear unchangeable; the saved custom ID was also not displayed after reopening settings. Only fall back to the default when nothing is configured and a populated list exists; keep the empty-ID behavior for the empty-list case. Adds a hook-level regression test and a ModelPicker component test covering the full user flow (open picker, use-custom-model, re-render with updated config).
1 parent 78c712a commit ddedab1

3 files changed

Lines changed: 154 additions & 20 deletions

File tree

‎webview-ui/src/components/settings/__tests__/ModelPicker.spec.tsx‎

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33
import { screen, fireEvent, renderWithExtensionState } from "@/utils/test-utils"
44
import { act } from "react"
55
import { QueryClient } from "@tanstack/react-query"
6+
import { type Mock } from "vitest"
67

7-
import { ModelInfo, providerIdentifiers } from "@roo-code/types"
8+
import { litellmDefaultModelId, ModelInfo, providerIdentifiers } from "@roo-code/types"
89

910
import { ModelPicker } from "../ModelPicker"
11+
import { useRouterModels } from "@src/components/ui/hooks/useRouterModels"
1012

1113
vi.mock("@src/context/ExtensionStateContext", () => ({
1214
ExtensionStateContextProvider: ({ children }: any) => children,
1315
useExtensionState: vi.fn(),
1416
}))
1517

18+
vi.mock("@src/components/ui/hooks/useRouterModels")
19+
20+
const mockUseRouterModels = useRouterModels as Mock<typeof useRouterModels>
21+
1622
Element.prototype.scrollIntoView = vi.fn()
1723

1824
describe("ModelPicker", () => {
@@ -55,6 +61,8 @@ describe("ModelPicker", () => {
5561
beforeEach(() => {
5662
vi.clearAllMocks()
5763
vi.useFakeTimers()
64+
// Default: no router models available. Provider-specific tests override per test.
65+
mockUseRouterModels.mockReturnValue({ data: {}, isLoading: false, isError: false } as any)
5866
})
5967

6068
afterEach(() => {
@@ -254,4 +262,96 @@ describe("ModelPicker", () => {
254262
expect(screen.getByTestId("automatic-fetch-hint")).toBeInTheDocument()
255263
})
256264
})
265+
266+
describe("LiteLLM custom model selection", () => {
267+
const litellmModels: Record<string, ModelInfo> = {
268+
"gpt-4o-mini": { description: "LiteLLM proxy model", ...modelInfo },
269+
}
270+
271+
const renderLiteLLMPicker = (
272+
apiConfiguration: Record<string, unknown>,
273+
setField: (field: string, value: unknown) => void,
274+
) =>
275+
renderWithExtensionState(
276+
<ModelPicker
277+
apiConfiguration={apiConfiguration as never}
278+
defaultModelId={litellmDefaultModelId}
279+
models={litellmModels}
280+
modelIdKey="litellmModelId"
281+
serviceName="LiteLLM"
282+
serviceUrl="https://docs.litellm.ai/"
283+
setApiConfigurationField={setField as never}
284+
organizationAllowList={{ allowAll: true, providers: {} }}
285+
/>,
286+
{ queryClient },
287+
)
288+
289+
beforeEach(() => {
290+
mockUseRouterModels.mockReturnValue({
291+
data: { litellm: litellmModels },
292+
isLoading: false,
293+
isError: false,
294+
} as any)
295+
})
296+
297+
it("keeps a custom model ID in the picker instead of reverting to the default", async () => {
298+
// Regression: on the LiteLLM settings screen the user could not change the
299+
// model ID to a value absent from the fetched /models list -- the picker
300+
// silently reverted to the hardcoded default model after the selection.
301+
const customModelId = "my-litellm-alias"
302+
let apiConfiguration: Record<string, unknown> = { apiProvider: providerIdentifiers.litellm }
303+
const setField = vi.fn((field: string, value: unknown) => {
304+
apiConfiguration = { ...apiConfiguration, [field]: value }
305+
})
306+
307+
const { rerender } = await act(async () => {
308+
return renderLiteLLMPicker(apiConfiguration, setField)
309+
})
310+
311+
// Before any selection the picker shows the provider default.
312+
expect(screen.getByTestId("model-picker-button")).toHaveTextContent(litellmDefaultModelId)
313+
314+
// Open the popover and type a model ID that is not in the fetched list.
315+
await act(async () => {
316+
fireEvent.click(screen.getByTestId("model-picker-button"))
317+
})
318+
await act(async () => {
319+
vi.advanceTimersByTime(100)
320+
})
321+
await act(async () => {
322+
fireEvent.input(screen.getByTestId("model-input"), { target: { value: customModelId } })
323+
})
324+
await act(async () => {
325+
vi.advanceTimersByTime(100)
326+
})
327+
await act(async () => {
328+
fireEvent.click(screen.getByTestId("use-custom-model"))
329+
})
330+
await act(async () => {
331+
vi.advanceTimersByTime(100)
332+
})
333+
334+
expect(setField).toHaveBeenCalledWith("litellmModelId", customModelId)
335+
336+
// Re-render with the updated configuration (as SettingsView does after the
337+
// setter runs) and assert the selection is kept, not reset to the default.
338+
await act(async () => {
339+
rerender(
340+
<ModelPicker
341+
apiConfiguration={apiConfiguration as never}
342+
defaultModelId={litellmDefaultModelId}
343+
models={litellmModels}
344+
modelIdKey="litellmModelId"
345+
serviceName="LiteLLM"
346+
serviceUrl="https://docs.litellm.ai/"
347+
setApiConfigurationField={setField as never}
348+
organizationAllowList={{ allowAll: true, providers: {} }}
349+
/>,
350+
)
351+
})
352+
353+
expect(screen.getByTestId("model-picker-button")).toHaveTextContent(customModelId)
354+
expect(screen.getByTestId("model-picker-button")).not.toHaveTextContent(litellmDefaultModelId)
355+
})
356+
})
257357
})

‎webview-ui/src/components/ui/hooks/__tests__/useSelectedModel.spec.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,12 @@ describe("useSelectedModel", () => {
899899
expect(result.current.id).toBe("my-custom-model")
900900
})
901901

902-
it("should use litellmDefaultModelInfo when selected model not found in routerModels", () => {
902+
it("preserves a configured model ID that is absent from the populated list", () => {
903+
// Regression: LiteLLM is a proxy whose users may configure aliases or models
904+
// that the fetched /models list does not include (custom aliases, incomplete or
905+
// stale listings). The configured ID is the user's explicit selection and must
906+
// not be replaced by a hardcoded default, which made the settings screen appear
907+
// to ignore model ID changes.
903908
mockUseRouterModels.mockReturnValue({
904909
data: {
905910
openrouter: {},
@@ -926,9 +931,40 @@ describe("useSelectedModel", () => {
926931
const { result } = renderHook(() => useSelectedModel(apiConfiguration), { wrapper })
927932

928933
expect(result.current.provider).toBe(providerIdentifiers.litellm)
929-
// Falls back to default model ID
934+
// The configured ID is preserved even though it is absent from the fetched list
935+
expect(result.current.id).toBe("non-existing-model")
936+
// Model info falls back to litellmDefaultModelInfo since the model is not in router models
937+
expect(result.current.info).toEqual(litellmDefaultModelInfo)
938+
})
939+
940+
it("falls back to the default model ID only when nothing is configured but a list exists", () => {
941+
mockUseRouterModels.mockReturnValue({
942+
data: {
943+
openrouter: {},
944+
requesty: {},
945+
litellm: {
946+
"existing-model": {
947+
maxTokens: 4096,
948+
contextWindow: 8192,
949+
supportsImages: false,
950+
supportsPromptCache: false,
951+
},
952+
},
953+
},
954+
isLoading: false,
955+
isError: false,
956+
} as any)
957+
958+
const apiConfiguration: ProviderSettings = {
959+
apiProvider: providerIdentifiers.litellm,
960+
// litellmModelId intentionally omitted
961+
}
962+
963+
const wrapper = createWrapper()
964+
const { result } = renderHook(() => useSelectedModel(apiConfiguration), { wrapper })
965+
966+
// Nothing configured: fall back to the provider default so the picker shows a selection
930967
expect(result.current.id).toBe("claude-3-7-sonnet-20250219")
931-
// Should use litellmDefaultModelInfo as fallback since default model also not in router models
932968
expect(result.current.info).toEqual(litellmDefaultModelInfo)
933969
})
934970

‎webview-ui/src/components/ui/hooks/useSelectedModel.ts‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,20 @@ function getSelectedModel({
191191
return { id, info: routerInfo }
192192
}
193193
case providerIdentifiers.litellm: {
194-
// When the model list is empty (not yet loaded or still loading),
195-
// preserve the configured model ID. LiteLLM is a proxy with no inherent
196-
// default model, so we never substitute a hardcoded default here -- when
197-
// nothing is configured we return an empty ID so the picker shows "no
198-
// selection" rather than a phantom model that does not exist on the server.
199-
const hasModels =
200-
routerModels[providerIdentifiers.litellm] &&
201-
Object.keys(routerModels[providerIdentifiers.litellm]).length > 0
202-
const id = hasModels
203-
? getValidatedModelId(
204-
apiConfiguration.litellmModelId,
205-
routerModels[providerIdentifiers.litellm],
206-
defaultModelId,
207-
)
208-
: (apiConfiguration.litellmModelId ?? "")
209-
const routerInfo = routerModels[providerIdentifiers.litellm]?.[id]
194+
// LiteLLM is a proxy that fronts arbitrary models and aliases, so a
195+
// configured model ID is the user's explicit selection even when it is
196+
// absent from the fetched list (custom aliases, incomplete or stale
197+
// listings, renamed deployments). Never substitute a hardcoded default
198+
// over a configured ID -- doing so silently discards the user's model
199+
// choice on the settings screen. Only fall back to the default when
200+
// nothing is configured and a populated list exists; when the list is
201+
// empty we return an empty ID so the picker shows "no selection" rather
202+
// than a phantom model that does not exist on the server.
203+
const litellmModels = routerModels[providerIdentifiers.litellm]
204+
const id =
205+
apiConfiguration.litellmModelId ??
206+
(litellmModels && Object.keys(litellmModels).length > 0 ? defaultModelId : "")
207+
const routerInfo = litellmModels?.[id]
210208
return { id, info: routerInfo ?? litellmDefaultModelInfo }
211209
}
212210
case providerIdentifiers.xai: {

0 commit comments

Comments
 (0)