From 8bb2edd538d24fcc06c5e56d4d3d4a116207d372 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 22:38:16 +0000 Subject: [PATCH 1/2] feat(settings): cloud model presets in Settings and the setup wizard Settings > LLM provider reuses the setup wizard's provider step: the model servers found nearby and, new, cloud providers with an OpenAI-compatible API (OpenAI, Anthropic, Google Gemini, OpenRouter, Groq, Mistral, DeepSeek). One click fills in the address and a model and focuses the API key field, so switching to a cloud model is a click and a paste. Agents get the same list from GET /api/v1/setup/llm/providers and the assistant's find_model_servers tool. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Bmpwkzx9nwb1vcDxNMagtu --- CHANGELOG.md | 6 + docs/configuration.md | 27 +++- fastapi_backend/app/api/v1/routes/setup.py | 8 ++ fastapi_backend/app/domain/ops_tools.py | 6 +- fastapi_backend/app/domain/setup.py | 55 ++++++++ fastapi_backend/app/schemas/setup.py | 10 ++ fastapi_backend/tests/api/test_setup.py | 17 +++ .../tests/api/test_setup_assistant.py | 2 + .../__tests__/settings-llm-presets.test.tsx | 108 ++++++++++++++ .../__tests__/setupWizard.test.tsx | 45 ++++++ nextjs-frontend/app/openapi-client/index.ts | 4 + nextjs-frontend/app/openapi-client/sdk.gen.ts | 17 +++ .../app/openapi-client/types.gen.ts | 45 ++++++ .../components/settings/bodies.tsx | 41 +++++- .../components/settings/fields.tsx | 31 +++- .../components/settings/model-providers.tsx | 133 ++++++++++++++++++ .../components/setup/setup-wizard.tsx | 71 ++++------ nextjs-frontend/openapi.json | 63 +++++++++ 18 files changed, 637 insertions(+), 52 deletions(-) create mode 100644 nextjs-frontend/__tests__/settings-llm-presets.test.tsx create mode 100644 nextjs-frontend/components/settings/model-providers.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 84f6d93f4..7b2e2e8c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Cloud model presets.** Settings → LLM provider and the setup wizard offer OpenAI, Anthropic, Google Gemini, + OpenRouter, Groq, Mistral and DeepSeek next to the model servers found nearby: one click fills in the address and a + model and puts the cursor in the API key field, with a link to where the provider hands out keys, so switching to a + cloud model is a click and a paste. Agents get the same list from `GET /api/v1/setup/llm/providers` and the + assistant's `find_model_servers` tool ([Cloud model providers](docs/configuration.md#cloud-model-providers)). + - **The graph as files.** Graph nodes are folders, nested along its links: collections inside collections, topics inside their broader topics, entities by type. Each folder holds its assets' files, with a `.json` sidecar of metadata and a `.txt` of the text; everyone sees what the web app shows them. Browse and read it through the API diff --git a/docs/configuration.md b/docs/configuration.md index b0a4f1640..07e352bdc 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -50,7 +50,7 @@ A fresh install (no accounts when the API first starts) walks its first admin th 2. **Namespace**: the first one, with its knowledge graph shared or isolated. Already done when archive.yaml or `LENS_NAMESPACE` names namespaces, or an install script made one. 3. **Model provider**: an OpenAI-compatible server (OpenAI, Ollama, llama.cpp, LM Studio, vLLM), its model and key, - with a test. + with a test. Servers running nearby and [cloud providers](#cloud-model-providers) are a click. 4. **Storage**: where data lives (shown; set in archive.yaml and `SURREAL_URL`), where Lens keeps its own files (this machine or a storage connection, [Storage](storage.md)), the largest upload, and optionally a folder inside `sources.local_roots` to watch. @@ -62,7 +62,8 @@ A fresh install (no accounts when the API first starts) walks its first admin th Every step can be skipped, and the whole wizard too; all of it stays in Settings. Values from the environment win and show locked. Installs that already had accounts never see the wizard. The API side is `GET /api/v1/setup`, `POST /api/v1/setup/namespace`, `PUT /api/v1/setup/llm`, `GET /api/v1/setup/llm/detect` (model servers running on -this machine or the Docker host, with their models), `PUT /api/v1/setup/storage`, `PUT /api/v1/setup/oauth`, +this machine or the Docker host, with their models), `GET /api/v1/setup/llm/providers` (the +[cloud providers](#cloud-model-providers)), `PUT /api/v1/setup/storage`, `PUT /api/v1/setup/oauth`, `PUT /api/v1/setup/telemetry` and `POST /api/v1/setup/finish` (admins); `GET /api/v1/auth/status` says whether it is still pending (`wizard_pending`). @@ -145,6 +146,28 @@ Changing the model drops the stored vectors (another model's can't be compared) doesn't have the model), indexing jobs skip for ten minutes rather than each waiting on it, and the hourly routine waits too; **Test** in Settings → Search, **Index now**, or `lens embed` try again at once. +## Cloud model providers + +The setup wizard's model provider step and Settings → LLM provider offer the model servers running nearby (Ollama, LM +Studio, llama.cpp, vLLM, LocalAI) and these cloud providers, each through its OpenAI-compatible API. Picking one fills +in the address and a model, and a cloud provider's API key field takes the focus, with a link to where the provider +hands out keys: paste the key and save. The model stays editable, with the provider's other models suggested. + +| Provider | Address | Model filled in | +|---|---|---| +| OpenAI | `https://api.openai.com/v1` | `gpt-5-mini` | +| Anthropic | `https://api.anthropic.com/v1` | `claude-opus-5-5` | +| Google Gemini | `https://generativelanguage.googleapis.com/v1beta/openai` | `gemini-flash-latest` | +| OpenRouter | `https://openrouter.ai/api/v1` | `openrouter/auto` | +| Groq | `https://api.groq.com/openai/v1` | `llama-3.3-70b-versatile` | +| Mistral | `https://api.mistral.ai/v1` | `mistral-small-latest` | +| DeepSeek | `https://api.deepseek.com/v1` | `deepseek-chat` | + +The list is `GET /api/v1/setup/llm/providers` (admins), and the assistant's `find_model_servers` tool returns it with +the servers it finds, so an agent setting Lens up uses the same addresses. Any other OpenAI-compatible service works +by typing its address. Picking a provider changes nothing until you save; from then on, what Lens asks its model +(summaries, chat, descriptions, with the text they need) goes to that provider. + ## Chat models People can choose the model a conversation uses, and ask a question again with another (Retry with another model). diff --git a/fastapi_backend/app/api/v1/routes/setup.py b/fastapi_backend/app/api/v1/routes/setup.py index cf8f085ba..9a19b6169 100644 --- a/fastapi_backend/app/api/v1/routes/setup.py +++ b/fastapi_backend/app/api/v1/routes/setup.py @@ -12,6 +12,7 @@ from app.api.deps import AdminReader, AdminWriter, Db from app.domain import auth, setup from app.schemas.setup import ( + CloudModelProvider, LocalModelServer, SetupFinish, SetupLlm, @@ -68,6 +69,13 @@ def detect_llm(user: AdminWriter) -> list[LocalModelServer]: return [LocalModelServer(**s) for s in setup.detect_llm()] +@router.get("/llm/providers") +def llm_providers(user: AdminReader) -> list[CloudModelProvider]: + """Cloud providers with an OpenAI-compatible API (OpenAI, Anthropic, Gemini, ...), each with its address and a + model, so picking one leaves only the API key to paste.""" + return [CloudModelProvider(**p) for p in setup.CLOUD_PROVIDERS] + + @router.put("/storage") def save_storage(body: SetupStorage, user: AdminWriter, request: Request, db: Db) -> SetupSaved: """The upload limit, and optionally a folder on this machine (inside sources.local_roots) to watch.""" diff --git a/fastapi_backend/app/domain/ops_tools.py b/fastapi_backend/app/domain/ops_tools.py index 40d08f9ab..c3694393f 100644 --- a/fastapi_backend/app/domain/ops_tools.py +++ b/fastapi_backend/app/domain/ops_tools.py @@ -55,7 +55,9 @@ ), ( "find_model_servers", - "Model servers running on this machine or the Docker host (Ollama, LM Studio, llama.cpp, vLLM, LocalAI), with their models.", + "Model servers running on this machine or the Docker host (Ollama, LM Studio, llama.cpp, vLLM, LocalAI), with their " + "models, and the cloud providers Lens knows (OpenAI, Anthropic, Gemini, ...) with their address and a model: " + "a cloud one also needs the admin's API key.", {}, [], False, @@ -242,7 +244,7 @@ def t_server_status(self): def t_find_model_servers(self): found = setup.detect_llm() - return {"servers": found}, f"Looked for model servers nearby: {len(found)} found" + return {"servers": found, "cloud": list(setup.CLOUD_PROVIDERS)}, f"Looked for model servers nearby: {len(found)} found" def t_read_settings(self, section): if section not in SECTIONS: diff --git a/fastapi_backend/app/domain/setup.py b/fastapi_backend/app/domain/setup.py index a4ab3255a..0e4510f5c 100644 --- a/fastapi_backend/app/domain/setup.py +++ b/fastapi_backend/app/domain/setup.py @@ -246,6 +246,61 @@ def save_storage(db, cfg, max_upload_mb=None, folder=None, namespace=None, user= return sources.create_watch(db, cfg, src, folder, sid, user) +# Cloud providers with an OpenAI-compatible API: picking one fills in its address and a model, so only the key is +# pasted. `models` are suggestions (the model stays editable); `key_url` is where the provider hands out keys. +CLOUD_PROVIDERS = ( + { + "name": "OpenAI", + "base_url": "https://api.openai.com/v1", + "model": "gpt-5-mini", + "models": ["gpt-5-mini", "gpt-5", "gpt-5-nano", "gpt-4.1-mini"], + "key_url": "https://platform.openai.com/api-keys", + }, + { + "name": "Anthropic", + "base_url": "https://api.anthropic.com/v1", + "model": "claude-opus-5-5", + "models": ["claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5"], + "key_url": "https://console.anthropic.com/settings/keys", + }, + { + "name": "Google Gemini", + "base_url": "https://generativelanguage.googleapis.com/v1beta/openai", + "model": "gemini-flash-latest", + "models": ["gemini-flash-latest", "gemini-2.5-pro", "gemini-2.5-flash-lite"], + "key_url": "https://aistudio.google.com/apikey", + }, + { + "name": "OpenRouter", + "base_url": "https://openrouter.ai/api/v1", + "model": "openrouter/auto", + "models": ["openrouter/auto", "openai/gpt-5-mini", "google/gemini-2.5-flash", "meta-llama/llama-3.3-70b-instruct"], + "key_url": "https://openrouter.ai/keys", + }, + { + "name": "Groq", + "base_url": "https://api.groq.com/openai/v1", + "model": "llama-3.3-70b-versatile", + "models": ["llama-3.3-70b-versatile", "llama-3.1-8b-instant", "openai/gpt-oss-120b"], + "key_url": "https://console.groq.com/keys", + }, + { + "name": "Mistral", + "base_url": "https://api.mistral.ai/v1", + "model": "mistral-small-latest", + "models": ["mistral-small-latest", "mistral-medium-latest", "mistral-large-latest"], + "key_url": "https://console.mistral.ai/api-keys", + }, + { + "name": "DeepSeek", + "base_url": "https://api.deepseek.com/v1", + "model": "deepseek-chat", + "models": ["deepseek-chat", "deepseek-reasoner"], + "key_url": "https://platform.deepseek.com/api_keys", + }, +) + + # Model servers people run themselves, on the ports they listen on by default. Looked for on this machine, on the # Docker host (host.docker.internal; the compose files map it on Linux too) and as a compose service named ollama. LOCAL_SERVERS = (("Ollama", 11434), ("LM Studio", 1234), ("llama.cpp", 8080), ("vLLM", 8000), ("LocalAI", 8081)) diff --git a/fastapi_backend/app/schemas/setup.py b/fastapi_backend/app/schemas/setup.py index 4f9498f74..1893db237 100644 --- a/fastapi_backend/app/schemas/setup.py +++ b/fastapi_backend/app/schemas/setup.py @@ -74,6 +74,16 @@ class LocalModelServer(ResponseModel): suggested: str +class CloudModelProvider(ResponseModel): + name: str + base_url: str + # the model picking it fills in, then other suggestions + model: str + models: list[str] + # where the provider hands out API keys + key_url: str + + class SetupStorage(RequestModel): max_upload_mb: int | None = None # a folder inside sources.local_roots to watch into `namespace` diff --git a/fastapi_backend/tests/api/test_setup.py b/fastapi_backend/tests/api/test_setup.py index 76a67c878..dc3b529ac 100644 --- a/fastapi_backend/tests/api/test_setup.py +++ b/fastapi_backend/tests/api/test_setup.py @@ -276,6 +276,23 @@ def test_finds_model_servers_running_nearby(fresh, monkeypatch): srv.shutdown() +def test_offers_cloud_model_providers(fresh): + """Picking a cloud provider fills in its address and a model; only the API key is left to paste.""" + app, client = fresh() + make_user(app.state.db, "ada@x.io", "admin password 1", admin=True) + make_user(app.state.db, "ed@x.io", "editor password 1") + app_settings.save(app.state.db, app.state.archive.base, "auth", {"passwords": True}) # made after startup + got = client.get("/api/v1/setup/llm/providers", headers=login(client, "ada@x.io", "admin password 1")).json() + names = [p["name"] for p in got] + assert names[:2] == ["OpenAI", "Anthropic"] and len(names) == len(set(names)) + openai = got[0] + assert openai["base_url"] == "https://api.openai.com/v1" and openai["model"] == openai["models"][0] + for p in got: + assert p["base_url"].startswith("https://") and not p["base_url"].endswith("/") + assert p["model"] in p["models"] and p["key_url"].startswith("https://") + assert client.get("/api/v1/setup/llm/providers", headers=login(client, "ed@x.io", "editor password 1")).status_code == 403 + + def test_log_prints_a_setup_link(fresh, caplog, monkeypatch): """The setup code comes with a link that fills it in, so nobody has to copy the code across.""" monkeypatch.setenv("LENS_SETUP_CODE", "a b&c") diff --git a/fastapi_backend/tests/api/test_setup_assistant.py b/fastapi_backend/tests/api/test_setup_assistant.py index 774e93db8..fff75313f 100644 --- a/fastapi_backend/tests/api/test_setup_assistant.py +++ b/fastapi_backend/tests/api/test_setup_assistant.py @@ -75,6 +75,8 @@ def test_a_setup_conversation_sets_the_server_up(app, db, cfg, folder, new_clien ] status = json.loads(llm.seen[-1]["messages"][3]["content"]) # what server_status told the model assert status["namespaces"] == ["calls", "pods"] and "missing" in status + found = json.loads(llm.seen[-1]["messages"][4]["content"]) # and find_model_servers: nearby and cloud providers + assert found["servers"][0]["kind"] == "Ollama" and found["cloud"][0]["base_url"] == "https://api.openai.com/v1" asked = llm.seen[-6]["messages"] assert "setting up this Lens server" in asked[0]["content"] assert f"attachment id {up['id']}" in asked[-1]["content"] # the model hears about the file diff --git a/nextjs-frontend/__tests__/settings-llm-presets.test.tsx b/nextjs-frontend/__tests__/settings-llm-presets.test.tsx new file mode 100644 index 000000000..7dd6220ae --- /dev/null +++ b/nextjs-frontend/__tests__/settings-llm-presets.test.tsx @@ -0,0 +1,108 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Admin, Setup } from "@/app/openapi-client"; +import { SettingsSection } from "@/components/settings/section"; +import type { SettingsView } from "@/components/settings/model"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Admin: { updateSettings: jest.fn(), testLlm: jest.fn() }, + Setup: { detectLlm: jest.fn(), llmProviders: jest.fn() }, + Metadata: {}, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +jest.mock("next/navigation", () => ({ useRouter: () => ({ push: jest.fn() }) })); +jest.mock("@/lib/hooks/session", () => ({ useArchive: () => ({ namespaces: [] }) })); +jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; + +const view = (keySet: boolean, locked: string[] = []) => + ({ + llm: { + values: { + base_url: "http://localhost:11434/v1", + model: "llama3.1", + chat_models: [], + vision_model: null, + describe_max: 20, + api_key: { secret: true, set: keySet }, + api_key_env: null, + max_chars: 24000, + timeout: 120, + }, + locked, + }, + }) as unknown as SettingsView; + +function show(v: SettingsView) { + const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }); + return render( + + + undefined} /> + + , + ); +} + +describe("Settings › LLM provider presets", () => { + beforeEach(() => { + m(Admin.updateSettings) + .mockReset() + .mockImplementation(() => ok({ ok: true })); + m(Setup.detectLlm) + .mockReset() + .mockImplementation(() => + ok([{ kind: "Ollama", base_url: "http://localhost:11434/v1", models: ["llama3.1"], suggested: "llama3.1" }]), + ); + m(Setup.llmProviders) + .mockReset() + .mockImplementation(() => + ok([ + { + name: "OpenAI", + base_url: "https://api.openai.com/v1", + model: "gpt-5-mini", + models: ["gpt-5-mini", "gpt-5"], + key_url: "https://platform.openai.com/api-keys", + }, + ]), + ); + }); + + it("switches to a cloud provider with one click and a pasted key", async () => { + show(view(false)); + expect(await screen.findByRole("button", { name: /Ollama/ })).toHaveAttribute("aria-pressed", "true"); + fireEvent.click(await screen.findByRole("button", { name: "OpenAI" })); + expect(screen.getByLabelText("Base URL (OpenAI-compatible)")).toHaveValue("https://api.openai.com/v1"); + expect(screen.getByLabelText("Model")).toHaveValue("gpt-5-mini"); + const key = screen.getByLabelText("API key"); + await waitFor(() => expect(key).toHaveFocus()); // ready for the paste + expect(screen.getByRole("link", { name: "Get a key from OpenAI" })).toBeInTheDocument(); + fireEvent.change(key, { target: { value: "sk-test" } }); + fireEvent.click(screen.getByRole("button", { name: "Save" })); + await waitFor(() => expect(Admin.updateSettings).toHaveBeenCalledTimes(1)); + expect(m(Admin.updateSettings).mock.calls[0][0]).toMatchObject({ + path: { section: "llm" }, + body: { base_url: "https://api.openai.com/v1", model: "gpt-5-mini", api_key: "sk-test" }, + }); + }); + + it("opens the saved key for replacing when the provider changes", async () => { + show(view(true)); + expect(screen.getByText(/^Set/)).toBeInTheDocument(); + fireEvent.click(await screen.findByRole("button", { name: "OpenAI" })); + await waitFor(() => expect(screen.getByPlaceholderText("Paste the new key")).toHaveFocus()); + }); + + it("offers no presets when .env sets the address", async () => { + show(view(false, ["base_url"])); + expect(screen.getByLabelText("Base URL (OpenAI-compatible)")).toHaveAttribute("readonly"); + expect(screen.queryByRole("button", { name: "OpenAI" })).not.toBeInTheDocument(); + expect(Setup.llmProviders).not.toHaveBeenCalled(); + }); +}); diff --git a/nextjs-frontend/__tests__/setupWizard.test.tsx b/nextjs-frontend/__tests__/setupWizard.test.tsx index 0fcb1de53..e815c49b8 100644 --- a/nextjs-frontend/__tests__/setupWizard.test.tsx +++ b/nextjs-frontend/__tests__/setupWizard.test.tsx @@ -13,6 +13,7 @@ jest.mock("@/app/openapi-client", () => ({ saveNamespace: jest.fn(), saveLlm: jest.fn(), detectLlm: jest.fn(), + llmProviders: jest.fn(), saveStorage: jest.fn(), saveTelemetry: jest.fn(), saveOauth: jest.fn(), @@ -28,6 +29,23 @@ jest.mock("@/lib/hooks/session", () => ({ useArchive: () => ({ me: { user: { ema const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); const m = (f: unknown) => f as jest.Mock; +const CLOUD = [ + { + name: "OpenAI", + base_url: "https://api.openai.com/v1", + model: "gpt-5-mini", + models: ["gpt-5-mini", "gpt-5"], + key_url: "https://platform.openai.com/api-keys", + }, + { + name: "Anthropic", + base_url: "https://api.anthropic.com/v1", + model: "claude-opus-5-5", + models: ["claude-opus-5-5", "claude-sonnet-5-5"], + key_url: "https://console.anthropic.com/settings/keys", + }, +]; + const VIEW = (over: Partial = {}): SetupView => ({ pending: true, admin: { from_env: false }, @@ -58,6 +76,7 @@ function wrap(ui: ReactNode) { beforeEach(() => { jest.clearAllMocks(); m(Setup.detectLlm).mockImplementation(() => ok([])); + m(Setup.llmProviders).mockImplementation(() => ok(CLOUD)); }); describe("SetupWizard", () => { @@ -91,6 +110,31 @@ describe("SetupWizard", () => { ); }); + it("fills in a cloud provider's address and model in one click, then waits for its key", async () => { + m(Setup.getSetup).mockImplementation(() => ok(VIEW({ namespace: { existing: ["media"], locked: false } }))); + m(Setup.saveLlm).mockImplementation(() => ok({ ok: true })); + wrap(); + fireEvent.click(await screen.findByRole("button", { name: "OpenAI" })); + expect(screen.getByLabelText("Base URL")).toHaveValue("https://api.openai.com/v1"); + expect(screen.getByLabelText("Model")).toHaveValue("gpt-5-mini"); + expect(screen.getByRole("button", { name: "OpenAI" })).toHaveAttribute("aria-pressed", "true"); + const key = screen.getByLabelText(/API key/); + await waitFor(() => expect(key).toHaveFocus()); // ready for the paste + expect(screen.getByRole("link", { name: "Get a key from OpenAI" })).toHaveAttribute( + "href", + "https://platform.openai.com/api-keys", + ); + fireEvent.change(key, { target: { value: "sk-test" } }); + fireEvent.click(screen.getByRole("button", { name: "Save and continue" })); + await waitFor(() => + expect(m(Setup.saveLlm).mock.calls[0][0].body).toEqual({ + base_url: "https://api.openai.com/v1", + model: "gpt-5-mini", + api_key: "sk-test", + }), + ); + }); + it("walks through namespace, model provider, storage, apps and telemetry, then finishes", async () => { m(Setup.getSetup).mockImplementation(() => ok(VIEW())); m(Setup.saveNamespace).mockImplementation(() => ok({ ok: true })); @@ -205,6 +249,7 @@ describe("SetupWizard", () => { wrap(); const base = await screen.findByLabelText("Base URL"); // nothing to choose for the namespace: straight here expect(Setup.detectLlm).not.toHaveBeenCalled(); // the address is set in .env: nothing to look for + expect(screen.queryByRole("button", { name: "OpenAI" })).not.toBeInTheDocument(); expect(base).toBeDisabled(); expect(base).toHaveValue("http://vllm:8000/v1"); expect(base).toHaveAccessibleDescription(/LENS_LLM_BASE_URL/); diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index 81cff73c6..512a23a74 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -277,6 +277,7 @@ export type { CloseRecordingToIpGroupErrors, CloseRecordingToIpGroupResponse, CloseRecordingToIpGroupResponses, + CloudModelProvider, Collection, CollectionCreate, CollectionDetail, @@ -1782,6 +1783,9 @@ export type { ListWorkflowsResponse, ListWorkflowsResponses, LlmEstimate, + LlmProvidersData, + LlmProvidersResponse, + LlmProvidersResponses, LlmTestResult, LocalLlmProgress, LocalLlmStatus, diff --git a/nextjs-frontend/app/openapi-client/sdk.gen.ts b/nextjs-frontend/app/openapi-client/sdk.gen.ts index 4fd670a0b..d22c8bcf6 100644 --- a/nextjs-frontend/app/openapi-client/sdk.gen.ts +++ b/nextjs-frontend/app/openapi-client/sdk.gen.ts @@ -919,6 +919,8 @@ import type { ListWorkersResponses, ListWorkflowsData, ListWorkflowsResponses, + LlmProvidersData, + LlmProvidersResponses, LocalLlmStatusData, LocalLlmStatusResponses, LockVaultData, @@ -3274,6 +3276,21 @@ export class Setup { }); } + /** + * Llm Providers + * + * Cloud providers with an OpenAI-compatible API (OpenAI, Anthropic, Gemini, ...), each with its address and a + * model, so picking one leaves only the API key to paste. + */ + public static llmProviders( + options?: Options, + ): RequestResult { + return (options?.client ?? client).get({ + url: "/api/v1/setup/llm/providers", + ...options, + }); + } + /** * Save Storage * diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index f27b999db..dee682e59 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -2032,6 +2032,33 @@ export type ClientRegistration = { scope?: string | null; }; +/** + * CloudModelProvider + */ +export type CloudModelProvider = { + /** + * Name + */ + name: string; + /** + * Base Url + */ + base_url: string; + /** + * Model + */ + model: string; + /** + * Models + */ + models: Array; + /** + * Key Url + */ + key_url: string; + [key: string]: unknown; +}; + /** * Collection */ @@ -21273,6 +21300,24 @@ export type DetectLlmResponses = { export type DetectLlmResponse = DetectLlmResponses[keyof DetectLlmResponses]; +export type LlmProvidersData = { + body?: never; + path?: never; + query?: never; + url: "/api/v1/setup/llm/providers"; +}; + +export type LlmProvidersResponses = { + /** + * Response Setup-Llm Providers + * + * Successful Response + */ + 200: Array; +}; + +export type LlmProvidersResponse = LlmProvidersResponses[keyof LlmProvidersResponses]; + export type SaveStorageData = { body: SetupStorage; path?: never; diff --git a/nextjs-frontend/components/settings/bodies.tsx b/nextjs-frontend/components/settings/bodies.tsx index 2b8a6c4d1..24cc8caa2 100644 --- a/nextjs-frontend/components/settings/bodies.tsx +++ b/nextjs-frontend/components/settings/bodies.tsx @@ -3,7 +3,7 @@ import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; import { PlugZap, RefreshCw, Terminal } from "lucide-react"; import Link from "next/link"; -import { useEffect, useRef, type ReactNode } from "react"; +import { useEffect, useRef, useState, type ReactNode } from "react"; import { Admin, Fedora, Metadata, Sensors, Sources } from "@/app/openapi-client"; import type { MatterbridgeStatus } from "@/app/openapi-client/types.gen"; @@ -14,6 +14,13 @@ import { ACCESS } from "@/components/iiif/metadata-model"; import { RIGHTS } from "@/components/iiif/rights"; import { hubText } from "@/components/sensors/sensor-model"; import { SecretSetting, SettingField, ZoneBar, type FieldState } from "@/components/settings/fields"; +import { + KeyLink, + ModelProviderPicker, + providerAt, + useModelProviders, + type ModelProvider, +} from "@/components/settings/model-providers"; import { SignInProviders } from "@/components/settings/sign-in-providers"; import { VoicePicker } from "@/components/settings/voice-picker"; import { AddConnection } from "@/components/storage/add-connection"; @@ -55,6 +62,7 @@ function F({ hint, options, className, + suggestions, }: { ctx: BodyCtx; id: string; @@ -62,6 +70,7 @@ function F({ hint?: ReactNode; options?: FieldSpec["options"]; className?: string; + suggestions?: string[]; }) { const spec = ctx.spec(id); return ( @@ -71,6 +80,7 @@ function F({ label={label} hint={hint} className={className} + suggestions={suggestions} /> ); } @@ -435,18 +445,28 @@ function LlmBody({ ctx }: { ctx: BodyCtx }) { const llm = ctx.view.llm?.values ?? {}; const secret = (llm.api_key ?? {}) as { set?: boolean }; const key = ctx.state("llm.api_key"); + const baseUrl = ctx.state("llm.base_url"); + const model = ctx.state("llm.model"); const test = useMutation({ mutationFn: () => data(Admin.testLlm({ client })), }); + // The setup wizard's provider step: a server found nearby or a cloud provider fills in the address and the model, + // and a cloud provider's key is asked for next + const canPick = !baseUrl.locked && !model.locked; + const { servers, cloud, looking } = useModelProviders(canPick); + const at = String(baseUrl.value ?? ""); + const here = providerAt([...servers, ...cloud], at); + const [askKey, setAskKey] = useState(0); + const pick = (p: ModelProvider) => { + if (p.key_url && !key.locked && !providerAt([p], at)) setAskKey((n) => n + 1); // a new provider: its key next + baseUrl.onChange(p.base_url); + model.onChange(p.model); + }; return ( <> + {canPick && } - - -
- - -
+ key.onChange(x)} + hint={here?.key_url ? : undefined} + focus={askKey} /> + +
+ + +
diff --git a/nextjs-frontend/components/settings/fields.tsx b/nextjs-frontend/components/settings/fields.tsx index a50423bc7..ba96219e0 100644 --- a/nextjs-frontend/components/settings/fields.tsx +++ b/nextjs-frontend/components/settings/fields.tsx @@ -1,7 +1,7 @@ "use client"; import { KeyRound, Lock, LockOpen } from "lucide-react"; -import { useId, useState, type ReactNode } from "react"; +import { useEffect, useId, useRef, useState, type ReactNode } from "react"; import { ChoiceCards, Pills } from "@/components/settings/controls"; import { fieldId, type FieldSpec } from "@/components/settings/model"; @@ -37,12 +37,15 @@ export function SettingField({ hint, className, label, + suggestions, }: { spec: FieldSpec; state: FieldState; hint?: ReactNode; className?: string; label?: ReactNode; + /** Values a text field offers as you type (a datalist); any other can still be typed. */ + suggestions?: string[]; }) { const auto = useId(); const id = `${fieldId(spec).replace(/\./g, "-")}-${auto}`; @@ -148,8 +151,16 @@ export function SettingField({ value={String(value ?? "")} readOnly={disabled} onChange={(e) => onChange(e.target.value)} + list={suggestions?.length ? `${id}-options` : undefined} /> )} + {suggestions?.length ? ( + + {suggestions.map((o) => ( + + ) : null}
); @@ -167,6 +178,8 @@ export function SecretSetting({ value, onChange, placeholder = "Paste an API key", + hint, + focus = 0, }: { label: string; isSet: boolean; @@ -175,11 +188,25 @@ export function SecretSetting({ value: string | undefined; onChange: (v: string | undefined) => void; placeholder?: string; + hint?: ReactNode; + /** Changed to ask for a key now (a cloud provider was picked): the field takes the focus, replacing a saved key. */ + focus?: number; }) { const id = useId(); const [replacing, setReplacing] = useState(false); const box = "flex h-10 items-center gap-1.5 rounded-sm border pl-3 pr-1"; const showSet = isSet && !replacing && value === undefined; + const input = useRef(null); + const asked = useRef(focus); // asked again, not on mounting (after a save) + useEffect(() => { + if (!focus || asked.current === focus) return; + if (showSet) { + setReplacing(true); // the field to paste into opens, and this runs again + return; + } + asked.current = focus; + input.current?.focus(); + }, [focus, showSet]); return (