From 985cb51c39f641e917948ea1434dcce7e3bc4efb Mon Sep 17 00:00:00 2001 From: celestial-vault <58194240+celestial-vault@users.noreply.github.com> Date: Thu, 26 Jun 2025 22:34:14 -0700 Subject: [PATCH] refactor lmstudio (#4514) * refactor lmstudio * merge conflict deleted stuff --- .../src/components/settings/ApiOptions.tsx | 99 +++------------- .../settings/providers/LMStudioProvider.tsx | 109 ++++++++++++++++++ 2 files changed, 123 insertions(+), 85 deletions(-) create mode 100644 webview-ui/src/components/settings/providers/LMStudioProvider.tsx diff --git a/webview-ui/src/components/settings/ApiOptions.tsx b/webview-ui/src/components/settings/ApiOptions.tsx index 3c675418..12d75d43 100644 --- a/webview-ui/src/components/settings/ApiOptions.tsx +++ b/webview-ui/src/components/settings/ApiOptions.tsx @@ -48,6 +48,7 @@ import { ClaudeCodeProvider } from "./providers/ClaudeCodeProvider" import { SapAiCoreProvider } from "./providers/SapAiCoreProvider" import { BedrockProvider } from "./providers/BedrockProvider" import { VSCodeLmProvider } from "./providers/VSCodeLmProvider" +import { LMStudioProvider } from "./providers/LMStudioProvider" interface ApiOptionsProps { showModelOptions: boolean @@ -92,7 +93,6 @@ const ApiOptions = ({ const extensionState = useExtensionState() const { apiConfiguration, setApiConfiguration, uriScheme } = extensionState const [ollamaModels, setOllamaModels] = useState([]) - const [lmStudioModels, setLmStudioModels] = useState([]) const [modelConfigurationSelected, setModelConfigurationSelected] = useState(false) const [isDescriptionExpanded, setIsDescriptionExpanded] = useState(false) @@ -130,7 +130,7 @@ const ApiOptions = ({ return normalizeApiConfiguration(apiConfiguration) }, [apiConfiguration]) - // Poll ollama/lmstudio models + // Poll ollama/vscode-lm models const requestLocalModels = useCallback(async () => { if (selectedProvider === "ollama") { try { @@ -146,28 +146,14 @@ const ApiOptions = ({ console.error("Failed to fetch Ollama models:", error) setOllamaModels([]) } - } else if (selectedProvider === "lmstudio") { - try { - const response = await ModelsServiceClient.getLmStudioModels( - StringRequest.create({ - value: apiConfiguration?.lmStudioBaseUrl || "", - }), - ) - if (response && response.values) { - setLmStudioModels(response.values) - } - } catch (error) { - console.error("Failed to fetch LM Studio models:", error) - setLmStudioModels([]) - } } - }, [selectedProvider, apiConfiguration?.ollamaBaseUrl, apiConfiguration?.lmStudioBaseUrl]) + }, [selectedProvider, apiConfiguration?.ollamaBaseUrl]) useEffect(() => { - if (selectedProvider === "ollama" || selectedProvider === "lmstudio") { + if (selectedProvider === "ollama") { requestLocalModels() } }, [selectedProvider, requestLocalModels]) - useInterval(requestLocalModels, selectedProvider === "ollama" || selectedProvider === "lmstudio" ? 2000 : null) + useInterval(requestLocalModels, selectedProvider === "ollama" ? 2000 : null) /* VSCodeDropdown has an open bug where dynamically rendered options don't auto select the provided value prop. You can see this for yourself by comparing it with normal select/option elements, which work as expected. @@ -426,72 +412,6 @@ const ApiOptions = ({ )} - {selectedProvider === "lmstudio" && ( -
- - Base URL (optional) - - - Model ID - - {lmStudioModels.length > 0 && ( - { - const value = (e.target as HTMLInputElement)?.value - // need to check value first since radio group returns empty string sometimes - if (value) { - handleInputChange("lmStudioModelId")({ - target: { value }, - }) - } - }}> - {lmStudioModels.map((model) => ( - - {model} - - ))} - - )} -

- LM Studio allows you to run models locally on your computer. For instructions on how to get started, see - their - - quickstart guide. - - You will also need to start LM Studio's{" "} - - local server - {" "} - feature to use it with this extension.{" "} - - (Note: Cline uses complex prompts and works best with Claude - models. Less capable models may not work as expected.) - -

-
- )} - {selectedProvider === "litellm" && (
)} + {apiConfiguration && selectedProvider === "lmstudio" && ( + + )} + {apiConfiguration && selectedProvider === "ollama" && ( (event: any) => void + showModelOptions: boolean + isPopup?: boolean +} + +/** + * The LM Studio provider configuration component + */ +export const LMStudioProvider = ({ apiConfiguration, handleInputChange, showModelOptions, isPopup }: LMStudioProviderProps) => { + const [lmStudioModels, setLmStudioModels] = useState([]) + + // Poll LM Studio models + const requestLmStudioModels = useCallback(async () => { + try { + const response = await ModelsServiceClient.getLmStudioModels( + StringRequest.create({ + value: apiConfiguration?.lmStudioBaseUrl || "", + }), + ) + if (response && response.values) { + setLmStudioModels(response.values) + } + } catch (error) { + console.error("Failed to fetch LM Studio models:", error) + setLmStudioModels([]) + } + }, [apiConfiguration?.lmStudioBaseUrl]) + + useEffect(() => { + requestLmStudioModels() + }, [requestLmStudioModels]) + + useInterval(requestLmStudioModels, 2000) + + return ( +
+ handleInputChange("lmStudioBaseUrl")({ target: { value } })} + placeholder="Default: http://localhost:1234" + label="Use custom base URL" + /> + + + Model ID + + + {lmStudioModels.length > 0 && ( + { + const value = (e.target as HTMLInputElement)?.value + // need to check value first since radio group returns empty string sometimes + if (value) { + handleInputChange("lmStudioModelId")({ + target: { value }, + }) + } + }}> + {lmStudioModels.map((model) => ( + + {model} + + ))} + + )} + +

+ LM Studio allows you to run models locally on your computer. For instructions on how to get started, see their + + quickstart guide. + + You will also need to start LM Studio's{" "} + + local server + {" "} + feature to use it with this extension.{" "} + + (Note: Cline uses complex prompts and works best with Claude models. + Less capable models may not work as expected.) + +

+
+ ) +}