forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: optimize router model fetching with single-provider filtering #8956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
src/core/webview/__tests__/webviewMessageHandler.routerModels.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest" | ||
| import { webviewMessageHandler } from "../webviewMessageHandler" | ||
| import type { ClineProvider } from "../ClineProvider" | ||
|
|
||
| // Mock vscode (minimal) | ||
| vi.mock("vscode", () => ({ | ||
| window: { | ||
| showErrorMessage: vi.fn(), | ||
| showWarningMessage: vi.fn(), | ||
| showInformationMessage: vi.fn(), | ||
| }, | ||
| workspace: { | ||
| workspaceFolders: undefined, | ||
| getConfiguration: vi.fn(() => ({ | ||
| get: vi.fn(), | ||
| update: vi.fn(), | ||
| })), | ||
| }, | ||
| env: { | ||
| clipboard: { writeText: vi.fn() }, | ||
| openExternal: vi.fn(), | ||
| }, | ||
| commands: { | ||
| executeCommand: vi.fn(), | ||
| }, | ||
| Uri: { | ||
| parse: vi.fn((s: string) => ({ toString: () => s })), | ||
| file: vi.fn((p: string) => ({ fsPath: p })), | ||
| }, | ||
| ConfigurationTarget: { | ||
| Global: 1, | ||
| Workspace: 2, | ||
| WorkspaceFolder: 3, | ||
| }, | ||
| })) | ||
|
|
||
| // Mock modelCache getModels/flushModels used by the handler | ||
| const getModelsMock = vi.fn() | ||
| vi.mock("../../../api/providers/fetchers/modelCache", () => ({ | ||
| getModels: (...args: any[]) => getModelsMock(...args), | ||
| flushModels: vi.fn(), | ||
| })) | ||
|
|
||
| describe("webviewMessageHandler - requestRouterModels provider filter", () => { | ||
| let mockProvider: ClineProvider & { | ||
| postMessageToWebview: ReturnType<typeof vi.fn> | ||
| getState: ReturnType<typeof vi.fn> | ||
| contextProxy: any | ||
| log: ReturnType<typeof vi.fn> | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
|
|
||
| mockProvider = { | ||
| // Only methods used by this code path | ||
| postMessageToWebview: vi.fn(), | ||
| getState: vi.fn().mockResolvedValue({ apiConfiguration: {} }), | ||
| contextProxy: { | ||
| getValue: vi.fn(), | ||
| setValue: vi.fn(), | ||
| globalStorageUri: { fsPath: "/mock/storage" }, | ||
| }, | ||
| log: vi.fn(), | ||
| } as any | ||
|
|
||
| // Default mock: return distinct model maps per provider so we can verify keys | ||
| getModelsMock.mockImplementation(async (options: any) => { | ||
| switch (options?.provider) { | ||
| case "roo": | ||
| return { "roo/sonnet": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "openrouter": | ||
| return { "openrouter/qwen2.5": { contextWindow: 32768, supportsPromptCache: false } } | ||
| case "requesty": | ||
| return { "requesty/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "deepinfra": | ||
| return { "deepinfra/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "glama": | ||
| return { "glama/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "unbound": | ||
| return { "unbound/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "vercel-ai-gateway": | ||
| return { "vercel/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "io-intelligence": | ||
| return { "io/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| case "litellm": | ||
| return { "litellm/model": { contextWindow: 8192, supportsPromptCache: false } } | ||
| default: | ||
| return {} | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| it("fetches only requested provider when values.provider is present ('roo')", async () => { | ||
| await webviewMessageHandler( | ||
| mockProvider as any, | ||
| { | ||
| type: "requestRouterModels", | ||
| values: { provider: "roo" }, | ||
| } as any, | ||
| ) | ||
|
|
||
| // Should post a single routerModels message | ||
| expect(mockProvider.postMessageToWebview).toHaveBeenCalledWith( | ||
| expect.objectContaining({ type: "routerModels", routerModels: expect.any(Object) }), | ||
| ) | ||
|
|
||
| const call = (mockProvider.postMessageToWebview as any).mock.calls.find( | ||
| (c: any[]) => c[0]?.type === "routerModels", | ||
| ) | ||
| expect(call).toBeTruthy() | ||
| const payload = call[0] | ||
| const routerModels = payload.routerModels as Record<string, Record<string, any>> | ||
|
|
||
| // Only "roo" key should be present | ||
| const keys = Object.keys(routerModels) | ||
| expect(keys).toEqual(["roo"]) | ||
| expect(Object.keys(routerModels.roo || {})).toContain("roo/sonnet") | ||
|
|
||
| // getModels should have been called exactly once for roo | ||
| const providersCalled = getModelsMock.mock.calls.map((c: any[]) => c[0]?.provider) | ||
| expect(providersCalled).toEqual(["roo"]) | ||
| }) | ||
|
|
||
| it("defaults to aggregate fetching when no provider filter is sent", async () => { | ||
| await webviewMessageHandler( | ||
| mockProvider as any, | ||
| { | ||
| type: "requestRouterModels", | ||
| } as any, | ||
| ) | ||
|
|
||
| const call = (mockProvider.postMessageToWebview as any).mock.calls.find( | ||
| (c: any[]) => c[0]?.type === "routerModels", | ||
| ) | ||
| expect(call).toBeTruthy() | ||
| const routerModels = call[0].routerModels as Record<string, Record<string, any>> | ||
|
|
||
| // Aggregate handler initializes many known routers - ensure a few expected keys exist | ||
| expect(routerModels).toHaveProperty("openrouter") | ||
| expect(routerModels).toHaveProperty("roo") | ||
| expect(routerModels).toHaveProperty("requesty") | ||
| }) | ||
|
|
||
| it("supports filtering another single provider ('openrouter')", async () => { | ||
| await webviewMessageHandler( | ||
| mockProvider as any, | ||
| { | ||
| type: "requestRouterModels", | ||
| values: { provider: "openrouter" }, | ||
| } as any, | ||
| ) | ||
|
|
||
| const call = (mockProvider.postMessageToWebview as any).mock.calls.find( | ||
| (c: any[]) => c[0]?.type === "routerModels", | ||
| ) | ||
| expect(call).toBeTruthy() | ||
| const routerModels = call[0].routerModels as Record<string, Record<string, any>> | ||
| const keys = Object.keys(routerModels) | ||
|
|
||
| expect(keys).toEqual(["openrouter"]) | ||
| expect(Object.keys(routerModels.openrouter || {})).toContain("openrouter/qwen2.5") | ||
|
|
||
| const providersCalled = getModelsMock.mock.calls.map((c: any[]) => c[0]?.provider) | ||
| expect(providersCalled).toEqual(["openrouter"]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical bug: The backend never includes the provider in the response values, so
msgProviderwill always beundefined. When a provider filter is used (e.g.,provider="roo"), the checkprovider !== msgProviderevaluates to"roo" !== undefined, which is alwaystrue, causing the response to be ignored indefinitely. The promise will timeout after 10 seconds, breaking router model fetching for all dynamic providers. The backend needs to include the provider in the response:provider.postMessageToWebview({ type: "routerModels", routerModels, values: { provider: requestedProvider } })