feat(providers): declare supported APIs for custom connections

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-10 01:33:02 +08:00
1 parent 73a27e0726
commit 34fd4ed75a
29 files changed
+799 -108

No files matched your search

+77 -1
View File
@@ -148,6 +148,82 @@ describe("Settings models", () => {
expect(vi.mocked(fetch).mock.calls.some(([url]) => String(url).includes("/model-api?"))).toBe(false);
});
it("saves and restores Anthropic Messages for a custom gateway", async () => {
let payload = settingsPayload();
payload.providers = [{
name: "tenant", label: "Tenant", configured: true,
request_apis: ["chat_completions", "responses", "anthropic_messages"],
}];
payload.model_presets[0].provider = "tenant";
payload.model_presets[0].resolved_provider = "tenant";
vi.stubGlobal("fetch", vi.fn(async () => jsonResponse(payload)));
requestMutationMock.mockImplementation(async (action, args) => {
expect(action).toBe("settings.model_configuration.update");
payload = {
...payload,
model_presets: payload.model_presets.map((preset) => ({ ...preset, api: args.api })),
};
return payload;
});
renderSettingsView({ initialSection: "models" });
await togglePresetEditor();
await openPopover(screen.getByLabelText("API connection"));
expect(screen.getAllByRole("option")).toHaveLength(4);
fireEvent.click(screen.getByRole("option", { name: "Anthropic Messages" }));
expect(screen.queryByRole("switch", { name: "Try Chat Completions if Responses is unsupported" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(requestMutationMock).toHaveBeenLastCalledWith(
"settings.model_configuration.update",
{ name: "primary", api: { supported_apis: ["anthropic_messages"], preferred_api: "anthropic_messages" } },
20_000,
));
await waitFor(() => expect(screen.getByRole("button", { name: "Save" })).toBeDisabled());
fireEvent.click(screen.getByRole("button", { name: "Close", exact: true }));
await togglePresetEditor();
expect(screen.getByLabelText("API connection")).toHaveTextContent("Anthropic Messages");
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
await openPopover(screen.getByLabelText("API connection"));
fireEvent.click(screen.getByRole("option", { name: "Responses" }));
expect(screen.getByRole("switch", { name: "Try Chat Completions if Responses is unsupported" })).not.toBeChecked();
});
it("saves an explicit API after an Auto provider resolves the changed model to a new connection", async () => {
const payload = settingsPayload();
payload.model_api_resolution_supported = true;
payload.model_presets[0] = {
...payload.model_presets[0], provider: "auto", model: "anthropic/claude-sonnet-4",
resolved_provider: "anthropic",
};
payload.providers = [
{ name: "anthropic", label: "Anthropic", configured: true, request_apis: ["anthropic_messages"] },
{ name: "openai", label: "OpenAI", configured: true, request_apis: ["chat_completions", "responses"] },
];
vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => {
const url = new URL(String(input), "http://localhost");
if (url.pathname !== "/api/settings/model-api") return jsonResponse(payload);
return jsonResponse(url.searchParams.get("model")?.startsWith("gpt")
? { provider: "openai", api: "chat_completions" }
: { provider: "anthropic", api: "anthropic_messages" });
}));
requestMutationMock.mockResolvedValue(payload);
renderSettingsView({ initialSection: "models", initialSettings: payload });
await togglePresetEditor();
await openPopover(screen.getByRole("button", { name: "anthropic/claude-sonnet-4", exact: true }));
const search = screen.getByRole("combobox", { name: "Search or type model ID" });
fireEvent.change(search, { target: { value: "gpt-4o" } });
fireEvent.keyDown(search, { key: "Enter" });
await waitFor(() => expect(screen.getByLabelText("API connection")).toHaveTextContent("Auto (Chat Completions)"));
await openPopover(screen.getByLabelText("API connection"));
fireEvent.click(screen.getByRole("option", { name: "Responses" }));
fireEvent.click(screen.getByRole("button", { name: "Save", exact: true }));
await waitFor(() => expect(requestMutationMock).toHaveBeenCalledWith(
"settings.model_configuration.update", {
name: "primary", model: "gpt-4o",
api: { supported_apis: ["responses"], preferred_api: "responses" },
}, 20_000,
));
});
it("shows the host's automatic API and discards an older reasoning resolution", async () => {
const payload = settingsPayload();
payload.model_api_resolution_supported = true;
@@ -228,7 +304,7 @@ describe("Settings models", () => {
expect(screen.getByRole("button", { name: "Save" })).toBeDisabled();
});
it.each([undefined, ["future_api"], "responses"])("hides API controls when host support is unconfirmed (%s)", async (requestAPIs) => {
it.each([undefined, ["future_api"], ["future_api", "future_api_v2"], "responses"])("hides API controls when host support is unconfirmed (%s)", async (requestAPIs) => {
const payload = settingsPayload();
vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({
...payload,
@@ -981,4 +981,61 @@ describe("Settings providers", () => {
screen.getByRole("button", { name: "Add provider" }),
).toBeInTheDocument();
});
it("declares custom connection APIs on creation and preserves them when editing", async () => {
const user = userEvent.setup();
let payload = settingsPayload();
payload.provider_api_configuration_supported = true;
const api = { supported_apis: ["anthropic_messages" as const], preferred_api: "anthropic_messages" as const };
requestMutationMock.mockImplementation(async (action, args) => {
if (action === "settings.provider.create") {
payload = {
...payload,
providers: [...payload.providers, {
name: "tenant", label: "Tenant", is_custom: true, configured: true,
api_base: args.apiBase, api: args.api,
provider_api_configurable: true,
request_apis: args.api.supported_apis,
}],
};
}
if (action === "settings.provider.update") {
payload = { ...payload, providers: payload.providers.map((row) => row.name === "tenant"
? { ...row, api: args.api, request_apis: args.api.supported_apis } : row) };
}
return payload;
});
renderSettingsView({ initialSection: "models", initialSettings: payload });
await user.click(screen.getByRole("button", { name: "Add provider" }));
await user.click(screen.getByRole("button", { name: "Custom provider" }));
await user.type(screen.getByPlaceholderText("My model provider"), "Tenant");
await user.type(screen.getByPlaceholderText("https://api.example.com/v1"), "https://tenant.test");
expect(screen.getByRole("switch", { name: "Chat Completions" })).toBeDisabled();
await user.click(screen.getByRole("switch", { name: "Anthropic Messages" }));
await user.click(screen.getByRole("switch", { name: "Chat Completions" }));
expect(screen.getByRole("combobox", { name: "Default API" })).toHaveTextContent("Anthropic Messages");
expect(screen.getByRole("switch", { name: "Anthropic Messages" })).toBeDisabled();
await user.click(screen.getByRole("button", { name: "Save provider" }));
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(requestMutationMock).toHaveBeenCalledWith("settings.provider.create", expect.objectContaining({ api }), 20_000);
await user.click(screen.getByRole("button", { name: "Tenant", exact: true }));
expect(screen.getByRole("combobox", { name: "Default API" })).toHaveTextContent("Anthropic Messages");
await user.click(screen.getByRole("switch", { name: "Responses" }));
await user.click(screen.getByRole("combobox", { name: "Default API" }));
await user.click(screen.getByRole("option", { name: "Responses" }));
await user.click(screen.getByRole("button", { name: "Save provider" }));
await waitFor(() => expect(requestMutationMock).toHaveBeenCalledWith("settings.provider.update", expect.objectContaining({
provider: "tenant", api: { supported_apis: ["anthropic_messages", "responses"], preferred_api: "responses" },
}), 20_000));
await user.click(screen.getByRole("button", { name: "Tenant", exact: true }));
expect(screen.getByRole("combobox", { name: "Default API" })).toHaveTextContent("Responses");
});
it("hides connection API declarations on hosts without the capability", async () => {
renderSettingsView({ initialSection: "models", initialSettings: settingsPayload() });
fireEvent.click(screen.getByRole("button", { name: "Add provider" }));
fireEvent.click(screen.getByRole("button", { name: "Custom provider" }));
expect(screen.queryByRole("combobox", { name: "Default API" })).not.toBeInTheDocument();
expect(screen.queryByRole("group", { name: "Supported APIs" })).not.toBeInTheDocument();
});
});