Skip to content

Commit eff47f7

Browse files
committed
ollama fetch fix
1 parent 13c803b commit eff47f7

5 files changed

Lines changed: 294 additions & 26 deletions

File tree

‎src/core/webview/__tests__/webviewMessageHandler.spec.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,70 @@ describe("webviewMessageHandler - requestOllamaModels", () => {
331331
ollamaModels: mockModels,
332332
})
333333
})
334+
335+
it("posts empty models response when no models are found", async () => {
336+
mockGetModels.mockResolvedValue({})
337+
338+
await webviewMessageHandler(mockClineProvider, {
339+
type: "requestOllamaModels",
340+
})
341+
342+
expect(mockClineProvider.postMessageToWebview).toHaveBeenCalledWith({
343+
type: "ollamaModels",
344+
ollamaModels: {},
345+
})
346+
})
347+
348+
it("posts empty models response with error message and logs to output on fetch failure", async () => {
349+
mockGetModels.mockRejectedValue(new Error("Connection refused"))
350+
351+
await webviewMessageHandler(mockClineProvider, {
352+
type: "requestOllamaModels",
353+
})
354+
355+
expect(mockClineProvider.postMessageToWebview).toHaveBeenCalledWith({
356+
type: "ollamaModels",
357+
ollamaModels: {},
358+
error: "Connection refused",
359+
})
360+
361+
expect(mockClineProvider.log).toHaveBeenCalledWith(
362+
expect.stringContaining("[requestOllamaModels] Failed to fetch models: Connection refused"),
363+
)
364+
})
365+
366+
it("uses baseUrl from message values over saved state", async () => {
367+
const mockModels: ModelRecord = {
368+
"remote-model": {
369+
maxTokens: 4096,
370+
contextWindow: 8192,
371+
supportsPromptCache: false,
372+
description: "Remote model",
373+
},
374+
}
375+
376+
mockGetModels.mockResolvedValue(mockModels)
377+
378+
await webviewMessageHandler(mockClineProvider, {
379+
type: "requestOllamaModels",
380+
values: {
381+
baseUrl: "https://ollama.example.com",
382+
apiKey: "secret-key",
383+
},
384+
})
385+
386+
// Should use the URL from message values, not the saved state
387+
expect(mockGetModels).toHaveBeenCalledWith({
388+
provider: "ollama",
389+
baseUrl: "https://ollama.example.com",
390+
apiKey: "secret-key",
391+
})
392+
393+
expect(mockClineProvider.postMessageToWebview).toHaveBeenCalledWith({
394+
type: "ollamaModels",
395+
ollamaModels: mockModels,
396+
})
397+
})
334398
})
335399

