-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: allow to update llm urls #1620
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 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5bcdadf
feat: allow to update llm urls
Olyno cf79a8e
fix: tests and few logic
Olyno 08df1c1
Merge branch 'main' into feat/allow-update-local-llm-url
Olyno f83f6fa
fix(test): was not passing
Olyno 958d32b
Merge branch 'main' of github.com:dyad-sh/dyad into feat/allow-update…
Olyno 571b6ad
feat(settings): move local endpoints to provider pages
Olyno a6e0571
test(e2e): cover local provider settings
Olyno df04228
Merge branch 'main' into feat/allow-update-local-llm-url
Olyno a272c61
fix(local-model): remove unreachable host check and add key
Olyno 03db1a0
Merge branch 'feat/allow-update-local-llm-url' of github.com:Olyno/dy…
Olyno 7460516
Merge branch 'main' into feat/allow-update-local-llm-url
Olyno 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
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,47 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| getLMStudioBaseUrl, | ||
| normalizeLmStudioBaseUrl, | ||
| } from "@/ipc/utils/lm_studio_utils"; | ||
| import { DEFAULT_LM_STUDIO_ENDPOINT } from "@/constants/localModels"; | ||
|
|
||
| describe("normalizeLmStudioBaseUrl", () => { | ||
| it("returns default endpoint when value is undefined", () => { | ||
| expect(normalizeLmStudioBaseUrl()).toBe(DEFAULT_LM_STUDIO_ENDPOINT); | ||
| }); | ||
|
|
||
| it("trims whitespace and adds protocol", () => { | ||
| expect(normalizeLmStudioBaseUrl(" localhost ")).toBe( | ||
| `${DEFAULT_LM_STUDIO_ENDPOINT}`, | ||
| ); | ||
| }); | ||
|
|
||
| it("adds default port when missing", () => { | ||
| expect(normalizeLmStudioBaseUrl("192.168.0.10")).toBe( | ||
| "http://192.168.0.10:1234", | ||
| ); | ||
| }); | ||
|
|
||
| it("removes trailing /v1 if present", () => { | ||
| expect(normalizeLmStudioBaseUrl("http://example.com:9000/v1")).toBe( | ||
| "http://example.com:9000", | ||
| ); | ||
| expect(normalizeLmStudioBaseUrl("http://example.com:9000/v1/")).toBe( | ||
| "http://example.com:9000", | ||
| ); | ||
| }); | ||
|
|
||
| it("preserves additional path segments", () => { | ||
| expect(normalizeLmStudioBaseUrl("http://example.com/custom/path/")).toBe( | ||
| "http://example.com/custom/path", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getLMStudioBaseUrl", () => { | ||
| it("prefers env override when set", () => { | ||
| process.env.LM_STUDIO_BASE_URL_FOR_TESTING = "http://override:9999/v1"; | ||
| expect(getLMStudioBaseUrl()).toBe("http://override:9999"); | ||
| delete process.env.LM_STUDIO_BASE_URL_FOR_TESTING; | ||
| }); | ||
| }); | ||
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
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,187 @@ | ||
| import { useEffect, useState } from "react"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Label } from "@/components/ui/label"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { useSettings } from "@/hooks/useSettings"; | ||
| import { showError, showSuccess } from "@/lib/toast"; | ||
| import type { UserSettings } from "@/lib/schemas"; | ||
| import { | ||
| DEFAULT_LM_STUDIO_ENDPOINT, | ||
| DEFAULT_OLLAMA_ENDPOINT, | ||
| } from "@/constants/localModels"; | ||
|
|
||
| type SavingTarget = "ollama" | "lmstudio" | null; | ||
|
|
||
| type EndpointKind = "ollama" | "lmstudio"; | ||
|
|
||
| const endpointConfig: Record< | ||
| EndpointKind, | ||
| { | ||
| defaultValue: string; | ||
| label: string; | ||
| description: string; | ||
| successMessage: string; | ||
| errorMessage: string; | ||
| } | ||
| > = { | ||
| ollama: { | ||
| defaultValue: DEFAULT_OLLAMA_ENDPOINT, | ||
| label: "Local model endpoint (Ollama-compatible)", | ||
| description: | ||
| "Used for listing and running Ollama-compatible local models, including remote hosts.", | ||
| successMessage: "Ollama endpoint updated", | ||
| errorMessage: "Failed to update Ollama endpoint", | ||
| }, | ||
| lmstudio: { | ||
| defaultValue: DEFAULT_LM_STUDIO_ENDPOINT, | ||
| label: "LM Studio API endpoint", | ||
| description: | ||
| "Base URL for the LM Studio server. Trailing /v1 is optional and will be handled automatically.", | ||
| successMessage: "LM Studio endpoint updated", | ||
| errorMessage: "Failed to update LM Studio endpoint", | ||
| }, | ||
| }; | ||
|
|
||
| export function LocalModelEndpointSettings() { | ||
| const { settings, updateSettings } = useSettings(); | ||
| const [ollamaValue, setOllamaValue] = useState(DEFAULT_OLLAMA_ENDPOINT); | ||
| const [lmStudioValue, setLmStudioValue] = useState( | ||
| DEFAULT_LM_STUDIO_ENDPOINT, | ||
| ); | ||
| const [saving, setSaving] = useState<SavingTarget>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (settings?.ollamaEndpoint) { | ||
| setOllamaValue(settings.ollamaEndpoint); | ||
| } else { | ||
| setOllamaValue(DEFAULT_OLLAMA_ENDPOINT); | ||
| } | ||
| }, [settings?.ollamaEndpoint]); | ||
|
|
||
| useEffect(() => { | ||
| if (settings?.lmStudioEndpoint) { | ||
| setLmStudioValue(settings.lmStudioEndpoint); | ||
| } else { | ||
| setLmStudioValue(DEFAULT_LM_STUDIO_ENDPOINT); | ||
| } | ||
| }, [settings?.lmStudioEndpoint]); | ||
|
|
||
| if (!settings) { | ||
| return null; | ||
| } | ||
|
|
||
| const handleSave = async (kind: EndpointKind) => { | ||
| const value = kind === "ollama" ? ollamaValue : lmStudioValue; | ||
| const config = endpointConfig[kind]; | ||
| const trimmed = value.trim(); | ||
| const valueToPersist = trimmed.length > 0 ? trimmed : config.defaultValue; | ||
| const payload: Partial<UserSettings> = | ||
| kind === "ollama" | ||
| ? { ollamaEndpoint: valueToPersist } | ||
| : { lmStudioEndpoint: valueToPersist }; | ||
|
|
||
| setSaving(kind); | ||
| try { | ||
| await updateSettings(payload); | ||
| if (kind === "ollama") { | ||
| setOllamaValue(valueToPersist); | ||
| } else { | ||
| setLmStudioValue(valueToPersist); | ||
| } | ||
| showSuccess(config.successMessage); | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error | ||
| ? error.message | ||
| : String(error ?? "Unknown error"); | ||
| showError(`${config.errorMessage}: ${message}`); | ||
| } finally { | ||
| setSaving(null); | ||
Olyno marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| const handleReset = async (kind: EndpointKind) => { | ||
| const config = endpointConfig[kind]; | ||
| const payload: Partial<UserSettings> = | ||
| kind === "ollama" | ||
| ? { ollamaEndpoint: config.defaultValue } | ||
| : { lmStudioEndpoint: config.defaultValue }; | ||
|
|
||
| setSaving(kind); | ||
| try { | ||
| await updateSettings(payload); | ||
| if (kind === "ollama") { | ||
| setOllamaValue(config.defaultValue); | ||
| } else { | ||
| setLmStudioValue(config.defaultValue); | ||
| } | ||
| showSuccess(`${config.successMessage} (reset)`); | ||
| } catch (error) { | ||
| const message = | ||
| error instanceof Error | ||
| ? error.message | ||
| : String(error ?? "Unknown error"); | ||
| showError(`${config.errorMessage}: ${message}`); | ||
| } finally { | ||
| setSaving(null); | ||
| } | ||
| }; | ||
|
|
||
| const renderEndpointField = (kind: EndpointKind) => { | ||
| const config = endpointConfig[kind]; | ||
| const value = kind === "ollama" ? ollamaValue : lmStudioValue; | ||
| const onChange = kind === "ollama" ? setOllamaValue : setLmStudioValue; | ||
| const isSaving = saving === kind; | ||
| const isDefault = value === config.defaultValue; | ||
|
|
||
| return ( | ||
| <div className="space-y-2"> | ||
| <div className="space-y-1"> | ||
| <Label htmlFor={`${kind}-endpoint`} className="text-sm font-medium"> | ||
| {config.label} | ||
| </Label> | ||
| <p className="text-sm text-gray-500 dark:text-gray-400"> | ||
| {config.description} | ||
| </p> | ||
| </div> | ||
| <div className="flex flex-col gap-2 sm:flex-row sm:items-center"> | ||
| <Input | ||
| id={`${kind}-endpoint`} | ||
| value={value} | ||
| onChange={(event) => onChange(event.target.value)} | ||
| className="sm:flex-1" | ||
| autoComplete="off" | ||
| spellCheck={false} | ||
| /> | ||
| <div className="flex gap-2"> | ||
| <Button | ||
| onClick={() => handleSave(kind)} | ||
| disabled={isSaving} | ||
| type="button" | ||
| > | ||
| {isSaving ? "Saving..." : "Save"} | ||
| </Button> | ||
| <Button | ||
| onClick={() => handleReset(kind)} | ||
| variant="ghost" | ||
| disabled={isSaving || isDefault} | ||
| type="button" | ||
| > | ||
| Reset | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| <p className="text-xs text-gray-500 dark:text-gray-400"> | ||
| Default: {config.defaultValue} | ||
| </p> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="space-y-6"> | ||
| {renderEndpointField("ollama")} | ||
| {renderEndpointField("lmstudio")} | ||
| </div> | ||
| ); | ||
| } | ||
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,5 @@ | ||
| export const DEFAULT_OLLAMA_ENDPOINT = "http://localhost:11434"; | ||
| export const DEFAULT_OLLAMA_PORT = 11434; | ||
|
|
||
| export const DEFAULT_LM_STUDIO_ENDPOINT = "http://localhost:1234"; | ||
| export const DEFAULT_LM_STUDIO_PORT = 1234; |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.