Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/core/webview/__tests__/webviewMessageHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,63 @@ describe("webviewMessageHandler - requestOllamaModels", () => {
})
})

describe("webviewMessageHandler - requestLiteLLMModels", () => {
beforeEach(() => {
vi.clearAllMocks()
mockClineProvider.getState = vi.fn().mockResolvedValue({
apiConfiguration: {
litellmApiKey: "test-key",
litellmBaseUrl: "http://localhost:4000",
},
})
})

it("successfully fetches models from LiteLLM", async () => {
const mockModels: ModelRecord = {
"model-1": {
maxTokens: 4096,
contextWindow: 8192,
supportsPromptCache: false,
description: "Test model 1",
},
"model-2": {
maxTokens: 8192,
contextWindow: 16384,
supportsPromptCache: false,
description: "Test model 2",
},
}

mockGetModels.mockResolvedValue(mockModels)

await webviewMessageHandler(mockClineProvider, {
type: "requestLiteLLMModels",
})

expect(mockGetModels).toHaveBeenCalledWith({
provider: "litellm",
apiKey: "test-key",
baseUrl: "http://localhost:4000",
})

expect(mockClineProvider.postMessageToWebview).toHaveBeenCalledWith({
type: "litellmModels",
litellmModels: mockModels,
})
})

it("silently fails when LiteLLM is not configured", async () => {
mockGetModels.mockRejectedValue(new Error("Failed to connect"))

await webviewMessageHandler(mockClineProvider, {
type: "requestLiteLLMModels",
})

// Should not call postMessageToWebview on error
expect(mockClineProvider.postMessageToWebview).not.toHaveBeenCalled()
})
})

describe("webviewMessageHandler - requestRouterModels", () => {
beforeEach(() => {
vi.clearAllMocks()
Expand Down
31 changes: 31 additions & 0 deletions src/core/webview/webviewMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,37 @@ export const webviewMessageHandler = async (
}
break
}
case "requestLiteLLMModels": {
// Specific handler for LiteLLM models only.
const { apiConfiguration: litellmApiConfig } = await provider.getState()
try {
// Flush cache and refresh to ensure fresh models.
await flushModels("litellm", true)

// Ensure both apiKey and baseUrl are defined before fetching
if (!litellmApiConfig.litellmApiKey || !litellmApiConfig.litellmBaseUrl) {
console.debug("LiteLLM models fetch failed: Missing API key or base URL")
break
}

const litellmModels = await getModels({
provider: "litellm",
apiKey: litellmApiConfig.litellmApiKey,
baseUrl: litellmApiConfig.litellmBaseUrl,
})

if (Object.keys(litellmModels).length > 0) {
provider.postMessageToWebview({
type: "litellmModels",
litellmModels: litellmModels,
})
}
} catch (error) {
// Silently fail - user hasn't configured LiteLLM yet.
console.debug("LiteLLM models fetch failed:", error)
}
break
}
case "requestRooModels": {
// Specific handler for Roo models only - flushes cache to ensure fresh auth token is used
try {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/ExtensionMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface ExtensionMessage {
| "openAiModels"
| "ollamaModels"
| "lmStudioModels"
| "litellmModels"
| "vsCodeLmModels"
| "huggingFaceModels"
| "sapAiCoreModels" // kilocode_change
Expand Down Expand Up @@ -244,6 +245,7 @@ export interface ExtensionMessage {
openAiModels?: string[]
ollamaModels?: ModelRecord
lmStudioModels?: ModelRecord
litellmModels?: ModelRecord
vsCodeLmModels?: { vendor?: string; family?: string; version?: string; id?: string }[]
huggingFaceModels?: Array<{
id: string
Expand Down
1 change: 1 addition & 0 deletions src/shared/WebviewMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export interface WebviewMessage {
| "requestOpenAiModels"
| "requestOllamaModels"
| "requestLmStudioModels"
| "requestLiteLLMModels"
| "requestRooModels"
| "requestRooCreditBalance"
| "requestVsCodeLmModels"
Expand Down