mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 09:01:56 +08:00
feat(settings): discover models automatically after credential edits
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -390,6 +390,7 @@ export function ProvidersWorkspace() {
|
||||
<RegistryProbe
|
||||
input={{ ...providerProbeInput(source, defaultBaseUrl), api_format: apiFormat }}
|
||||
discovery={connection.discovery}
|
||||
automatic={Boolean(connection.api_key || (!option?.requires_api_key && (connection.base_url || defaultBaseUrl)))}
|
||||
hints={hints}
|
||||
showCapabilities={false}
|
||||
onResult={(result) => change("discovery", result)}
|
||||
|
||||
@@ -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<AbortController | null>(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 (
|
||||
<section className={`space-y-3 p-4 ${subPanelClass}`}>
|
||||
<div className="flex flex-wrap items-center justify-between gap-3">
|
||||
|
||||
@@ -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(<Harness page="providers" />);
|
||||
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(<Harness page="llm" />);
|
||||
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(<RegistryProbe automatic input={{ binding: "custom", base_url: "https://gateway.test/v1", api_key: "bad-key" }} onResult={result} />);
|
||||
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(); }
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user