Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(llm-connections): keep the custom TypeSafe base URL when switching upstreams (#17904)

David Traina942ef1227559
diff --git a/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsx b/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsx
new file mode 100644
index 0000000000..fd4e441b84
--- /dev/null
+++ b/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsx
@@ -0,0 +1,73 @@
+import { fireEvent, render, screen } from "@testing-library/react";
+
+import { LayerProvider } from "@/src/context/LayerContext/LayerContext";
+import { type RouterOutputs } from "@/src/utils/api";
+import { CreateLLMApiKeyForm } from "./CreateLLMApiKeyForm";
+
+vi.mock("@/src/features/posthog-analytics", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+vi.mock("@/src/utils/api", () => {
+ const mutation = { useMutation: () => ({ mutateAsync: vi.fn() }) };
+ return {
+ api: {
+ llmApiKey: {
+ all: { useQuery: () => ({ data: { data: [] } }) },
+ create: mutation,
+ update: mutation,
+ test: mutation,
+ testUpdate: mutation,
+ },
+ useUtils: () => ({ llmApiKey: { invalidate: vi.fn() } }),
+ },
+ reportNonTrpcError: vi.fn(),
+ };
+});
+
+describe("CreateLLMApiKeyForm", () => {
+ beforeAll(() => {
+ vi.stubGlobal(
+ "ResizeObserver",
+ class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+ });
+
+ it("restores a TypeSafe connection's custom base URL after switching upstreams", () => {
+ const existingKey = {
+ id: "key",
+ projectId: "project",
+ provider: "jev-via-proxy",
+ adapter: "typesafe",
+ baseURL: "https://gateway.example.com/v1",
+ displaySecretKey: "...test",
+ customModels: [],
+ withDefaultModels: true,
+ extraHeaderKeys: [],
+ } as unknown as RouterOutputs["llmApiKey"]["all"]["data"][number];
+
+ render(
+ <CreateLLMApiKeyForm
+ projectId="project"
+ onSuccess={vi.fn()}
+ customization={null}
+ mode="update"
+ existingKey={existingKey}
+ />,
+ { wrapper: LayerProvider },
+ );
+
+ fireEvent.click(screen.getByRole("radio", { name: "OpenRouter" }));
+ expect(
+ screen.queryByRole("textbox", { name: "Custom base URL" }),
+ ).not.toBeInTheDocument();
+ fireEvent.click(screen.getByRole("radio", { name: "Custom" }));
+
+ expect(
+ screen.getByRole("textbox", { name: "Custom base URL" }),
+ ).toHaveValue("https://gateway.example.com/v1");
+ });
+});
diff --git a/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx b/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx
index 7918f13799..f0ce5777cd 100644
--- a/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx
+++ b/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx
@@ -1,6 +1,6 @@
/* eslint-disable no-nested-ternary */
import { useFieldArray, useForm } from "react-hook-form";
-import { useState } from "react";
+import { useRef, useState } from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import {
type BedrockApiKey,
@@ -405,6 +405,7 @@ export function CreateLLMApiKeyForm({
TYPESAFE_UPSTREAMS.find(
(upstream) => upstream.id === currentTypeSafeUpstreamId,
) ?? TYPESAFE_UPSTREAMS[0];
+ const customTypeSafeBaseURLDraft = useRef("");
const isKeepingCurrentBedrockAuthMethod =
mode === "update" &&
currentAdapter === LLMAdapter.Bedrock &&
@@ -739,6 +740,7 @@ export function CreateLLMApiKeyForm({
"typeSafeUpstream",
TYPESAFE_UPSTREAMS[0].id,
);
+ customTypeSafeBaseURLDraft.current = "";
}
field.onChange(value as LLMAdapter);
}}
@@ -836,12 +838,18 @@ export function CreateLLMApiKeyForm({
aria-label="Upstream"
value={field.value}
onValueChange={(id) => {
+ if (field.value === "custom") {
+ customTypeSafeBaseURLDraft.current =
+ form.getValues("baseURL");
+ }
field.onChange(id);
form.setValue(
"baseURL",
- TYPESAFE_UPSTREAMS.find(
- (upstream) => upstream.id === id,
- )?.baseURL ?? "",
+ id === "custom"
+ ? customTypeSafeBaseURLDraft.current
+ : (TYPESAFE_UPSTREAMS.find(
+ (upstream) => upstream.id === id,
+ )?.baseURL ?? ""),
);
}}
/>