336400
describe("webviewMessageHandler - requestRouterModels", () => {

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,23 +1203,38 @@ export const webviewMessageHandler = async (
12031203
case "requestOllamaModels": {
12041204
// Specific handler for Ollama models only.
12051205
const { apiConfiguration: ollamaApiConfig } = await provider.getState()
1206+
// Prefer the baseUrl/apiKey from the message values (which reflect
1207+
// the user's unsaved edits in the settings form) over the saved
1208+
// state, so the refresh uses the URL the user is actually looking
1209+
// at — not the stale one from before they started editing.
1210+
const baseUrl = message.values?.baseUrl ?? ollamaApiConfig.ollamaBaseUrl
1211+
const apiKey = message.values?.apiKey ?? ollamaApiConfig.ollamaApiKey
12061212
try {
12071213
const ollamaOptions = {
12081214
provider: "ollama" as const,
1209-
baseUrl: ollamaApiConfig.ollamaBaseUrl,
1210-
apiKey: ollamaApiConfig.ollamaApiKey,
1215+
baseUrl,
1216+
apiKey,
12111217
}
12121218
// Flush cache and refresh to ensure fresh models.
12131219
await flushModels(ollamaOptions, true)
12141220

12151221
const ollamaModels = await getModels(ollamaOptions)
12161222

1217-
if (Object.keys(ollamaModels).length > 0) {
1218-
provider.postMessageToWebview({ type: "ollamaModels", ollamaModels: ollamaModels })
1219-
}
1223+
// Always post a response so the webview refresh status can
1224+
// transition out of "loading" — even when no models are found.
1225+
provider.postMessageToWebview({ type: "ollamaModels", ollamaModels: ollamaModels })
12201226
} catch (error) {
1221-
// Silently fail - user hasn't configured Ollama yet
1222-
console.debug("Ollama models fetch failed:", error)
1227+
// Log the error to the output channel for debugging, but still
1228+
// post an empty response so the webview doesn't stay stuck in
1229+
// the loading state. Include the error message so the webview
1230+
// can display it directly in the settings panel.
1231+
const errorMsg = error instanceof Error ? error.message : String(error)
1232+
provider.log(`[requestOllamaModels] Failed to fetch models: ${errorMsg}`)
1233+
provider.postMessageToWebview({
1234+
type: "ollamaModels",
1235+
ollamaModels: {},
1236+
error: errorMsg,
1237+
})
12231238
}
12241239
break
12251240
}

‎webview-ui/src/components/settings/ApiOptions.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,13 @@ const ApiOptions = ({
218218
},
219219
})
220220
} else if (selectedProvider === "ollama") {
221-
vscode.postMessage({ type: "requestOllamaModels" })
221+
vscode.postMessage({
222+
type: "requestOllamaModels",
223+
values: {
224+
baseUrl: apiConfiguration?.ollamaBaseUrl,
225+
apiKey: apiConfiguration?.ollamaApiKey,
226+
},
227+
})
222228
} else if (selectedProvider === "lmstudio") {
223229
requestLmStudioModels(apiConfiguration?.lmStudioBaseUrl)
224230
} else if (selectedProvider === "vscode-lm") {

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

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useState, useCallback, useMemo, useEffect } from "react"
2-
import { useEvent } from "react-use"
32
import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
43
import { Checkbox } from "vscrui"
54

65
import { type ProviderSettings, type ExtensionMessage, type ModelRecord, ollamaDefaultModelInfo } from "@roo-code/types"
76

87
import { useAppTranslation } from "@src/i18n/TranslationContext"
98
import { useRouterModels } from "@src/components/ui/hooks/useRouterModels"
9+
import { Button } from "@src/components/ui"
1010
import { vscode } from "@src/utils/vscode"
1111

1212
import { inputEventTransform } from "../transforms"
@@ -22,6 +22,8 @@ export const Ollama = ({ apiConfiguration, setApiConfigurationField }: OllamaPro
2222
const { t } = useAppTranslation()
2323

2424
const [ollamaModels, setOllamaModels] = useState<ModelRecord>({})
25+
const [refreshStatus, setRefreshStatus] = useState<"idle" | "loading" | "success" | "error">("idle")
26+
const [refreshError, setRefreshError] = useState<string | undefined>()
2527
const routerModels = useRouterModels()
2628

2729
const handleInputChange = useCallback(
@@ -35,20 +37,42 @@ export const Ollama = ({ apiConfiguration, setApiConfigurationField }: OllamaPro
3537
[setApiConfigurationField],
3638
)
3739

38-
const onMessage = useCallback((event: MessageEvent) => {
39-
const message: ExtensionMessage = event.data
40+
useEffect(() => {
41+
const handleMessage = (event: MessageEvent) => {
42+
const message: ExtensionMessage = event.data
43+
44+
if (message.type === "ollamaModels") {
45+
const newModels = message.ollamaModels ?? {}
46+
setOllamaModels(newModels)
4047

41-
switch (message.type) {
42-
case "ollamaModels":
43-
{
44-
const newModels = message.ollamaModels ?? {}
45-
setOllamaModels(newModels)
48+
if (refreshStatus === "loading") {
49+
if (Object.keys(newModels).length > 0) {
50+
setRefreshStatus("success")
51+
} else {
52+
setRefreshStatus("error")
53+
setRefreshError(message.error)
54+
}
4655
}
47-
break
56+
}
4857
}
49-
}, [])
5058

51-
useEvent("message", onMessage)
59+
window.addEventListener("message", handleMessage)
60+
return () => {
61+
window.removeEventListener("message", handleMessage)
62+
}
63+
}, [refreshStatus])
64+
65+
const handleRefreshModels = useCallback(() => {
66+
setRefreshStatus("loading")
67+
setRefreshError(undefined)
68+
vscode.postMessage({
69+
type: "requestOllamaModels",
70+
values: {
71+
baseUrl: apiConfiguration?.ollamaBaseUrl,
72+
apiKey: apiConfiguration?.ollamaApiKey,
73+
},
74+
})
75+
}, [apiConfiguration?.ollamaBaseUrl, apiConfiguration?.ollamaApiKey])
5276

5377
// Refresh models on mount
5478
useEffect(() => {
@@ -102,6 +126,33 @@ export const Ollama = ({ apiConfiguration, setApiConfigurationField }: OllamaPro
102126
</div>
103127
</VSCodeTextField>
104128
)}
129+
<Button
130+
variant="outline"
131+
onClick={handleRefreshModels}
132+
disabled={refreshStatus === "loading"}
133+
className="w-full">
134+
<div className="flex items-center gap-2">
135+
{refreshStatus === "loading" ? (
136+
<span className="codicon codicon-loading codicon-modifier-spin" />
137+
) : (
138+
<span className="codicon codicon-refresh" />
139+
)}
140+
{t("settings:providers.refreshModels.label")}
141+
</div>
142+
</Button>
143+
{refreshStatus === "loading" && (
144+
<div className="text-sm text-vscode-descriptionForeground">
145+
{t("settings:providers.refreshModels.loading")}
146+
</div>
147+
)}
148+
{refreshStatus === "success" && (
149+
<div className="text-sm text-vscode-foreground">{t("settings:providers.refreshModels.success")}</div>
150+
)}
151+
{refreshStatus === "error" && (
152+
<div className="text-sm text-vscode-errorForeground">
153+
{refreshError || t("settings:providers.refreshModels.error")}
154+
</div>
155+
)}
105156
<ModelPicker
106157
apiConfiguration={apiConfiguration}
107158
setApiConfigurationField={setApiConfigurationField}

0 commit comments

Comments
 (0)