forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: Add provider filtering support to router models backend #8916
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
Closed
Closed
Changes from all commits
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 providers 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.providers is present (['roo'])", async () => { | ||
| await webviewMessageHandler( | ||
| mockProvider as any, | ||
| { | ||
| type: "requestRouterModels", | ||
| values: { providers: ["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 providers 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: { providers: ["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
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.
Is there a reason for this filter to be an array instead of a single provider?
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.
In general, if we only want to fetch one at a time I think we should make a function that only fetches one and use that instead of having the conditional logic. The only reason for backwards compatibility is that we broke this into multiple PRs, right? That seems ok but I think we should make a new method for the single lookup and then delete the old one once the webview is updated. Or just do it all in one PR, idk