From 9afd724b1ef3324727fbbd9656030d6b5f0168ea Mon Sep 17 00:00:00 2001 From: yert <22763025+yertYang@users.noreply.github.com> Date: Mon, 2 Nov 2026 05:34:46 +0000 Subject: [PATCH] feat(settings): discover models automatically after credential edits --- web/components/settings/BulkModelImport.tsx | 1 + .../settings/ProvidersWorkspace.tsx | 1 + web/components/settings/RegistryControls.tsx | 18 +++++-- web/tests/provider-registry-workflow.spec.tsx | 54 +++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/web/components/settings/BulkModelImport.tsx b/web/components/settings/BulkModelImport.tsx index d20091455..c525a6f1d 100644 --- a/web/components/settings/BulkModelImport.tsx +++ b/web/components/settings/BulkModelImport.tsx @@ -55,6 +55,7 @@ function ImportForm({ source, service, existingModels, onImport, input }: Props input={input} discovery={discovery} listing + automatic onResult={(result) => { setPicked([]); mutateCatalog((next) => updateProvider(next, source.ref, "discovery", result)); diff --git a/web/components/settings/ProvidersWorkspace.tsx b/web/components/settings/ProvidersWorkspace.tsx index 2a804f2c0..e7e177327 100644 --- a/web/components/settings/ProvidersWorkspace.tsx +++ b/web/components/settings/ProvidersWorkspace.tsx @@ -390,6 +390,7 @@ export function ProvidersWorkspace() { change("discovery", result)} diff --git a/web/components/settings/RegistryControls.tsx b/web/components/settings/RegistryControls.tsx index b8a39a31d..afa335146 100644 --- a/web/components/settings/RegistryControls.tsx +++ b/web/components/settings/RegistryControls.tsx @@ -1,7 +1,7 @@ "use client"; import Tooltip from "@/shared/ui/Tooltip"; -import { type KeyboardEvent, useEffect, useId, useRef, useState } from "react"; +import { type KeyboardEvent, useCallback, useEffect, useId, useRef, useState } from "react"; import { Check, ChevronDown, Loader2, Pencil, PlugZap, X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { apiFetch, apiUrl } from "@/lib/api"; @@ -115,6 +115,7 @@ export function RegistryProbe({ discovery, onResult, listing = false, + automatic = false, hints = [], showCapabilities = true, }: { @@ -122,6 +123,7 @@ export function RegistryProbe({ discovery?: Discovery; onResult: (result: Discovery) => void; listing?: boolean; + automatic?: boolean; hints?: string[]; showCapabilities?: boolean; }) { @@ -133,6 +135,7 @@ export function RegistryProbe({ discovery={discovery} onResult={onResult} listing={listing} + automatic={automatic} hints={hints} showCapabilities={showCapabilities} /> @@ -143,6 +146,7 @@ function ProbeForm({ discovery, onResult, listing, + automatic, hints, showCapabilities, }: { @@ -150,6 +154,7 @@ function ProbeForm({ discovery?: Discovery; onResult: (result: Discovery) => void; listing: boolean; + automatic: boolean; hints: string[]; showCapabilities: boolean; }) { @@ -158,7 +163,7 @@ function ProbeForm({ const [pending, setPending] = useState(false); const controller = useRef(null); useEffect(() => () => { controller.current?.abort(); controller.current = null; }, []); - const probe = async () => { + const probe = useCallback(async () => { controller.current?.abort(); const request = new AbortController(); controller.current = request; @@ -188,7 +193,14 @@ function ProbeForm({ clearTimeout(timeout); if (controller.current === request) setPending(false); } - }; + }, [input, onResult]); + useEffect(() => { + // The keyed form resets on credential/URL/protocol changes. Wait for typing + // to settle; cached results and failures keep manual retry under user control. + if (!automatic || discovery) return; + const timer = setTimeout(() => { void probe(); }, 1200); + return () => clearTimeout(timer); + }, [automatic, discovery, probe]); return (
diff --git a/web/tests/provider-registry-workflow.spec.tsx b/web/tests/provider-registry-workflow.spec.tsx index f6eb2c73a..7b1c6ebc3 100644 --- a/web/tests/provider-registry-workflow.spec.tsx +++ b/web/tests/provider-registry-workflow.spec.tsx @@ -814,3 +814,57 @@ it("imports embeddings using the existing service adapter and preserves task inh expect(mocks.settings.draft.services.task).toEqual(task); await waitFor(() => expect(screen.queryByText("Loading...")).toBeNull()); }); + +it("automatically lists models after key edits settle, using only the latest key", async () => { + vi.useFakeTimers(); + const view = render(); + try { + mocks.fetch.mockResolvedValue({ ok: true, json: async () => ({ status: "connected", models: [{ id: "auto-model", services: ["llm"] }] }) }); + fireEvent.click(screen.getByRole("button", { name: /My account/ })); + const key = screen.getByLabelText("API key"); + fireEvent.change(key, { target: { value: "first-key" } }); + await act(async () => { await vi.advanceTimersByTimeAsync(800); }); + fireEvent.change(key, { target: { value: "latest-key" } }); + await act(async () => { await vi.advanceTimersByTimeAsync(1199); }); + expect(mocks.fetch).not.toHaveBeenCalled(); + await act(async () => { await vi.advanceTimersByTimeAsync(1); }); + expect(mocks.fetch).toHaveBeenCalledTimes(1); + expect(JSON.parse(mocks.fetch.mock.calls[0][1].body).api_key).toBe("latest-key"); + expect(mocks.settings.draft.services.llm.profiles[0].discovery?.models).toEqual([{ id: "auto-model", services: ["llm"] }]); + expect(live.services.llm.profiles[0].api_key).toBe("***"); + } finally { view.unmount(); vi.useRealTimers(); } +}); + +it("automatically lists on the add panel but reuses an existing provider catalog", async () => { + vi.useFakeTimers(); + const view = render(); + try { + mocks.fetch.mockResolvedValue({ ok: true, json: async () => ({ status: "connected", models: [{ id: "auto-model" }] }) }); + fireEvent.click(screen.getByRole("button", { name: "Add model" })); + fireEvent.change(screen.getByLabelText("Configured provider"), { target: { value: "llm:legacy" } }); + await act(async () => { await vi.advanceTimersByTimeAsync(1200); }); + expect(screen.getByRole("checkbox", { name: "auto-model" })).toBeTruthy(); + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + fireEvent.click(screen.getByRole("button", { name: "Add model" })); + fireEvent.change(screen.getByLabelText("Configured provider"), { target: { value: "llm:legacy" } }); + await act(async () => { await vi.advanceTimersByTimeAsync(1200); }); + expect(mocks.fetch).toHaveBeenCalledTimes(1); + expect(screen.getByRole("checkbox", { name: "auto-model" })).toBeTruthy(); + } finally { view.unmount(); vi.useRealTimers(); } +}); + +it("does not automatically retry a failed discovery and retains manual retry", async () => { + vi.useFakeTimers(); + const result = vi.fn(); + mocks.fetch.mockResolvedValue({ ok: true, json: async () => ({ status: "auth_error", models: [] }) }); + const view = render(); + try { + await act(async () => { await vi.advanceTimersByTimeAsync(1200); }); + expect(result).toHaveBeenCalledWith(expect.objectContaining({ status: "auth_error" })); + await act(async () => { await vi.advanceTimersByTimeAsync(10000); }); + expect(mocks.fetch).toHaveBeenCalledTimes(1); + fireEvent.click(screen.getByRole("button", { name: "Test provider" })); + await act(async () => { await vi.advanceTimersByTimeAsync(0); }); + expect(mocks.fetch).toHaveBeenCalledTimes(2); + } finally { view.unmount(); vi.useRealTimers(); } +});