Skip to content

Commit b3ff693

Browse files
committed
fix(webview): refresh shared LiteLLM model cache
1 parent c8e3c3c commit b3ff693

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

‎webview-ui/src/components/settings/providers/LiteLLM.tsx‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ export const LiteLLM = ({
5858
if (refreshStatus === "loading") {
5959
if (!litellmErrorJustReceived.current) {
6060
setRefreshStatus("success")
61-
// Invalidate only the LiteLLM router-models query so useSelectedModel
62-
// picks up the refreshed list. useSelectedModel reads LiteLLM under the
63-
// compound key ["routerModels", providerIdentifiers.litellm] (see useRouterModels), so we
64-
// target that exact key rather than the bare ["routerModels"] prefix,
65-
// which would needlessly invalidate every other provider's query too.
61+
// Refresh the provider-scoped cache used by useSelectedModel and the shared cache used by
62+
// ApiOptions. Target both exact keys rather than the bare ["routerModels"] prefix, which
63+
// would needlessly invalidate every other provider's query too.
6664
queryClient.invalidateQueries({ queryKey: ["routerModels", providerIdentifiers.litellm] })
65+
queryClient.invalidateQueries({ queryKey: ["routerModels", "all"] })
6766
}
6867
// If litellmErrorJustReceived.current is true, status is already (or will be) "error".
6968
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
2+
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react"
3+
4+
import { providerIdentifiers, type OrganizationAllowList, type ProviderSettings } from "@roo-code/types"
5+
6+
import { LiteLLM } from "../LiteLLM"
7+
8+
const { postMessageMock, mockUseExtensionState } = vi.hoisted(() => ({
9+
postMessageMock: vi.fn(),
10+
mockUseExtensionState: vi.fn(),
11+
}))
12+
13+
vi.mock("@src/utils/vscode", () => ({
14+
vscode: { postMessage: postMessageMock },
15+
}))
16+
17+
vi.mock("@src/context/ExtensionStateContext", () => ({
18+
useExtensionState: mockUseExtensionState,
19+
}))
20+
21+
vi.mock("@src/i18n/TranslationContext", () => ({
22+
useAppTranslation: () => ({ t: (key: string) => key }),
23+
}))
24+
25+
vi.mock("@vscode/webview-ui-toolkit/react", () => ({
26+
VSCodeTextField: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
27+
VSCodeCheckbox: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
28+
}))
29+
30+
vi.mock("@src/components/ui", () => ({
31+
Button: ({ children, onClick, disabled }: React.ComponentProps<"button">) => (
32+
<button data-testid="refresh-button" onClick={onClick} disabled={disabled}>
33+
{children}
34+
</button>
35+
),
36+
}))
37+
38+
vi.mock("../../ModelPicker", () => ({
39+
ModelPicker: () => <div data-testid="model-picker" />,
40+
}))
41+
42+
describe("LiteLLM", () => {
43+
const organizationAllowList: OrganizationAllowList = { allowAll: true, providers: {} }
44+
45+
beforeEach(() => {
46+
vi.clearAllMocks()
47+
mockUseExtensionState.mockReturnValue({ routerModels: { [providerIdentifiers.litellm]: {} } })
48+
})
49+
50+
it("invalidates both LiteLLM caches after a successful model refresh", async () => {
51+
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
52+
const invalidateQueries = vi.spyOn(queryClient, "invalidateQueries")
53+
const apiConfiguration: ProviderSettings = {
54+
apiProvider: providerIdentifiers.litellm,
55+
litellmApiKey: "test-key",
56+
litellmBaseUrl: "http://localhost:4000",
57+
}
58+
59+
render(
60+
<QueryClientProvider client={queryClient}>
61+
<LiteLLM
62+
apiConfiguration={apiConfiguration}
63+
setApiConfigurationField={vi.fn()}
64+
organizationAllowList={organizationAllowList}
65+
/>
66+
</QueryClientProvider>,
67+
)
68+
69+
fireEvent.click(screen.getByTestId("refresh-button"))
70+
act(() => {
71+
window.dispatchEvent(new MessageEvent("message", { data: { type: "routerModels" } }))
72+
})
73+
74+
await waitFor(() => {
75+
expect(invalidateQueries).toHaveBeenCalledWith({
76+
queryKey: ["routerModels", providerIdentifiers.litellm],
77+
})
78+
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ["routerModels", "all"] })
79+
})
80+
})
81+
})

0 commit comments

Comments
 (0)