🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(ai-gateway): validate connection names server-side and autofill them (#18929)
Tobias Wochinger3e660df14e03
diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.tsindex b021644371..122a0627aa 100644--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts@@ -20,6 +20,7 @@ import {createInnerTRPCContext,type OrgAuthedContext,} from "@/src/server/api/trpc";+import { GATEWAY_CONNECTION_NAME_ERRORS } from "@/src/features/ai-gateway/constants/gatewayConnection";import { GatewayApiKeyAuthenticator } from "@/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator";import {type GatewayResolveError,@@ -368,7 +369,7 @@ describe("AI gateway control plane", () => {const setup = await prepare();const connection = await setup.caller.aiGateway.createConnection({orgId: setup.org.id,- name: "Shared OpenAI",+ name: "Shared-OpenAI",provider: "OPENAI",credential: "sk-shared-openai",});@@ -393,7 +394,7 @@ describe("AI gateway control plane", () => {).resolves.toEqual([{id: connection.id,- name: "Shared OpenAI",+ name: "Shared-OpenAI",provider: "OPENAI",allowedModels: ["enabled-model"],},@@ -412,7 +413,7 @@ describe("AI gateway control plane", () => {const setup = await prepare();const connection = await setup.caller.aiGateway.createConnection({orgId: setup.org.id,- name: "Shared OpenAI",+ name: "Shared-OpenAI",provider: "OPENAI",credential: "sk-shared-openai",});@@ -461,7 +462,7 @@ describe("AI gateway control plane", () => {const credential = "sk-test-gateway-redaction";const created = await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI primary",+ name: "OpenAI-primary",provider: "OPENAI",credential,});@@ -486,7 +487,7 @@ describe("AI gateway control plane", () => {).resolves.toEqual([{id: created.id,- name: "OpenAI primary",+ name: "OpenAI-primary",provider: "OPENAI",allowedModels: null,},@@ -530,7 +531,7 @@ describe("AI gateway control plane", () => {const connection = await caller.aiGateway.createConnection({orgId: org.id,- name: "Anthropic primary",+ name: "Anthropic-primary",provider: "ANTHROPIC",credential: "sk-ant-test-gateway",});@@ -620,7 +621,7 @@ describe("AI gateway control plane", () => {});await providerService.create({organizationId: org.id,- name: "Audited provider",+ name: "Audited-provider",provider: "OPENAI",credential: "sk-audited-provider",session,@@ -669,7 +670,7 @@ describe("AI gateway control plane", () => {await expect(failingProviderService.create({organizationId: org.id,- name: "Rejected provider",+ name: "Rejected-provider",provider: "OPENAI",credential: "sk-rejected",session,@@ -758,13 +759,13 @@ describe("AI gateway control plane", () => {});await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI first",+ name: "OpenAI-first",provider: "OPENAI",credential: "sk-test-openai-primary",});const openAiSecondary = await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI second",+ name: "OpenAI-second",provider: "OPENAI",credential: "sk-test-openai-secondary",});@@ -785,12 +786,12 @@ describe("AI gateway control plane", () => {const result = await authenticateRequest({fastHashedSecretKey: createShaHash(gatewayKey.secretKey, env.SALT),apiFormat,- model: "OpenAI second:openai/gpt-4o",+ model: "OpenAI-second:openai/gpt-4o",});expect(result.connection).toEqual({id: openAiSecondary.id,- name: "OpenAI second",+ name: "OpenAI-second",provider: "openai",provider_model: "openai/gpt-4o",api_format: apiFormat,@@ -830,13 +831,13 @@ describe("AI gateway control plane", () => {});const openAiPrimary = await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI primary",+ name: "OpenAI-primary",provider: "OPENAI",credential: "sk-test-openai-primary",});const openAiSecondary = await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI secondary",+ name: "OpenAI-secondary",provider: "OPENAI",credential: "sk-test-openai-secondary",});@@ -887,7 +888,7 @@ describe("AI gateway control plane", () => {await expect(authenticateRequest({...params,- model: "OpenAI secondary:gpt-4o",+ model: "OpenAI-secondary:gpt-4o",}),).resolves.toMatchObject({connection: {@@ -1043,18 +1044,70 @@ describe("AI gateway control plane", () => {).resolves.toMatchObject({ allowedModels: ["gpt-4o"] });});- it("requires connection names to be unique within an organization", async () => {+ it("rejects connection names that are not safe as model slug prefixes", async () => {const { caller, org } = await prepare();+ for (const [name, message] of [+ ["Production:Primary", GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters],+ ["OpenAI primary", GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters],+ ["-openai", GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters],+ ["openai.v2", GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters],+ [" ", GATEWAY_CONNECTION_NAME_ERRORS.required],+ ["a".repeat(65), GATEWAY_CONNECTION_NAME_ERRORS.tooLong],+ ["FT", GATEWAY_CONNECTION_NAME_ERRORS.reserved],+ ] as const) {+ await expect(+ caller.aiGateway.createConnection({+ orgId: org.id,+ name,+ provider: "OPENAI",+ credential: "sk-openai",+ }),+ ).rejects.toThrow(message);+ }+await expect(caller.aiGateway.createConnection({orgId: org.id,- name: "Production:Primary",+ name: " OpenAI_eu-v2 ",provider: "OPENAI",credential: "sk-openai",}),- ).rejects.toThrow("Connection names cannot contain a colon.");+ ).resolves.toMatchObject({ name: "OpenAI_eu-v2" });+ });- await caller.aiGateway.createConnection({+ it("keeps a legacy connection name usable until it is renamed", async () => {+ const { caller, org } = await prepare();+ const created = await caller.aiGateway.createConnection({+ orgId: org.id,+ name: "Legacy",+ provider: "OPENAI",+ credential: "sk-openai",+ });+ const legacy = await prisma.gatewayAiConnection.update({+ where: { id: created.id },+ data: { name: "Legacy name" },+ });++ await expect(+ caller.aiGateway.updateConnection({+ orgId: org.id,+ id: legacy.id,+ name: "Legacy name",+ credential: "sk-openai-rotated",+ }),+ ).resolves.toMatchObject({ name: "Legacy name" });+ await expect(+ caller.aiGateway.updateConnection({+ orgId: org.id,+ id: legacy.id,+ name: "Legacy name 2",+ }),+ ).rejects.toThrow(GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters);+ });++ it("requires connection names to be unique within an organization, ignoring case", async () => {+ const { caller, org } = await prepare();+ const production = await caller.aiGateway.createConnection({orgId: org.id,name: "Production",provider: "OPENAI",@@ -1064,11 +1117,11 @@ describe("AI gateway control plane", () => {await expect(caller.aiGateway.createConnection({orgId: org.id,- name: "Production",+ name: "production",provider: "ANTHROPIC",credential: "sk-anthropic",}),- ).rejects.toThrow("A provider credential with this name already exists.");+ ).rejects.toThrow(GATEWAY_CONNECTION_NAME_ERRORS.duplicate);const backup = await caller.aiGateway.createConnection({orgId: org.id,@@ -1080,9 +1133,36 @@ describe("AI gateway control plane", () => {caller.aiGateway.updateConnection({orgId: org.id,id: backup.id,- name: "Production",+ name: "PRODUCTION",+ }),+ ).rejects.toThrow(GATEWAY_CONNECTION_NAME_ERRORS.duplicate);++ await expect(+ caller.aiGateway.updateConnection({+ orgId: org.id,+ id: production.id,+ name: "production",+ }),+ ).resolves.toMatchObject({ name: "production" });+ });++ it("treats underscores in connection names literally when checking duplicates", async () => {+ const { caller, org } = await prepare();+ await caller.aiGateway.createConnection({+ orgId: org.id,+ name: "openai-1",+ provider: "OPENAI",+ credential: "sk-openai",+ });++ await expect(+ caller.aiGateway.createConnection({+ orgId: org.id,+ name: "openai_1",+ provider: "OPENAI",+ credential: "sk-openai",}),- ).rejects.toThrow("A provider credential with this name already exists.");+ ).resolves.toMatchObject({ name: "openai_1" });});it("changes ERROR only for credential auth failures and explicit recovery", async () => {@@ -1273,7 +1353,7 @@ describe("AI gateway control plane", () => {});await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI backup",+ name: "OpenAI-backup",provider: "OPENAI",credential: "sk-test-backup",});@@ -1296,7 +1376,7 @@ describe("AI gateway control plane", () => {await expect(authenticateRequest({...resolveParams,- model: "OpenAI backup:gpt-4o",+ model: "OpenAI-backup:gpt-4o",}),).resolves.toMatchObject({connection: { auth: { type: "Bearer", token: "sk-test-backup" } },@@ -1309,7 +1389,7 @@ describe("AI gateway control plane", () => {await caller.aiGateway.updateConnection({orgId: org.id,id: connection.id,- name: "OpenAI renamed",+ name: "OpenAI-renamed",});await expect(authenticateRequest(resolveParams)).rejects.toEqual(expect.objectContaining<Partial<GatewayResolveError>>({ status: 404 }),@@ -1318,10 +1398,10 @@ describe("AI gateway control plane", () => {await expect(authenticateRequest({...resolveParams,- model: "OpenAI renamed:gpt-4o",+ model: "OpenAI-renamed:gpt-4o",}),).resolves.toMatchObject({- connection: { name: "OpenAI renamed" },+ connection: { name: "OpenAI-renamed" },});expect(lookup).toHaveBeenCalledTimes(2);lookup.mockRestore();@@ -1450,13 +1530,13 @@ describe("AI gateway control plane", () => {const { caller, org } = await prepare();await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI primary",+ name: "OpenAI-primary",provider: "OPENAI",credential: "sk-openai",});await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI backup",+ name: "OpenAI-backup",provider: "OPENAI",credential: "sk-openai-backup",});@@ -1529,28 +1609,28 @@ describe("AI gateway control plane", () => {expect(result).toEqual({data: [{- id: "OpenAI backup:a-model",+ id: "OpenAI-backup:a-model",provider: "OPENAI",canonicalSlug: "a-model",displayName: "a-model",createdAt: "1970-01-01T00:00:10.000Z",},{- id: "OpenAI backup:shared-model",+ id: "OpenAI-backup:shared-model",provider: "OPENAI",canonicalSlug: "shared-model",displayName: "shared-model",createdAt: "1970-01-01T00:00:15.000Z",},{- id: "OpenAI primary:a-model",+ id: "OpenAI-primary:a-model",provider: "OPENAI",canonicalSlug: "a-model",displayName: "a-model",createdAt: "1970-01-01T00:00:10.000Z",},{- id: "OpenAI primary:shared-model",+ id: "OpenAI-primary:shared-model",provider: "OPENAI",canonicalSlug: "shared-model",displayName: "shared-model",@@ -1599,7 +1679,7 @@ describe("AI gateway control plane", () => {});await caller.aiGateway.createConnection({orgId: org.id,- name: "OpenAI fallback",+ name: "OpenAI-fallback",provider: "OPENAI",credential: "sk-openai-fallback",});@@ -1632,7 +1712,7 @@ describe("AI gateway control plane", () => {).resolves.toEqual({data: [{- id: "OpenAI fallback:available-model",+ id: "OpenAI-fallback:available-model",provider: "OPENAI",canonicalSlug: "available-model",displayName: "available-model",diff --git a/web/src/components/design-system/internal/InputControl/InputControl.tsx b/web/src/components/design-system/internal/InputControl/InputControl.tsxindex 09f683b6bf..d6eb1cb35c 100644--- a/web/src/components/design-system/internal/InputControl/InputControl.tsx+++ b/web/src/components/design-system/internal/InputControl/InputControl.tsx@@ -2,6 +2,7 @@ import { Slot } from "@radix-ui/react-slot";import { cva } from "class-variance-authority";import { type LucideIcon } from "lucide-react";import { type ComponentPropsWithoutRef } from "react";+import { cn } from "@/src/utils/tailwind";const inputControlVariants = cva("bg-background ring-offset-background placeholder:text-muted-foreground focus-visible:ring-ring disabled:bg-muted/50 h-8 w-full rounded-md border text-sm file:border-0 file:bg-transparent file:text-sm file:font-bold focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden disabled:cursor-not-allowed disabled:opacity-50",@@ -17,7 +18,7 @@ const inputControlVariants = cva(false: null,},error: {- true: "border-destructive [&_svg]:text-destructive",+ true: "border-destructive focus-visible:ring-destructive/40 focus-visible:ring-offset-0 [&_svg]:text-destructive",false: "border-input",},disabled: {@@ -62,12 +63,14 @@ export function InputControl({}: InputControlProps) {const control = (<Slot- className={inputControlVariants({- contentLayout,- error,- disabled,- trailingAction: Boolean(trailingAction),- })}+ className={cn(+ inputControlVariants({+ contentLayout,+ error,+ disabled,+ trailingAction: Boolean(trailingAction),+ }),+ )}{...props}/>);diff --git a/web/src/features/ai-gateway/components/GatewayModelsPage/components/ConnectedGatewayModelsTable/components/GatewayModelsTable/GatewayModelsTable.tsx b/web/src/features/ai-gateway/components/GatewayModelsPage/components/ConnectedGatewayModelsTable/components/GatewayModelsTable/GatewayModelsTable.tsxindex 32cf739cfc..6bbd012d38 100644--- a/web/src/features/ai-gateway/components/GatewayModelsPage/components/ConnectedGatewayModelsTable/components/GatewayModelsTable/GatewayModelsTable.tsx+++ b/web/src/features/ai-gateway/components/GatewayModelsPage/components/ConnectedGatewayModelsTable/components/GatewayModelsTable/GatewayModelsTable.tsx@@ -1,4 +1,3 @@-import { SiAnthropic, SiOpenai } from "react-icons/si";import Link from "next/link";import { useMemo } from "react";@@ -12,9 +11,9 @@ import {type PaginationBarProps,} from "@/src/components/design-system/PaginationBar/PaginationBar";import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";+import { providerIcons } from "@/src/features/ai-gateway/constants/providerIcons";import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";import type { ApiFormat } from "@/src/features/ai-gateway/components/GatewayModelsPage/fns/aggregateModels";-import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";import { buildGatewayModelsUrl } from "@/src/features/ai-gateway/fns/gatewayUrls/buildGatewayModelsUrl";import type { LangfuseColumnDef } from "@/src/components/table/types";import type { GatewayModelRow } from "../../fns/filterGatewayModels";@@ -126,11 +125,6 @@ function ProviderBadge({);}-const providerIcons = {- OPENAI: SiOpenai,- ANTHROPIC: SiAnthropic,-} satisfies Record<GatewayProvider, typeof SiOpenai>;-const apiFormatLabels = {"Anthropic Messages": "Messages","OpenAI Chat Completions": "Completions",diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsxindex b25946e57e..50fddaf845 100644--- a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsx+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/ProviderDialogController.tsx@@ -15,8 +15,9 @@ import { Input } from "@/src/components/ui/input";import { Label } from "@/src/components/ui/label";import { CredentialFields } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/CredentialFields";import { ProviderSelect } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect";-import { DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR } from "@/src/features/ai-gateway/constants/gatewayConnection";import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";+import { isGatewayConnectionNameError } from "@/src/features/ai-gateway/fns/isGatewayConnectionNameError";+import { suggestGatewayConnectionName } from "@/src/features/ai-gateway/fns/suggestGatewayConnectionName";import type {GatewayConnection,GatewayProvider,@@ -26,9 +27,8 @@ import {reportNonTrpcError,reportTrpcErrorWithoutToast,} from "@/src/utils/api";--const INVALID_CONNECTION_NAME_ERROR =- "Connection names cannot contain a colon.";+import { getLiveConnectionNameError } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError";+import { useExistingConnectionNames } from "@/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/hooks/useExistingConnectionNames";export function ProviderDialogController({organizationId,@@ -45,10 +45,15 @@ export function ProviderDialogController({const [provider, setProvider] = useState<GatewayProvider>(connection?.provider ?? "OPENAI",);- const [name, setName] = useState(connection?.name ?? "");- const [nameError, setNameError] = useState<string | null>(null);+ /** `null` until the user types, so the name follows the provider's suggestion. */+ const [nameDraft, setNameDraft] = useState<string | null>(+ connection?.name ?? null,+ );const [credential, setCredential] = useState("");const utils = api.useUtils();+ const existingNames = useExistingConnectionNames(organizationId);+ const name =+ nameDraft ?? suggestGatewayConnectionName(provider, existingNames);const test = api.aiGateway.testConnection.useMutation({onError: (error) =>reportTrpcErrorWithoutToast(error, "ai-gateway-providers"),@@ -62,27 +67,56 @@ export function ProviderDialogController({reportTrpcErrorWithoutToast(error, "ai-gateway-providers"),});const isSaving = create.isPending || update.isPending;+ /** Stays true until reset, while the dialog awaits the list refetch before closing. */+ const isSaved = create.isSuccess || update.isSuccess;const isPending = test.isPending || isSaving;const saveError = create.error ?? update.error;- const isNameMutationError =- saveError?.message === DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR;- const isError =- test.isError ||- ((create.isError || update.isError) && !isNameMutationError);+ const serverNameError =+ saveError && isGatewayConnectionNameError(saveError.message)+ ? saveError.message+ : null;+ const isError = test.isError || (Boolean(saveError) && !serverNameError);const errorTitle = test.isError? "Provider validation failed": "Provider credential could not be saved";+ /** Skips the live check after saving, as the refetched list already holds the saved name. */+ const getNameError = (selectedConnection: GatewayConnection | null) =>+ isSaved+ ? null+ : (getLiveConnectionNameError({+ name,+ currentName: selectedConnection?.name,+ existingNames,+ }) ?? serverNameError);++ const resetMutations = () => {+ test.reset();+ create.reset();+ update.reset();+ };+const reset = () => {setProvider("OPENAI");- setName("");- setNameError(null);+ setNameDraft(null);setCredential("");+ resetMutations();+ };++ /** Resetting a pending mutation would re-enable saving mid-request. */+ const clearSaveError = () => {+ if (create.isError) create.reset();+ if (update.isError) update.reset();+ };++ const updateProvider = (value: GatewayProvider) => {+ setProvider(value);+ clearSaveError();};const updateName = (value: string) => {- setName(value);- setNameError(null);+ setNameDraft(value);+ clearSaveError();};const testCredential = async (@@ -103,12 +137,6 @@ export function ProviderDialogController({selectedConnection: GatewayConnection | null,closeDialog: () => void,) => {- if (name.includes(":")) {- setNameError(INVALID_CONNECTION_NAME_ERROR);- return;- }- setNameError(null);-try {if (selectedConnection) {await update.mutateAsync({@@ -126,6 +154,8 @@ export function ProviderDialogController({});}+ /** Pins the autofilled name so the refetched list cannot bump it to the next suffix. */+ setNameDraft(name);await Promise.all([utils.aiGateway.listConnections.invalidate({ orgId: organizationId }),utils.aiGateway.refreshModels.invalidate({ orgId: organizationId }),@@ -133,13 +163,6 @@ export function ProviderDialogController({closeDialog();reset();} catch (error) {- if (- error instanceof Error &&- error.message === DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR- ) {- setNameError(DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR);- return;- }reportNonTrpcError(error, "ai-gateway-providers");}};@@ -150,104 +173,109 @@ export function ProviderDialogController({closeOnInteractionOutside={false}onDismiss={reset}onBeforeClose={() => !isPending}- renderContent={({ state: selectedConnection, closeDialog }) => (- <>- <DialogHeader>- <DialogTitle>- {selectedConnection- ? "Edit provider credential"- : "Add provider credential"}- </DialogTitle>- </DialogHeader>- <DialogBody>- {selectedConnection ? (- <div>- <Label>Provider</Label>- <Input- className="mt-1.5"- value={providerLabels[selectedConnection.provider]}- disabled- />- </div>- ) : (- <ProviderSelect value={provider} onChange={setProvider} />- )}- <CredentialFields- name={name}- nameError={nameError}- credential={credential}- credentialPlaceholder={- selectedConnection- ? "Leave blank to keep current key"- : "Enter secret key"- }- onNameChange={updateName}- onCredentialChange={setCredential}- />- {selectedConnection && name.trim() !== selectedConnection.name ? (- <Alert variant="warning" icon={TriangleAlert}>- <Alert.Title>Renaming changes model slugs</Alert.Title>- <Alert.Description>- Requests using model slugs with the old connection name will- stop resolving.- </Alert.Description>- </Alert>- ) : null}- {isError ? (- <Alert variant="destructive">- <Alert.Title>{errorTitle}</Alert.Title>- <Alert.Description>- {test.error?.message ??- create.error?.message ??- update.error?.message ??- "The credential could not be saved or validated. Check the key and try again."}- </Alert.Description>- </Alert>- ) : test.isSuccess ? (- <Alert>- <Alert.Title>Provider credential is valid</Alert.Title>- <Alert.Description>No changes were saved.</Alert.Description>- </Alert>- ) : null}- </DialogBody>- <DialogFooter>- <Button- variant="secondary"- disabled={!credential || isPending}- loading={test.isPending}- onClick={() => testCredential(selectedConnection)}- >- Test- </Button>- <Button- disabled={- !name.trim() ||- (!selectedConnection && !credential) ||- isPending- }- loading={isSaving}- onClick={() => submit(selectedConnection, closeDialog)}- >- Test and save- </Button>- </DialogFooter>- </>- )}+ renderContent={({ state: selectedConnection, closeDialog }) => {+ const nameError = getNameError(selectedConnection);+ return (+ <>+ <DialogHeader>+ <DialogTitle>+ {selectedConnection+ ? "Edit provider credential"+ : "Add provider credential"}+ </DialogTitle>+ </DialogHeader>+ <DialogBody>+ {selectedConnection ? (+ <div>+ <Label>Provider</Label>+ <Input+ className="mt-1.5"+ value={providerLabels[selectedConnection.provider]}+ disabled+ />+ </div>+ ) : (+ <ProviderSelect value={provider} onChange={updateProvider} />+ )}+ <CredentialFields+ name={name}+ nameError={nameError}+ credential={credential}+ credentialPlaceholder={+ selectedConnection+ ? "Leave blank to keep current key"+ : "Enter secret key"+ }+ onNameChange={updateName}+ onCredentialChange={setCredential}+ />+ {selectedConnection && name.trim() !== selectedConnection.name ? (+ <Alert variant="warning" icon={TriangleAlert}>+ <Alert.Title>Renaming changes model slugs</Alert.Title>+ <Alert.Description>+ Requests using model slugs with the old connection name will+ stop resolving.+ </Alert.Description>+ </Alert>+ ) : null}+ {isError ? (+ <Alert variant="destructive">+ <Alert.Title>{errorTitle}</Alert.Title>+ <Alert.Description>+ {test.error?.message ??+ create.error?.message ??+ update.error?.message ??+ "The credential could not be saved or validated. Check the key and try again."}+ </Alert.Description>+ </Alert>+ ) : test.isSuccess ? (+ <Alert>+ <Alert.Title>Provider credential is valid</Alert.Title>+ <Alert.Description>No changes were saved.</Alert.Description>+ </Alert>+ ) : null}+ </DialogBody>+ <DialogFooter>+ <Button+ variant="secondary"+ disabled={!credential || isPending}+ loading={test.isPending}+ onClick={() => testCredential(selectedConnection)}+ >+ Test+ </Button>+ <Button+ disabled={+ !name.trim() ||+ Boolean(nameError) ||+ (!selectedConnection && !credential) ||+ isPending ||+ isSaved+ }+ loading={isSaving}+ onClick={() => submit(selectedConnection, closeDialog)}+ >+ Test and save+ </Button>+ </DialogFooter>+ </>+ );+ }}>{({ openDialog }) =>children({openAddDialog: () => {setProvider(connection?.provider ?? "OPENAI");- setName(connection?.name ?? "");- setNameError(null);+ setNameDraft(connection?.name ?? null);setCredential("");+ resetMutations();openDialog(connection ?? null);},openEditDialog: (selectedConnection) => {setProvider(selectedConnection.provider);- setName(selectedConnection.name);- setNameError(null);+ setNameDraft(selectedConnection.name);setCredential("");+ resetMutations();openDialog(selectedConnection);},})diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError.clienttest.ts b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError.clienttest.tsnew file mode 100644index 0000000000..d43597b007--- /dev/null+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError.clienttest.ts@@ -0,0 +1,45 @@+import { describe, expect, it } from "vitest";+import { GATEWAY_CONNECTION_NAME_ERRORS } from "@/src/features/ai-gateway/constants/gatewayConnection";+import { getLiveConnectionNameError } from "./getLiveConnectionNameError";++describe("getLiveConnectionNameError", () => {+ it("flags a name taken by another connection, ignoring case", () => {+ expect(+ getLiveConnectionNameError({+ name: "PRODUCTION",+ currentName: undefined,+ existingNames: ["production"],+ }),+ ).toBe(GATEWAY_CONNECTION_NAME_ERRORS.duplicate);+ });++ it("allows re-casing the connection's own name", () => {+ expect(+ getLiveConnectionNameError({+ name: "Production",+ currentName: "production",+ existingNames: ["production"],+ }),+ ).toBeNull();+ });++ it("leaves an unchanged legacy name alone", () => {+ expect(+ getLiveConnectionNameError({+ name: "OpenAI production",+ currentName: "OpenAI production",+ existingNames: ["OpenAI production"],+ }),+ ).toBeNull();+ });++ it("flags periods", () => {+ expect(+ getLiveConnectionNameError({+ name: "openai.v2",+ currentName: undefined,+ existingNames: [],+ }),+ ).toBe(GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters);+ });+});diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError.ts b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError.tsnew file mode 100644index 0000000000..9609010c95--- /dev/null+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/fns/getLiveConnectionNameError.ts@@ -0,0 +1,23 @@+import { GATEWAY_CONNECTION_NAME_ERRORS } from "@/src/features/ai-gateway/constants/gatewayConnection";+import { getGatewayConnectionNameError } from "@/src/features/ai-gateway/fns/getGatewayConnectionNameError";++/**+ * Mirrors the server checks while typing. An empty field stays quiet (saving is+ * disabled) and an unchanged name passes, matching the server's legacy-name rule.+ */+export function getLiveConnectionNameError(params: {+ name: string;+ currentName: string | undefined;+ existingNames: readonly string[];+}) {+ const name = params.name.trim();+ if (!name || name === params.currentName) return null;+ const formatError = getGatewayConnectionNameError(name);+ if (formatError) return formatError;+ const lowerName = name.toLowerCase();+ const isTaken = params.existingNames.some(+ (existing) =>+ existing !== params.currentName && existing.toLowerCase() === lowerName,+ );+ return isTaken ? GATEWAY_CONNECTION_NAME_ERRORS.duplicate : null;+}diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/hooks/useExistingConnectionNames.ts b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/hooks/useExistingConnectionNames.tsnew file mode 100644index 0000000000..586001ed35--- /dev/null+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderDialogController/hooks/useExistingConnectionNames.ts@@ -0,0 +1,14 @@+import { api } from "@/src/utils/api";++/** Shares the providers page's query input, so it reads the already-loaded pages. */+export function useExistingConnectionNames(organizationId: string) {+ const connectionsQuery = api.aiGateway.listConnections.useInfiniteQuery(+ { orgId: organizationId, limit: 50 },+ { getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined },+ );+ return (+ connectionsQuery.data?.pages.flatMap((page) =>+ page.data.map((connection) => connection.name),+ ) ?? []+ );+}diff --git a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsx b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsxindex 2b9323696f..225b521bee 100644--- a/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsx+++ b/web/src/features/ai-gateway/components/GatewayProvidersPage/components/ProviderSelect.tsx@@ -6,9 +6,12 @@ import {SelectTrigger,SelectValue,} from "@/src/components/ui/select";+import { providerIcons } from "@/src/features/ai-gateway/constants/providerIcons";import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";+const providers = Object.keys(providerLabels) as GatewayProvider[];+export function ProviderSelect({value,onChange,@@ -27,11 +30,19 @@ export function ProviderSelect({<SelectValue /></SelectTrigger><SelectContent>- {Object.entries(providerLabels).map(([provider, label]) => (- <SelectItem key={provider} value={provider}>- {label}- </SelectItem>- ))}+ {providers.map((provider) => {+ const ProviderIcon = providerIcons[provider];+ return (+ <SelectItem key={provider} value={provider}>+ <span className="flex items-center gap-2">+ <span className="bg-background flex size-6 shrink-0 items-center justify-center rounded-md border">+ <ProviderIcon aria-hidden className="icon-sm" />+ </span>+ {providerLabels[provider]}+ </span>+ </SelectItem>+ );+ })}</SelectContent></Select></div>diff --git a/web/src/features/ai-gateway/constants/gatewayConnection.ts b/web/src/features/ai-gateway/constants/gatewayConnection.tsindex 5757e9b255..58690e6caa 100644--- a/web/src/features/ai-gateway/constants/gatewayConnection.ts+++ b/web/src/features/ai-gateway/constants/gatewayConnection.ts@@ -1,2 +1,10 @@-export const DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR =- "A provider credential with this name already exists.";+export const GATEWAY_CONNECTION_NAME_MAX_LENGTH = 64;++export const GATEWAY_CONNECTION_NAME_ERRORS = {+ duplicate: "A provider credential with this name already exists.",+ required: "Enter a connection name.",+ tooLong: `Connection names can be at most ${GATEWAY_CONNECTION_NAME_MAX_LENGTH} characters.`,+ invalidCharacters:+ "Use only letters, numbers, hyphens, and underscores, starting with a letter or number.",+ reserved: "“ft” is reserved for OpenAI fine-tuned model IDs.",+} as const;diff --git a/web/src/features/ai-gateway/constants/providerIcons.ts b/web/src/features/ai-gateway/constants/providerIcons.tsnew file mode 100644index 0000000000..e11bb5ede8--- /dev/null+++ b/web/src/features/ai-gateway/constants/providerIcons.ts@@ -0,0 +1,8 @@+import { SiAnthropic, SiOpenai } from "react-icons/si";++import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";++export const providerIcons = {+ OPENAI: SiOpenai,+ ANTHROPIC: SiAnthropic,+} satisfies Record<GatewayProvider, typeof SiOpenai>;diff --git a/web/src/features/ai-gateway/fns/getGatewayConnectionNameError.ts b/web/src/features/ai-gateway/fns/getGatewayConnectionNameError.tsnew file mode 100644index 0000000000..1fb8fcfb11--- /dev/null+++ b/web/src/features/ai-gateway/fns/getGatewayConnectionNameError.ts@@ -0,0 +1,25 @@+import {+ GATEWAY_CONNECTION_NAME_ERRORS,+ GATEWAY_CONNECTION_NAME_MAX_LENGTH,+} from "@/src/features/ai-gateway/constants/gatewayConnection";++/** Keeps the `<name>:` model slug prefix safe in SDK configs, URLs, and env vars. */+const CONNECTION_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;++/** `ft` collides with the `ft:` prefix of OpenAI fine-tuned model IDs. */+const RESERVED_CONNECTION_NAMES = new Set(["ft"]);++/** Returns the format rule a trimmed connection name breaks, or `null`. */+export function getGatewayConnectionNameError(name: string) {+ if (!name) return GATEWAY_CONNECTION_NAME_ERRORS.required;+ if (name.length > GATEWAY_CONNECTION_NAME_MAX_LENGTH) {+ return GATEWAY_CONNECTION_NAME_ERRORS.tooLong;+ }+ if (!CONNECTION_NAME_PATTERN.test(name)) {+ return GATEWAY_CONNECTION_NAME_ERRORS.invalidCharacters;+ }+ if (RESERVED_CONNECTION_NAMES.has(name.toLowerCase())) {+ return GATEWAY_CONNECTION_NAME_ERRORS.reserved;+ }+ return null;+}diff --git a/web/src/features/ai-gateway/fns/isGatewayConnectionNameError.ts b/web/src/features/ai-gateway/fns/isGatewayConnectionNameError.tsnew file mode 100644index 0000000000..301151bdaf--- /dev/null+++ b/web/src/features/ai-gateway/fns/isGatewayConnectionNameError.ts@@ -0,0 +1,9 @@+import { GATEWAY_CONNECTION_NAME_ERRORS } from "@/src/features/ai-gateway/constants/gatewayConnection";++const connectionNameErrorMessages = new Set<string>(+ Object.values(GATEWAY_CONNECTION_NAME_ERRORS),+);++export function isGatewayConnectionNameError(message: string) {+ return connectionNameErrorMessages.has(message);+}diff --git a/web/src/features/ai-gateway/fns/suggestGatewayConnectionName.clienttest.ts b/web/src/features/ai-gateway/fns/suggestGatewayConnectionName.clienttest.tsnew file mode 100644index 0000000000..5dc734e280--- /dev/null+++ b/web/src/features/ai-gateway/fns/suggestGatewayConnectionName.clienttest.ts@@ -0,0 +1,20 @@+import { describe, expect, it } from "vitest";+import { suggestGatewayConnectionName } from "./suggestGatewayConnectionName";++describe("suggestGatewayConnectionName", () => {+ it("uses the lowercased provider label when no connection has taken it", () => {+ expect(suggestGatewayConnectionName("OPENAI", ["anthropic"])).toBe(+ "openai",+ );+ });++ it("appends the first free numeric suffix, ignoring case", () => {+ expect(+ suggestGatewayConnectionName("OPENAI", [+ "OpenAI",+ "openai-1",+ "OpenAI-3",+ ]),+ ).toBe("openai-2");+ });+});diff --git a/web/src/features/ai-gateway/fns/suggestGatewayConnectionName.ts b/web/src/features/ai-gateway/fns/suggestGatewayConnectionName.tsnew file mode 100644index 0000000000..5de5c2bf01--- /dev/null+++ b/web/src/features/ai-gateway/fns/suggestGatewayConnectionName.ts@@ -0,0 +1,18 @@+import { providerLabels } from "@/src/features/ai-gateway/constants/providerLabels";+import type { GatewayProvider } from "@/src/features/ai-gateway/types/gatewayProvider";++export function suggestGatewayConnectionName(+ provider: GatewayProvider,+ existingNames: Iterable<string>,+) {+ const base = providerLabels[provider].toLowerCase();+ /** Case-insensitive so suggestions never near-duplicate an existing name. */+ const taken = new Set(+ Array.from(existingNames, (name) => name.toLowerCase()),+ );+ if (!taken.has(base)) return base;+ for (let suffix = 1; ; suffix++) {+ const candidate = `${base}-${suffix}`;+ if (!taken.has(candidate)) return candidate;+ }+}diff --git a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.ts b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.tsindex b694f08d98..0ea00f1347 100644--- a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.ts+++ b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderRepository.ts@@ -82,6 +82,26 @@ export class GatewayProviderRepository {return connection ? toSafeConnection(connection) : null;}+ async hasConnectionNamedIgnoringCase(params: {+ organizationId: string;+ name: string;+ excludeId?: string;+ }) {+ /** Postgres runs insensitive `equals` as an unescaped `ILIKE`, so `_` matches any character. */+ const candidates = await this.prisma.gatewayAiConnection.findMany({+ where: {+ organizationId: params.organizationId,+ name: { equals: params.name, mode: "insensitive" },+ ...(params.excludeId ? { id: { not: params.excludeId } } : {}),+ },+ select: { name: true },+ });+ const lowerName = params.name.toLowerCase();+ return candidates.some(+ (candidate) => candidate.name.toLowerCase() === lowerName,+ );+ }+async createConnection(params: {organizationId: string;name: string;diff --git a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.ts b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.tsindex 1b6fa8ea66..e674091b47 100644--- a/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.ts+++ b/web/src/features/ai-gateway/server/provider/connection/gatewayProviderService.ts@@ -14,13 +14,14 @@ import { LLMAdapter, testModelCall } from "@langfuse/shared/src/server";import { getDisplaySecretKey } from "@langfuse/shared/src/server/auth/apiKeys";import { auditLog } from "@/src/features/audit-logs/server";-import { DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR } from "@/src/features/ai-gateway/constants/gatewayConnection";+import { GATEWAY_CONNECTION_NAME_ERRORS } from "@/src/features/ai-gateway/constants/gatewayConnection";import type { OrgAuthedContext } from "@/src/server/api/trpc";import {type GatewayProviderName,getGatewayProviderDefinition,} from "@/src/features/ai-gateway/server/provider/registry";import { invalidateGatewayResolveCacheForOrganization } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";+import { parseGatewayConnectionName } from "./parseGatewayConnectionName";import { GatewayProviderRepository } from "./gatewayProviderRepository";import { GatewayModelCatalogService } from "../models/gatewayModelCatalogService";@@ -75,6 +76,11 @@ export class GatewayProviderService {credential: string;session: OrgAuthedContext["session"];}) {+ const name = parseGatewayConnectionName(params.name);+ await this.assertNameAvailable({+ organizationId: params.organizationId,+ name,+ });await this.testCredential({provider: params.provider,credential: params.credential,@@ -83,7 +89,7 @@ export class GatewayProviderService {try {connection = await this.repository.createConnection({organizationId: params.organizationId,- name: params.name,+ name,provider: params.provider,encryptedCredential: encrypt(params.credential),displaySecret: getDisplaySecretKey(params.credential),@@ -120,6 +126,18 @@ export class GatewayProviderService {id: params.id,});if (!existing) throw new LangfuseNotFoundError("Gateway connection");+ /** Unchanged names skip validation so pre-existing names stay editable. */+ const name =+ params.name === undefined || params.name.trim() === existing.name+ ? undefined+ : parseGatewayConnectionName(params.name);+ if (name !== undefined) {+ await this.assertNameAvailable({+ organizationId: params.organizationId,+ name,+ excludeId: existing.id,+ });+ }if (existing.status === "ERROR" &¶ms.status === "ENABLED" &&@@ -141,7 +159,7 @@ export class GatewayProviderService {updated = await this.repository.updateConnection({organizationId: params.organizationId,id: params.id,- name: params.name,+ name,status: params.credential ? "ENABLED" : params.status,encryptedCredential: params.credential? encrypt(params.credential)@@ -201,6 +219,17 @@ export class GatewayProviderService {return deleted;}+ /** Names differing only by case read as the same slug prefix, so they count as taken. */+ private async assertNameAvailable(params: {+ organizationId: string;+ name: string;+ excludeId?: string;+ }) {+ if (await this.repository.hasConnectionNamedIgnoringCase(params)) {+ throw new InvalidRequestError(GATEWAY_CONNECTION_NAME_ERRORS.duplicate);+ }+ }+async updateAllowedModels(params: {organizationId: string;id: string;@@ -247,7 +276,7 @@ function rethrowConnectionMutationError(error: unknown): never {error instanceof Prisma.PrismaClientKnownRequestError &&error.code === "P2002") {- throw new InvalidRequestError(DUPLICATE_GATEWAY_CONNECTION_NAME_ERROR);+ throw new InvalidRequestError(GATEWAY_CONNECTION_NAME_ERRORS.duplicate);}throw error;}diff --git a/web/src/features/ai-gateway/server/provider/connection/parseGatewayConnectionName.ts b/web/src/features/ai-gateway/server/provider/connection/parseGatewayConnectionName.tsnew file mode 100644index 0000000000..759f649c04--- /dev/null+++ b/web/src/features/ai-gateway/server/provider/connection/parseGatewayConnectionName.ts@@ -0,0 +1,10 @@+import { InvalidRequestError } from "@langfuse/shared";++import { getGatewayConnectionNameError } from "@/src/features/ai-gateway/fns/getGatewayConnectionNameError";++export function parseGatewayConnectionName(rawName: string) {+ const name = rawName.trim();+ const error = getGatewayConnectionNameError(name);+ if (error) throw new InvalidRequestError(error);+ return name;+}diff --git a/web/src/features/ai-gateway/server/provider/registry.servertest.ts b/web/src/features/ai-gateway/server/provider/registry.servertest.tsindex f157efe6b5..0415ffa860 100644--- a/web/src/features/ai-gateway/server/provider/registry.servertest.ts+++ b/web/src/features/ai-gateway/server/provider/registry.servertest.ts@@ -38,7 +38,10 @@ describe("AI gateway provider registry", () => {const validator = vi.fn().mockRejectedValue(new Error("invalid provider credential"));- const service = new GatewayProviderService({} as PrismaClient, validator);+ const prisma = {+ gatewayAiConnection: { findMany: vi.fn().mockResolvedValue([]) },+ } as unknown as PrismaClient;+ const service = new GatewayProviderService(prisma, validator);await expect(service.create({diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.tsindex 7c58d1c5e3..548eaa8524 100644--- a/web/src/features/ai-gateway/server/router.ts+++ b/web/src/features/ai-gateway/server/router.ts@@ -32,14 +32,8 @@ import {} from "./provider";const organizationInput = z.object({ orgId: z.string() });-const connectionNameSchema = z- .string()- .trim()- .min(1)- .max(200)- .refine((name) => !name.includes(":"), {- message: "Connection names cannot contain a colon.",- });+/** Naming rules live in `parseGatewayConnectionName` so the dialog can show them on the field. */+const connectionNameSchema = z.string().max(1_000);const paginatedOrganizationInput = organizationInput.extend({cursor: z.string().optional(),limit: z.number().int().min(1).max(100).default(50),diff --git a/web/src/utils/trpcErrorClassification.ts b/web/src/utils/trpcErrorClassification.tsindex f16308fe50..590d575d90 100644--- a/web/src/utils/trpcErrorClassification.ts+++ b/web/src/utils/trpcErrorClassification.ts@@ -20,6 +20,8 @@ export const EXPECTED_TRPC_BAD_REQUEST_PATHS = ["datasets.triggerRemoteExperiment","datasets.upsertRemoteExperiment","models.upsert",+ "aiGateway.createConnection",+ "aiGateway.updateConnection",] as const;const FAILED_FETCH_MESSAGE = /^failed to fetch(?: \([^)]+\))?$/i;