-
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
Open
Olyno
wants to merge
11
commits into
dyad-sh:main
Choose a base branch
from
Olyno:feat/allow-update-local-llm-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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,70 @@ | ||
| import { expect } from "@playwright/test"; | ||
| import { test as testWithPo } from "./helpers/test_helper"; | ||
|
|
||
| testWithPo("Local provider endpoint settings persist", async ({ po }) => { | ||
| await po.setUp(); | ||
| await po.goToSettingsTab(); | ||
|
|
||
| await expect(po.page.getByText("Ollama", { exact: true })).toBeVisible(); | ||
|
|
||
| await po.page.getByText("Ollama", { exact: true }).click(); | ||
| await po.page.waitForSelector('h1:has-text("Configure Ollama")', { | ||
| state: "visible", | ||
| timeout: 5000, | ||
| }); | ||
|
|
||
| const ollamaInput = po.page.getByLabel( | ||
| "Local model endpoint (Ollama-compatible)", | ||
| ); | ||
| await expect(ollamaInput).toBeVisible(); | ||
| await expect( | ||
| po.page.getByText("LM Studio API endpoint", { exact: true }), | ||
| ).toHaveCount(0); | ||
|
|
||
| const ollamaEndpoint = "http://localhost:11435"; | ||
| await ollamaInput.fill(ollamaEndpoint); | ||
| await po.page.getByRole("button", { name: "Save" }).click(); | ||
| await expect(ollamaInput).toHaveValue(ollamaEndpoint); | ||
|
|
||
| await po.page.getByRole("button", { name: "Go Back" }).click(); | ||
| await expect(po.page.getByText("AI Providers")).toBeVisible(); | ||
|
|
||
| await po.page.getByText("Ollama", { exact: true }).click(); | ||
| await po.page.waitForSelector('h1:has-text("Configure Ollama")', { | ||
| state: "visible", | ||
| timeout: 5000, | ||
| }); | ||
| await expect(ollamaInput).toHaveValue(ollamaEndpoint); | ||
|
|
||
| await po.page.getByRole("button", { name: "Go Back" }).click(); | ||
| await expect(po.page.getByText("AI Providers")).toBeVisible(); | ||
|
|
||
| await po.page.getByText("LM Studio", { exact: true }).click(); | ||
| await po.page.waitForSelector('h1:has-text("Configure LM Studio")', { | ||
| state: "visible", | ||
| timeout: 5000, | ||
| }); | ||
|
|
||
| const lmStudioInput = po.page.getByLabel("LM Studio API endpoint"); | ||
| await expect(lmStudioInput).toBeVisible(); | ||
| await expect( | ||
| po.page.getByText("Local model endpoint (Ollama-compatible)", { | ||
| exact: true, | ||
| }), | ||
| ).toHaveCount(0); | ||
|
|
||
| const lmStudioEndpoint = "http://localhost:12345"; | ||
| await lmStudioInput.fill(lmStudioEndpoint); | ||
| await po.page.getByRole("button", { name: "Save" }).click(); | ||
| await expect(lmStudioInput).toHaveValue(lmStudioEndpoint); | ||
|
|
||
| await po.page.getByRole("button", { name: "Go Back" }).click(); | ||
| await expect(po.page.getByText("AI Providers")).toBeVisible(); | ||
|
|
||
| await po.page.getByText("LM Studio", { exact: true }).click(); | ||
| await po.page.waitForSelector('h1:has-text("Configure LM Studio")', { | ||
| state: "visible", | ||
| timeout: 5000, | ||
| }); | ||
| await expect(lmStudioInput).toHaveValue(lmStudioEndpoint); | ||
| }); | ||
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,64 @@ | ||
| import { DEFAULT_LM_STUDIO_ENDPOINT } from "@/constants/localModels"; | ||
| import { | ||
| getLMStudioBaseUrl, | ||
| normalizeLmStudioBaseUrl, | ||
| } from "@/ipc/utils/lm_studio_utils"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| 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("adds default port when protocol provided without port", () => { | ||
| expect(normalizeLmStudioBaseUrl("http://localhost")).toBe( | ||
| "http://localhost:1234", | ||
| ); | ||
| expect(normalizeLmStudioBaseUrl("https://example.com")).toBe( | ||
| "https://example.com: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:1234/custom/path", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getLMStudioBaseUrl", () => { | ||
| it("prefers env override when set", () => { | ||
| const original = process.env.LM_STUDIO_BASE_URL_FOR_TESTING; | ||
| try { | ||
| process.env.LM_STUDIO_BASE_URL_FOR_TESTING = "http://override:9999/v1"; | ||
| expect(getLMStudioBaseUrl()).toBe("http://override:9999"); | ||
| } finally { | ||
| if (original === undefined) { | ||
| delete process.env.LM_STUDIO_BASE_URL_FOR_TESTING; | ||
| } else { | ||
| process.env.LM_STUDIO_BASE_URL_FOR_TESTING = original; | ||
| } | ||
| } | ||
| }); | ||
| }); |
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
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,196 @@ | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Label } from "@/components/ui/label"; | ||
| import { | ||
| DEFAULT_LM_STUDIO_ENDPOINT, | ||
| DEFAULT_OLLAMA_ENDPOINT, | ||
| } from "@/constants/localModels"; | ||
| import { useSettings } from "@/hooks/useSettings"; | ||
| import type { UserSettings } from "@/lib/schemas"; | ||
| import { showError, showSuccess } from "@/lib/toast"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| 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", | ||
| }, | ||
| }; | ||
|
|
||
| type LocalModelEndpointSettingsProps = { | ||
| kind?: EndpointKind; | ||
| }; | ||
|
|
||
| export function LocalModelEndpointSettings({ | ||
| kind, | ||
| }: LocalModelEndpointSettingsProps) { | ||
| 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); | ||
| const ollamaEndpoint = settings?.ollamaEndpoint; | ||
| const lmStudioEndpoint = settings?.lmStudioEndpoint; | ||
| const endpointKinds: EndpointKind[] = kind ? [kind] : ["ollama", "lmstudio"]; | ||
|
|
||
| useEffect(() => { | ||
| if (!settings) { | ||
| return; | ||
| } | ||
| const endpoint = ollamaEndpoint ?? DEFAULT_OLLAMA_ENDPOINT; | ||
| setOllamaValue(endpoint); | ||
| }, [ollamaEndpoint, settings]); | ||
|
|
||
| useEffect(() => { | ||
| if (!settings) { | ||
| return; | ||
| } | ||
| const endpoint = lmStudioEndpoint ?? DEFAULT_LM_STUDIO_ENDPOINT; | ||
| setLmStudioValue(endpoint); | ||
| }, [lmStudioEndpoint, settings]); | ||
|
|
||
| if (!settings) { | ||
| return null; | ||
| } | ||
|
|
||
| const handleSave = async (kind: EndpointKind) => { | ||
| const value = kind === "ollama" ? ollamaValue : lmStudioValue; | ||
| const config = endpointConfig[kind]; | ||
| const valueToPersist = value.trim(); | ||
| 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((current) => (current === kind ? null : current)); | ||
| } | ||
| }; | ||
|
|
||
| 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((current) => (current === kind ? null : current)); | ||
| } | ||
| }; | ||
|
|
||
| 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"> | ||
| {endpointKinds.map((endpointKind) => ( | ||
| <div key={endpointKind}>{renderEndpointField(endpointKind)}</div> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Timeout.MEDIUM instead of hardcoding 5000 - same below
dyad/e2e-tests/helpers/test_helper.ts
Line 20 in 319d2b7