diff --git a/packages/types/src/vscode-extension-host.ts b/packages/types/src/vscode-extension-host.ts index fb3ec1fd21..1e900db45d 100644 --- a/packages/types/src/vscode-extension-host.ts +++ b/packages/types/src/vscode-extension-host.ts @@ -582,8 +582,8 @@ export interface WebviewMessage { // eslint-disable-next-line @typescript-eslint/no-explicit-any settings?: any url?: string // For openExternal - ollamaBaseUrl?: string // For testOllamaConnection - allows passing current value from UI - ollamaApiKey?: string // For testOllamaConnection - allows passing current value from UI + ollamaBaseUrl?: string // For testOllamaConnection and refreshOllamaModels - allows passing current value from UI + ollamaApiKey?: string // For testOllamaConnection and refreshOllamaModels - allows passing current value from UI mpItem?: MarketplaceItem mpInstallOptions?: InstallMarketplaceItemOptions // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/core/webview/webviewMessageHandler.ts b/src/core/webview/webviewMessageHandler.ts index 52dc8304fd..cd06c90494 100644 --- a/src/core/webview/webviewMessageHandler.ts +++ b/src/core/webview/webviewMessageHandler.ts @@ -1059,11 +1059,16 @@ export const webviewMessageHandler = async ( const { apiConfiguration: ollamaApiConfig } = await provider.getState() const startTime = Date.now() + // Use the baseUrl and apiKey from the message if provided (current UI values), + // otherwise fall back to saved state + const baseUrl = message.ollamaBaseUrl ?? ollamaApiConfig.ollamaBaseUrl + const apiKey = message.ollamaApiKey ?? ollamaApiConfig.ollamaApiKey + try { const ollamaOptions = { provider: "ollama" as const, - baseUrl: ollamaApiConfig.ollamaBaseUrl, - apiKey: ollamaApiConfig.ollamaApiKey, + baseUrl: baseUrl, + apiKey: apiKey, ollamaModelDiscoveryTimeout: ollamaApiConfig.ollamaModelDiscoveryTimeout, ollamaMaxRetries: ollamaApiConfig.ollamaMaxRetries, ollamaRetryDelay: ollamaApiConfig.ollamaRetryDelay, @@ -1072,16 +1077,12 @@ export const webviewMessageHandler = async ( await flushModels(ollamaOptions, true) - const result = await discoverOllamaModelsWithSorting( - ollamaApiConfig.ollamaBaseUrl, - ollamaApiConfig.ollamaApiKey, - { - modelDiscoveryTimeout: ollamaApiConfig.ollamaModelDiscoveryTimeout, - maxRetries: ollamaApiConfig.ollamaMaxRetries, - retryDelay: ollamaApiConfig.ollamaRetryDelay, - enableLogging: ollamaApiConfig.ollamaEnableLogging, - }, - ) + const result = await discoverOllamaModelsWithSorting(baseUrl, apiKey, { + modelDiscoveryTimeout: ollamaApiConfig.ollamaModelDiscoveryTimeout, + maxRetries: ollamaApiConfig.ollamaMaxRetries, + retryDelay: ollamaApiConfig.ollamaRetryDelay, + enableLogging: ollamaApiConfig.ollamaEnableLogging, + }) const durationMs = Date.now() - startTime @@ -1093,7 +1094,7 @@ export const webviewMessageHandler = async ( if (ollamaApiConfig.ollamaEnableLogging) { console.debug("[Ollama Model Refresh]", { - baseUrl: ollamaApiConfig.ollamaBaseUrl, + baseUrl: baseUrl, modelsWithTools: result.modelsWithTools.length, modelsWithoutTools: result.modelsWithoutTools.length, totalCount: result.totalCount, diff --git a/webview-ui/src/components/settings/providers/Ollama.tsx b/webview-ui/src/components/settings/providers/Ollama.tsx index ca2dfc328a..5db3d7e31b 100644 --- a/webview-ui/src/components/settings/providers/Ollama.tsx +++ b/webview-ui/src/components/settings/providers/Ollama.tsx @@ -127,8 +127,12 @@ export const Ollama = ({ apiConfiguration, setApiConfigurationField }: OllamaPro const handleRefreshModels = useCallback(() => { setRefreshingModels(true) setRefreshResult(null) - vscode.postMessage({ type: "refreshOllamaModels" }) - }, []) + vscode.postMessage({ + type: "refreshOllamaModels", + ollamaBaseUrl: apiConfiguration?.ollamaBaseUrl || "", + ollamaApiKey: apiConfiguration?.ollamaApiKey || "", + }) + }, [apiConfiguration?.ollamaBaseUrl, apiConfiguration?.ollamaApiKey]) useEffect(() => { return () => { @@ -170,6 +174,11 @@ export const Ollama = ({ apiConfiguration, setApiConfigurationField }: OllamaPro return false }, [apiConfiguration?.ollamaModelId, routerModels.data, ollamaModels]) + // Sort models with tools by name for consistent ordering + const sortedModelsWithTools = useMemo(() => { + return [...modelsWithTools].sort((a, b) => a.name.localeCompare(b.name)) + }, [modelsWithTools]) + return ( <>