🪢 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(llm): add organization-level connections (#18450)
Tobias Wochinger40f84e750098
diff --git a/fern/apis/server/definition/llm-connections.yml b/fern/apis/server/definition/llm-connections.ymlindex 431d5f9485..e633bc1366 100644--- a/fern/apis/server/definition/llm-connections.yml+++ b/fern/apis/server/definition/llm-connections.yml@@ -33,6 +33,33 @@ service:path-parameters:id: stringresponse: DeleteLlmConnectionResponse+ listOrganization:+ method: GET+ docs: Get all organization-level LLM connections. Requires an organization-scoped API key.+ path: /organizations/llm-connections+ request:+ name: GetOrganizationLlmConnectionsRequest+ query-parameters:+ page:+ type: optional<integer>+ docs: page number, starts at 1+ limit:+ type: optional<integer>+ docs: limit of items per page+ response: PaginatedLlmConnections+ upsertOrganization:+ method: PUT+ docs: Create or update an organization-level LLM connection. The connection is upserted on provider and inherited by projects without a provider override. Requires an organization-scoped API key.+ path: /organizations/llm-connections+ request: UpsertLlmConnectionRequest+ response: LlmConnection+ deleteOrganization:+ method: DELETE+ docs: Delete an organization-level LLM connection by id. Evaluators in projects without an override for the provider are automatically paused. Requires an organization-scoped API key.+ path: /organizations/llm-connections/{id}+ path-parameters:+ id: string+ response: DeleteLlmConnectionResponsetypes:LlmConnection:diff --git a/packages/shared/prisma/migrations/20261013120000_add_organization_llm_connections/migration.sql b/packages/shared/prisma/migrations/20261013120000_add_organization_llm_connections/migration.sqlnew file mode 100644index 0000000000..2f9e239a27--- /dev/null+++ b/packages/shared/prisma/migrations/20261013120000_add_organization_llm_connections/migration.sql@@ -0,0 +1,14 @@+-- Allow LLM connections to be owned by either a project or an organization.+ALTER TABLE "llm_api_keys"+ ADD COLUMN IF NOT EXISTS "organization_id" TEXT,+ ALTER COLUMN "project_id" DROP NOT NULL;++ALTER TABLE "llm_api_keys"+ ADD CONSTRAINT "llm_api_keys_owner_check"+ CHECK (num_nonnulls("project_id", "organization_id") = 1),+ ADD CONSTRAINT "llm_api_keys_organization_id_fkey"+ FOREIGN KEY ("organization_id") REFERENCES "organizations"("id")+ ON DELETE CASCADE ON UPDATE CASCADE;++CREATE UNIQUE INDEX IF NOT EXISTS "llm_api_keys_organization_id_provider_key"+ ON "llm_api_keys"("organization_id", "provider");diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prismaindex dda40d1a22..e2c431c3b2 100644--- a/packages/shared/prisma/schema.prisma+++ b/packages/shared/prisma/schema.prisma@@ -136,6 +136,7 @@ model Organization {verifiedDomains VerifiedDomain[]gatewayConfig GatewayConfig?gatewayAiConnections GatewayAiConnection[]+ organizationLlmApiKeys LlmApiKeys[] @relation("OrganizationLlmApiKeys")roleAssignments RoleAssignment[] @relation("RoleAssignmentTenant")deprecatedRoleAssignments RoleAssignment[] @relation("DeprecatedRoleAssignmentOrganization") @ignore@@ -505,14 +506,18 @@ model LlmApiKeys {extraHeaderKeys String[] @default([]) @map("extra_header_keys")config Json?- projectId String @map("project_id")- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)+ projectId String? @map("project_id")+ project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)+ organizationId String? @map("organization_id")+ organization Organization? @relation("OrganizationLlmApiKeys", fields: [organizationId], references: [id], onDelete: Cascade)+DefaultLlmModel DefaultLlmModel[] @relation("LlmApiKeyId")topicSummaryConfigs TopicConfig[] @relation("TopicConfigSummaryLlmApiKey")topicEmbeddingConfigs TopicConfig[] @relation("TopicConfigEmbeddingLlmApiKey")topicClusteringConfigs TopicConfig[] @relation("TopicConfigClusteringLlmApiKey")@@unique([projectId, provider])+ @@unique([organizationId, provider])@@map("llm_api_keys")}diff --git a/packages/shared/src/features/rbac/organizationAccessRights.ts b/packages/shared/src/features/rbac/organizationAccessRights.tsindex 97786ea4dd..1ed313763e 100644--- a/packages/shared/src/features/rbac/organizationAccessRights.ts+++ b/packages/shared/src/features/rbac/organizationAccessRights.ts@@ -7,6 +7,8 @@ export const organizationScopes = ["organization:CRUD_apiKeys","organization:update","organization:delete",+ "organizationLlmApiKeys:read",+ "organizationLlmApiKeys:CUD","gateway:manage","gateway:invoke","organizationMembers:read",@@ -27,6 +29,8 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {"organization:CRUD_apiKeys","organization:update","organization:delete",+ "organizationLlmApiKeys:read",+ "organizationLlmApiKeys:CUD","gateway:manage","gateway:invoke","organizationMembers:CUD",@@ -42,6 +46,8 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {"projects:create","projects:transfer_org","organization:update",+ "organizationLlmApiKeys:read",+ "organizationLlmApiKeys:CUD","gateway:manage","gateway:invoke","organizationMembers:CUD",diff --git a/packages/shared/src/server/index.ts b/packages/shared/src/server/index.tsindex d1bf952d56..78086b12ec 100644--- a/packages/shared/src/server/index.ts+++ b/packages/shared/src/server/index.ts@@ -28,6 +28,7 @@ export * from "./services/InMemoryFilterService";export * from "./automations";export * from "./services/DatasetService";export * from "./services/commentFilterService";+export * from "./services/LLMApiKeys";export * from "./datasets/schemaValidation";export * from "./datasets/schemaTypes";export * from "./evalJobConfigCache";diff --git a/packages/shared/src/server/llm/types.ts b/packages/shared/src/server/llm/types.tsindex 0a88d3d140..cba1d11efc 100644--- a/packages/shared/src/server/llm/types.ts+++ b/packages/shared/src/server/llm/types.ts@@ -1,4 +1,3 @@-import { LlmApiKeys } from "@prisma/client";import z from "zod";import {BedrockConfigSchema,@@ -548,10 +547,7 @@ export const LLMApiKeySchema = z// https://github.com/colinhacks/zod?tab=readme-ov-file#strict.strict();-export type LLMApiKey =- z.infer<typeof LLMApiKeySchema> extends LlmApiKeys- ? z.infer<typeof LLMApiKeySchema>- : never;+export type LLMApiKey = z.infer<typeof LLMApiKeySchema>;export enum LangfuseInternalTraceEnvironment {PromptExperiments = "langfuse-prompt-experiment",diff --git a/packages/shared/src/server/services/DefaultEvaluationModelService/DefaultEvalModelService.ts b/packages/shared/src/server/services/DefaultEvaluationModelService/DefaultEvalModelService.tsindex df3e0876d4..81aa5c04d2 100644--- a/packages/shared/src/server/services/DefaultEvaluationModelService/DefaultEvalModelService.ts+++ b/packages/shared/src/server/services/DefaultEvaluationModelService/DefaultEvalModelService.ts@@ -1,9 +1,20 @@+import { EvaluatorBlockReason, Prisma } from "@prisma/client";import z from "zod";import { prisma } from "../../../db";import { ForbiddenError, LangfuseNotFoundError } from "../../../errors";import { getClientInitiatedNonStreamingLlmTimeoutMs } from "../../llm/llmText";import { LLMAdapter, LLMApiKeySchema, ZodModelConfig } from "../../llm/types";import { testModelCall } from "../../llm/testModelCall";+import {+ findCompatibleLlmApiKeysForProjects,+ resolveLlmApiKey,+} from "../LLMApiKeys";+import {+ blockEvaluatorsUsingDefaultModel,+ EvaluatorBlockSource,+ finalizeEvaluatorBlocks,+ type EvaluatorBlockResult,+} from "../blockEvaluators";type ValidConfig = {provider: string;@@ -34,15 +45,9 @@ export class DefaultEvalModelService {model: string;modelParams?: z.infer<typeof ZodModelConfig>;}) {- const { projectId, provider, adapter, model, modelParams } = params;+ const { projectId, provider, model, modelParams } = params;- // Find the LLM API key for the provider- const llmApiKey = await prisma.llmApiKeys.findFirst({- where: {- projectId,- provider,- },- });+ const llmApiKey = await resolveLlmApiKey({ projectId, provider });if (!llmApiKey) {throw new LangfuseNotFoundError(@@ -76,7 +81,7 @@ export class DefaultEvalModelService {update: {llmApiKeyId: llmApiKey.id,provider,- adapter,+ adapter: llmApiKey.adapter,model,modelParams: modelParams ? modelParams : undefined,},@@ -84,13 +89,140 @@ export class DefaultEvalModelService {projectId,llmApiKeyId: llmApiKey.id,provider,- adapter,+ adapter: llmApiKey.adapter,model,modelParams: modelParams ? modelParams : undefined,},});}+ /**+ * Keeps default-model references valid before an LLM connection is deleted.+ */+ public static async handleLlmApiKeyDeletion(params: {+ tx: Prisma.TransactionClient;+ llmApiKeyId: string;+ llmApiKeyOwner: {+ projectId: string | null;+ organizationId: string | null;+ };+ provider: string;+ adapter: string;+ }): Promise<+ Array<{ projectId: string; defaultModelBlock: EvaluatorBlockResult }>+ > {+ const { tx, llmApiKeyId, llmApiKeyOwner, provider, adapter } = params;+ const defaultModels = await tx.defaultLlmModel.findMany({+ where: {+ llmApiKeyId,+ LlmApiKey: llmApiKeyOwner,+ },+ select: { id: true, projectId: true },+ });++ if (defaultModels.length === 0) {+ return [];+ }++ const replacements = await findCompatibleLlmApiKeysForProjects({+ tx,+ projectIds: defaultModels.map(({ projectId }) => projectId),+ provider,+ adapter,+ excludedLlmApiKeyId: llmApiKeyId,+ });+ const modelsWithoutReplacement = [];+ for (const defaultModel of defaultModels) {+ const replacement = replacements.get(defaultModel.projectId);+ if (replacement) {+ await tx.defaultLlmModel.update({+ where: { id: defaultModel.id },+ data: { llmApiKeyId: replacement.id },+ });+ } else {+ modelsWithoutReplacement.push(defaultModel);+ }+ }++ const blockResults: Array<{+ projectId: string;+ defaultModelBlock: EvaluatorBlockResult;+ }> = [];+ for (const { projectId } of modelsWithoutReplacement) {+ blockResults.push({+ projectId,+ defaultModelBlock: await blockEvaluatorsUsingDefaultModel({+ tx,+ projectId,+ }),+ });+ }++ await tx.defaultLlmModel.deleteMany({+ where: {+ id: { in: modelsWithoutReplacement.map(({ id }) => id) },+ },+ });++ return blockResults;+ }++ /**+ * Keeps the default-model connection valid after a project changes organization.+ */+ public static async reconcileConnectionForProject(+ projectId: string,+ ): Promise<void> {+ const defaultModelBlock = await prisma.$transaction(async (tx) => {+ const defaultModel = await tx.defaultLlmModel.findUnique({+ where: { projectId },+ include: {+ Project: { select: { orgId: true } },+ LlmApiKey: {+ select: { projectId: true, organizationId: true },+ },+ },+ });+ if (!defaultModel) return null;++ const connectionIsAccessible =+ defaultModel.LlmApiKey.projectId === projectId ||+ defaultModel.LlmApiKey.organizationId === defaultModel.Project.orgId;+ if (connectionIsAccessible) return null;++ const replacements = await findCompatibleLlmApiKeysForProjects({+ tx,+ projectIds: [projectId],+ provider: defaultModel.provider,+ adapter: defaultModel.adapter,+ excludedLlmApiKeyId: defaultModel.llmApiKeyId,+ });+ const replacement = replacements.get(projectId);+ if (replacement) {+ await tx.defaultLlmModel.update({+ where: { id: defaultModel.id },+ data: { llmApiKeyId: replacement.id },+ });+ return null;+ }++ const block = await blockEvaluatorsUsingDefaultModel({ tx, projectId });+ await tx.defaultLlmModel.delete({ where: { id: defaultModel.id } });+ return block;+ });++ if (defaultModelBlock) {+ await finalizeEvaluatorBlocks({+ projectId,+ source: EvaluatorBlockSource.DEFAULT_EVAL_MODEL_DELETION,+ evaluatorIdsByReason: {+ [EvaluatorBlockReason.DEFAULT_EVAL_MODEL_MISSING]:+ defaultModelBlock.blockedEvaluatorIds,+ },+ });+ }+ }+/*** Simple validation that can also be used client side* Validates if the provider, model, and model parameters are valid@@ -182,12 +314,9 @@ export class DefaultEvalModelService {};}- // Check if API key exists for this provider- const apiKey = await prisma.llmApiKeys.findFirst({- where: {- projectId,- provider: selectedModel.provider,- },+ const apiKey = await resolveLlmApiKey({+ projectId,+ provider: selectedModel.provider,});const parsedKey = LLMApiKeySchema.safeParse(apiKey);@@ -204,6 +333,7 @@ export class DefaultEvalModelService {config: {...selectedModel,apiKey: parsedKey.data,+ adapter: parsedKey.data.adapter,},};}diff --git a/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyRepository.ts b/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyRepository.tsnew file mode 100644index 0000000000..7534006948--- /dev/null+++ b/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyRepository.ts@@ -0,0 +1,36 @@+import { prisma } from "../../../db";++export async function findProjectLlmApiKeyCandidates(params: {+ projectId: string;+ provider?: string;+}) {+ const providerWhere =+ params.provider !== undefined ? { provider: params.provider } : {};+ const project = await prisma.project.findUnique({+ where: {+ id: params.projectId,+ deletedAt: null,+ },+ select: {+ LlmApiKeys: {+ where: providerWhere,+ },+ organization: {+ select: {+ organizationLlmApiKeys: {+ where: providerWhere,+ },+ },+ },+ },+ });++ if (!project) {+ return null;+ }++ return {+ projectConnections: project.LlmApiKeys,+ organizationConnections: project.organization.organizationLlmApiKeys,+ };+}diff --git a/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyService.test.ts b/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyService.test.tsnew file mode 100644index 0000000000..cce6d34ae7--- /dev/null+++ b/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyService.test.ts@@ -0,0 +1,140 @@+import { type LlmApiKeys } from "@prisma/client";+import { describe, expect, it, vi } from "vitest";++import { listEffectiveLlmApiKeys, resolveLlmApiKey } from "./LlmApiKeyService";++const mocks = vi.hoisted(() => ({+ findProjectLlmApiKeyCandidates: vi.fn(),+}));++vi.mock("./LlmApiKeyRepository", () => ({+ findProjectLlmApiKeyCandidates: mocks.findProjectLlmApiKeyCandidates,+}));++const projectId = "project-id";+const organizationId = "organization-id";++const connection = (+ overrides: Partial<LlmApiKeys> & Pick<LlmApiKeys, "id" | "provider">,+): LlmApiKeys => {+ const { id, provider, ...rest } = overrides;+ return {+ id,+ provider,+ adapter: "openai",+ secretKey: "encrypted-secret",+ displaySecretKey: "...cret",+ baseURL: null,+ customModels: [],+ withDefaultModels: true,+ extraHeaders: null,+ extraHeaderKeys: [],+ config: null,+ projectId: null,+ organizationId: null,+ createdAt: new Date("2026-01-01T00:00:00.000Z"),+ updatedAt: new Date("2026-01-01T00:00:00.000Z"),+ ...rest,+ };+};++describe("LlmApiKeyService", () => {+ it("prefers a project connection over an organization connection", async () => {+ mocks.findProjectLlmApiKeyCandidates.mockResolvedValue({+ organizationConnections: [+ connection({+ id: "organization-connection",+ provider: "openai",+ organizationId,+ }),+ ],+ projectConnections: [+ connection({+ id: "project-connection",+ provider: "openai",+ projectId,+ }),+ ],+ });++ const resolved = await resolveLlmApiKey({+ projectId,+ provider: "openai",+ });++ expect(resolved).toMatchObject({+ id: "project-connection",+ projectId,+ provider: "openai",+ });+ expect(resolved).not.toHaveProperty("organizationId");+ });++ it("falls back to the organization connection", async () => {+ mocks.findProjectLlmApiKeyCandidates.mockResolvedValue({+ projectConnections: [],+ organizationConnections: [+ connection({+ id: "organization-connection",+ provider: "anthropic",+ organizationId,+ }),+ ],+ });++ const resolved = await resolveLlmApiKey({+ projectId,+ provider: "anthropic",+ });++ expect(resolved).toMatchObject({+ id: "organization-connection",+ projectId,+ provider: "anthropic",+ });+ expect(resolved).not.toHaveProperty("organizationId");+ });++ it("lists one effective connection per provider with override metadata", async () => {+ mocks.findProjectLlmApiKeyCandidates.mockResolvedValue({+ organizationConnections: [+ connection({+ id: "organization-openai",+ provider: "openai",+ organizationId,+ }),+ connection({+ id: "organization-anthropic",+ provider: "anthropic",+ organizationId,+ }),+ ],+ projectConnections: [+ connection({+ id: "project-openai",+ provider: "openai",+ projectId,+ }),+ ],+ });++ const effective = await listEffectiveLlmApiKeys(projectId);++ expect(effective).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ scope: "project",+ organizationConnectionId: "organization-openai",+ connection: expect.objectContaining({ id: "project-openai" }),+ }),+ expect.objectContaining({+ scope: "organization",+ connection: expect.objectContaining({+ id: "organization-anthropic",+ }),+ }),+ ]),+ );+ expect(effective).toHaveLength(2);+ });+});diff --git a/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyService.ts b/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyService.tsnew file mode 100644index 0000000000..bb02c13b34--- /dev/null+++ b/packages/shared/src/server/services/LLMApiKeys/LlmApiKeyService.ts@@ -0,0 +1,82 @@+import { type LlmApiKeys } from "@prisma/client";++import { LLMApiKeySchema, type LLMApiKey } from "../../llm/types";+import { findProjectLlmApiKeyCandidates } from "./LlmApiKeyRepository";++export type EffectiveLlmApiKey = {+ connection: LLMApiKey;+ scope: "project" | "organization";+ organizationConnectionId?: string;+};++const toProjectConnection = (+ connection: LlmApiKeys,+ projectId: string,+): LLMApiKey => {+ const { organizationId: _organizationId, ...storedConnection } = connection;++ return LLMApiKeySchema.parse({+ ...storedConnection,+ projectId,+ });+};++export async function resolveLlmApiKey(params: {+ projectId: string;+ provider: string;+}): Promise<LLMApiKey | null> {+ const candidates = await findProjectLlmApiKeyCandidates(params);+ if (!candidates) {+ return null;+ }++ const connection =+ candidates.projectConnections[0] ?? candidates.organizationConnections[0];++ return connection ? toProjectConnection(connection, params.projectId) : null;+}++export async function listEffectiveLlmApiKeys(+ projectId: string,+): Promise<EffectiveLlmApiKey[]> {+ const candidates = await findProjectLlmApiKeyCandidates({ projectId });+ if (!candidates) {+ return [];+ }++ const organizationConnections = new Map(+ candidates.organizationConnections.map((connection) => [+ connection.provider,+ connection,+ ]),+ );+ const projectConnections = new Map(+ candidates.projectConnections.map((connection) => [+ connection.provider,+ connection,+ ]),+ );++ const providers = new Set([+ ...organizationConnections.keys(),+ ...projectConnections.keys(),+ ]);++ return Array.from(providers, (provider): EffectiveLlmApiKey => {+ const projectConnection = projectConnections.get(provider);+ const organizationConnection = organizationConnections.get(provider);+ const connection = projectConnection ?? organizationConnection;++ if (!connection) {+ throw new Error(`LLM connection "${provider}" could not be resolved`);+ }++ return {+ connection: toProjectConnection(connection, projectId),+ scope: projectConnection ? "project" : "organization",+ ...(projectConnection && organizationConnection+ ? { organizationConnectionId: organizationConnection.id }+ : {}),+ };+ });+}diff --git a/packages/shared/src/server/services/LLMApiKeys/findCompatibleLlmApiKeysForProjects.test.ts b/packages/shared/src/server/services/LLMApiKeys/findCompatibleLlmApiKeysForProjects.test.tsnew file mode 100644index 0000000000..7c90d29a0f--- /dev/null+++ b/packages/shared/src/server/services/LLMApiKeys/findCompatibleLlmApiKeysForProjects.test.ts@@ -0,0 +1,57 @@+import { type LlmApiKeys, type Prisma } from "@prisma/client";+import { describe, expect, it, vi } from "vitest";++import { findCompatibleLlmApiKeysForProjects } from "./findCompatibleLlmApiKeysForProjects";++const connection = (+ id: string,+ owner: { projectId?: string; organizationId?: string },+) =>+ ({+ id,+ projectId: owner.projectId ?? null,+ organizationId: owner.organizationId ?? null,+ }) as LlmApiKeys;++describe("findCompatibleLlmApiKeysForProjects", () => {+ it("prefers a project connection over its organization connection", async () => {+ const tx = {+ project: {+ findMany: vi.fn().mockResolvedValue([+ { id: "project", orgId: "organization" },+ { id: "inherited-project", orgId: "organization" },+ ]),+ },+ llmApiKeys: {+ findMany: vi.fn().mockResolvedValue([+ connection("organization-key", {+ organizationId: "organization",+ }),+ connection("project-key", { projectId: "project" }),+ ]),+ },+ } as unknown as Prisma.TransactionClient;++ const replacements = await findCompatibleLlmApiKeysForProjects({+ tx,+ projectIds: ["project", "inherited-project"],+ provider: "shared-name",+ adapter: "openai",+ excludedLlmApiKeyId: "deleted-key",+ });++ expect(replacements.get("project")?.id).toBe("project-key");+ expect(replacements.get("inherited-project")?.id).toBe("organization-key");+ expect(tx.llmApiKeys.findMany).toHaveBeenCalledWith({+ where: {+ provider: "shared-name",+ adapter: "openai",+ id: { not: "deleted-key" },+ OR: [+ { projectId: { in: ["project", "inherited-project"] } },+ { organizationId: { in: ["organization"] } },+ ],+ },+ });+ });+});diff --git a/packages/shared/src/server/services/LLMApiKeys/findCompatibleLlmApiKeysForProjects.ts b/packages/shared/src/server/services/LLMApiKeys/findCompatibleLlmApiKeysForProjects.tsnew file mode 100644index 0000000000..5223c3c9c6--- /dev/null+++ b/packages/shared/src/server/services/LLMApiKeys/findCompatibleLlmApiKeysForProjects.ts@@ -0,0 +1,52 @@+import { type LlmApiKeys, type Prisma } from "@prisma/client";++export async function findCompatibleLlmApiKeysForProjects(params: {+ tx: Prisma.TransactionClient;+ projectIds: string[];+ provider: string;+ adapter: string;+ excludedLlmApiKeyId?: string;+}): Promise<Map<string, LlmApiKeys>> {+ const projectIds = [...new Set(params.projectIds)];+ if (projectIds.length === 0) return new Map();++ const projects = await params.tx.project.findMany({+ where: { id: { in: projectIds }, deletedAt: null },+ select: { id: true, orgId: true },+ });+ const organizationIds = [...new Set(projects.map(({ orgId }) => orgId))];+ const connections = await params.tx.llmApiKeys.findMany({+ where: {+ provider: params.provider,+ adapter: params.adapter,+ ...(params.excludedLlmApiKeyId+ ? { id: { not: params.excludedLlmApiKeyId } }+ : {}),+ OR: [+ { projectId: { in: projectIds } },+ { organizationId: { in: organizationIds } },+ ],+ },+ });++ const projectConnections = new Map(+ connections.flatMap((connection) =>+ connection.projectId ? [[connection.projectId, connection] as const] : [],+ ),+ );+ const organizationConnections = new Map(+ connections.flatMap((connection) =>+ connection.organizationId+ ? [[connection.organizationId, connection] as const]+ : [],+ ),+ );++ return new Map(+ projects.flatMap(({ id, orgId }) => {+ const connection =+ projectConnections.get(id) ?? organizationConnections.get(orgId);+ return connection ? [[id, connection] as const] : [];+ }),+ );+}diff --git a/packages/shared/src/server/services/LLMApiKeys/index.ts b/packages/shared/src/server/services/LLMApiKeys/index.tsnew file mode 100644index 0000000000..1d1a94f3b1--- /dev/null+++ b/packages/shared/src/server/services/LLMApiKeys/index.ts@@ -0,0 +1,3 @@+export * from "./LlmApiKeyRepository";+export * from "./LlmApiKeyService";+export * from "./findCompatibleLlmApiKeysForProjects";diff --git a/packages/shared/src/server/topics/model-config.test.ts b/packages/shared/src/server/topics/model-config.test.tsindex eb1317550d..f53258ed82 100644--- a/packages/shared/src/server/topics/model-config.test.ts+++ b/packages/shared/src/server/topics/model-config.test.ts@@ -3,14 +3,19 @@ import { LLMAdapter } from "../llm/types";import {getEnabledTopicsModels,prepareTopicsModelSettings,+ reconcileTopicsConnectionsForProject,writeTopicsModelSettings,} from "./model-config";const mocks = vi.hoisted(() => ({keys: vi.fn(),+ projects: vi.fn(),configs: vi.fn(),current: vi.fn(),+ update: vi.fn(),upsert: vi.fn(),+ transaction: vi.fn(),+ invalidate: vi.fn(),hasEmbeddings: vi.fn(),testCall: vi.fn(),embed: vi.fn(),@@ -18,16 +23,30 @@ const mocks = vi.hoisted(() => ({vi.mock("./clickhouse", () => ({ hasTopicEmbeddings: mocks.hasEmbeddings }));vi.mock("../llm/testModelCall", () => ({ testModelCall: mocks.testCall }));vi.mock("./embeddings", () => ({ generateTopicEmbeddings: mocks.embed }));-vi.mock("../../db", () => ({- prisma: {+vi.mock("./processing-gate", () => ({+ invalidateTopicsProcessingActive: mocks.invalidate,+}));+vi.mock("../../db", () => {+ const tx = {llmApiKeys: { findMany: mocks.keys },+ project: { findMany: mocks.projects },topicConfig: {findMany: mocks.configs,findUnique: mocks.current,+ update: mocks.update,upsert: mocks.upsert,},- },-}));+ };+ return {+ prisma: {+ ...tx,+ $transaction: (callback: (client: typeof tx) => unknown) => {+ mocks.transaction();+ return callback(tx);+ },+ },+ };+});const connection = (id: string, adapter: LLMAdapter) => ({id,@@ -62,6 +81,7 @@ beforeEach(() => {connection("anthropic", LLMAdapter.Anthropic),]);mocks.current.mockResolvedValue(null);+ mocks.projects.mockResolvedValue([{ id: "project", orgId: "target-org" }]);mocks.hasEmbeddings.mockResolvedValue(false);mocks.testCall.mockResolvedValue(undefined);mocks.embed.mockImplementation(async ({ summaries, dimensions }) => ({@@ -70,6 +90,35 @@ beforeEach(() => {});describe("prepareTopicsModelSettings", () => {+ it("accepts an organization connection inherited by the project", async () => {+ mocks.keys.mockResolvedValue([+ {+ ...connection("openai", LLMAdapter.OpenAI),+ projectId: null,+ organizationId: "organization",+ },+ ]);++ await saveTopicsModelSettings("project", settings);++ expect(mocks.keys).toHaveBeenCalledWith({+ where: {+ id: { in: ["openai", "openai", "openai"] },+ OR: [+ { projectId: "project" },+ {+ organization: {+ projects: {+ some: { id: "project", deletedAt: null },+ },+ },+ },+ ],+ },+ });+ expect(mocks.upsert).toHaveBeenCalledOnce();+ });+it("saves an Anthropic connection when its test call succeeds", async () => {await saveTopicsModelSettings("project", {...settings,@@ -162,6 +211,80 @@ describe("prepareTopicsModelSettings", () => {);});+describe("reconcileTopicsConnectionsForProject", () => {+ it("rebinds inherited connections after a project transfer", async () => {+ const sourceConnection = {+ ...connection("source", LLMAdapter.OpenAI),+ projectId: null,+ organizationId: "source-org",+ };+ const targetConnection = {+ ...connection("target", LLMAdapter.OpenAI),+ projectId: null,+ organizationId: "target-org",+ };+ mocks.current.mockResolvedValue({+ id: "config",+ projectId: "project",+ project: { orgId: "target-org" },+ summaryLlmApiKey: sourceConnection,+ embeddingLlmApiKey: sourceConnection,+ clusteringLlmApiKey: sourceConnection,+ });+ mocks.keys.mockResolvedValue([targetConnection]);++ await reconcileTopicsConnectionsForProject("project");++ expect(mocks.update).toHaveBeenCalledWith({+ where: { id: "config" },+ data: {+ summaryLlmApiKeyId: "target",+ embeddingLlmApiKeyId: "target",+ clusteringLlmApiKeyId: "target",+ },+ });+ expect(mocks.invalidate).toHaveBeenCalledWith("project");+ });++ it("pauses Topics rather than rebinding to an incompatible adapter", async () => {+ const sourceConnection = {+ ...connection("source", LLMAdapter.OpenAI),+ provider: "shared-provider",+ projectId: null,+ organizationId: "source-org",+ };+ const targetConnection = {+ ...connection("target", LLMAdapter.Anthropic),+ provider: "shared-provider",+ projectId: null,+ organizationId: "target-org",+ };+ mocks.current.mockResolvedValue({+ id: "config",+ projectId: "project",+ project: { orgId: "target-org" },+ summaryLlmApiKey: sourceConnection,+ embeddingLlmApiKey: sourceConnection,+ clusteringLlmApiKey: sourceConnection,+ });+ mocks.keys.mockImplementation(async ({ where }) =>+ where.adapter === targetConnection.adapter ? [targetConnection] : [],+ );++ await reconcileTopicsConnectionsForProject("project");++ expect(mocks.update).toHaveBeenCalledWith({+ where: { id: "config" },+ data: expect.objectContaining({+ summaryLlmApiKeyId: null,+ embeddingLlmApiKeyId: null,+ clusteringLlmApiKeyId: null,+ enabled: false,+ }),+ });+ });+});+describe("getEnabledTopicsModels", () => {it("returns only enabled projects whose three connections are usable", async () => {const openai = {@@ -170,6 +293,7 @@ describe("getEnabledTopicsModels", () => {};const row = {projectId: "complete",+ project: { orgId: "organization" },enabled: true,summaryLlmApiKey: openai,summaryModel: "gpt-6-luna",diff --git a/packages/shared/src/server/topics/model-config.ts b/packages/shared/src/server/topics/model-config.tsindex 31db238b0e..8a85dcd3fe 100644--- a/packages/shared/src/server/topics/model-config.ts+++ b/packages/shared/src/server/topics/model-config.ts@@ -12,6 +12,7 @@ import {} from "../llm/llmText";import { testModelCall } from "../llm/testModelCall";import { LLMAdapter, type LLMApiKeySchema } from "../llm/types";+import { findCompatibleLlmApiKeysForProjects } from "../services/LLMApiKeys";import {TOPICS_MODEL_SLOT_DETAILS,TOPICS_MODEL_SLOTS,@@ -41,12 +42,14 @@ export type TopicsModels = {};const withConnections = {+ project: { select: { orgId: true } },summaryLlmApiKey: true,embeddingLlmApiKey: true,clusteringLlmApiKey: true,} as const;type ConfigWithConnections = TopicConfig & {+ project: { orgId: string };summaryLlmApiKey: LlmApiKeys | null;embeddingLlmApiKey: LlmApiKeys | null;clusteringLlmApiKey: LlmApiKeys | null;@@ -56,15 +59,23 @@ function resolveSlot(slot: TopicsModelSlotName,key: LlmApiKeys | null,model: string | null,+ projectId?: string,+ organizationId?: string,): TopicsModel | null {if (!key || !model) return null;+ if (+ projectId &&+ key.projectId !== projectId &&+ key.organizationId !== organizationId+ )+ return null;return {slot,provider: key.provider,adapter: key.adapter as LLMAdapter,model,connection: {- projectId: key.projectId,+ projectId: projectId ?? key.projectId ?? undefined,secretKey: key.secretKey,extraHeaders: key.extraHeaders,baseURL: key.baseURL,@@ -78,16 +89,22 @@ function resolveModels(row: ConfigWithConnections): TopicsModels | null {"summary",row.summaryLlmApiKey,row.summaryModel,+ row.projectId,+ row.project.orgId,);const embedding = resolveSlot("embedding",row.embeddingLlmApiKey,row.embeddingModel,+ row.projectId,+ row.project.orgId,);const clustering = resolveSlot("clustering",row.clusteringLlmApiKey,row.clusteringModel,+ row.projectId,+ row.project.orgId,);if (!summary || !embedding || !clustering) return null;return {@@ -144,33 +161,172 @@ export async function pauseTopicsModels(await invalidateTopicsProcessingActive(projectId);}-/**- * Pauses Topics in every running config that uses a connection about to be- * deleted. Callers that pass a transaction call- * `invalidateTopicsProcessingActive` after it commits.- */-export async function pauseTopicsModelsUsingConnection(- params: { projectId: string; llmApiKeyId: string; provider: string },- tx: Prisma.TransactionClient = prisma,-): Promise<void> {- const { projectId, llmApiKeyId, provider } = params;- await tx.topicConfig.updateMany({+/** Keeps Topics connection references valid before an LLM connection is deleted. */+export async function handleTopicsLlmApiKeyDeletion(params: {+ tx: Prisma.TransactionClient;+ llmApiKeyId: string;+ llmApiKeyOwner: {+ projectId: string | null;+ organizationId: string | null;+ };+ provider: string;+ adapter: string;+}): Promise<string[]> {+ const { tx, llmApiKeyId, llmApiKeyOwner, provider, adapter } = params;+ const configs = await tx.topicConfig.findMany({where: {- projectId,- enabled: true,OR: [- { summaryLlmApiKeyId: llmApiKeyId },- { embeddingLlmApiKeyId: llmApiKeyId },- { clusteringLlmApiKeyId: llmApiKeyId },+ {+ summaryLlmApiKeyId: llmApiKeyId,+ summaryLlmApiKey: llmApiKeyOwner,+ },+ {+ embeddingLlmApiKeyId: llmApiKeyId,+ embeddingLlmApiKey: llmApiKeyOwner,+ },+ {+ clusteringLlmApiKeyId: llmApiKeyId,+ clusteringLlmApiKey: llmApiKeyOwner,+ },],},- data: {- enabled: false,- blockedAt: new Date(),- blockReason: EvaluatorBlockReason.LLM_CONNECTION_MISSING,- blockMessage: `The LLM connection "${provider}" was deleted. Choose another connection in Configure Topics, then turn automatic processing back on.`,+ select: {+ id: true,+ projectId: true,+ enabled: true,+ summaryLlmApiKeyId: true,+ embeddingLlmApiKeyId: true,+ clusteringLlmApiKeyId: true,},});++ const replacements = await findCompatibleLlmApiKeysForProjects({+ tx,+ projectIds: configs.map(({ projectId }) => projectId),+ provider,+ adapter,+ excludedLlmApiKeyId: llmApiKeyId,+ });++ for (const config of configs) {+ const replacement = replacements.get(config.projectId)?.id;+ const slotUpdates = {+ ...(config.summaryLlmApiKeyId === llmApiKeyId+ ? { summaryLlmApiKeyId: replacement ?? null }+ : {}),+ ...(config.embeddingLlmApiKeyId === llmApiKeyId+ ? { embeddingLlmApiKeyId: replacement ?? null }+ : {}),+ ...(config.clusteringLlmApiKeyId === llmApiKeyId+ ? { clusteringLlmApiKeyId: replacement ?? null }+ : {}),+ };+ await tx.topicConfig.update({+ where: { id: config.id },+ data: replacement+ ? slotUpdates+ : {+ ...slotUpdates,+ ...(config.enabled+ ? {+ enabled: false,+ blockedAt: new Date(),+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_MISSING,+ blockMessage: `The LLM connection "${provider}" was deleted. Choose another connection and turn Topics on again.`,+ }+ : {}),+ },+ });+ }++ return configs.map(({ projectId }) => projectId);+}++/** Rebinds inherited Topics connections after a project changes organization. */+export async function reconcileTopicsConnectionsForProject(+ projectId: string,+): Promise<void> {+ const changed = await prisma.$transaction(async (tx) => {+ const config = await tx.topicConfig.findUnique({+ where: { projectId },+ include: {+ project: { select: { orgId: true } },+ summaryLlmApiKey: true,+ embeddingLlmApiKey: true,+ clusteringLlmApiKey: true,+ },+ });+ if (!config) return false;++ const keys = [+ config.summaryLlmApiKey,+ config.embeddingLlmApiKey,+ config.clusteringLlmApiKey,+ ];+ const inaccessibleKeys = keys.filter(+ (key) =>+ key &&+ key.projectId !== projectId &&+ key.organizationId !== config.project.orgId,+ );+ if (inaccessibleKeys.length === 0) return false;++ const uniqueInaccessibleKeys = [+ ...new Map(+ inaccessibleKeys.flatMap((key) => (key ? [[key.id, key]] : [])),+ ).values(),+ ];+ const replacementByLlmApiKeyId = new Map(+ await Promise.all(+ uniqueInaccessibleKeys.map(async (key) => {+ const replacements = await findCompatibleLlmApiKeysForProjects({+ tx,+ projectIds: [projectId],+ provider: key.provider,+ adapter: key.adapter,+ excludedLlmApiKeyId: key.id,+ });+ return [key.id, replacements.get(projectId)?.id] as const;+ }),+ ),+ );++ const resolveKeyId = (key: LlmApiKeys | null) => {+ if (!key) return null;+ if (+ key.projectId === projectId ||+ key.organizationId === config.project.orgId+ )+ return key.id;+ return replacementByLlmApiKeyId.get(key.id) ?? null;+ };+ const summaryLlmApiKeyId = resolveKeyId(config.summaryLlmApiKey);+ const embeddingLlmApiKeyId = resolveKeyId(config.embeddingLlmApiKey);+ const clusteringLlmApiKeyId = resolveKeyId(config.clusteringLlmApiKey);+ const hasMissingConnection =+ !summaryLlmApiKeyId || !embeddingLlmApiKeyId || !clusteringLlmApiKeyId;++ await tx.topicConfig.update({+ where: { id: config.id },+ data: {+ summaryLlmApiKeyId,+ embeddingLlmApiKeyId,+ clusteringLlmApiKeyId,+ ...(hasMissingConnection+ ? {+ enabled: false,+ blockedAt: new Date(),+ blockReason: EvaluatorBlockReason.LLM_CONNECTION_MISSING,+ blockMessage:+ "A configured LLM connection is not available in this organization. Choose another connection and turn Topics on again.",+ }+ : {}),+ },+ });+ return true;+ });++ if (changed) await invalidateTopicsProcessingActive(projectId);}/**@@ -211,7 +367,17 @@ async function loadTopicsModelKeys(settings.clustering,].flatMap((slot) => (slot ? [slot.llmApiKeyId] : []));const keys = await prisma.llmApiKeys.findMany({- where: { projectId, id: { in: ids } },+ where: {+ id: { in: ids },+ OR: [+ { projectId },+ {+ organization: {+ projects: { some: { id: projectId, deletedAt: null } },+ },+ },+ ],+ },});for (const id of ids) {if (!keys.some((key) => key.id === id))diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.ymlindex c82a0d03f8..32bb462938 100644--- a/web/public/generated/api/openapi.yml+++ b/web/public/generated/api/openapi.yml@@ -2617,6 +2617,158 @@ paths:application/json:schema: {}security: *ref_0+ /api/public/organizations/llm-connections:+ get:+ description: >-+ Get all organization-level LLM connections. Requires an+ organization-scoped API key.+ operationId: llmConnections_listOrganization+ tags:+ - LlmConnections+ parameters:+ - name: page+ in: query+ description: page number, starts at 1+ required: false+ schema:+ type: integer+ nullable: true+ - name: limit+ in: query+ description: limit of items per page+ required: false+ schema:+ type: integer+ nullable: true+ responses:+ '200':+ description: ''+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/PaginatedLlmConnections'+ '400':+ description: ''+ content:+ application/json:+ schema: {}+ '401':+ description: ''+ content:+ application/json:+ schema: {}+ '403':+ description: ''+ content:+ application/json:+ schema: {}+ '404':+ description: ''+ content:+ application/json:+ schema: {}+ '405':+ description: ''+ content:+ application/json:+ schema: {}+ security: *ref_0+ put:+ description: >-+ Create or update an organization-level LLM connection. The connection is+ upserted on provider and inherited by projects without a provider+ override. Requires an organization-scoped API key.+ operationId: llmConnections_upsertOrganization+ tags:+ - LlmConnections+ parameters: []+ responses:+ '200':+ description: ''+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/LlmConnection'+ '400':+ description: ''+ content:+ application/json:+ schema: {}+ '401':+ description: ''+ content:+ application/json:+ schema: {}+ '403':+ description: ''+ content:+ application/json:+ schema: {}+ '404':+ description: ''+ content:+ application/json:+ schema: {}+ '405':+ description: ''+ content:+ application/json:+ schema: {}+ security: *ref_0+ requestBody:+ required: true+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/UpsertLlmConnectionRequest'+ /api/public/organizations/llm-connections/{id}:+ delete:+ description: >-+ Delete an organization-level LLM connection by id. Evaluators in+ projects without an override for the provider are automatically paused.+ Requires an organization-scoped API key.+ operationId: llmConnections_deleteOrganization+ tags:+ - LlmConnections+ parameters:+ - name: id+ in: path+ required: true+ schema:+ type: string+ responses:+ '200':+ description: ''+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/DeleteLlmConnectionResponse'+ '400':+ description: ''+ content:+ application/json:+ schema: {}+ '401':+ description: ''+ content:+ application/json:+ schema: {}+ '403':+ description: ''+ content:+ application/json:+ schema: {}+ '404':+ description: ''+ content:+ application/json:+ schema: {}+ '405':+ description: ''+ content:+ application/json:+ schema: {}+ security: *ref_0/api/public/media/{mediaId}:get:description: Get a media recorddiff --git a/web/src/__tests__/organization-settings-pages.clienttest.tsx b/web/src/__tests__/organization-settings-pages.clienttest.tsxindex 679f5398c4..1bc4d15bec 100644--- a/web/src/__tests__/organization-settings-pages.clienttest.tsx+++ b/web/src/__tests__/organization-settings-pages.clienttest.tsx@@ -64,7 +64,8 @@ vi.mock("@/src/ee/features/sso-settings", () => ({SSOSettings: () => null,}));-vi.mock("@langfuse/shared", () => ({+vi.mock("@langfuse/shared", async () => ({+ ...(await vi.importActual("@langfuse/shared")),isCloudPlan: vi.fn(() => false),}));diff --git a/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap b/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snapindex 06722caee9..bcda97cd04 100644--- a/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap+++ b/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap@@ -51,6 +51,13 @@ exports[`public-api auth parity > legacy matches the captured baseline except do"DELETE models/[modelId]/index | org/bearer": 401,"DELETE models/[modelId]/index | project/basic": 404,"DELETE models/[modelId]/index | project/bearer": 403,+ "DELETE organizations/llm-connections/[id] | admin/admin": 401,+ "DELETE organizations/llm-connections/[id] | agent/basic": 401,+ "DELETE organizations/llm-connections/[id] | agent/bearer": 401,+ "DELETE organizations/llm-connections/[id] | org/basic": 404,+ "DELETE organizations/llm-connections/[id] | org/bearer": 401,+ "DELETE organizations/llm-connections/[id] | project/basic": 403,+ "DELETE organizations/llm-connections/[id] | project/bearer": 403,"DELETE organizations/memberships/index | admin/admin": 401,"DELETE organizations/memberships/index | agent/basic": 401,"DELETE organizations/memberships/index | agent/bearer": 401,@@ -338,6 +345,13 @@ exports[`public-api auth parity > legacy matches the captured baseline except do"GET organizations/apiKeys/index | org/bearer": 401,"GET organizations/apiKeys/index | project/basic": 403,"GET organizations/apiKeys/index | project/bearer": 403,+ "GET organizations/llm-connections/index | admin/admin": 401,+ "GET organizations/llm-connections/index | agent/basic": 401,+ "GET organizations/llm-connections/index | agent/bearer": 401,+ "GET organizations/llm-connections/index | org/basic": 200,+ "GET organizations/llm-connections/index | org/bearer": 401,+ "GET organizations/llm-connections/index | project/basic": 403,+ "GET organizations/llm-connections/index | project/bearer": 403,"GET organizations/memberships/index | admin/admin": 401,"GET organizations/memberships/index | agent/basic": 401,"GET organizations/memberships/index | agent/bearer": 401,@@ -951,6 +965,13 @@ exports[`public-api auth parity > legacy matches the captured baseline except do"PUT models/[modelId]/index | org/bearer": 401,"PUT models/[modelId]/index | project/basic": 400,"PUT models/[modelId]/index | project/bearer": 403,+ "PUT organizations/llm-connections/index | admin/admin": 401,+ "PUT organizations/llm-connections/index | agent/basic": 401,+ "PUT organizations/llm-connections/index | agent/bearer": 401,+ "PUT organizations/llm-connections/index | org/basic": 400,+ "PUT organizations/llm-connections/index | org/bearer": 401,+ "PUT organizations/llm-connections/index | project/basic": 403,+ "PUT organizations/llm-connections/index | project/bearer": 403,"PUT organizations/memberships/index | admin/admin": 401,"PUT organizations/memberships/index | agent/basic": 401,"PUT organizations/memberships/index | agent/bearer": 401,diff --git a/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts b/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.tsindex cb0dab3c77..a3006a5546 100644--- a/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts+++ b/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts@@ -1,5 +1,5 @@import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";-import { type Plan, Role } from "@langfuse/shared";+import { LLMAdapter, type Plan, Role } from "@langfuse/shared";import { prisma } from "@langfuse/shared/src/db";import {createApiKeyCacheKey,@@ -19,6 +19,15 @@ import {vi,} from "vitest";+const mocks = vi.hoisted(() => ({+ reconcileTopicsConnectionsForProject: vi.fn(),+}));++vi.mock("@langfuse/shared/topics/server", () => ({+ reconcileTopicsConnectionsForProject:+ mocks.reconcileTopicsConnectionsForProject,+}));+// organizations.delete cancels Stripe before deleting; the test env has a cloud// region but no Stripe, so force the self-hosted path to reach the eviction.vi.mock("@/src/ee/features/billing/utils/isCloudBillingEnabled", () => ({@@ -236,6 +245,121 @@ describe("API-key cache invalidation on project/org lifecycle", () => {expect(await survivingKeys([...sourceKeys, ...targetKeys])).toEqual([]);});+ it("projects.transfer rebinds the default eval model to the destination organization", async () => {+ const sourceOrgId = await createOrg();+ const targetOrgId = await createOrg();+ const projectId = await createProject(sourceOrgId);+ const user = await createUserInOrgs([sourceOrgId, targetOrgId]);+ const provider = `transfer-provider-${uuidv4()}`;+ const sourceConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: sourceOrgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ displaySecretKey: "source",+ secretKey: "source",+ },+ });+ const targetConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: targetOrgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ displaySecretKey: "target",+ secretKey: "target",+ },+ });+ await prisma.defaultLlmModel.create({+ data: {+ projectId,+ llmApiKeyId: sourceConnection.id,+ provider,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o-mini",+ },+ });++ const caller = makeCaller({+ userId: user.id,+ orgIds: [sourceOrgId, targetOrgId],+ projectId,+ });+ await caller.projects.transfer({ projectId, targetOrgId });++ await expect(+ prisma.defaultLlmModel.findUnique({ where: { projectId } }),+ ).resolves.toMatchObject({ llmApiKeyId: targetConnection.id });+ });++ it("projects.transfer removes the default eval model when no compatible connection exists", async () => {+ const sourceOrgId = await createOrg();+ const targetOrgId = await createOrg();+ const projectId = await createProject(sourceOrgId);+ const user = await createUserInOrgs([sourceOrgId, targetOrgId]);+ const provider = `transfer-provider-${uuidv4()}`;+ const sourceConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: sourceOrgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ displaySecretKey: "source",+ secretKey: "source",+ },+ });+ await prisma.llmApiKeys.create({+ data: {+ organizationId: targetOrgId,+ provider,+ adapter: LLMAdapter.Anthropic,+ displaySecretKey: "target",+ secretKey: "target",+ },+ });+ await prisma.defaultLlmModel.create({+ data: {+ projectId,+ llmApiKeyId: sourceConnection.id,+ provider,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o-mini",+ },+ });++ const caller = makeCaller({+ userId: user.id,+ orgIds: [sourceOrgId, targetOrgId],+ projectId,+ });+ await caller.projects.transfer({ projectId, targetOrgId });++ await expect(+ prisma.defaultLlmModel.findUnique({ where: { projectId } }),+ ).resolves.toBeNull();+ });++ it("projects.transfer still evicts cached keys when Topics refresh fails", async () => {+ const sourceOrgId = await createOrg();+ const targetOrgId = await createOrg();+ const projectId = await createProject(sourceOrgId);+ const user = await createUserInOrgs([sourceOrgId, targetOrgId]);+ const sourceKeys = await seedOrgScopedKey(sourceOrgId);+ const targetKeys = await seedOrgScopedKey(targetOrgId);+ mocks.reconcileTopicsConnectionsForProject.mockRejectedValueOnce(+ new Error("Topics refresh failed"),+ );++ const caller = makeCaller({+ userId: user.id,+ orgIds: [sourceOrgId, targetOrgId],+ projectId,+ });+ await expect(+ caller.projects.transfer({ projectId, targetOrgId }),+ ).rejects.toThrow();++ expect(await survivingKeys([...sourceKeys, ...targetKeys])).toEqual([]);+ });+it("organizations.delete evicts cached keys before the cascade removes the rows", async () => {const orgId = await createOrg();const user = await createUserInOrgs([orgId]);diff --git a/web/src/__tests__/server/llm-api-key.servertest.ts b/web/src/__tests__/server/llm-api-key.servertest.tsindex 99aed01ad3..9545412d16 100644--- a/web/src/__tests__/server/llm-api-key.servertest.ts+++ b/web/src/__tests__/server/llm-api-key.servertest.ts@@ -10,6 +10,7 @@ vi.mock("@langfuse/shared/src/server", async () => {});import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";+import * as auditLogs from "@/src/features/audit-logs/server";import type { Session } from "next-auth";import { BEDROCK_USE_DEFAULT_CREDENTIALS, LLMAdapter } from "@langfuse/shared";import { env } from "@/src/env.mjs";@@ -23,8 +24,10 @@ import { appRouter } from "@/src/server/api/root";import { createInnerTRPCContext } from "@/src/server/api/trpc";import { decrypt, encrypt } from "@langfuse/shared/encryption";import { AuthMethod } from "@/src/features/llm-api-key/types";+import { LlmConnectionService } from "@/src/features/llm-api-key/server/llmConnectionService";import {createOrgProjectAndApiKey,+ DefaultEvalModelService,EvaluatorBlockSource,generateLLMText,} from "@langfuse/shared/src/server";@@ -123,7 +126,7 @@ describe("llmApiKey.all RPC", () => {const baseURL = "https://example.com/v1";const withDefaultModels = false;- await caller.llmApiKey.create({+ const result = await caller.llmApiKey.create({projectId,secretKey: secret,provider,@@ -132,6 +135,7 @@ describe("llmApiKey.all RPC", () => {customModels,withDefaultModels,});+ expect(result).toBeUndefined();const llmApiKeys = await prisma.llmApiKeys.findMany({where: {@@ -152,6 +156,41 @@ describe("llmApiKey.all RPC", () => {expect(llmApiKeys[0].displaySecretKey).toMatch(/^...[a-zA-Z0-9]{4}$/);});+ it("resolves an organization connection for the project default model", async () => {+ const provider = `organization-default-${randomUUID()}`;+ const organizationConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: orgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: encrypt("organization-secret"),+ displaySecretKey: "...cret",+ },+ });+ await prisma.defaultLlmModel.create({+ data: {+ projectId,+ llmApiKeyId: organizationConnection.id,+ provider,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o",+ },+ });++ const result =+ await DefaultEvalModelService.fetchValidModelConfig(projectId);++ expect(result).toMatchObject({+ valid: true,+ config: {+ apiKey: {+ id: organizationConnection.id,+ projectId,+ },+ },+ });+ });+it("returns CONFLICT when creating a key for an existing provider", async () => {const provider = `duplicate-provider-${randomUUID()}`;const input = {@@ -175,6 +214,124 @@ describe("llmApiKey.all RPC", () => {).resolves.toBe(1);});+ it("rejects an incompatible project override without blocking edits to an existing mismatch", async () => {+ const provider = `incompatible-project-override-${randomUUID()}`;+ await prisma.llmApiKeys.create({+ data: {+ organizationId: orgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: encrypt("organization-secret"),+ displaySecretKey: "...cret",+ },+ });++ await expect(+ caller.llmApiKey.create({+ projectId,+ provider,+ adapter: LLMAdapter.Anthropic,+ secretKey: "project-secret",+ }),+ ).rejects.toThrow(+ "Project and organization connections with the same provider name must use the same adapter.",+ );++ const incompatibleProjectConnection = await prisma.llmApiKeys.create({+ data: {+ projectId,+ provider,+ adapter: LLMAdapter.Anthropic,+ secretKey: encrypt("project-secret"),+ displaySecretKey: "...cret",+ },+ });+ await expect(+ caller.llmApiKey.update({+ id: incompatibleProjectConnection.id,+ projectId,+ provider,+ adapter: LLMAdapter.Anthropic,+ customModels: ["claude-sonnet-4-5"],+ }),+ ).resolves.toBeUndefined();+ await expect(+ prisma.llmApiKeys.findUniqueOrThrow({+ where: { id: incompatibleProjectConnection.id },+ select: { customModels: true },+ }),+ ).resolves.toEqual({ customModels: ["claude-sonnet-4-5"] });++ const service = new LlmConnectionService();+ await expect(+ service.upsert({+ owner: {+ type: "project",+ projectId,+ organizationId: orgId,+ },+ input: {+ provider,+ adapter: LLMAdapter.Anthropic,+ secretKey: "rotated-project-secret",+ },+ actor: { userId: session.user!.id, orgId, projectId },+ }),+ ).resolves.toMatchObject({ created: false });+ await expect(+ service.upsert({+ owner: {+ type: "project",+ projectId,+ organizationId: orgId,+ },+ input: {+ provider,+ adapter: LLMAdapter.Azure,+ secretKey: "azure-project-secret",+ },+ actor: { userId: session.user!.id, orgId, projectId },+ }),+ ).rejects.toThrow(+ "Project and organization connections with the same provider name must use the same adapter.",+ );+ });++ it("should rotate a secret without revalidating an unchanged stored base URL", async () => {+ const provider = `operator-provisioned-${randomUUID()}`;+ const connection = await prisma.llmApiKeys.create({+ data: {+ projectId,+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: encrypt("old-secret"),+ displaySecretKey: "...cret",+ baseURL: "http://localhost:11434/v1",+ },+ });++ const result = await new LlmConnectionService().upsert({+ owner: {+ type: "project",+ projectId,+ organizationId: orgId,+ },+ input: {+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: "rotated-secret",+ baseURL: connection.baseURL,+ },+ actor: { userId: session.user!.id, orgId, projectId },+ });++ expect(result.created).toBe(false);+ const updatedConnection = await prisma.llmApiKeys.findUniqueOrThrow({+ where: { id: connection.id },+ });+ expect(decrypt(updatedConnection.secretKey)).toBe("rotated-secret");+ });+it("should create a Bedrock llm api key with a Bedrock API key", async () => {const secret = "bedrock-api-key-1234";@@ -318,7 +475,7 @@ describe("llmApiKey.all RPC", () => {},});- await caller.llmApiKey.update({+ const result = await caller.llmApiKey.update({id: existingKey.id,projectId,provider: "openai-responses",@@ -328,6 +485,7 @@ describe("llmApiKey.all RPC", () => {withDefaultModels: false,config: { useResponsesApi: false },});+ expect(result).toBeUndefined();const { data: llmApiKeys } = await caller.llmApiKey.all({projectId,@@ -498,7 +656,10 @@ describe("llmApiKey.all RPC", () => {adapter: LLMAdapter.TypeSafe,baseURL: "http://localhost:8080/v1",}),- ).resolves.toMatchObject({ success: false });+ ).resolves.toEqual({+ success: false,+ error: "Blocked hostname detected",+ });const { data: llmApiKeys } = await caller.llmApiKey.all({projectId,@@ -849,6 +1010,44 @@ describe("llmApiKey.all RPC", () => {);});+ it("should test an operator-provisioned Bedrock connection without revalidating its stored secret", async () => {+ const originalRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "eu";++ try {+ const existingKey = await prisma.llmApiKeys.create({+ data: {+ projectId,+ provider: "bedrock-runtime",+ adapter: LLMAdapter.Bedrock,+ secretKey: encrypt(BEDROCK_USE_DEFAULT_CREDENTIALS),+ displaySecretKey: "Default AWS credentials",+ customModels: ["us.anthropic.claude-3-5-sonnet-20240620-v1:0"],+ withDefaultModels: false,+ config: { region: "us-east-1" },+ },+ });++ const result = await caller.llmApiKey.testUpdate({+ id: existingKey.id,+ projectId,+ provider: existingKey.provider,+ adapter: LLMAdapter.Bedrock,+ customModels: existingKey.customModels,+ config: { region: "us-east-1" },+ });++ expect(result).toEqual({ success: true });+ expect(mockGenerateLLMText).toHaveBeenCalledWith(+ expect.objectContaining({+ connection: expect.objectContaining({ projectId }),+ }),+ );+ } finally {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalRegion;+ }+ });+it("should fill masked header values from the stored headers in testUpdate", async () => {await caller.llmApiKey.create({projectId,@@ -1666,11 +1865,8 @@ describe("llmApiKey.all RPC", () => {});expect(updatedKeys.length).toBe(1);- // Note: Current router logic doesn't actually clear headers when passing empty object- // because Prisma undefined means "don't update", not "set to null"- // The headers remain unchanged when an empty object is passed- expect(updatedKeys[0].extraHeaders).not.toBeNull();- expect(updatedKeys[0].extraHeaderKeys).not.toBeNull();+ expect(updatedKeys[0].extraHeaders).toBeNull();+ expect(updatedKeys[0].extraHeaderKeys).toEqual([]);// Other fields should remain unchangedexpect(updatedKeys[0].secretKey).toEqual(initialKeys[0].secretKey);@@ -1742,6 +1938,32 @@ describe("llmApiKey.all RPC", () => {expect(updatedKeys[0].extraHeaderKeys).toContain("X-New-Header");});+ it("rolls back connection deletion when audit logging fails", async () => {+ const connection = await prisma.llmApiKeys.create({+ data: {+ projectId,+ secretKey: encrypt("project-secret"),+ displaySecretKey: "...cret",+ provider: `audit-rollback-${randomUUID()}`,+ adapter: LLMAdapter.OpenAI,+ },+ });+ const auditLogSpy = vi+ .spyOn(auditLogs, "auditLog")+ .mockRejectedValueOnce(new Error("audit unavailable"));++ try {+ await expect(+ caller.llmApiKey.delete({ projectId, id: connection.id }),+ ).rejects.toThrow();+ await expect(+ prisma.llmApiKeys.findUniqueOrThrow({ where: { id: connection.id } }),+ ).resolves.toBeDefined();+ } finally {+ auditLogSpy.mockRestore();+ }+ });+describe("deleting a connection pauses the evaluators that ran on it", () => {const PROVIDER = "openai";@@ -1796,8 +2018,8 @@ describe("llmApiKey.all RPC", () => {createV2Evaluator([{ provider: "anthropic", model: "claude" }]),]);- // Point the project's default eval model at the connection too, so both- // block reasons fire from one deletion.+ // Use this provider as the project's default too, so both block reasons+ // fire when its last effective connection is deleted.await prisma.defaultLlmModel.create({data: {projectId,@@ -1807,7 +2029,6 @@ describe("llmApiKey.all RPC", () => {model: "gpt-4o",},});-await caller.llmApiKey.delete({ projectId, id: connection.id });expect(mockFinalizeEvaluatorBlocks).toHaveBeenCalledOnce();@@ -1845,6 +2066,231 @@ describe("llmApiKey.all RPC", () => {reason: null,});}+ expect(+ await prisma.defaultLlmModel.findUnique({ where: { projectId } }),+ ).toBeNull();+ });++ it("keeps the default model when deleting an override with an organization fallback", async () => {+ const organizationConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: orgId,+ secretKey: encrypt("organization-secret"),+ displaySecretKey: "...cret",+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ },+ });+ const projectConnection = await prisma.llmApiKeys.create({+ data: {+ projectId,+ secretKey: encrypt("project-secret"),+ displaySecretKey: "...cret",+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ },+ });+ const providerEvaluatorId = await createV2Evaluator([+ { provider: PROVIDER, model: "gpt-4o" },+ ]);+ const defaultModelEvaluatorId = await createV2Evaluator([+ { provider: null, model: null },+ ]);+ await prisma.defaultLlmModel.create({+ data: {+ projectId,+ llmApiKeyId: projectConnection.id,+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o",+ },+ });+ await prisma.topicConfig.create({+ data: {+ projectId,+ enabled: true,+ summaryLlmApiKeyId: projectConnection.id,+ summaryModel: "gpt-6-luna",+ embeddingLlmApiKeyId: projectConnection.id,+ embeddingModel: "text-embedding-3-small",+ clusteringLlmApiKeyId: projectConnection.id,+ clusteringModel: "gpt-5.6-terra",+ },+ });++ await caller.llmApiKey.delete({+ projectId,+ id: projectConnection.id,+ });++ expect(mockFinalizeEvaluatorBlocks).not.toHaveBeenCalled();+ expect(+ await prisma.evaluator.findUniqueOrThrow({+ where: { id: providerEvaluatorId },+ select: { blockedAt: true, blockReason: true },+ }),+ ).toEqual({ blockedAt: null, blockReason: null });+ expect(+ await prisma.evaluator.findUniqueOrThrow({+ where: { id: defaultModelEvaluatorId },+ select: { blockedAt: true, blockReason: true },+ }),+ ).toEqual({ blockedAt: null, blockReason: null });+ expect(+ await prisma.defaultLlmModel.findUniqueOrThrow({+ where: { projectId },+ select: {+ llmApiKeyId: true,+ provider: true,+ adapter: true,+ model: true,+ },+ }),+ ).toEqual({+ llmApiKeyId: organizationConnection.id,+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o",+ });+ expect(+ await prisma.topicConfig.findUniqueOrThrow({+ where: { projectId },+ select: {+ enabled: true,+ summaryLlmApiKeyId: true,+ embeddingLlmApiKeyId: true,+ clusteringLlmApiKeyId: true,+ },+ }),+ ).toEqual({+ enabled: true,+ summaryLlmApiKeyId: organizationConnection.id,+ embeddingLlmApiKeyId: organizationConnection.id,+ clusteringLlmApiKeyId: organizationConnection.id,+ });+ });++ it("deletes the default model when the organization fallback uses a different adapter", async () => {+ await prisma.llmApiKeys.createMany({+ data: [+ {+ organizationId: orgId,+ secretKey: encrypt("organization-secret"),+ displaySecretKey: "...cret",+ provider: PROVIDER,+ adapter: LLMAdapter.Anthropic,+ },+ {+ projectId,+ secretKey: encrypt("project-secret"),+ displaySecretKey: "...cret",+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ },+ ],+ });+ const projectConnection = await prisma.llmApiKeys.findFirstOrThrow({+ where: { projectId, provider: PROVIDER },+ });+ const defaultModelEvaluatorId = await createV2Evaluator([+ { provider: null, model: null },+ ]);+ await prisma.defaultLlmModel.create({+ data: {+ projectId,+ llmApiKeyId: projectConnection.id,+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o",+ },+ });+ await caller.llmApiKey.delete({+ projectId,+ id: projectConnection.id,+ });++ expect(mockFinalizeEvaluatorBlocks).toHaveBeenCalledWith({+ projectId,+ source: EvaluatorBlockSource.LLM_API_KEY_DELETION,+ evaluatorIdsByReason: {+ [EvaluatorBlockReason.LLM_CONNECTION_MISSING]: [],+ [EvaluatorBlockReason.DEFAULT_EVAL_MODEL_MISSING]: [+ defaultModelEvaluatorId,+ ],+ },+ });+ expect(+ await prisma.defaultLlmModel.findUnique({ where: { projectId } }),+ ).toBeNull();+ });++ it("does not rebind organization-backed configuration to an incompatible project override", async () => {+ const organizationConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: orgId,+ secretKey: encrypt("organization-secret"),+ displaySecretKey: "...cret",+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ },+ });+ await prisma.llmApiKeys.create({+ data: {+ projectId,+ secretKey: encrypt("project-secret"),+ displaySecretKey: "...cret",+ provider: PROVIDER,+ adapter: LLMAdapter.Anthropic,+ },+ });+ await prisma.defaultLlmModel.create({+ data: {+ projectId,+ llmApiKeyId: organizationConnection.id,+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o",+ },+ });+ await prisma.topicConfig.create({+ data: {+ projectId,+ enabled: true,+ summaryLlmApiKeyId: organizationConnection.id,+ summaryModel: "gpt-6-luna",+ embeddingLlmApiKeyId: organizationConnection.id,+ embeddingModel: "text-embedding-3-small",+ clusteringLlmApiKeyId: organizationConnection.id,+ clusteringModel: "gpt-5.6-terra",+ },+ });++ await caller.organizationLlmApiKey.delete({+ orgId,+ id: organizationConnection.id,+ });++ expect(+ await prisma.defaultLlmModel.findUnique({+ where: { projectId },+ }),+ ).toBeNull();+ expect(+ await prisma.topicConfig.findUniqueOrThrow({+ where: { projectId },+ select: {+ enabled: true,+ summaryLlmApiKeyId: true,+ embeddingLlmApiKeyId: true,+ clusteringLlmApiKeyId: true,+ },+ }),+ ).toEqual({+ enabled: false,+ summaryLlmApiKeyId: null,+ embeddingLlmApiKeyId: null,+ clusteringLlmApiKeyId: null,+ });});it("turns Topics off and records why when its connection is deleted", async () => {@@ -1888,5 +2334,47 @@ describe("llmApiKey.all RPC", () => {clusteringLlmApiKeyId: null,});});++ it("clears deleted Topics connections without marking an already disabled config as paused", async () => {+ await caller.llmApiKey.create({+ projectId,+ secretKey: "test-secret",+ provider: PROVIDER,+ adapter: LLMAdapter.OpenAI,+ customModels: [],+ withDefaultModels: true,+ });+ const connection = await prisma.llmApiKeys.findFirstOrThrow({+ where: { projectId, provider: PROVIDER },+ });+ await prisma.topicConfig.create({+ data: {+ projectId,+ enabled: false,+ summaryLlmApiKeyId: connection.id,+ summaryModel: "gpt-6-luna",+ embeddingLlmApiKeyId: connection.id,+ embeddingModel: "text-embedding-3-small",+ clusteringLlmApiKeyId: connection.id,+ clusteringModel: "gpt-5.6-terra",+ },+ });++ await caller.llmApiKey.delete({ projectId, id: connection.id });++ expect(+ await prisma.topicConfig.findUniqueOrThrow({+ where: { projectId },+ }),+ ).toMatchObject({+ enabled: false,+ blockReason: null,+ blockMessage: null,+ blockedAt: null,+ summaryLlmApiKeyId: null,+ embeddingLlmApiKeyId: null,+ clusteringLlmApiKeyId: null,+ });+ });});});diff --git a/web/src/__tests__/server/llm-connections-api.servertest.ts b/web/src/__tests__/server/llm-connections-api.servertest.tsindex a1a22bb5cf..71b614c118 100644--- a/web/src/__tests__/server/llm-connections-api.servertest.ts+++ b/web/src/__tests__/server/llm-connections-api.servertest.ts@@ -160,6 +160,34 @@ describe("/api/public/llm-connections API Endpoints", () => {expect(secondConnection.config).toBeNull();});+ it("should include decision-model connections", async () => {+ const connection = await prisma.llmApiKeys.create({+ data: {+ projectId,+ provider: generateUniqueProvider("decision-model"),+ adapter: LLMAdapter.TypeSafe,+ secretKey: encrypt("sk-decision-model"),+ displaySecretKey: "...odel",+ },+ });++ const response = await makeZodVerifiedAPICall(+ GetLlmConnectionsV1Response,+ "GET",+ "/api/public/llm-connections",+ undefined,+ auth,+ );++ expect(response.body.data).toEqual([+ expect.objectContaining({+ id: connection.id,+ adapter: LLMAdapter.TypeSafe,+ }),+ ]);+ expect(response.body.meta.totalItems).toBe(1);+ });+it("should handle pagination correctly", async () => {// Create 3 test connectionsawait Promise.all([@@ -415,7 +443,7 @@ describe("/api/public/llm-connections API Endpoints", () => {expect(response.status).toBe(400);expect(response.body.message).toBe(- "Invalid baseURL: Blocked hostname detected",+ "Invalid base URL: Blocked hostname detected",);});@@ -465,6 +493,38 @@ describe("/api/public/llm-connections API Endpoints", () => {expect(response.body.withDefaultModels).toBe(false); // Updated});+ it("should preserve config when an upsert omits it", async () => {+ const provider = generateUniqueProvider("openai-config");+ await makeZodVerifiedAPICall(+ PutLlmConnectionV1Response,+ "PUT",+ "/api/public/llm-connections",+ {+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: "sk-original",+ config: { useResponsesApi: true },+ },+ auth,+ 201,+ );++ const response = await makeZodVerifiedAPICall(+ PutLlmConnectionV1Response,+ "PUT",+ "/api/public/llm-connections",+ {+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: "sk-updated",+ },+ auth,+ 200,+ );++ expect(response.body.config).toEqual({ useResponsesApi: true });+ });+it("should return 401 for invalid auth", async () => {const createData = {provider: generateUniqueProvider("test-provider"),diff --git a/web/src/__tests__/server/organization-llm-api-key.servertest.ts b/web/src/__tests__/server/organization-llm-api-key.servertest.tsnew file mode 100644index 0000000000..4155ec3453--- /dev/null+++ b/web/src/__tests__/server/organization-llm-api-key.servertest.ts@@ -0,0 +1,237 @@+import { randomUUID } from "crypto";+import type { Session } from "next-auth";++import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";+import { appRouter } from "@/src/server/api/root";+import { createInnerTRPCContext } from "@/src/server/api/trpc";+import { LLMAdapter } from "@langfuse/shared";+import { prisma } from "@langfuse/shared/src/db";+import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";++const createSession = (params: {+ orgId: string;+ projectId: string;+ orgRole: "OWNER" | "ADMIN" | "MEMBER" | "VIEWER";+ projectRole: "ADMIN" | "MEMBER" | "VIEWER";+}): Session => ({+ expires: "1",+ user: {+ id: randomUUID(),+ name: "LLM connection test user",+ canCreateOrganizations: true,+ organizations: [+ {+ id: params.orgId,+ role: params.orgRole,+ plan: "cloud:hobby",+ cloudConfig: undefined,+ name: "Test Organization",+ metadata: {},+ aiFeaturesEnabled: false,+ aiTelemetryEnabled: false,+ projects: [+ {+ id: params.projectId,+ role: params.projectRole,+ name: "Test Project",+ deletedAt: null,+ retentionDays: null,+ hasTraces: false,+ metadata: {},+ createdAt: new Date().toISOString(),+ },+ ],+ },+ ],+ featureFlags: testFeatureFlags(),+ admin: false,+ },+ environment: {} as never,+});++const createCaller = (session: Session) => {+ const ctx = createInnerTRPCContext({ session, headers: {} });+ return appRouter.createCaller({ ...ctx, prisma });+};++describe("organization LLM connections", () => {+ it("lets an owner manage a connection without exposing its secret", async () => {+ const setup = await createOrgProjectAndApiKey();+ const caller = createCaller(+ createSession({+ orgId: setup.orgId,+ projectId: setup.projectId,+ orgRole: "OWNER",+ projectRole: "ADMIN",+ }),+ );++ const created = await caller.organizationLlmApiKey.create({+ orgId: setup.orgId,+ provider: "openai",+ adapter: LLMAdapter.OpenAI,+ secretKey: "sk-organization-test",+ });+ expect(created).not.toHaveProperty("secretKey");++ const result = await caller.organizationLlmApiKey.all({+ orgId: setup.orgId,+ });+ expect(result.data).toHaveLength(1);+ expect(result.data[0]).toMatchObject({+ id: created.id,+ organizationId: setup.orgId,+ scope: "organization",+ provider: "openai",+ });+ expect(result.data[0]).not.toHaveProperty("secretKey");+ expect(result.data[0]).not.toHaveProperty("extraHeaders");+ });++ it("rejects an incompatible organization connection without blocking edits to an existing mismatch", async () => {+ const setup = await createOrgProjectAndApiKey();+ const caller = createCaller(+ createSession({+ orgId: setup.orgId,+ projectId: setup.projectId,+ orgRole: "OWNER",+ projectRole: "ADMIN",+ }),+ );+ const provider = `incompatible-organization-connection-${randomUUID()}`;+ await prisma.llmApiKeys.create({+ data: {+ projectId: setup.projectId,+ provider,+ adapter: LLMAdapter.Anthropic,+ secretKey: "encrypted-project-secret",+ displaySecretKey: "...cret",+ },+ });++ await expect(+ caller.organizationLlmApiKey.create({+ orgId: setup.orgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: "organization-secret",+ }),+ ).rejects.toThrow(+ "Project and organization connections with the same provider name must use the same adapter.",+ );++ const incompatibleOrganizationConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: setup.orgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey: "encrypted-organization-secret",+ displaySecretKey: "...cret",+ },+ });+ await expect(+ caller.organizationLlmApiKey.update({+ id: incompatibleOrganizationConnection.id,+ orgId: setup.orgId,+ provider,+ adapter: LLMAdapter.OpenAI,+ customModels: ["gpt-5"],+ }),+ ).resolves.toMatchObject({+ id: incompatibleOrganizationConnection.id,+ customModels: ["gpt-5"],+ });+ });++ it("lets a project viewer read inherited metadata but not organization settings", async () => {+ const setup = await createOrgProjectAndApiKey();+ await prisma.llmApiKeys.create({+ data: {+ organizationId: setup.orgId,+ provider: "anthropic",+ adapter: LLMAdapter.Anthropic,+ secretKey: "encrypted-test-value",+ displaySecretKey: "...test",+ },+ });+ const caller = createCaller(+ createSession({+ orgId: setup.orgId,+ projectId: setup.projectId,+ orgRole: "MEMBER",+ projectRole: "VIEWER",+ }),+ );++ await expect(+ caller.organizationLlmApiKey.all({ orgId: setup.orgId }),+ ).rejects.toThrow("does not have access");++ const inherited = await caller.llmApiKey.inherited({+ projectId: setup.projectId,+ });+ expect(inherited).toEqual([+ expect.objectContaining({+ provider: "anthropic",+ organizationId: setup.orgId,+ overriddenByProject: false,+ }),+ ]);+ expect(inherited[0]).not.toHaveProperty("secretKey");+ });++ it("does not list an organization connection overridden by a project decision model", async () => {+ const setup = await createOrgProjectAndApiKey();+ await prisma.llmApiKeys.createMany({+ data: [+ {+ organizationId: setup.orgId,+ provider: "shared-provider",+ adapter: LLMAdapter.OpenAI,+ secretKey: "encrypted-organization-openai",+ displaySecretKey: "...open",+ },+ {+ projectId: setup.projectId,+ provider: "shared-provider",+ adapter: LLMAdapter.TypeSafe,+ secretKey: "encrypted-project-typesafe",+ displaySecretKey: "...safe",+ },+ ],+ });+ const caller = createCaller(+ createSession({+ orgId: setup.orgId,+ projectId: setup.projectId,+ orgRole: "MEMBER",+ projectRole: "VIEWER",+ }),+ );++ const effective = await caller.llmApiKey.effective({+ projectId: setup.projectId,+ });++ expect(effective.data).toEqual([]);+ });++ it("rejects organization access across tenants", async () => {+ const [ownSetup, otherSetup] = await Promise.all([+ createOrgProjectAndApiKey(),+ createOrgProjectAndApiKey(),+ ]);+ const caller = createCaller(+ createSession({+ orgId: ownSetup.orgId,+ projectId: ownSetup.projectId,+ orgRole: "OWNER",+ projectRole: "ADMIN",+ }),+ );++ await expect(+ caller.organizationLlmApiKey.all({ orgId: otherSetup.orgId }),+ ).rejects.toThrow("not a member of this organization");+ });+});diff --git a/web/src/__tests__/server/organization-llm-connections-api.servertest.ts b/web/src/__tests__/server/organization-llm-connections-api.servertest.tsnew file mode 100644index 0000000000..c3c4bb3492--- /dev/null+++ b/web/src/__tests__/server/organization-llm-connections-api.servertest.ts@@ -0,0 +1,177 @@+import { randomUUID } from "node:crypto";++import { LLMAdapter } from "@langfuse/shared";+import { encrypt } from "@langfuse/shared/encryption";+import { OrganizationId, SystemRoleId } from "@langfuse/shared/rbac";+import { prisma } from "@langfuse/shared/src/db";+import {+ createApiKey,+ createBasicAuthHeader,+ createOrgProjectAndApiKey,+} from "@langfuse/shared/src/server";++import {+ makeAPICall,+ makeZodVerifiedAPICall,+} from "@/src/__tests__/test-utils";+import {+ DeleteLlmConnectionV1Response,+ GetLlmConnectionsV1Response,+ PutLlmConnectionV1Response,+} from "@/src/features/public-api/types/llm-connections";++async function createOrganizationApiKey(orgId: string): Promise<string> {+ const { publicKey, secretKey } = await createApiKey(prisma, {+ owner: OrganizationId(orgId),+ role: SystemRoleId("LEGACY_ORGANIZATION_API_KEY"),+ createdBy: "system",+ });++ return createBasicAuthHeader(publicKey, secretKey);+}++describe("/api/public/organizations/llm-connections", () => {+ it("creates and lists organization connections without exposing secrets", async () => {+ const fixture = await createOrgProjectAndApiKey({ plan: "Team" });+ const auth = await createOrganizationApiKey(fixture.orgId);+ const provider = `organization-provider-${randomUUID()}`;+ const secretKey = "sk-organization-secret";+ const extraHeaderValue = "private-header-value";++ const createResponse = await makeZodVerifiedAPICall(+ PutLlmConnectionV1Response,+ "PUT",+ "/api/public/organizations/llm-connections",+ {+ provider,+ adapter: LLMAdapter.OpenAI,+ secretKey,+ extraHeaders: { Authorization: extraHeaderValue },+ },+ auth,+ 201,+ );++ expect(createResponse.body).not.toHaveProperty("secretKey");+ expect(createResponse.body).not.toHaveProperty("extraHeaders");+ expect(JSON.stringify(createResponse.body)).not.toContain(secretKey);+ expect(JSON.stringify(createResponse.body)).not.toContain(extraHeaderValue);++ const stored = await prisma.llmApiKeys.findUnique({+ where: {+ organizationId_provider: {+ organizationId: fixture.orgId,+ provider,+ },+ },+ });+ expect(stored).toMatchObject({+ projectId: null,+ organizationId: fixture.orgId,+ provider,+ });+ expect(stored?.secretKey).not.toBe(secretKey);++ const listResponse = await makeZodVerifiedAPICall(+ GetLlmConnectionsV1Response,+ "GET",+ "/api/public/organizations/llm-connections",+ undefined,+ auth,+ );++ expect(listResponse.body.data).toHaveLength(1);+ expect(listResponse.body.data[0]).toMatchObject({+ id: createResponse.body.id,+ provider,+ extraHeaderKeys: ["Authorization"],+ });+ expect(JSON.stringify(listResponse.body)).not.toContain(secretKey);+ expect(JSON.stringify(listResponse.body)).not.toContain(extraHeaderValue);+ });++ it("rejects project API keys on organization routes", async () => {+ const fixture = await createOrgProjectAndApiKey({ plan: "Team" });++ const response = await makeAPICall(+ "GET",+ "/api/public/organizations/llm-connections",+ undefined,+ fixture.auth,+ );++ expect(response.status).toBe(403);+ });++ it("does not delete a connection owned by another organization", async () => {+ const [caller, other] = await Promise.all([+ createOrgProjectAndApiKey({ plan: "Team" }),+ createOrgProjectAndApiKey({ plan: "Team" }),+ ]);+ const auth = await createOrganizationApiKey(caller.orgId);+ const otherConnection = await prisma.llmApiKeys.create({+ data: {+ organizationId: other.orgId,+ provider: `other-provider-${randomUUID()}`,+ adapter: LLMAdapter.OpenAI,+ secretKey: encrypt("sk-other"),+ displaySecretKey: "...1234",+ },+ });++ const response = await makeAPICall(+ "DELETE",+ `/api/public/organizations/llm-connections/${otherConnection.id}`,+ undefined,+ auth,+ );++ expect(response.status).toBe(404);+ expect(+ await prisma.llmApiKeys.findUnique({+ where: { id: otherConnection.id },+ }),+ ).not.toBeNull();+ });++ it("deletes an organization connection with its owning key", async () => {+ const fixture = await createOrgProjectAndApiKey({ plan: "Team" });+ const auth = await createOrganizationApiKey(fixture.orgId);+ const connection = await prisma.llmApiKeys.create({+ data: {+ organizationId: fixture.orgId,+ provider: `delete-provider-${randomUUID()}`,+ adapter: LLMAdapter.OpenAI,+ secretKey: encrypt("sk-delete"),+ displaySecretKey: "...lete",+ },+ });+ await prisma.defaultLlmModel.create({+ data: {+ projectId: fixture.projectId,+ llmApiKeyId: connection.id,+ provider: connection.provider,+ adapter: LLMAdapter.OpenAI,+ model: "gpt-4o",+ },+ });++ const response = await makeZodVerifiedAPICall(+ DeleteLlmConnectionV1Response,+ "DELETE",+ `/api/public/organizations/llm-connections/${connection.id}`,+ undefined,+ auth,+ );++ expect(response.status).toBe(200);+ expect(+ await prisma.llmApiKeys.findUnique({ where: { id: connection.id } }),+ ).toBeNull();+ expect(+ await prisma.defaultLlmModel.findUnique({+ where: { projectId: fixture.projectId },+ }),+ ).toBeNull();+ });+});diff --git a/web/src/__tests__/server/public-api-auth-parity.servertest.ts b/web/src/__tests__/server/public-api-auth-parity.servertest.tsindex a733cfd128..6f1faddc2f 100644--- a/web/src/__tests__/server/public-api-auth-parity.servertest.ts+++ b/web/src/__tests__/server/public-api-auth-parity.servertest.ts@@ -137,6 +137,14 @@ const projectRoutes: Route[] = [// Org and misc routes call shadowAuth directly from the handler body.const orgRoutes: Route[] = [{ route: "organizations/apiKeys/index", methods: ["GET"] },+ {+ route: "organizations/llm-connections/index",+ methods: ["GET", "PUT"],+ },+ {+ route: "organizations/llm-connections/[id]",+ methods: ["DELETE"],+ },{route: "organizations/memberships/index",methods: ["GET", "PUT", "DELETE"],diff --git a/web/src/__tests__/server/topics-setup.servertest.ts b/web/src/__tests__/server/topics-setup.servertest.tsindex 116c976d25..18bafcfecb 100644--- a/web/src/__tests__/server/topics-setup.servertest.ts+++ b/web/src/__tests__/server/topics-setup.servertest.ts@@ -22,7 +22,6 @@ vi.mock(import {listTopicRules,- pauseTopicsModelsUsingConnection,saveTopicsSetup,setTopicsAutomaticProcessing,} from "@langfuse/shared/topics/server";@@ -277,20 +276,3 @@ describe("setTopicsAutomaticProcessing", () => {).rejects.toThrow("Save the Topics setup");});});--describe("pauseTopicsModelsUsingConnection", () => {- it("does not mark a switched-off config paused", async () => {- const { projectId, connection } = await topicsProject();-- await pauseTopicsModelsUsingConnection({- projectId,- llmApiKeyId: connection.id,- provider: "openai",- });-- expect(await storedConfig(projectId)).toMatchObject({- enabled: false,- ...clearedBlock,- });- });-});diff --git a/web/src/__tests__/server/unit/chat-completion-handler.servertest.ts b/web/src/__tests__/server/unit/chat-completion-handler.servertest.tsindex 6d094ec2be..e9326fc663 100644--- a/web/src/__tests__/server/unit/chat-completion-handler.servertest.ts+++ b/web/src/__tests__/server/unit/chat-completion-handler.servertest.ts@@ -20,10 +20,6 @@ vi.mock("@/src/features/playground/server/authorizeRequest", () => ({authorizeRequestOrThrow: mocks.authorize,}));-vi.mock("@langfuse/shared/src/db", () => ({- prisma: { llmApiKeys: { findFirst: mocks.findConnection } },-}));-vi.mock("@opentelemetry/api", () => ({context: {with: (_context: unknown, callback: () => unknown) => callback(),@@ -43,6 +39,7 @@ vi.mock("@langfuse/shared/src/server", async (importOriginal) => {createLLMToolSet: mocks.createToolSet,generateLLMText: mocks.generate,mapLegacyLLMCompletionParams: mocks.mapLegacyParams,+ resolveLlmApiKey: mocks.findConnection,streamLLMText: mocks.stream,};});diff --git a/web/src/components/ModelParameters/LLMApiKeyComponent.tsx b/web/src/components/ModelParameters/LLMApiKeyComponent.tsxindex 2c01f14c0e..2637f78166 100644--- a/web/src/components/ModelParameters/LLMApiKeyComponent.tsx+++ b/web/src/components/ModelParameters/LLMApiKeyComponent.tsx@@ -24,7 +24,7 @@ export const LLMApiKeyComponent = (p: {);}- const apiKeys = api.llmApiKey.all.useQuery({+ const apiKeys = api.llmApiKey.effective.useQuery({projectId: p.projectId,});diff --git a/web/src/components/SettingsTable/SettingsTable.tsx b/web/src/components/SettingsTable/SettingsTable.tsxindex c0228b276d..5d184dc072 100644--- a/web/src/components/SettingsTable/SettingsTable.tsx+++ b/web/src/components/SettingsTable/SettingsTable.tsx@@ -1,4 +1,4 @@-import { useState, type ComponentProps } from "react";+import { useState, type ComponentProps, type ReactNode } from "react";import {ActionButton,type ActionButtonProps,@@ -60,6 +60,8 @@ export type SettingsTableProps<TData extends object> = Omit<onChange: (value: string) => void;};filters?: SettingsTableFilter[];+ emptyState?: ReactNode;+ toolbarContent?: ReactNode;toolbarActions?: SettingsTableToolbarAction[];rowHeightControl?: {rowHeight: RowHeight;@@ -74,6 +76,8 @@ export function SettingsTable<TData extends object>({columnVisibilityKey,search,filters,+ emptyState,+ toolbarContent,toolbarActions,rowHeightControl,pagination,@@ -101,10 +105,15 @@ export function SettingsTable<TData extends object>({const hasToolbar = Boolean(search ||hasFilters ||+ toolbarContent ||columnVisibilityKey ||toolbarActions ||rowHeightControl,);+ const showEmptyState =+ emptyState !== undefined &&+ tableProps.data.status === "success" &&+ tableProps.data.data.length === 0;return (<div className="flex min-h-0 flex-col gap-2">@@ -121,6 +130,7 @@ export function SettingsTable<TData extends object>({hasFilters ? "min-w-72" : "min-w-0",)}>+ {toolbarContent}{search && (<div className="w-full max-w-sm min-w-0"><SearchInput@@ -229,17 +239,21 @@ export function SettingsTable<TData extends object>({</div>)}- <SettingsTableCard>- <Table- columns={columns}- columnVisibility={columnVisibility}- onColumnVisibilityChange={setColumnVisibility}- columnOrder={columnOrder}- onColumnOrderChange={setColumnOrder}- {...tableProps}- />- {pagination && <PaginationBar {...pagination} />}- </SettingsTableCard>+ {showEmptyState ? (+ emptyState+ ) : (+ <SettingsTableCard>+ <Table+ columns={columns}+ columnVisibility={columnVisibility}+ onColumnVisibilityChange={setColumnVisibility}+ columnOrder={columnOrder}+ onColumnOrderChange={setColumnOrder}+ {...tableProps}+ />+ {pagination && <PaginationBar {...pagination} />}+ </SettingsTableCard>+ )}</div>);}diff --git a/web/src/components/design-system/table/Table.tsx b/web/src/components/design-system/table/Table.tsxindex 349b8372e9..e3f6e78c02 100644--- a/web/src/components/design-system/table/Table.tsx+++ b/web/src/components/design-system/table/Table.tsx@@ -14,6 +14,7 @@ import { type ComponentProps, useMemo } from "react";import { DropdownMenu } from "@/src/components/design-system/DropdownMenu/DropdownMenu";import { IconButton } from "@/src/components/design-system/IconButton/IconButton";+import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";import DocPopup from "@/src/components/layouts/doc-popup";import { Skeleton } from "@/src/components/ui/skeleton";import { cn } from "@/src/utils/tailwind";@@ -35,6 +36,7 @@ export interface TableProps<TData> {tableName: string;columns: ColumnDef<TData>[];actions?: (row: TData) => ComponentProps<typeof DropdownMenu>["items"];+ actionsDisabledReason?: string;data: AsyncTableData<TData[]>;orderBy?: OrderByState;setOrderBy?: (state: OrderByState) => void;@@ -52,6 +54,7 @@ export function Table<TData extends object>({tableName,columns,actions,+ actionsDisabledReason,data,orderBy,setOrderBy,@@ -84,18 +87,37 @@ export function Table<TData extends object>({className="ml-auto flex size-6 items-center justify-end"onClick={(event) => event.stopPropagation()}>- <DropdownMenu items={items} placement="bottom-end">- {({ getTriggerProps }) => (- <IconButton- icon={MoreVertical}- label="Open actions menu"- size="sm"- variant="subtle"- {...getTriggerProps()}- disabled={items.length === 0}- />- )}- </DropdownMenu>+ {items.length === 0 && actionsDisabledReason ? (+ <Tooltip label={actionsDisabledReason}>+ {({ getTriggerProps }) => (+ <span+ {...getTriggerProps()}+ className="inline-flex cursor-not-allowed [&>button]:pointer-events-none"+ >+ <IconButton+ icon={MoreVertical}+ label="Open actions menu"+ size="sm"+ variant="subtle"+ disabled+ />+ </span>+ )}+ </Tooltip>+ ) : (+ <DropdownMenu items={items} placement="bottom-end">+ {({ getTriggerProps }) => (+ <IconButton+ icon={MoreVertical}+ label="Open actions menu"+ size="sm"+ variant="subtle"+ {...getTriggerProps()}+ disabled={items.length === 0}+ />+ )}+ </DropdownMenu>+ )}</div>);},@@ -109,7 +131,7 @@ export function Table<TData extends object>({),},];- }, [actions, columns]);+ }, [actions, actionsDisabledReason, columns]);const table = useReactTable({data: data.status === "success" ? data.data : [],columns: tableColumns,diff --git a/web/src/components/layouts/header.tsx b/web/src/components/layouts/header.tsxindex 29394466a8..1a210bc3ce 100644--- a/web/src/components/layouts/header.tsx+++ b/web/src/components/layouts/header.tsx@@ -103,9 +103,11 @@ function BaseHeader({ ...props }: HeaderProps & { level: "h3" | "h4" | "h5" }) {</Link>)}</div>- <div className="flex flex-wrap items-center gap-3">- {props.actionButtons ?? null}- </div>+ {props.actionButtons ? (+ <div className="flex flex-wrap items-center gap-3">+ {props.actionButtons}+ </div>+ ) : null}</div></div>);diff --git a/web/src/features/audit-logs/auditLog.ts b/web/src/features/audit-logs/auditLog.tsindex 58a067b1d6..12c4c7aa86 100644--- a/web/src/features/audit-logs/auditLog.ts+++ b/web/src/features/audit-logs/auditLog.ts@@ -62,13 +62,7 @@ type AuditableResource =// legacy resources| "membership";-type AuditLog = {- resourceType: AuditableResource;- resourceId: string;- action: string;- before?: unknown;- after?: unknown;-} & (+export type AuditLogActor =| {userId: string;orgId: string;@@ -91,8 +85,15 @@ type AuditLog = {apiKeyId: string;orgId: string;projectId?: string;- }-);+ };++export type AuditLog = {+ resourceType: AuditableResource;+ resourceId: string;+ action: string;+ before?: unknown;+ after?: unknown;+} & AuditLogActor;// Mirrors each audit log record into the application logs so that actors can be// correlated with web/worker log lines (e.g. trace deletions) without queryingdiff --git a/web/src/features/audit-logs/server/index.ts b/web/src/features/audit-logs/server/index.tsindex bb630dd773..1d12cec3b8 100644--- a/web/src/features/audit-logs/server/index.ts+++ b/web/src/features/audit-logs/server/index.ts@@ -1,5 +1,8 @@// The audit-logs feature's server surface (RFC rule 9, amended). auditLog// writes through Prisma, so it has no client-safe surface: every caller is// server code and reaches it through here.-export { auditLog } from "@/src/features/audit-logs/auditLog";+export {+ auditLog,+ type AuditLogActor,+} from "@/src/features/audit-logs/auditLog";export { getAuditRequestContext } from "@/src/features/audit-logs/server/getAuditRequestContext";diff --git a/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelQuestionsEditor/DecisionModelQuestionsEditor.tsx b/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelQuestionsEditor/DecisionModelQuestionsEditor.tsxindex 044b794806..24f27fffd2 100644--- a/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelQuestionsEditor/DecisionModelQuestionsEditor.tsx+++ b/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelQuestionsEditor/DecisionModelQuestionsEditor.tsx@@ -34,7 +34,7 @@ export function DecisionModelQuestionsEditor({actions: state.actions,})),);- const connections = api.llmApiKey.all.useQuery({+ const connections = api.llmApiKey.effective.useQuery({projectId,includeDecisionModels: true,});diff --git a/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.clienttest.tsx b/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.clienttest.tsxindex 98e94d91bf..97369d32a9 100644--- a/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.clienttest.tsx+++ b/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.clienttest.tsx@@ -14,7 +14,7 @@ const connection = vi.hoisted(() => ({vi.mock("@/src/utils/api", () => ({api: {llmApiKey: {- all: {+ effective: {useQuery: () => ({ isSuccess: true, data: { data: [connection] } }),},},diff --git a/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.tsx b/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.tsxindex f202bb734c..d27ac774a5 100644--- a/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.tsx+++ b/web/src/features/evals/v2/components/Evaluators/EvaluatorSetupEditor/components/DefinitionStep/components/DecisionModelSelector/DecisionModelSelector.tsx@@ -35,7 +35,7 @@ export function DecisionModelSelector({const [open, setOpen] = useState(false);const selectedModel = useStore(store, (state) => state.selectedModel);const selectModel = store.getState().actions.selectModel;- const connections = api.llmApiKey.all.useQuery({+ const connections = api.llmApiKey.effective.useQuery({projectId,includeDecisionModels: true,});diff --git a/web/src/features/evals/v2/hooks/useEvaluatorTestAvailability.clienttest.tsx b/web/src/features/evals/v2/hooks/useEvaluatorTestAvailability.clienttest.tsxindex e88bc8b09c..faa1b2f6a3 100644--- a/web/src/features/evals/v2/hooks/useEvaluatorTestAvailability.clienttest.tsx+++ b/web/src/features/evals/v2/hooks/useEvaluatorTestAvailability.clienttest.tsx@@ -12,7 +12,7 @@ vi.mock("@/src/features/evals/v2/hooks/useEvaluatorSetupSample", () => ({vi.mock("@/src/utils/api", () => ({api: {llmApiKey: {- all: {+ effective: {useQuery: () => ({ data: undefined }),},},diff --git a/web/src/features/evals/v2/hooks/useFallbackDecisionModel.ts b/web/src/features/evals/v2/hooks/useFallbackDecisionModel.tsindex d06560d217..9258325d9a 100644--- a/web/src/features/evals/v2/hooks/useFallbackDecisionModel.ts+++ b/web/src/features/evals/v2/hooks/useFallbackDecisionModel.ts@@ -3,7 +3,7 @@ import { api } from "@/src/utils/api";/** The decision model to show and save when the user has not chosen one yet. */export function useFallbackDecisionModel(projectId: string, enabled: boolean) {- const connections = api.llmApiKey.all.useQuery(+ const connections = api.llmApiKey.effective.useQuery({ projectId, includeDecisionModels: true },{ enabled },);diff --git a/web/src/features/evals/v2/hooks/useProjectDefaultModel.clienttest.ts b/web/src/features/evals/v2/hooks/useProjectDefaultModel.clienttest.tsindex be33229314..6d0c6a92a2 100644--- a/web/src/features/evals/v2/hooks/useProjectDefaultModel.clienttest.ts+++ b/web/src/features/evals/v2/hooks/useProjectDefaultModel.clienttest.ts@@ -53,7 +53,7 @@ vi.mock("@/src/utils/api", () => ({},},llmApiKey: {- all: {+ effective: {useQuery: () => ({data: { data: [] },isPending: false,diff --git a/web/src/features/evals/v2/hooks/useProjectDefaultModel.ts b/web/src/features/evals/v2/hooks/useProjectDefaultModel.tsindex 707b1ce4ad..890b46e5a4 100644--- a/web/src/features/evals/v2/hooks/useProjectDefaultModel.ts+++ b/web/src/features/evals/v2/hooks/useProjectDefaultModel.ts@@ -44,7 +44,7 @@ export function useProjectDefaultModel({projectId,scope: "llmApiKeys:read",});- const connectionsQuery = api.llmApiKey.all.useQuery(+ const connectionsQuery = api.llmApiKey.effective.useQuery({ projectId },// Gated on the scope the router enforces, so a viewer does not fire a// request that can only come back 403.diff --git a/web/src/features/llm-api-key/server/llmConnectionRepository.ts b/web/src/features/llm-api-key/server/llmConnectionRepository.tsnew file mode 100644index 0000000000..2c6bd8815e--- /dev/null+++ b/web/src/features/llm-api-key/server/llmConnectionRepository.ts@@ -0,0 +1,276 @@+import { prisma, type LlmApiKeys, type Prisma } from "@langfuse/shared/src/db";+import { DECISION_MODEL_ADAPTERS } from "@langfuse/shared/src/server";++export type LlmConnectionOwner =+ | {+ type: "project";+ projectId: string;+ organizationId: string;+ }+ | {+ type: "organization";+ organizationId: string;+ };++type PrismaClient = typeof prisma | Prisma.TransactionClient;++const ownerWhere = (owner: LlmConnectionOwner): Prisma.LlmApiKeysWhereInput =>+ owner.type === "project"+ ? { projectId: owner.projectId }+ : { organizationId: owner.organizationId };++export class LlmConnectionRepository {+ constructor(private readonly db: PrismaClient = prisma) {}++ withDb(db: PrismaClient): LlmConnectionRepository {+ return new LlmConnectionRepository(db);+ }++ findById(params: {+ owner: LlmConnectionOwner;+ id: string;+ }): Promise<LlmApiKeys | null> {+ return this.db.llmApiKeys.findFirst({+ where: {+ id: params.id,+ ...ownerWhere(params.owner),+ },+ });+ }++ findByProvider(params: {+ owner: LlmConnectionOwner;+ provider: string;+ }): Promise<LlmApiKeys | null> {+ return this.db.llmApiKeys.findFirst({+ where: {+ provider: params.provider,+ ...ownerWhere(params.owner),+ },+ });+ }++ findIncompatibleConnection(params: {+ owner: LlmConnectionOwner;+ provider: string;+ adapter: string;+ }): Promise<LlmApiKeys | null> {+ return this.db.llmApiKeys.findFirst({+ where: {+ provider: params.provider,+ adapter: { not: params.adapter },+ ...(params.owner.type === "project"+ ? { organizationId: params.owner.organizationId }+ : {+ project: {+ orgId: params.owner.organizationId,+ deletedAt: null,+ },+ }),+ },+ });+ }++ list(params: {+ owner: LlmConnectionOwner;+ includeDecisionModels: boolean;+ limit?: number;+ offset?: number;+ }): Promise<LlmApiKeys[]> {+ return this.db.llmApiKeys.findMany({+ where: {+ ...ownerWhere(params.owner),+ ...(params.includeDecisionModels+ ? {}+ : { adapter: { notIn: [...DECISION_MODEL_ADAPTERS] } }),+ },+ orderBy: { createdAt: "desc" },+ take: params.limit,+ skip: params.offset,+ });+ }++ count(params: {+ owner: LlmConnectionOwner;+ includeDecisionModels: boolean;+ }): Promise<number> {+ return this.db.llmApiKeys.count({+ where: {+ ...ownerWhere(params.owner),+ ...(params.includeDecisionModels+ ? {}+ : { adapter: { notIn: [...DECISION_MODEL_ADAPTERS] } }),+ },+ });+ }++ async listOrganizationConnectionsForProject(params: {+ projectId: string;+ organizationId: string;+ includeDecisionModels: boolean;+ }): Promise<{+ connections: LlmApiKeys[];+ overriddenProviders: Set<string>;+ }> {+ const [connections, projectConnections] = await Promise.all([+ this.db.llmApiKeys.findMany({+ where: {+ organizationId: params.organizationId,+ ...(params.includeDecisionModels+ ? {}+ : { adapter: { notIn: [...DECISION_MODEL_ADAPTERS] } }),+ },+ orderBy: { createdAt: "desc" },+ }),+ this.db.llmApiKeys.findMany({+ where: { projectId: params.projectId },+ select: { provider: true },+ }),+ ]);++ return {+ connections,+ overriddenProviders: new Set(+ projectConnections.map((connection) => connection.provider),+ ),+ };+ }++ async listEffectiveForProject(params: {+ projectId: string;+ organizationId: string;+ includeDecisionModels: boolean;+ }): Promise<LlmApiKeys[]> {+ const connections = await this.db.llmApiKeys.findMany({+ where: {+ OR: [+ { projectId: params.projectId },+ { organizationId: params.organizationId },+ ],+ },+ orderBy: { createdAt: "desc" },+ });+ const projectProviders = new Set(+ connections+ .filter((connection) => connection.projectId === params.projectId)+ .map((connection) => connection.provider),+ );+ const visibleConnections = params.includeDecisionModels+ ? connections+ : connections.filter(+ (connection) =>+ !DECISION_MODEL_ADAPTERS.some(+ (adapter) => adapter === connection.adapter,+ ),+ );++ return visibleConnections.filter(+ (connection) =>+ connection.projectId === params.projectId ||+ !projectProviders.has(connection.provider),+ );+ }++ create(params: {+ owner: LlmConnectionOwner;+ data: Omit<+ Prisma.LlmApiKeysUncheckedCreateInput,+ "projectId" | "organizationId"+ >;+ }): Promise<LlmApiKeys> {+ return this.db.llmApiKeys.create({+ data: {+ ...params.data,+ ...(params.owner.type === "project"+ ? { projectId: params.owner.projectId }+ : { organizationId: params.owner.organizationId }),+ },+ });+ }++ upsert(params: {+ owner: LlmConnectionOwner;+ provider: string;+ create: Omit<+ Prisma.LlmApiKeysUncheckedCreateInput,+ "projectId" | "organizationId"+ >;+ update: Prisma.LlmApiKeysUpdateInput;+ }): Promise<LlmApiKeys> {+ if (params.owner.type === "project") {+ return this.db.llmApiKeys.upsert({+ where: {+ projectId_provider: {+ projectId: params.owner.projectId,+ provider: params.provider,+ },+ },+ create: {+ ...params.create,+ projectId: params.owner.projectId,+ },+ update: params.update,+ });+ }++ return this.db.llmApiKeys.upsert({+ where: {+ organizationId_provider: {+ organizationId: params.owner.organizationId,+ provider: params.provider,+ },+ },+ create: {+ ...params.create,+ organizationId: params.owner.organizationId,+ },+ update: params.update,+ });+ }++ update(params: {+ owner: LlmConnectionOwner;+ id: string;+ data: Prisma.LlmApiKeysUpdateInput;+ }): Promise<LlmApiKeys> {+ return this.db.llmApiKeys.update({+ where:+ params.owner.type === "project"+ ? { id: params.id, projectId: params.owner.projectId }+ : { id: params.id, organizationId: params.owner.organizationId },+ data: params.data,+ });+ }++ delete(params: {+ owner: LlmConnectionOwner;+ id: string;+ }): Promise<LlmApiKeys> {+ return this.db.llmApiKeys.delete({+ where:+ params.owner.type === "project"+ ? { id: params.id, projectId: params.owner.projectId }+ : { id: params.id, organizationId: params.owner.organizationId },+ });+ }++ async listProjectsUsingOrganizationConnection(params: {+ organizationId: string;+ provider: string;+ }): Promise<string[]> {+ const projects = await this.db.project.findMany({+ where: {+ orgId: params.organizationId,+ deletedAt: null,+ LlmApiKeys: {+ none: {+ provider: params.provider,+ },+ },+ },+ select: { id: true },+ });++ return projects.map((project) => project.id);+ }+}diff --git a/web/src/features/llm-api-key/server/llmConnectionService.ts b/web/src/features/llm-api-key/server/llmConnectionService.tsnew file mode 100644index 0000000000..186fe0a7b3--- /dev/null+++ b/web/src/features/llm-api-key/server/llmConnectionService.ts@@ -0,0 +1,942 @@+import { auditLog, type AuditLogActor } from "@/src/features/audit-logs/server";+import { env } from "@/src/env.mjs";+import {+ AuthMethod,+ type BedrockAuthMethod,+} from "@/src/features/llm-api-key/types";+import {+ BEDROCK_USE_DEFAULT_CREDENTIALS,+ BedrockConfigSchema,+ BedrockCredentialSchema,+ ChatMessageRole,+ EvaluatorBlockReason,+ GCPServiceAccountKeySchema,+ InvalidRequestError,+ InternalServerError,+ isDecisionModelAdapter,+ LangfuseConflictError,+ LangfuseNotFoundError,+ type LLMConnectionConfig,+ LLMConnectionConfigSchema,+ LLMAdapter,+ OpenAIConfigSchema,+ supportedModels,+ VERTEXAI_USE_DEFAULT_CREDENTIALS,+ VertexAIConfigSchema,+ type ChatMessage,+ type JSONValue,+} from "@langfuse/shared";+import { decrypt, encrypt } from "@langfuse/shared/encryption";+import {+ blockEvaluatorsUsingProvider,+ ChatMessageType,+ createTypeSafeDecisionModelClient,+ decryptAndParseExtraHeaders,+ DefaultEvalModelService,+ EMPTY_EVALUATOR_BLOCK,+ EvaluatorBlockSource,+ finalizeEvaluatorBlocks,+ generateLLMText,+ getClientInitiatedNonStreamingLlmTimeoutMs,+ isPrismaRecordNotFoundError,+ logger,+ mapLegacyLLMCompletionParams,+ validateLlmConnectionBaseURL,+} from "@langfuse/shared/src/server";+import { prisma, Prisma, type LlmApiKeys } from "@langfuse/shared/src/db";+import {+ handleTopicsLlmApiKeyDeletion,+ invalidateTopicsProcessingActive,+} from "@langfuse/shared/topics/server";++import {+ LlmConnectionRepository,+ type LlmConnectionOwner,+} from "./llmConnectionRepository";++export type LlmConnectionWriteInput = {+ provider: string;+ adapter: LLMAdapter;+ secretKey: string;+ baseURL?: string | null;+ withDefaultModels?: boolean;+ customModels?: string[];+ config?: LLMConnectionConfig;+ extraHeaders?: Record<string, string>;+};++export type LlmConnectionUpdateInput = Omit<+ LlmConnectionWriteInput,+ "secretKey"+> & {+ id: string;+ secretKey?: string;+ extraHeaders?: Record<string, string | null | undefined>;+};++export type SafeLlmConnection = {+ id: string;+ createdAt: Date;+ updatedAt: Date;+ provider: string;+ displaySecretKey: string;+ adapter: LLMAdapter;+ baseURL: string | null;+ customModels: string[];+ withDefaultModels: boolean;+ extraHeaderKeys: string[];+ config: JSONValue | null;+ authMethod?: BedrockAuthMethod;+ secretKey?: undefined;+ extraHeaders?: undefined;+ scope?: "project" | "organization";+ projectId?: string;+ organizationId?: string;+};++type TestLlmConnectionParams = Omit<+ LlmConnectionWriteInput,+ "withDefaultModels"+> & {+ /** Set only when reusing a credential from a project-owned database row. */+ projectId?: string;+ validateBaseUrlBeforeTest?: boolean;+ validateSecretBeforeTest?: boolean;+};++const toSafeConnection = (connection: LlmApiKeys): SafeLlmConnection => {+ const common = {+ id: connection.id,+ createdAt: connection.createdAt,+ updatedAt: connection.updatedAt,+ provider: connection.provider,+ displaySecretKey: connection.displaySecretKey,+ adapter: connection.adapter as LLMAdapter,+ baseURL: connection.baseURL,+ customModels: connection.customModels,+ withDefaultModels: connection.withDefaultModels,+ extraHeaderKeys: connection.extraHeaderKeys,+ config: LLMConnectionConfigSchema.nullable().parse(connection.config),+ ...(connection.adapter === LLMAdapter.Bedrock+ ? { authMethod: getBedrockAuthMethod(decrypt(connection.secretKey)) }+ : {}),+ };++ if (connection.projectId) {+ return {+ ...common,+ scope: "project",+ projectId: connection.projectId,+ };+ }++ if (connection.organizationId) {+ return {+ ...common,+ scope: "organization",+ organizationId: connection.organizationId,+ };+ }++ throw new Error(`LLM connection "${connection.id}" has no owner`);+};++function getDisplaySecretKey(secretKey: string): string {+ if (secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {+ return "Default AWS credentials";+ }+ if (secretKey === VERTEXAI_USE_DEFAULT_CREDENTIALS) {+ return "Default GCP credentials (ADC)";+ }+ return secretKey.endsWith('"}')+ ? `...${secretKey.slice(-6, -2)}`+ : `...${secretKey.slice(-4)}`;+}++function validateBedrockSecretKey(secretKey: string): void {+ if (secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {+ return;+ }++ try {+ BedrockCredentialSchema.parse(JSON.parse(secretKey));+ } catch {+ throw new InvalidRequestError(+ "Invalid Bedrock credentials. Expected a JSON object with either {accessKeyId, secretAccessKey} or {apiKey}.",+ );+ }+}++function getBedrockAuthMethod(+ secretKey: string,+): BedrockAuthMethod | undefined {+ if (secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {+ return AuthMethod.DefaultCredentials;+ }++ try {+ const parsed = BedrockCredentialSchema.parse(JSON.parse(secretKey));+ return "apiKey" in parsed ? AuthMethod.ApiKey : AuthMethod.AccessKeys;+ } catch (error) {+ logger.warn("Failed to derive Bedrock auth method from stored secret", {+ error,+ });+ return undefined;+ }+}++async function testDecisionModelConnection(params: {+ secretKey: string;+ model: string;+ baseURL?: string | null;+ extraHeaders?: Record<string, string>;+}): Promise<{ success: boolean; error?: string }> {+ try {+ const client = createTypeSafeDecisionModelClient({+ apiKey: params.secretKey,+ model: params.model,+ baseURL: params.baseURL,+ extraHeaders: params.extraHeaders,+ });+ await client.evaluate({+ state: { message: "Hello, is anyone there?" },+ questions: {+ kind: {+ type: "choice",+ instructions: "What kind of message is `message`?",+ choices: [{ value: "greeting" }, { value: "other" }],+ },+ },+ });+ return { success: true };+ } catch (error) {+ logger.error(error);+ return {+ success: false,+ error: error instanceof Error ? error.message : "Unknown error",+ };+ }+}++async function validateBaseUrl(params: {+ adapter: LLMAdapter;+ baseURL?: string | null;+}): Promise<void> {+ if (!params.baseURL) {+ return;+ }++ if (params.adapter === LLMAdapter.TypeSafe) {+ const url = new URL(params.baseURL);+ if (/\/systemone\/?$/.test(url.pathname)) {+ throw new Error(+ "Remove /systemone from the end of the base URL. Langfuse appends it.",+ );+ }+ if (url.search || url.hash) {+ throw new Error(+ "Remove the query string from the base URL. Langfuse appends /systemone to it.",+ );+ }+ }++ await validateLlmConnectionBaseURL(params.baseURL);+}++async function validateBaseUrlForWrite(params: {+ adapter: LLMAdapter;+ baseURL?: string | null;+}): Promise<void> {+ try {+ await validateBaseUrl(params);+ } catch (error) {+ throw new InvalidRequestError(+ `Invalid base URL: ${error instanceof Error ? error.message : "Unknown error"}`,+ );+ }+}++function validateSecret(params: {+ adapter: LLMAdapter;+ secretKey: string;+}): void {+ const isLangfuseCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);++ if (params.secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {+ if (isLangfuseCloud || params.adapter !== LLMAdapter.Bedrock) {+ throw new InvalidRequestError(+ "Default AWS credentials are only allowed for Bedrock in self-hosted deployments.",+ );+ }+ }++ if (params.adapter === LLMAdapter.Bedrock) {+ validateBedrockSecretKey(params.secretKey);+ }++ if (params.secretKey === VERTEXAI_USE_DEFAULT_CREDENTIALS) {+ if (isLangfuseCloud || params.adapter !== LLMAdapter.VertexAI) {+ throw new InvalidRequestError(+ "Default GCP credentials (ADC) are only allowed for Vertex AI in self-hosted deployments.",+ );+ }+ }++ if (!env.ENCRYPTION_KEY) {+ if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {+ throw new InternalServerError("Internal server error");+ }+ throw new InvalidRequestError(+ "Missing environment variable: `ENCRYPTION_KEY`. Please consult our docs: https://langfuse.com/self-hosting",+ );+ }+}++function resolveUpdatedExtraHeaders(params: {+ inputHeaders: Record<string, string | null | undefined> | undefined;+ storedHeaders: string | null;+ isBaseURLChanged: boolean;+}): Record<string, string> | undefined {+ const existingHeaders: Record<string, string> = params.isBaseURLChanged+ ? {}+ : (decryptAndParseExtraHeaders(params.storedHeaders) ?? {});++ if (params.inputHeaders === undefined) {+ return Object.keys(existingHeaders).length > 0+ ? existingHeaders+ : undefined;+ }++ const extraHeaders: Record<string, string> = {};+ for (const [key, value] of Object.entries(params.inputHeaders)) {+ if (value === null || value === undefined || value === "") {+ if (existingHeaders[key] !== undefined) {+ extraHeaders[key] = existingHeaders[key];+ }+ } else {+ extraHeaders[key] = value;+ }+ }++ return Object.keys(extraHeaders).length > 0 ? extraHeaders : undefined;+}++async function testLlmConnection(+ params: TestLlmConnectionParams,+): Promise<{ success: boolean; error?: string }> {+ try {+ if (params.validateBaseUrlBeforeTest !== false) {+ await validateBaseUrl(params);+ }+ if (params.validateSecretBeforeTest !== false) {+ validateSecret({ adapter: params.adapter, secretKey: params.secretKey });+ }++ const model = params.customModels?.length+ ? params.customModels[0]+ : supportedModels[params.adapter][0];+ if (!model) {+ throw new Error("No model found");+ }++ if (isDecisionModelAdapter(params.adapter)) {+ return testDecisionModelConnection({+ secretKey: params.secretKey,+ model,+ baseURL: params.baseURL,+ extraHeaders: params.extraHeaders,+ });+ }++ if (+ params.adapter === LLMAdapter.VertexAI &&+ params.secretKey !== VERTEXAI_USE_DEFAULT_CREDENTIALS+ ) {+ const parsed = GCPServiceAccountKeySchema.safeParse(+ JSON.parse(params.secretKey),+ );+ if (!parsed.success) {+ throw new Error("Invalid GCP service account JSON key");+ }+ }++ const messages: ChatMessage[] = [+ {+ role: ChatMessageRole.User,+ content: "How are you?",+ type: ChatMessageType.User,+ },+ ];++ let parsedConfig: LLMConnectionConfig | null = null;+ if (params.config && params.adapter === LLMAdapter.Bedrock) {+ const config = BedrockConfigSchema.parse(params.config);+ parsedConfig = { region: config.region };+ } else if (params.config && params.adapter === LLMAdapter.OpenAI) {+ parsedConfig = OpenAIConfigSchema.parse(params.config);+ } else if (params.config && params.adapter === LLMAdapter.VertexAI) {+ const config = VertexAIConfigSchema.parse(params.config);+ parsedConfig = config.location ? { location: config.location } : null;+ }++ await generateLLMText({+ ...mapLegacyLLMCompletionParams({+ modelParams: {+ adapter: params.adapter,+ provider: params.provider,+ model,+ },+ connection: {+ secretKey: encrypt(params.secretKey),+ projectId: params.projectId,+ extraHeaders: params.extraHeaders+ ? encrypt(JSON.stringify(params.extraHeaders))+ : undefined,+ baseURL: params.baseURL || undefined,+ config: parsedConfig,+ },+ messages,+ }),+ maxRetries: 1,+ timeout: getClientInitiatedNonStreamingLlmTimeoutMs(),+ });++ return { success: true };+ } catch (error) {+ logger.error(error);+ return {+ success: false,+ error: error instanceof Error ? error.message : "Unknown error",+ };+ }+}++export class LlmConnectionService {+ constructor(+ private readonly repository = new LlmConnectionRepository(),+ private readonly db: typeof prisma = prisma,+ ) {}++ private async validateHierarchyAdapter(params: {+ repository: LlmConnectionRepository;+ owner: LlmConnectionOwner;+ provider: string;+ adapter: string;+ }): Promise<void> {+ const incompatibleConnection =+ await params.repository.findIncompatibleConnection({+ owner: params.owner,+ provider: params.provider,+ adapter: params.adapter,+ });+ if (incompatibleConnection) {+ throw new InvalidRequestError(+ "Project and organization connections with the same provider name must use the same adapter.",+ );+ }+ }++ async list(params: {+ owner: LlmConnectionOwner;+ includeDecisionModels?: boolean;+ limit?: number;+ page?: number;+ }): Promise<{ data: SafeLlmConnection[]; totalCount: number }> {+ const includeDecisionModels = params.includeDecisionModels ?? false;+ const [connections, totalCount] = await Promise.all([+ this.repository.list({+ owner: params.owner,+ includeDecisionModels,+ limit: params.limit,+ offset:+ params.limit && params.page+ ? (params.page - 1) * params.limit+ : undefined,+ }),+ this.repository.count({+ owner: params.owner,+ includeDecisionModels,+ }),+ ]);++ return {+ data: connections.map(toSafeConnection),+ totalCount,+ };+ }++ async listInherited(params: {+ owner: Extract<LlmConnectionOwner, { type: "project" }>;+ includeDecisionModels?: boolean;+ }): Promise<Array<SafeLlmConnection & { overriddenByProject: boolean }>> {+ const result = await this.repository.listOrganizationConnectionsForProject({+ projectId: params.owner.projectId,+ organizationId: params.owner.organizationId,+ includeDecisionModels: params.includeDecisionModels ?? false,+ });++ return result.connections.map((connection) => ({+ ...toSafeConnection(connection),+ overriddenByProject: result.overriddenProviders.has(connection.provider),+ }));+ }++ async listEffective(params: {+ owner: Extract<LlmConnectionOwner, { type: "project" }>;+ includeDecisionModels?: boolean;+ }): Promise<{ data: SafeLlmConnection[] }> {+ const connections = await this.repository.listEffectiveForProject({+ projectId: params.owner.projectId,+ organizationId: params.owner.organizationId,+ includeDecisionModels: params.includeDecisionModels ?? false,+ });++ return { data: connections.map(toSafeConnection) };+ }++ async exists(params: {+ owner: LlmConnectionOwner;+ provider: string;+ }): Promise<boolean> {+ return Boolean(await this.repository.findByProvider(params));+ }++ async create(params: {+ owner: LlmConnectionOwner;+ input: LlmConnectionWriteInput;+ actor: AuditLogActor;+ }): Promise<SafeLlmConnection> {+ await validateBaseUrlForWrite(params.input);+ validateSecret(params.input);++ let key: LlmApiKeys;+ try {+ key = await this.db.$transaction(async (tx) => {+ const repository = this.repository.withDb(tx);+ await this.validateHierarchyAdapter({+ repository,+ owner: params.owner,+ provider: params.input.provider,+ adapter: params.input.adapter,+ });+ return repository.create({+ owner: params.owner,+ data: {+ provider: params.input.provider,+ adapter: params.input.adapter,+ secretKey: encrypt(params.input.secretKey),+ displaySecretKey: getDisplaySecretKey(params.input.secretKey),+ baseURL: params.input.baseURL,+ withDefaultModels: params.input.withDefaultModels,+ customModels: params.input.customModels,+ config: params.input.config,+ extraHeaders: params.input.extraHeaders+ ? encrypt(JSON.stringify(params.input.extraHeaders))+ : undefined,+ extraHeaderKeys: params.input.extraHeaders+ ? Object.keys(params.input.extraHeaders)+ : undefined,+ },+ });+ });+ } catch (error) {+ if (+ error instanceof Prisma.PrismaClientKnownRequestError &&+ error.code === "P2002"+ ) {+ throw new LangfuseConflictError(+ "There already exists an API key for this provider.",+ );+ }+ throw error;+ }++ await auditLog({+ ...params.actor,+ resourceType: "llmApiKey",+ resourceId: key.id,+ action: "create",+ });++ return toSafeConnection(key);+ }++ async upsert(params: {+ owner: LlmConnectionOwner;+ input: LlmConnectionWriteInput;+ actor: AuditLogActor;+ }): Promise<{ connection: SafeLlmConnection; created: boolean }> {+ const storedConnection = await this.repository.findByProvider({+ owner: params.owner,+ provider: params.input.provider,+ });+ if (+ !storedConnection ||+ params.input.baseURL !== storedConnection.baseURL+ ) {+ await validateBaseUrlForWrite(params.input);+ }+ validateSecret(params.input);++ const encryptedSecretKey = encrypt(params.input.secretKey);+ const displaySecretKey = getDisplaySecretKey(params.input.secretKey);+ const encryptedExtraHeaders = params.input.extraHeaders+ ? encrypt(JSON.stringify(params.input.extraHeaders))+ : undefined;+ const extraHeaderKeys = params.input.extraHeaders+ ? Object.keys(params.input.extraHeaders)+ : undefined;+ const { connection, existing } = await this.db.$transaction(async (tx) => {+ const repository = this.repository.withDb(tx);+ const existing = await repository.findByProvider({+ owner: params.owner,+ provider: params.input.provider,+ });+ if (!existing || existing.adapter !== params.input.adapter) {+ await this.validateHierarchyAdapter({+ repository,+ owner: params.owner,+ provider: params.input.provider,+ adapter: params.input.adapter,+ });+ }+ const connection = await repository.upsert({+ owner: params.owner,+ provider: params.input.provider,+ create: {+ provider: params.input.provider,+ adapter: params.input.adapter,+ secretKey: encryptedSecretKey,+ displaySecretKey,+ baseURL: params.input.baseURL,+ withDefaultModels: params.input.withDefaultModels,+ customModels: params.input.customModels,+ config: params.input.config,+ extraHeaders: encryptedExtraHeaders,+ extraHeaderKeys,+ },+ update: {+ adapter: params.input.adapter,+ secretKey: encryptedSecretKey,+ displaySecretKey,+ baseURL: params.input.baseURL ?? null,+ withDefaultModels: params.input.withDefaultModels,+ customModels: params.input.customModels ?? [],+ ...(params.input.config !== undefined+ ? { config: params.input.config ?? Prisma.DbNull }+ : {}),+ extraHeaders: encryptedExtraHeaders ?? null,+ extraHeaderKeys: extraHeaderKeys ?? [],+ },+ });+ return { connection, existing };+ });++ await auditLog({+ ...params.actor,+ resourceType: "llmApiKey",+ resourceId: connection.id,+ action: existing ? "update" : "create",+ ...(existing+ ? {+ before: toSafeConnection(existing),+ after: toSafeConnection(connection),+ }+ : {}),+ });++ return {+ connection: toSafeConnection(connection),+ created: existing === null,+ };+ }++ async update(params: {+ owner: LlmConnectionOwner;+ input: LlmConnectionUpdateInput;+ actor: AuditLogActor;+ }): Promise<SafeLlmConnection> {+ const existingKey = await this.repository.findById({+ owner: params.owner,+ id: params.input.id,+ });+ if (!existingKey) {+ throw new LangfuseNotFoundError("LLM connection not found");+ }+ if (+ params.input.provider !== existingKey.provider ||+ params.input.adapter !== existingKey.adapter+ ) {+ throw new InvalidRequestError("Provider and adapter cannot be changed");+ }++ const isBaseURLChanged =+ params.input.baseURL !== undefined &&+ params.input.baseURL !== existingKey.baseURL;+ if (isBaseURLChanged && !params.input.secretKey) {+ throw new InvalidRequestError(+ "Secret key is required when changing the base URL",+ );+ }+ if (isBaseURLChanged) {+ await validateBaseUrlForWrite(params.input);+ }+ if (params.input.secretKey) {+ validateSecret({+ adapter: params.input.adapter,+ secretKey: params.input.secretKey,+ });+ }++ const extraHeaders = resolveUpdatedExtraHeaders({+ inputHeaders: params.input.extraHeaders,+ storedHeaders: existingKey.extraHeaders,+ isBaseURLChanged,+ });+ let encryptedExtraHeaders: string | null | undefined;+ let extraHeaderKeys: string[] | undefined;+ if (extraHeaders) {+ encryptedExtraHeaders = encrypt(JSON.stringify(extraHeaders));+ extraHeaderKeys = Object.keys(extraHeaders);+ } else if (isBaseURLChanged || params.input.extraHeaders !== undefined) {+ encryptedExtraHeaders = null;+ extraHeaderKeys = [];+ }++ const key = await this.repository.update({+ owner: params.owner,+ id: params.input.id,+ data: {+ ...(params.input.secretKey+ ? {+ secretKey: encrypt(params.input.secretKey),+ displaySecretKey: getDisplaySecretKey(params.input.secretKey),+ }+ : {}),+ extraHeaders: encryptedExtraHeaders,+ extraHeaderKeys,+ baseURL: params.input.baseURL,+ withDefaultModels: params.input.withDefaultModels,+ customModels: params.input.customModels,+ config: params.input.config,+ },+ });++ await auditLog({+ ...params.actor,+ resourceType: "llmApiKey",+ resourceId: key.id,+ action: "update",+ before: toSafeConnection(existingKey),+ after: toSafeConnection(key),+ });++ return toSafeConnection(key);+ }++ test(input: LlmConnectionWriteInput): Promise<{+ success: boolean;+ error?: string;+ }> {+ return testLlmConnection(input);+ }++ async testUpdate(params: {+ owner: LlmConnectionOwner;+ input: LlmConnectionUpdateInput;+ }): Promise<{ success: boolean; error?: string }> {+ try {+ const existingKey = await this.repository.findById({+ owner: params.owner,+ id: params.input.id,+ });+ if (!existingKey) {+ throw new LangfuseNotFoundError("LLM connection not found");+ }++ const hasNewSecretKey = Boolean(params.input.secretKey);+ const baseURL =+ params.input.baseURL !== undefined+ ? params.input.baseURL+ : existingKey.baseURL;+ const isBaseURLChanged = baseURL !== existingKey.baseURL;+ if (isBaseURLChanged && !hasNewSecretKey) {+ throw new InvalidRequestError(+ "Secret key is required when changing the base URL",+ );+ }++ const extraHeaders = resolveUpdatedExtraHeaders({+ inputHeaders: params.input.extraHeaders,+ storedHeaders: existingKey.extraHeaders,+ isBaseURLChanged,+ });++ return testLlmConnection({+ adapter: params.input.adapter,+ provider: params.input.provider,+ secretKey: params.input.secretKey || decrypt(existingKey.secretKey),+ projectId: hasNewSecretKey+ ? undefined+ : (existingKey.projectId ?? undefined),+ baseURL,+ customModels: params.input.customModels ?? existingKey.customModels,+ extraHeaders,+ config:+ params.input.config ??+ LLMConnectionConfigSchema.nullable().parse(existingKey.config) ??+ undefined,+ validateBaseUrlBeforeTest: isBaseURLChanged,+ validateSecretBeforeTest: hasNewSecretKey,+ });+ } catch (error) {+ logger.error(error);+ return {+ success: false,+ error: error instanceof Error ? error.message : "Unknown error",+ };+ }+ }++ async delete(params: {+ owner: LlmConnectionOwner;+ id: string;+ actor: AuditLogActor;+ }): Promise<void> {+ const connection = await this.repository.findById({+ owner: params.owner,+ id: params.id,+ });+ if (!connection) {+ throw new LangfuseNotFoundError("LLM connection not found");+ }++ const organizationFallback =+ params.owner.type === "project"+ ? await this.repository.findByProvider({+ owner: {+ type: "organization",+ organizationId: params.owner.organizationId,+ },+ provider: connection.provider,+ })+ : null;+ const hasCompatibleOrganizationFallback =+ organizationFallback?.adapter === connection.adapter;+ let providerProjectIds: string[];+ if (params.owner.type === "project") {+ providerProjectIds = hasCompatibleOrganizationFallback+ ? []+ : [params.owner.projectId];+ } else {+ providerProjectIds =+ await this.repository.listProjectsUsingOrganizationConnection({+ organizationId: params.owner.organizationId,+ provider: connection.provider,+ });+ }+ const { blockResults, topicProjectIds } = await this.db.$transaction(+ async (tx) => {+ const txRepository = this.repository.withDb(tx);+ const defaultModelResults =+ await DefaultEvalModelService.handleLlmApiKeyDeletion({+ tx,+ llmApiKeyId: connection.id,+ llmApiKeyOwner: {+ projectId: connection.projectId,+ organizationId: connection.organizationId,+ },+ provider: connection.provider,+ adapter: connection.adapter,+ });+ const defaultModelBlocksByProjectId = new Map(+ defaultModelResults.map(({ projectId, defaultModelBlock }) => [+ projectId,+ defaultModelBlock,+ ]),+ );+ const affectedProjectIds = new Set([+ ...providerProjectIds,+ ...defaultModelBlocksByProjectId.keys(),+ ]);+ const providerProjectIdSet = new Set(providerProjectIds);+ const results: Array<{+ projectId: string;+ providerBlock: typeof EMPTY_EVALUATOR_BLOCK;+ defaultModelBlock: typeof EMPTY_EVALUATOR_BLOCK;+ }> = [];++ for (const projectId of affectedProjectIds) {+ const providerBlock = providerProjectIdSet.has(projectId)+ ? await blockEvaluatorsUsingProvider({+ tx,+ projectId,+ provider: connection.provider,+ })+ : EMPTY_EVALUATOR_BLOCK;+ const defaultModelBlock =+ defaultModelBlocksByProjectId.get(projectId) ??+ EMPTY_EVALUATOR_BLOCK;+ results.push({ projectId, providerBlock, defaultModelBlock });+ }++ const topicProjectIds = await handleTopicsLlmApiKeyDeletion({+ tx,+ llmApiKeyId: connection.id,+ llmApiKeyOwner: {+ projectId: connection.projectId,+ organizationId: connection.organizationId,+ },+ provider: connection.provider,+ adapter: connection.adapter,+ });++ try {+ await txRepository.delete({+ owner: params.owner,+ id: params.id,+ });+ } catch (error) {+ if (isPrismaRecordNotFoundError(error)) {+ throw new LangfuseNotFoundError("LLM connection not found");+ }+ throw error;+ }++ await auditLog(+ {+ ...params.actor,+ resourceType: "llmApiKey",+ resourceId: connection.id,+ action: "delete",+ before: toSafeConnection(connection),+ },+ tx,+ );++ return {+ blockResults: results,+ topicProjectIds,+ };+ },+ );++ await Promise.all([+ ...topicProjectIds.map((projectId) =>+ invalidateTopicsProcessingActive(projectId),+ ),+ ...blockResults.map((result) =>+ finalizeEvaluatorBlocks({+ projectId: result.projectId,+ source: EvaluatorBlockSource.LLM_API_KEY_DELETION,+ evaluatorIdsByReason: {+ [EvaluatorBlockReason.LLM_CONNECTION_MISSING]:+ result.providerBlock.blockedEvaluatorIds,+ [EvaluatorBlockReason.DEFAULT_EVAL_MODEL_MISSING]:+ result.defaultModelBlock.blockedEvaluatorIds,+ },+ }),+ ),+ ]);+ }+}diff --git a/web/src/features/llm-api-key/server/organizationRouter.ts b/web/src/features/llm-api-key/server/organizationRouter.tsnew file mode 100644index 0000000000..a399bcc8b0--- /dev/null+++ b/web/src/features/llm-api-key/server/organizationRouter.ts@@ -0,0 +1,129 @@+import { z } from "zod";+import type { Session } from "next-auth";++import { throwIfNoOrganizationAccess } from "@/src/features/rbac";+import {+ CreateOrganizationLlmApiKey,+ UpdateOrganizationLlmApiKey,+} from "@/src/features/llm-api-key/types";+import {+ createTRPCRouter,+ protectedOrganizationProcedure,+ protectedOrganizationProcedureWithoutTracing,+} from "@/src/server/api/trpc";++import { LlmConnectionService } from "./llmConnectionService";++const requireAccess = (params: {+ session: Session;+ orgId: string;+ scope: "organizationLlmApiKeys:read" | "organizationLlmApiKeys:CUD";+}) =>+ throwIfNoOrganizationAccess({+ session: params.session,+ organizationId: params.orgId,+ scope: params.scope,+ });++export const organizationLlmApiKeyRouter = createTRPCRouter({+ all: protectedOrganizationProcedure+ .input(+ z.object({+ orgId: z.string(),+ includeDecisionModels: z.boolean().optional().default(false),+ }),+ )+ .query(async ({ input, ctx }) => {+ requireAccess({+ session: ctx.session,+ orgId: input.orgId,+ scope: "organizationLlmApiKeys:read",+ });++ return new LlmConnectionService().list({+ owner: {+ type: "organization",+ organizationId: input.orgId,+ },+ includeDecisionModels: input.includeDecisionModels,+ });+ }),++ create: protectedOrganizationProcedureWithoutTracing+ .input(CreateOrganizationLlmApiKey)+ .mutation(async ({ input, ctx }) => {+ requireAccess({+ session: ctx.session,+ orgId: input.orgId,+ scope: "organizationLlmApiKeys:CUD",+ });+ const { orgId, ...connectionInput } = input;+ return new LlmConnectionService().create({+ owner: { type: "organization", organizationId: orgId },+ input: connectionInput,+ actor: { session: ctx.session },+ });+ }),++ update: protectedOrganizationProcedureWithoutTracing+ .input(UpdateOrganizationLlmApiKey)+ .mutation(async ({ input, ctx }) => {+ requireAccess({+ session: ctx.session,+ orgId: input.orgId,+ scope: "organizationLlmApiKeys:CUD",+ });+ const { orgId, ...connectionInput } = input;+ return new LlmConnectionService().update({+ owner: { type: "organization", organizationId: orgId },+ input: connectionInput,+ actor: { session: ctx.session },+ });+ }),++ delete: protectedOrganizationProcedure+ .input(z.object({ orgId: z.string(), id: z.string() }))+ .mutation(async ({ input, ctx }) => {+ requireAccess({+ session: ctx.session,+ orgId: input.orgId,+ scope: "organizationLlmApiKeys:CUD",+ });+ await new LlmConnectionService().delete({+ owner: {+ type: "organization",+ organizationId: input.orgId,+ },+ id: input.id,+ actor: { session: ctx.session },+ });+ return { success: true };+ }),++ test: protectedOrganizationProcedureWithoutTracing+ .input(CreateOrganizationLlmApiKey)+ .mutation(async ({ input, ctx }) => {+ requireAccess({+ session: ctx.session,+ orgId: input.orgId,+ scope: "organizationLlmApiKeys:CUD",+ });+ const { orgId: _orgId, ...connectionInput } = input;+ return new LlmConnectionService().test(connectionInput);+ }),++ testUpdate: protectedOrganizationProcedureWithoutTracing+ .input(UpdateOrganizationLlmApiKey)+ .mutation(async ({ input, ctx }) => {+ requireAccess({+ session: ctx.session,+ orgId: input.orgId,+ scope: "organizationLlmApiKeys:CUD",+ });+ const { orgId, ...connectionInput } = input;+ return new LlmConnectionService().testUpdate({+ owner: { type: "organization", organizationId: orgId },+ input: connectionInput,+ });+ }),+});diff --git a/web/src/features/llm-api-key/server/router.ts b/web/src/features/llm-api-key/server/router.tsindex 1fa37b64e9..aaaa192655 100644--- a/web/src/features/llm-api-key/server/router.ts+++ b/web/src/features/llm-api-key/server/router.ts@@ -1,420 +1,35 @@-/* eslint-disable no-nested-ternary */import { z } from "zod";-import { auditLog } from "@/src/features/audit-logs/server";import {- AuthMethod,CreateLlmApiKey,UpdateLlmApiKey,- SafeLlmApiKeySchema,- type BedrockAuthMethod,} from "@/src/features/llm-api-key/types";-import { isPrismaRecordNotFoundError } from "@/src/features/analytics-integrations/server";import { throwIfNoProjectAccess } from "@/src/features/rbac";import {createTRPCRouter,protectedProjectProcedure,protectedProjectProcedureWithoutTracing,} from "@/src/server/api/trpc";-import {- type ChatMessage,- ChatMessageRole,- supportedModels,- GCPServiceAccountKeySchema,- BedrockConfigSchema,- BedrockCredentialSchema,- OpenAIConfigSchema,- VertexAIConfigSchema,- BEDROCK_USE_DEFAULT_CREDENTIALS,- VERTEXAI_USE_DEFAULT_CREDENTIALS,- EvaluatorBlockReason,- LangfuseNotFoundError,- type LLMConnectionConfig,-} from "@langfuse/shared";--import { encrypt, decrypt } from "@langfuse/shared/encryption";-import { Prisma } from "@langfuse/shared/src/db";-import {- ChatMessageType,- createTypeSafeDecisionModelClient,- DECISION_MODEL_ADAPTERS,- generateLLMText,- getClientInitiatedNonStreamingLlmTimeoutMs,- isDecisionModelAdapter,- LLMAdapter,- logger,- mapLegacyLLMCompletionParams,- decryptAndParseExtraHeaders,- blockEvaluatorsUsingDefaultModel,- blockEvaluatorsUsingProvider,- EMPTY_EVALUATOR_BLOCK,- EvaluatorBlockSource,- finalizeEvaluatorBlocks,- validateLlmConnectionBaseURL,-} from "@langfuse/shared/src/server";-import { env } from "@/src/env.mjs";-import {- invalidateTopicsProcessingActive,- pauseTopicsModelsUsingConnection,-} from "@langfuse/shared/topics/server";-import { TRPCError } from "@trpc/server";--export function getDisplaySecretKey(secretKey: string) {- if (secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {- return "Default AWS credentials";- }- if (secretKey === VERTEXAI_USE_DEFAULT_CREDENTIALS) {- return "Default GCP credentials (ADC)";- }- return secretKey.endsWith('"}')- ? "..." + secretKey.slice(-6, -2)- : "..." + secretKey.slice(-4);-}--function validateBedrockSecretKey(secretKey: string) {- if (secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {- return;- }-- try {- BedrockCredentialSchema.parse(JSON.parse(secretKey));- } catch {- throw new Error(- "Invalid Bedrock credentials. Expected a JSON object with either {accessKeyId, secretAccessKey} or {apiKey}.",- );- }-}--function getBedrockAuthMethod(- secretKey: string,-): BedrockAuthMethod | undefined {- if (secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {- return AuthMethod.DefaultCredentials;- }-- try {- const parsed = BedrockCredentialSchema.parse(JSON.parse(secretKey));- return parsed && "apiKey" in parsed- ? AuthMethod.ApiKey- : AuthMethod.AccessKeys;- } catch (error) {- logger.warn("Failed to derive Bedrock auth method from stored secret", {- error,- });- return undefined;- }-}--type TestLLMConnectionParams = {- adapter: LLMAdapter;- provider: string;- secretKey: string;- /** Set only when reusing a credential from the scoped database row. */- projectId?: string;- baseURL?: string | null;- customModels?: string[];- extraHeaders?: Record<string, string>;- config?: unknown;-};--async function testDecisionModelConnection(params: {- secretKey: string;- model: string;- baseURL?: string | null;- extraHeaders?: Record<string, string>;-}): Promise<{ success: boolean; error?: string }> {- try {- const client = createTypeSafeDecisionModelClient({- apiKey: params.secretKey,- model: params.model,- baseURL: params.baseURL,- extraHeaders: params.extraHeaders,- });- await client.evaluate({- state: { message: "Hello, is anyone there?" },- questions: {- kind: {- type: "choice",- instructions: "What kind of message is `message`?",- choices: [{ value: "greeting" }, { value: "other" }],- },- },- });- return { success: true };- } catch (err) {- logger.error(err);- return {- success: false,- error: err instanceof Error ? err.message : "Unknown error",- };- }-}--async function testLLMConnection(- params: TestLLMConnectionParams,-): Promise<{ success: boolean; error?: string }> {- try {- const model = params.customModels?.length- ? params.customModels[0]- : supportedModels[params.adapter][0];-- if (!model) throw Error("No model found");-- if (isDecisionModelAdapter(params.adapter)) {- return await testDecisionModelConnection({- secretKey: params.secretKey,- model,- baseURL: params.baseURL,- extraHeaders: params.extraHeaders,- });- }-- if (params.adapter === LLMAdapter.VertexAI) {- // Skip validation if using ADC (Application Default Credentials)- if (params.secretKey !== VERTEXAI_USE_DEFAULT_CREDENTIALS) {- const parsed = GCPServiceAccountKeySchema.safeParse(- JSON.parse(params.secretKey),- );- if (!parsed.success)- throw Error("Invalid GCP service account JSON key");- }- }-- const testMessages: ChatMessage[] = [- {- role: ChatMessageRole.User,- content: "How are you?",- type: ChatMessageType.User,- },- ];-- // Parse config properly for type safety- let parsedConfig: LLMConnectionConfig | null = null;- if (params.config && params.adapter === LLMAdapter.Bedrock) {- const bedrockConfig = BedrockConfigSchema.parse(params.config);-- parsedConfig = { region: bedrockConfig.region };- } else if (params.config && params.adapter === LLMAdapter.OpenAI) {- parsedConfig = OpenAIConfigSchema.parse(params.config);- } else if (params.config && params.adapter === LLMAdapter.VertexAI) {- const vertexAIConfig = VertexAIConfigSchema.parse(params.config);- parsedConfig = vertexAIConfig.location- ? { location: vertexAIConfig.location }- : null;- }-- await generateLLMText({- ...mapLegacyLLMCompletionParams({- modelParams: {- adapter: params.adapter,- provider: params.provider,- model,- },- connection: {- secretKey: encrypt(params.secretKey),- projectId: params.projectId,- extraHeaders:- params.extraHeaders && encrypt(JSON.stringify(params.extraHeaders)),- baseURL: params.baseURL || undefined,- config: parsedConfig,- },- messages: testMessages,- }),- maxRetries: 1,- timeout: getClientInitiatedNonStreamingLlmTimeoutMs(),- });-- return { success: true };- } catch (err) {- logger.error(err);-- return {- success: false,- error: err instanceof Error ? err.message : "Unknown error",- };- }-}--async function validateBaseURLForAdapter(params: {- adapter: LLMAdapter;- baseURL: string;-}): Promise<void> {- // The TypeSafe provider appends /systemone to the raw base URL string.- if (params.adapter === LLMAdapter.TypeSafe) {- const url = new URL(params.baseURL);- if (/\/systemone\/?$/.test(url.pathname)) {- throw new Error(- "Remove /systemone from the end of the base URL. Langfuse appends it.",- );- }- if (url.search || url.hash) {- throw new Error(- "Remove the query string from the base URL. Langfuse appends /systemone to it.",- );- }- }-- await validateLlmConnectionBaseURL(params.baseURL);-}--async function validateBaseURLForWrite(params: {- adapter: LLMAdapter;- baseURL?: string | null;- errorPrefix?: string;-}): Promise<void> {- if (!params.baseURL) {- return;- }-- try {- await validateBaseURLForAdapter({- adapter: params.adapter,- baseURL: params.baseURL,- });- } catch (error) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- error instanceof Error- ? `${params.errorPrefix ?? "Invalid base URL"}: ${error.message}`- : (params.errorPrefix ?? "Invalid base URL"),- });- }-}--function resolveUpdatedExtraHeaders(params: {- inputHeaders: Record<string, string | null | undefined> | undefined;- storedHeaders: string | null;- isBaseURLChanged: boolean;-}): Record<string, string> | undefined {- const existingHeaders: Record<string, string> = params.isBaseURLChanged- ? {}- : (decryptAndParseExtraHeaders(params.storedHeaders) ?? {});-- if (params.inputHeaders === undefined) {- return Object.keys(existingHeaders).length > 0- ? existingHeaders- : undefined;- }-- const extraHeaders: Record<string, string> = {};- for (const [key, value] of Object.entries(params.inputHeaders)) {- if (value === null || value === undefined || value === "") {- if (existingHeaders[key] !== undefined) {- extraHeaders[key] = existingHeaders[key];- }- } else {- extraHeaders[key] = value;- }- }-- return Object.keys(extraHeaders).length > 0 ? extraHeaders : undefined;-}+import { LlmConnectionService } from "./llmConnectionService";export const llmApiKeyRouter = createTRPCRouter({create: protectedProjectProcedureWithoutTracing.input(CreateLlmApiKey).mutation(async ({ input, ctx }) => {- try {- throwIfNoProjectAccess({- session: ctx.session,- projectId: input.projectId,- scope: "llmApiKeys:create",- });-- await validateBaseURLForWrite({- adapter: input.adapter,- baseURL: input.baseURL,- });-- // Validate that default credentials sentinel is only allowed for Bedrock/VertexAI in self-hosted deployments- const isLangfuseCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);-- if (input.secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {- if (isLangfuseCloud || input.adapter !== LLMAdapter.Bedrock) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- "Default AWS credentials are only allowed for Bedrock in self-hosted deployments.",- });- }- }-- if (input.adapter === LLMAdapter.Bedrock) {- try {- validateBedrockSecretKey(input.secretKey);- } catch (e) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- e instanceof Error ? e.message : "Invalid Bedrock credentials.",- });- }- }-- if (input.secretKey === VERTEXAI_USE_DEFAULT_CREDENTIALS) {- if (isLangfuseCloud || input.adapter !== LLMAdapter.VertexAI) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- "Default GCP credentials (ADC) are only allowed for Vertex AI in self-hosted deployments.",- });- }- }-- if (!env.ENCRYPTION_KEY) {- if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {- throw new TRPCError({- code: "INTERNAL_SERVER_ERROR",- message: "Internal server error",- });- } else {- throw new TRPCError({- code: "BAD_REQUEST",- message:- "Missing environment variable: `ENCRYPTION_KEY`. Please consult our docs: https://langfuse.com/self-hosting",- });- }- }-- const key = await ctx.prisma.llmApiKeys.create({- data: {- projectId: input.projectId,- secretKey: encrypt(input.secretKey),- extraHeaders: input.extraHeaders- ? encrypt(JSON.stringify(input.extraHeaders))- : undefined,- extraHeaderKeys: input.extraHeaders- ? Object.keys(input.extraHeaders)- : undefined,- adapter: input.adapter,- displaySecretKey: getDisplaySecretKey(input.secretKey),- provider: input.provider,- baseURL: input.baseURL,- withDefaultModels: input.withDefaultModels,- customModels: input.customModels,- config: input.config,- },- });-- await auditLog({- session: ctx.session,- resourceType: "llmApiKey",- resourceId: key.id,- action: "create",- });- } catch (e) {- if (- e instanceof Prisma.PrismaClientKnownRequestError &&- e.code === "P2002"- ) {- throw new TRPCError({- code: "CONFLICT",- message: "There already exists an API key for this provider.",- });- }- logger.error(e);- throw e;- }+ throwIfNoProjectAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "llmApiKeys:create",+ });+ const { projectId, ...connectionInput } = input;+ await new LlmConnectionService().create({+ owner: {+ type: "project",+ projectId,+ organizationId: ctx.session.orgId,+ },+ input: connectionInput,+ actor: { session: ctx.session },+ });}),delete: protectedProjectProcedure.input(@@ -429,92 +44,15 @@ export const llmApiKeyRouter = createTRPCRouter({projectId: input.projectId,scope: "llmApiKeys:delete",});-- const llmApiKey = await ctx.prisma.llmApiKeys.findUnique({- where: {- id: input.id,+ await new LlmConnectionService().delete({+ owner: {+ type: "project",projectId: input.projectId,+ organizationId: ctx.session.orgId,},+ id: input.id,+ actor: { session: ctx.session },});-- if (!llmApiKey) {- throw new LangfuseNotFoundError("LLM API key not found");- }-- let result;- try {- result = await ctx.prisma.$transaction(async (tx) => {- // Check if the llm api key is used for the default evaluation model- const defaultModel = await tx.defaultLlmModel.findFirst({- where: {- projectId: input.projectId,- },- select: {- llmApiKeyId: true,- },- });-- const providerBlock = llmApiKey.provider- ? await blockEvaluatorsUsingProvider({- tx,- projectId: input.projectId,- provider: llmApiKey.provider,- })- : EMPTY_EVALUATOR_BLOCK;-- const defaultModelBlock =- !!defaultModel && defaultModel.llmApiKeyId === llmApiKey.id- ? await blockEvaluatorsUsingDefaultModel({- tx,- projectId: input.projectId,- })- : EMPTY_EVALUATOR_BLOCK;-- await pauseTopicsModelsUsingConnection(- {- projectId: input.projectId,- llmApiKeyId: llmApiKey.id,- provider: llmApiKey.provider,- },- tx,- );-- await tx.llmApiKeys.delete({- where: {- id: input.id,- projectId: input.projectId,- },- });-- await auditLog({- session: ctx.session,- resourceType: "llmApiKey",- resourceId: input.id,- before: llmApiKey,- action: "delete",- });-- return { providerBlock, defaultModelBlock };- });- } catch (error) {- if (isPrismaRecordNotFoundError(error)) {- throw new LangfuseNotFoundError("LLM API key not found");- }- throw error;- }-- await invalidateTopicsProcessingActive(input.projectId);- await finalizeEvaluatorBlocks({- projectId: input.projectId,- source: EvaluatorBlockSource.LLM_API_KEY_DELETION,- evaluatorIdsByReason: {- [EvaluatorBlockReason.LLM_CONNECTION_MISSING]:- result.providerBlock.blockedEvaluatorIds,- [EvaluatorBlockReason.DEFAULT_EVAL_MODEL_MISSING]:- result.defaultModelBlock.blockedEvaluatorIds,- },- });-return { success: true };}),all: protectedProjectProcedure@@ -530,52 +68,60 @@ export const llmApiKeyRouter = createTRPCRouter({projectId: input.projectId,scope: "llmApiKeys:read",});+ return new LlmConnectionService().list({+ owner: {+ type: "project",+ projectId: input.projectId,+ organizationId: ctx.session.orgId,+ },+ includeDecisionModels: input.includeDecisionModels,+ });+ }),- const where = {+ inherited: protectedProjectProcedure+ .input(+ z.object({+ projectId: z.string(),+ includeDecisionModels: z.boolean().optional().default(false),+ }),+ )+ .query(async ({ input, ctx }) => {+ throwIfNoProjectAccess({+ session: ctx.session,projectId: input.projectId,- ...(input.includeDecisionModels- ? {}- : { adapter: { notIn: [...DECISION_MODEL_ADAPTERS] } }),- };-- const storedApiKeys = await ctx.prisma.llmApiKeys.findMany({- // secretKey is selected server-side only to derive a safe auth-method enum for Bedrock- select: {- id: true,- createdAt: true,- updatedAt: true,- provider: true,- displaySecretKey: true,- projectId: true,- adapter: true,- baseURL: true,- customModels: true,- withDefaultModels: true,- extraHeaderKeys: true,- config: true,- secretKey: true,+ scope: "llmApiKeys:read",+ });+ return new LlmConnectionService().listInherited({+ owner: {+ type: "project",+ projectId: input.projectId,+ organizationId: ctx.session.orgId,},- where,+ includeDecisionModels: input.includeDecisionModels,});+ }),- const apiKeys = z.array(SafeLlmApiKeySchema).parse(- storedApiKeys.map(({ secretKey, ...apiKey }) => ({- ...apiKey,- secretKey: undefined,- extraHeaders: undefined,- authMethod:- apiKey.adapter === LLMAdapter.Bedrock- ? getBedrockAuthMethod(decrypt(secretKey))- : undefined,- })),- );-- const count = await ctx.prisma.llmApiKeys.count({ where });-- return {- data: apiKeys, // does not contain the secret key- totalCount: count,- };+ effective: protectedProjectProcedure+ .input(+ z.object({+ projectId: z.string(),+ includeDecisionModels: z.boolean().optional().default(false),+ }),+ )+ .query(async ({ input, ctx }) => {+ throwIfNoProjectAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "llmApiKeys:read",+ });+ return new LlmConnectionService().listEffective({+ owner: {+ type: "project",+ projectId: input.projectId,+ organizationId: ctx.session.orgId,+ },+ includeDecisionModels: input.includeDecisionModels,+ });}),test: protectedProjectProcedureWithoutTracing@@ -586,30 +132,8 @@ export const llmApiKeyRouter = createTRPCRouter({projectId: input.projectId,scope: "llmApiKeys:create",});-- if (input.baseURL) {- try {- await validateBaseURLForAdapter({- adapter: input.adapter,- baseURL: input.baseURL,- });- } catch (error) {- return {- success: false,- error: error instanceof Error ? error.message : "Invalid base URL",- };- }- }-- return testLLMConnection({- adapter: input.adapter,- provider: input.provider,- secretKey: input.secretKey,- baseURL: input.baseURL,- customModels: input.customModels,- extraHeaders: input.extraHeaders,- config: input.config,- });+ const { projectId: _projectId, ...connectionInput } = input;+ return new LlmConnectionService().test(connectionInput);}),testUpdate: protectedProjectProcedureWithoutTracing@@ -620,215 +144,34 @@ export const llmApiKeyRouter = createTRPCRouter({projectId: input.projectId,scope: "llmApiKeys:update",});-- try {- // Get the existing key from the database- const existingKey = await ctx.prisma.llmApiKeys.findUnique({- where: {- id: input.id,- projectId: input.projectId,- },- });-- if (!existingKey) {- throw new TRPCError({- code: "NOT_FOUND",- message: "API key not found",- });- }-- const hasNewSecretKey =- typeof input.secretKey === "string" && input.secretKey.length > 0;- const baseURL =- input.baseURL !== undefined ? input.baseURL : existingKey.baseURL;- const isBaseURLChanged = baseURL !== existingKey.baseURL;-- if (isBaseURLChanged && !hasNewSecretKey) {- throw new TRPCError({- code: "BAD_REQUEST",- message: "Secret key is required when changing the base URL",- });- }-- if (input.baseURL && isBaseURLChanged) {- await validateBaseURLForAdapter({- adapter: input.adapter,- baseURL: input.baseURL,- });- }-- const secretKey = hasNewSecretKey- ? (input.secretKey as string)- : decrypt(existingKey.secretKey);-- // Merge existing key with provided input, giving priority to input- const adapter = input.adapter ?? (existingKey.adapter as LLMAdapter);- const provider = input.provider ?? existingKey.provider;- const customModels = input.customModels ?? existingKey.customModels;- const config = input.config ?? existingKey.config;-- const extraHeaders = resolveUpdatedExtraHeaders({- inputHeaders: input.extraHeaders,- storedHeaders: existingKey.extraHeaders,- isBaseURLChanged,- });-- return testLLMConnection({- adapter,- provider,- secretKey,- projectId: hasNewSecretKey ? undefined : existingKey.projectId,- baseURL,- customModels,- extraHeaders,- config,- });- } catch (err) {- logger.error(err);-- return {- success: false,- error: err instanceof Error ? err.message : "Unknown error",- };- }+ const { projectId, ...connectionInput } = input;+ return new LlmConnectionService().testUpdate({+ owner: {+ type: "project",+ projectId,+ organizationId: ctx.session.orgId,+ },+ input: connectionInput,+ });}),update: protectedProjectProcedureWithoutTracing.input(UpdateLlmApiKey).mutation(async ({ input, ctx }) => {- try {- throwIfNoProjectAccess({- session: ctx.session,- projectId: input.projectId,- scope: "llmApiKeys:update",- });-- // Get existing key to verify provider and adapter- const existingKey = await ctx.prisma.llmApiKeys.findUnique({- where: {- id: input.id,- projectId: input.projectId,- },- });-- if (!existingKey) {- throw new TRPCError({- code: "NOT_FOUND",- message: "API key not found",- });- }-- // Ensure provider and adapter cannot be changed- if (- input.provider !== existingKey.provider ||- input.adapter !== existingKey.adapter- ) {- throw new TRPCError({- code: "BAD_REQUEST",- message: "Provider and adapter cannot be changed",- });- }-- // Validate that default credentials sentinel is only allowed for Bedrock/VertexAI in self-hosted deployments- const isLangfuseCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);- const isBaseURLChanged =- input.baseURL !== undefined- ? input.baseURL !== existingKey.baseURL- : false;-- if (isBaseURLChanged && !input.secretKey) {- throw new TRPCError({- code: "BAD_REQUEST",- message: "Secret key is required when changing the base URL",- });- }-- if (input.baseURL && isBaseURLChanged) {- await validateBaseURLForWrite({- adapter: input.adapter,- baseURL: input.baseURL,- });- }-- if (input.secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {- if (isLangfuseCloud || input.adapter !== LLMAdapter.Bedrock) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- "Default AWS credentials are only allowed for Bedrock in self-hosted deployments.",- });- }- }-- if (input.secretKey && input.adapter === LLMAdapter.Bedrock) {- try {- validateBedrockSecretKey(input.secretKey);- } catch (e) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- e instanceof Error ? e.message : "Invalid Bedrock credentials.",- });- }- }-- if (input.secretKey === VERTEXAI_USE_DEFAULT_CREDENTIALS) {- if (isLangfuseCloud || input.adapter !== LLMAdapter.VertexAI) {- throw new TRPCError({- code: "BAD_REQUEST",- message:- "Default GCP credentials (ADC) are only allowed for Vertex AI in self-hosted deployments.",- });- }- }-- // Ensure we delete extra headers if they existed before and were removed- if (input.extraHeaders === undefined && existingKey.extraHeaders) {- input.extraHeaders = {};- }-- const extraHeaders = resolveUpdatedExtraHeaders({- inputHeaders: input.extraHeaders,- storedHeaders: existingKey.extraHeaders,- isBaseURLChanged,- });-- const key = await ctx.prisma.llmApiKeys.update({- where: {- id: input.id,- projectId: input.projectId,- },- data: {- ...(input.secretKey ? { secretKey: encrypt(input.secretKey) } : {}),- extraHeaders: extraHeaders- ? encrypt(JSON.stringify(extraHeaders))- : isBaseURLChanged- ? null- : undefined,- extraHeaderKeys: extraHeaders- ? Object.keys(extraHeaders)- : isBaseURLChanged- ? []- : undefined,- displaySecretKey: input.secretKey- ? getDisplaySecretKey(input.secretKey)- : undefined,- baseURL: input.baseURL,- withDefaultModels: input.withDefaultModels,- customModels: input.customModels,- config: input.config,- },- });-- await auditLog({- session: ctx.session,- resourceType: "llmApiKey",- resourceId: key.id,- action: "update",- });- } catch (e) {- logger.error(e);- throw e;- }+ throwIfNoProjectAccess({+ session: ctx.session,+ projectId: input.projectId,+ scope: "llmApiKeys:update",+ });+ const { projectId, ...connectionInput } = input;+ await new LlmConnectionService().update({+ owner: {+ type: "project",+ projectId,+ organizationId: ctx.session.orgId,+ },+ input: connectionInput,+ actor: { session: ctx.session },+ });}),});diff --git a/web/src/features/llm-api-key/types.ts b/web/src/features/llm-api-key/types.tsindex a3ddd12f52..cb54df5632 100644--- a/web/src/features/llm-api-key/types.ts+++ b/web/src/features/llm-api-key/types.ts@@ -4,10 +4,9 @@ import {BedrockConfigSchema,OpenAIConfigSchema,VertexAIConfigSchema,- LLMApiKeySchema,} from "@langfuse/shared";-const LlmApiKeySchema = z.object({+const LlmApiKeyInputSchema = z.object({projectId: z.string(),provider: z.string()@@ -23,11 +22,11 @@ const LlmApiKeySchema = z.object({extraHeaders: z.record(z.string(), z.string()).optional(),});-export const CreateLlmApiKey = LlmApiKeySchema.extend({+export const CreateLlmApiKey = LlmApiKeyInputSchema.extend({secretKey: z.string().min(1),});-export const UpdateLlmApiKey = LlmApiKeySchema.extend({+export const UpdateLlmApiKey = LlmApiKeyInputSchema.extend({secretKey: z.string().optional()@@ -38,6 +37,18 @@ export const UpdateLlmApiKey = LlmApiKeySchema.extend({id: z.string(),});+export const CreateOrganizationLlmApiKey = CreateLlmApiKey.omit({+ projectId: true,+}).extend({+ orgId: z.string(),+});++export const UpdateOrganizationLlmApiKey = UpdateLlmApiKey.omit({+ projectId: true,+}).extend({+ orgId: z.string(),+});+export const AuthMethod = {ApiKey: "api-key",AccessKeys: "access-keys",@@ -51,9 +62,3 @@ export const BedrockAuthMethodSchema = z.enum([]);export type BedrockAuthMethod = z.infer<typeof BedrockAuthMethodSchema>;--export const SafeLlmApiKeySchema = LLMApiKeySchema.extend({- secretKey: z.undefined(),- extraHeaders: z.undefined(),- authMethod: BedrockAuthMethodSchema.optional(),-});diff --git a/web/src/features/organizations/OrganizationSettingsPage.tsx b/web/src/features/organizations/OrganizationSettingsPage.tsxindex 20db6dac70..a7f1fe8ec0 100644--- a/web/src/features/organizations/OrganizationSettingsPage.tsx+++ b/web/src/features/organizations/OrganizationSettingsPage.tsx@@ -1,5 +1,6 @@import Header from "@/src/components/layouts/header";import { Button } from "@/src/components/ui/button";+import { InfoTooltip } from "@/src/components/ui/InfoTooltip/InfoTooltip";import { ConnectedMembershipInvitesSettingsTable } from "@/src/features/rbac/components/MembershipInvitesSettingsTable/ConnectedMembershipInvitesSettingsTable";import { ConnectedMembersSettingsTable } from "@/src/features/rbac/components/MembersSettingsTable/ConnectedMembersSettingsTable";import { useHasOrganizationAccess } from "@/src/features/rbac";@@ -13,7 +14,9 @@ import { OrganizationUsageBreakdown } from "@/src/features/organization-usage";import { useHasEntitlement, usePlan } from "@/src/features/entitlements";import { SSOSettings } from "@/src/ee/features/sso-settings";import { type CloudConfigSchema, isCloudPlan } from "@langfuse/shared";+import { Building2 } from "lucide-react";import { useQueryProjectOrOrganization } from "@/src/features/projects";+import { LlmConnectionEmptyState } from "@/src/features/public-api";import { ApiKeyList } from "@/src/features/public-api/components/ApiKeyList";import AIFeatureSwitch from "@/src/features/organizations/components/AIFeatureSwitch";import { useIsCloudBillingAvailable } from "@/src/ee/features/billing";@@ -30,6 +33,7 @@ import {} from "@/src/features/ai-gateway";import useSessionStorage from "@/src/components/useSessionStorage";import { api } from "@/src/utils/api";+import { ConnectedLLMApiKeySettingsTable } from "@/src/features/public-api/components/LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable";type OrganizationSettingsPage = {title: string;@@ -57,6 +61,10 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {organizationId: organization?.id,scope: "gateway:manage",});+ const canReadOrganizationLlmConnections = useHasOrganizationAccess({+ organizationId: organization?.id,+ scope: "organizationLlmApiKeys:read",+ });const canReadUsage = useHasOrganizationAccess({organizationId: organization?.id,scope: "organizationUsage:read",@@ -85,6 +93,7 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {isLangfuseCloud,showV4Migration,showAiGateway: canManageGateway && isAiGatewayEnabled,+ showOrganizationLlmConnections: canReadOrganizationLlmConnections,showFeaturePreviews:canUpdateOrganization && organization.id !== env.NEXT_PUBLIC_DEMO_ORG_ID,});@@ -99,6 +108,7 @@ const getOrganizationSettingsPages = ({isLangfuseCloud,showV4Migration,showAiGateway,+ showOrganizationLlmConnections,showFeaturePreviews,}: {organization: {@@ -119,6 +129,7 @@ const getOrganizationSettingsPages = ({isLangfuseCloud: boolean;showV4Migration: boolean;showAiGateway: boolean;+ showOrganizationLlmConnections: boolean;showFeaturePreviews: boolean;}): OrganizationSettingsPage[] => [{@@ -195,6 +206,39 @@ const getOrganizationSettingsPages = ({),show: showOrgApiKeySettings,},+ {+ title: "LLM Connections",+ slug: "llm-connections",+ section: "Organization",+ cmdKKeywords: ["llm", "models", "providers", "credentials"],+ content: (+ <div>+ <ConnectedLLMApiKeySettingsTable+ owner={{+ scope: "organization",+ organizationId: organization.id,+ }}+ toolbarContent={+ <div className="flex items-center gap-1">+ <Header title="Organization LLM Connections" className="mb-0!" />+ <InfoTooltip label="About organization connections">+ Connections are available to all current and future projects.+ Project connections with the same provider take precedence.+ </InfoTooltip>+ </div>+ }+ emptyState={+ <LlmConnectionEmptyState+ icon={Building2}+ title="No organization connections"+ description={`Organization connections are shared with every project in ${organization.name}.`}+ />+ }+ />+ </div>+ ),+ show: showOrganizationLlmConnections,+ },{title: "Members",slug: "members",diff --git a/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsx b/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsxindex 687a332f38..55be655b1c 100644--- a/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsx+++ b/web/src/features/playground/page/components/JumpToPlaygroundDropdownMenuController.tsx@@ -108,7 +108,7 @@ export function useJumpToPlayground(return `playground-${props.source}-${sourceId}`;}, [props.source, sourceId]);- const apiKeys = api.llmApiKey.all.useQuery(+ const apiKeys = api.llmApiKey.effective.useQuery({projectId: projectId as string,},diff --git a/web/src/features/playground/page/hooks/useModelParams.ts b/web/src/features/playground/page/hooks/useModelParams.tsindex e0ac8ba0dd..7a18adab8f 100644--- a/web/src/features/playground/page/hooks/useModelParams.ts+++ b/web/src/features/playground/page/hooks/useModelParams.ts@@ -47,7 +47,7 @@ export const useModelParams = (// Set initial model paramsconst projectId = useProjectIdFromURL();- const availableLLMApiKeys = api.llmApiKey.all.useQuery(+ const availableLLMApiKeys = api.llmApiKey.effective.useQuery({projectId: projectId as string,},diff --git a/web/src/features/playground/server/chatCompletionHandler.ts b/web/src/features/playground/server/chatCompletionHandler.tsindex f574e14ac4..7802d5cddc 100644--- a/web/src/features/playground/server/chatCompletionHandler.ts+++ b/web/src/features/playground/server/chatCompletionHandler.ts@@ -3,7 +3,6 @@ import { NextResponse, type NextRequest } from "next/server";import {BaseError,ForbiddenError,- InternalServerError,InvalidRequestError,} from "@langfuse/shared";@@ -11,9 +10,7 @@ import { authorizeRequestOrThrow } from "./authorizeRequest";import { validateChatCompletionBody } from "./validateChatCompletionBody";import { env } from "@/src/env.mjs";-import { prisma } from "@langfuse/shared/src/db";import {- LLMApiKeySchema,createLLMOutput,createLLMToolSet,generateLLMText,@@ -22,6 +19,7 @@ import {logger,contextWithLangfuseProps,mapLegacyLLMCompletionParams,+ resolveLlmApiKey,streamLLMText,} from "@langfuse/shared/src/server";import * as opentelemetry from "@opentelemetry/api";@@ -52,25 +50,16 @@ export default async function chatCompletionHandler(req: NextRequest) {streaming,} = body;- const LLMApiKey = await prisma.llmApiKeys.findFirst({- where: {- projectId: body.projectId,- provider: modelParams.provider,- },+ const llmApiKey = await resolveLlmApiKey({+ projectId: body.projectId,+ provider: modelParams.provider,});- if (!LLMApiKey)+ if (!llmApiKey)throw new InvalidRequestError(`No ${modelParams.provider} API key found in project. Please add one in the project settings.`,);- const parsedKey = LLMApiKeySchema.safeParse(LLMApiKey);- if (!parsedKey.success) {- throw new InternalServerError(- `Could not parse API key for provider ${body.modelParams.provider}: ${parsedKey.error.message}`,- );- }-// If messages contain tool results, we include tools in the requestconst hasToolResults = messages.some((msg) => msg.type === "tool-result");// Fix empty tool_call_id values by mapping to langgraph IDs@@ -107,7 +96,7 @@ export default async function chatCompletionHandler(req: NextRequest) {: messages;const completionParams = mapLegacyLLMCompletionParams({- connection: parsedKey.data,+ connection: llmApiKey,messages: fixedMessages,modelParams,});diff --git a/web/src/features/posthog-analytics/usePostHogClientCapture.ts b/web/src/features/posthog-analytics/usePostHogClientCapture.tsindex 53ec6fd1e0..0b7f35c53c 100644--- a/web/src/features/posthog-analytics/usePostHogClientCapture.ts+++ b/web/src/features/posthog-analytics/usePostHogClientCapture.ts@@ -422,6 +422,9 @@ const events = {"delete_membership","api_key_create","api_key_delete",+ "llm_api_key_create",+ "llm_api_key_update",+ "llm_api_key_delete","pricing_dialog_opened","delete_organization","ai_features_toggle",diff --git a/web/src/features/projects/ProjectSettingsPage.tsx b/web/src/features/projects/ProjectSettingsPage.tsxindex 13b5579581..08242bb276 100644--- a/web/src/features/projects/ProjectSettingsPage.tsx+++ b/web/src/features/projects/ProjectSettingsPage.tsx@@ -224,7 +224,12 @@ const getProjectSettingsPages = ({],content: (<div className="flex flex-col gap-6">- <LlmApiKeyList projectId={project.id} />+ <LlmApiKeyList+ projectId={project.id}+ projectName={project.name}+ organizationId={organization.id}+ organizationName={organization.name}+ /></div>),show: showLLMConnectionsSettings,diff --git a/web/src/features/projects/server/projectsRouter.ts b/web/src/features/projects/server/projectsRouter.tsindex 970b6320a9..ae16d66825 100644--- a/web/src/features/projects/server/projectsRouter.ts+++ b/web/src/features/projects/server/projectsRouter.ts@@ -17,10 +17,12 @@ import {QueueJobs,redis,ProjectDeleteQueue,+ DefaultEvalModelService,getEnvironmentsForProject,invalidateCachedOrgApiKeys,} from "@langfuse/shared/src/server";import { transferRoleAssignments } from "@langfuse/shared/rbac/server";+import { reconcileTopicsConnectionsForProject } from "@langfuse/shared/topics/server";import { randomUUID } from "crypto";import { LangfuseConflictError, StringNoHTMLNonEmpty } from "@langfuse/shared";import type { PrismaClient } from "@langfuse/shared/src/db";@@ -371,6 +373,11 @@ export const projectsRouter = createTRPCRouter({orgId: input.targetOrgId,projectId: input.projectId,});++ await Promise.all([+ DefaultEvalModelService.reconcileConnectionForProject(input.projectId),+ reconcileTopicsConnectionsForProject(input.projectId),+ ]);}),environmentFilterOptions: protectedProjectProcedurediff --git a/web/src/features/public-api/components/CreateLLMApiKeyDialog.tsx b/web/src/features/public-api/components/CreateLLMApiKeyDialog.tsxindex ea3f62f154..2a76100f7a 100644--- a/web/src/features/public-api/components/CreateLLMApiKeyDialog.tsx+++ b/web/src/features/public-api/components/CreateLLMApiKeyDialog.tsx@@ -36,7 +36,7 @@ export function CreateLLMApiKeyDialog({});const uiCustomization = useUiCustomization();- if (!hasAccess) return null;+ if (!projectId || !hasAccess) return null;return (<Dialog@@ -59,7 +59,7 @@ export function CreateLLMApiKeyDialog({</DialogHeader>{open && (<CreateLLMApiKeyForm- projectId={projectId}+ owner={{ scope: "project", projectId }}onSuccess={() => setOpen(false)}customization={uiCustomization}/>diff --git a/web/src/features/public-api/components/CreateLLMApiKeyDialogController.tsx b/web/src/features/public-api/components/CreateLLMApiKeyDialogController.tsxindex 171a2f1a24..133385129d 100644--- a/web/src/features/public-api/components/CreateLLMApiKeyDialogController.tsx+++ b/web/src/features/public-api/components/CreateLLMApiKeyDialogController.tsx@@ -7,23 +7,36 @@ import {DialogTitle,} from "@/src/components/ui/dialog";import { useUiCustomization } from "@/src/ee/features/ui-customization";-import { CreateLLMApiKeyForm } from "@/src/features/public-api/components/CreateLLMApiKeyForm";-import { useHasProjectAccess } from "@/src/features/rbac";+import {+ CreateLLMApiKeyForm,+ type LlmConnectionFormOwner,+} from "@/src/features/public-api/components/CreateLLMApiKeyForm";+import {+ useHasOrganizationAccess,+ useHasProjectAccess,+} from "@/src/features/rbac";export function CreateLLMApiKeyDialogController({- projectId,+ owner,children,}: {- projectId: string;+ owner: LlmConnectionFormOwner;children: (control: {hasAccess: boolean;openDialog: () => void;}) => ReactNode;}) {- const hasAccess = useHasProjectAccess({- projectId,+ const hasProjectAccess = useHasProjectAccess({+ projectId: owner.scope === "project" ? owner.projectId : undefined,scope: "llmApiKeys:create",});+ const hasOrganizationAccess = useHasOrganizationAccess({+ organizationId:+ owner.scope === "organization" ? owner.organizationId : undefined,+ scope: "organizationLlmApiKeys:CUD",+ });+ const hasAccess =+ owner.scope === "project" ? hasProjectAccess : hasOrganizationAccess;const uiCustomization = useUiCustomization();const [formKey, setFormKey] = useState(0);@@ -37,7 +50,7 @@ export function CreateLLMApiKeyDialogController({</DialogHeader><CreateLLMApiKeyFormkey={formKey}- projectId={projectId}+ owner={owner}onSuccess={closeDialog}customization={uiCustomization}/>diff --git a/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsx b/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsxindex fd4e441b84..a5fb4a5080 100644--- a/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsx+++ b/web/src/features/public-api/components/CreateLLMApiKeyForm.clienttest.tsx@@ -4,6 +4,13 @@ import { LayerProvider } from "@/src/context/LayerContext/LayerContext";import { type RouterOutputs } from "@/src/utils/api";import { CreateLLMApiKeyForm } from "./CreateLLMApiKeyForm";+const mocks = vi.hoisted(() => ({+ createProject: vi.fn(),+ testProject: vi.fn(),+ reportNonTrpcError: vi.fn(),+ reportTrpcErrorWithoutToast: vi.fn(),+}));+vi.mock("@/src/features/posthog-analytics", () => ({usePostHogClientCapture: () => vi.fn(),}));@@ -12,15 +19,30 @@ vi.mock("@/src/utils/api", () => {return {api: {llmApiKey: {+ all: { useQuery: () => ({ data: { data: [] } }) },+ create: {+ useMutation: () => ({ mutateAsync: mocks.createProject }),+ },+ update: mutation,+ test: {+ useMutation: () => ({ mutateAsync: mocks.testProject }),+ },+ testUpdate: mutation,+ },+ organizationLlmApiKey: {all: { useQuery: () => ({ data: { data: [] } }) },create: mutation,update: mutation,test: mutation,testUpdate: mutation,},- useUtils: () => ({ llmApiKey: { invalidate: vi.fn() } }),+ useUtils: () => ({+ llmApiKey: { invalidate: vi.fn() },+ organizationLlmApiKey: { invalidate: vi.fn() },+ }),},- reportNonTrpcError: vi.fn(),+ reportNonTrpcError: mocks.reportNonTrpcError,+ reportTrpcErrorWithoutToast: mocks.reportTrpcErrorWithoutToast,};});@@ -36,6 +58,11 @@ describe("CreateLLMApiKeyForm", () => {);});+ beforeEach(() => {+ vi.clearAllMocks();+ mocks.testProject.mockResolvedValue({ success: true });+ });+it("restores a TypeSafe connection's custom base URL after switching upstreams", () => {const existingKey = {id: "key",@@ -51,7 +78,7 @@ describe("CreateLLMApiKeyForm", () => {render(<CreateLLMApiKeyForm- projectId="project"+ owner={{ scope: "project", projectId: "project" }}onSuccess={vi.fn()}customization={null}mode="update"@@ -70,4 +97,35 @@ describe("CreateLLMApiKeyForm", () => {screen.getByRole("textbox", { name: "Custom base URL" }),).toHaveValue("https://gateway.example.com/v1");});++ it("shows save errors in the form instead of reporting a toast", async () => {+ const error = new Error(+ "Project and organization connections with the same provider name must use the same adapter.",+ );+ mocks.createProject.mockRejectedValue(error);++ render(+ <CreateLLMApiKeyForm+ owner={{ scope: "project", projectId: "project" }}+ onSuccess={vi.fn()}+ customization={null}+ />,+ { wrapper: LayerProvider },+ );++ fireEvent.change(screen.getByRole("textbox", { name: "Provider name" }), {+ target: { value: "shared-provider" },+ });+ fireEvent.change(screen.getByLabelText("API Key"), {+ target: { value: "secret-key" },+ });+ fireEvent.click(screen.getByRole("button", { name: "Create connection" }));++ expect(await screen.findByText(error.message)).toBeInTheDocument();+ expect(mocks.reportTrpcErrorWithoutToast).toHaveBeenCalledWith(+ error,+ "llm-api-keys",+ );+ expect(mocks.reportNonTrpcError).not.toHaveBeenCalled();+ });});diff --git a/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx b/web/src/features/public-api/components/CreateLLMApiKeyForm.tsxindex 2e88c2bbaa..2e6c8be02d 100644--- a/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx+++ b/web/src/features/public-api/components/CreateLLMApiKeyForm.tsx@@ -39,7 +39,11 @@ import {import { Switch } from "@/src/components/design-system/Switch/Switch";import { TypeSafeUpstreamCards } from "@/src/features/llm-api-key/components/TypeSafeUpstreamCards/TypeSafeUpstreamCards";import { Tabs } from "@/src/components/design-system/Tabs/Tabs";-import { api, reportNonTrpcError, type RouterOutputs } from "@/src/utils/api";+import {+ api,+ reportTrpcErrorWithoutToast,+ type RouterOutputs,+} from "@/src/utils/api";import { usePostHogClientCapture } from "@/src/features/posthog-analytics";import { type useUiCustomization } from "@/src/ee/features/ui-customization";import { DialogFooter, DialogBody } from "@/src/components/ui/dialog";@@ -87,7 +91,9 @@ const isMatchingBedrockAuthMethod = ((existingAuthMethod === AuthMethod.AccessKeys ||existingAuthMethod === AuthMethod.DefaultCredentials));-type LlmApiKeyListItem = RouterOutputs["llmApiKey"]["all"]["data"][number];+export type LlmApiKeyListItem =+ | RouterOutputs["llmApiKey"]["all"]["data"][number]+ | RouterOutputs["organizationLlmApiKey"]["all"]["data"][number];const getInitialBedrockAuthMethod = (params: {mode: "create" | "update";@@ -278,8 +284,16 @@ const createFormSchema = (params: {},);+export type LlmConnectionFormOwner =+ | { scope: "project"; projectId: string }+ | { scope: "organization"; organizationId: string };++// Save failures render beside the submit button; a local handler replaces the+// React Query default that would show the same error in a toast.+const suppressSaveErrorToast = () => undefined;+interface CreateLLMApiKeyFormProps {- projectId?: string;+ owner: LlmConnectionFormOwner;onSuccess: () => void;customization: ReturnType<typeof useUiCustomization>;mode?: "create" | "update";@@ -287,7 +301,7 @@ interface CreateLLMApiKeyFormProps {}export function CreateLLMApiKeyForm({- projectId,+ owner,onSuccess,customization,mode = "create",@@ -301,25 +315,62 @@ export function CreateLLMApiKeyForm({const utils = api.useUtils();const capture = usePostHogClientCapture();- const existingKeys = api.llmApiKey.all.useQuery(+ const projectExistingKeys = api.llmApiKey.all.useQuery(+ {+ projectId: owner.scope === "project" ? owner.projectId : "",+ includeDecisionModels: true,+ },+ { enabled: owner.scope === "project" },+ );+ const organizationExistingKeys = api.organizationLlmApiKey.all.useQuery({- projectId: projectId as string,+ orgId: owner.scope === "organization" ? owner.organizationId : "",includeDecisionModels: true,},- { enabled: Boolean(projectId) },+ { enabled: owner.scope === "organization" },);+ const existingKeys =+ owner.scope === "project"+ ? projectExistingKeys.data?.data+ : organizationExistingKeys.data?.data;const adapterOptions = Object.values(LLMAdapter);const mutCreateLlmApiKey = api.llmApiKey.create.useMutation({onSuccess: () => utils.llmApiKey.invalidate(),+ onError: suppressSaveErrorToast,});const mutUpdateLlmApiKey = api.llmApiKey.update.useMutation({onSuccess: () => utils.llmApiKey.invalidate(),+ onError: suppressSaveErrorToast,});const mutTestLLMApiKey = api.llmApiKey.test.useMutation();const mutTestUpdateLLMApiKey = api.llmApiKey.testUpdate.useMutation();+ const mutCreateOrganizationLlmApiKey =+ api.organizationLlmApiKey.create.useMutation({+ onSuccess: async () => {+ await Promise.all([+ utils.organizationLlmApiKey.invalidate(),+ utils.llmApiKey.invalidate(),+ ]);+ },+ onError: suppressSaveErrorToast,+ });+ const mutUpdateOrganizationLlmApiKey =+ api.organizationLlmApiKey.update.useMutation({+ onSuccess: async () => {+ await Promise.all([+ utils.organizationLlmApiKey.invalidate(),+ utils.llmApiKey.invalidate(),+ ]);+ },+ onError: suppressSaveErrorToast,+ });+ const mutTestOrganizationLlmApiKey =+ api.organizationLlmApiKey.test.useMutation();+ const mutTestUpdateOrganizationLlmApiKey =+ api.organizationLlmApiKey.testUpdate.useMutation();const defaultAdapter: LLMAdapter = customization?.defaultModelAdapter? LLMAdapter[customization.defaultModelAdapter]@@ -564,27 +615,29 @@ export function CreateLLMApiKeyForm({};async function onSubmit(values: z.infer<typeof formSchema>) {- if (!projectId) return console.error("No project ID found.");-if (mode === "create") {- if (- existingKeys?.data?.data- .map((k) => k.provider)- .includes(values.provider)- ) {+ if (existingKeys?.map((key) => key.provider).includes(values.provider)) {form.setError("provider", {type: "manual",message: "There already exists an API key for this provider.",});return;}- capture("project_settings:llm_api_key_create", {- provider: values.provider,- });+ if (owner.scope === "project") {+ capture("project_settings:llm_api_key_create", {+ provider: values.provider,+ });+ } else {+ capture("organization_settings:llm_api_key_create");+ }} else {- capture("project_settings:llm_api_key_update", {- provider: values.provider,- });+ if (owner.scope === "project") {+ capture("project_settings:llm_api_key_update", {+ provider: values.provider,+ });+ } else {+ capture("organization_settings:llm_api_key_update");+ }}let secretKey = values.secretKey;@@ -650,20 +703,21 @@ export function CreateLLMApiKeyForm({: undefined;}- const extraHeaders =- values.extraHeaders.length > 0- ? values.extraHeaders.reduce(- (acc, header) => {- acc[header.key] = header.value ?? "";- return acc;- },- {} as Record<string, string>,- )- : undefined;-- const newLlmApiKey = {+ let extraHeaders: Record<string, string> | undefined;+ if (values.extraHeaders.length > 0) {+ extraHeaders = values.extraHeaders.reduce<Record<string, string>>(+ (headers, header) => {+ headers[header.key] = header.value ?? "";+ return headers;+ },+ {},+ );+ } else if (mode === "update") {+ extraHeaders = {};+ }++ const connectionInput = {id: existingKey?.id ?? "",- projectId,secretKey: secretKey ?? "",provider: values.provider,adapter: values.adapter,@@ -683,10 +737,20 @@ export function CreateLLMApiKeyForm({};try {- const testResult =- mode === "create"- ? await mutTestLLMApiKey.mutateAsync(newLlmApiKey)- : await mutTestUpdateLLMApiKey.mutateAsync(newLlmApiKey);+ let testResult;+ if (owner.scope === "project") {+ const input = { ...connectionInput, projectId: owner.projectId };+ testResult =+ mode === "create"+ ? await mutTestLLMApiKey.mutateAsync(input)+ : await mutTestUpdateLLMApiKey.mutateAsync(input);+ } else {+ const input = { ...connectionInput, orgId: owner.organizationId };+ testResult =+ mode === "create"+ ? await mutTestOrganizationLlmApiKey.mutateAsync(input)+ : await mutTestUpdateOrganizationLlmApiKey.mutateAsync(input);+ }if (!testResult.success) throw new Error(testResult.error);} catch (error) {@@ -701,13 +765,36 @@ export function CreateLLMApiKeyForm({return;}- return (mode === "create" ? mutCreateLlmApiKey : mutUpdateLlmApiKey)- .mutateAsync(newLlmApiKey)+ let mutation;+ if (owner.scope === "project") {+ const input = { ...connectionInput, projectId: owner.projectId };+ mutation =+ mode === "create"+ ? mutCreateLlmApiKey.mutateAsync(input)+ : mutUpdateLlmApiKey.mutateAsync(input);+ } else {+ const input = { ...connectionInput, orgId: owner.organizationId };+ mutation =+ mode === "create"+ ? mutCreateOrganizationLlmApiKey.mutateAsync(input)+ : mutUpdateOrganizationLlmApiKey.mutateAsync(input);+ }++ return mutation.then(() => {form.reset();onSuccess();})- .catch((error) => reportNonTrpcError(error, "llm-api-keys"));+ .catch((error) => {+ reportTrpcErrorWithoutToast(error, "llm-api-keys");+ form.setError("root", {+ type: "manual",+ message:+ error instanceof Error+ ? error.message+ : "Could not save the connection.",+ });+ });}return (diff --git a/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsx b/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsxindex fde6c1dcdd..e9d7706b96 100644--- a/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsx+++ b/web/src/features/public-api/components/LLMApiKeyList.clienttest.tsx@@ -1,14 +1,28 @@import { fireEvent, render, screen, within } from "@testing-library/react";+import type { PropsWithChildren } from "react";import { LayerProvider } from "@/src/context/LayerContext/LayerContext";+import { TooltipProvider } from "@/src/components/ui/tooltip";import { api } from "@/src/utils/api";import { LlmApiKeyList } from "./LLMApiKeyList";+const organizationQueryResult = vi.hoisted(() => ({+ data: { data: [] },+ isLoading: false,+ isError: false,+}));+vi.mock("@/src/components/layouts/header", () => ({default: () => null,}));+vi.mock("@/src/components/ui/InfoTooltip/InfoTooltip", () => ({+ InfoTooltip: ({ children, label }: PropsWithChildren<{ label: string }>) => (+ <span aria-label={label}>{children}</span>+ ),+}));vi.mock("@/src/features/rbac", () => ({useHasProjectAccess: () => true,+ useHasOrganizationAccess: () => true,}));vi.mock("@/src/features/posthog-analytics", () => ({usePostHogClientCapture: () => vi.fn(),@@ -38,14 +52,40 @@ vi.mock("@/src/utils/api", () => ({api: {llmApiKey: {all: { useQuery: vi.fn() },+ inherited: { useQuery: vi.fn() },+ delete: { useMutation: () => ({ isPending: false }) },+ },+ organizationLlmApiKey: {+ all: {+ useQuery: () => organizationQueryResult,+ },delete: { useMutation: () => ({ isPending: false }) },},- useUtils: () => ({ llmApiKey: { invalidate: vi.fn() } }),+ useUtils: () => ({+ llmApiKey: { invalidate: vi.fn() },+ organizationLlmApiKey: { invalidate: vi.fn() },+ }),},reportNonTrpcError: vi.fn(),}));+function TestProvider({ children }: PropsWithChildren) {+ return (+ <TooltipProvider>+ <LayerProvider>{children}</LayerProvider>+ </TooltipProvider>+ );+}+describe("LLM connection editing", () => {+ beforeEach(() => {+ vi.mocked(api.llmApiKey.inherited.useQuery).mockReturnValue({+ data: [],+ isLoading: false,+ isError: false,+ } as ReturnType<typeof api.llmApiKey.inherited.useQuery>);+ });+it("preserves the dialog and draft across list updates, then opens a fresh form for another connection", () => {const keys = ["First", "Second"].map((provider) => ({id: provider,@@ -64,9 +104,17 @@ describe("LLM connection editing", () => {query as ReturnType<typeof api.llmApiKey.all.useQuery>,);- const { rerender } = render(<LlmApiKeyList projectId="project" />, {- wrapper: LayerProvider,- });+ const { rerender } = render(+ <LlmApiKeyList+ projectId="project"+ projectName="Test Project"+ organizationId="organization"+ organizationName="Test Organization"+ />,+ {+ wrapper: TestProvider,+ },+ );fireEvent.click(screen.getByText("First").closest("tr")!);const dialog = screen.getByRole("dialog");fireEvent.change(screen.getByRole("textbox", { name: "API key" }), {@@ -76,7 +124,14 @@ describe("LLM connection editing", () => {query.data = {data: keys.map((key) => ({ ...key, extraHeaderKeys: ["X-Test"] })),};- rerender(<LlmApiKeyList projectId="project" />);+ rerender(+ <LlmApiKeyList+ projectId="project"+ projectName="Test Project"+ organizationId="organization"+ organizationName="Test Organization"+ />,+ );expect(screen.getByRole("dialog")).toBe(dialog);expect(screen.getByRole("textbox", { name: "API key" })).toHaveValue(@@ -100,15 +155,23 @@ describe("LLM connection editing", () => {isError: false,} as ReturnType<typeof api.llmApiKey.all.useQuery>);- render(<LlmApiKeyList projectId="project" />, {- wrapper: LayerProvider,- });- fireEvent.click(screen.getByRole("button", { name: "Add LLM Connection" }));+ render(+ <LlmApiKeyList+ projectId="project"+ projectName="Test Project"+ organizationId="organization"+ organizationName="Test Organization"+ />,+ {+ wrapper: TestProvider,+ },+ );+ fireEvent.click(screen.getByRole("button", { name: "Project Connection" }));fireEvent.change(screen.getByRole("textbox", { name: "API key" }), {target: { value: "Unsaved draft" },});fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });- fireEvent.click(screen.getByRole("button", { name: "Add LLM Connection" }));+ fireEvent.click(screen.getByRole("button", { name: "Project Connection" }));expect(screen.getByRole("textbox", { name: "API key" })).toHaveValue("");});@@ -128,9 +191,17 @@ describe("LLM connection editing", () => {isError: false,} as ReturnType<typeof api.llmApiKey.all.useQuery>);- render(<LlmApiKeyList projectId="project" />, {- wrapper: LayerProvider,- });+ render(+ <LlmApiKeyList+ projectId="project"+ projectName="Test Project"+ organizationId="organization"+ organizationName="Test Organization"+ />,+ {+ wrapper: TestProvider,+ },+ );expect(screen.getByText("Provider 0")).toBeInTheDocument();expect(screen.getByText("Provider 10")).toBeInTheDocument();@@ -138,4 +209,68 @@ describe("LLM connection editing", () => {screen.queryByRole("button", { name: "Go to next page" }),).not.toBeInTheDocument();});++ it("identifies project overrides and explains organization inheritance", async () => {+ const organizationConnection = {+ id: "organization-openai",+ provider: "openai-tobi",+ adapter: "openai",+ baseURL: null,+ displaySecretKey: "...TSEA",+ extraHeaderKeys: [] as string[],+ overriddenByProject: true,+ };+ vi.mocked(api.llmApiKey.inherited.useQuery).mockReturnValue({+ data: [organizationConnection],+ isLoading: false,+ isError: false,+ } as ReturnType<typeof api.llmApiKey.inherited.useQuery>);+ vi.mocked(api.llmApiKey.all.useQuery).mockReturnValue({+ data: {+ data: [+ {+ ...organizationConnection,+ id: "project-openai",+ overriddenByProject: undefined,+ },+ ],+ },+ isLoading: false,+ isError: false,+ } as ReturnType<typeof api.llmApiKey.all.useQuery>);++ render(+ <LlmApiKeyList+ projectId="project"+ projectName="Test Project"+ organizationId="organization"+ organizationName="Test Organization"+ />,+ { wrapper: TestProvider },+ );++ expect(+ screen.getByRole("button", { name: "Organization Connection" }),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: "Project Connection" }),+ ).toBeInTheDocument();+ expect(screen.getAllByText("Override")).toHaveLength(1);++ fireEvent.focus(screen.getByText("Override").parentElement!);+ expect(+ await screen.findByText(+ "This project connection overrides the organization secret with the same name.",+ ),+ ).toBeInTheDocument();++ expect(+ screen.getByLabelText("About organization connections"),+ ).toBeInTheDocument();+ expect(+ screen.getByText(+ "Organization connections are inherited by this project. Project connections with the same provider name take precedence.",+ ),+ ).toBeInTheDocument();+ });});diff --git a/web/src/features/public-api/components/LLMApiKeyList.tsx b/web/src/features/public-api/components/LLMApiKeyList.tsxindex c934399037..360da4f473 100644--- a/web/src/features/public-api/components/LLMApiKeyList.tsx+++ b/web/src/features/public-api/components/LLMApiKeyList.tsx@@ -1,35 +1,110 @@+import { useMemo } from "react";+import { Building2, Plug } from "lucide-react";+import { useHasProjectAccess } from "@/src/features/rbac";-import Header from "@/src/components/layouts/header";import { Alert } from "@/src/components/design-system/Alert/Alert";+import { InfoTooltip } from "@/src/components/ui/InfoTooltip/InfoTooltip";+import Header from "@/src/components/layouts/header";+import { LlmConnectionEmptyState } from "@/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState";import { ConnectedLLMApiKeySettingsTable } from "./LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable";+import { type LLMApiKeySettingsTableRow } from "./LLMApiKeySettingsTable/LLMApiKeySettingsTable";+import type { AsyncTableData } from "@/src/components/design-system/table/Table";+import { api } from "@/src/utils/api";-export function LlmApiKeyList(props: { projectId: string }) {+export function LlmApiKeyList(props: {+ organizationId: string;+ organizationName: string;+ projectId: string;+ projectName: string;+}) {const hasAccess = useHasProjectAccess({projectId: props.projectId,scope: "llmApiKeys:read",});+ const inherited = api.llmApiKey.inherited.useQuery(+ {+ projectId: props.projectId,+ includeDecisionModels: true,+ },+ { enabled: hasAccess },+ );+ const organizationTableData = useMemo<+ AsyncTableData<LLMApiKeySettingsTableRow[]>+ >(() => {+ if (inherited.isLoading) return { status: "loading" };+ if (inherited.isError) {+ return {+ status: "error",+ error: "Failed to load organization connections",+ };+ }+ return {+ status: "success",+ data: (inherited.data ?? []).map((apiKey) => ({+ ...apiKey,+ overriddenByProject: undefined,+ })),+ };+ }, [inherited.data, inherited.isError, inherited.isLoading]);+ const organizationProviders = useMemo(+ () => new Set((inherited.data ?? []).map((apiKey) => apiKey.provider)),+ [inherited.data],+ );+if (!hasAccess) {return (- <div>- <Header title="LLM Connections" />- <Alert>- <Alert.Title>Access Denied</Alert.Title>- <Alert.Description>- You do not have permission to view LLM API keys for this project.- </Alert.Description>- </Alert>- </div>+ <Alert>+ <Alert.Title>Access Denied</Alert.Title>+ <Alert.Description>+ You do not have permission to view LLM API keys for this project.+ </Alert.Description>+ </Alert>);}return (<div id="llm-api-keys">- <Header title="LLM Connections" />- <p className="mb-4 text-sm">- Connect your LLM services to enable evaluations and playground features.- Your provider will charge based on usage.- </p>- <ConnectedLLMApiKeySettingsTable projectId={props.projectId} />+ <section>+ <ConnectedLLMApiKeySettingsTable+ owner={{+ scope: "organization",+ organizationId: props.organizationId,+ }}+ data={organizationTableData}+ emptyState={+ <LlmConnectionEmptyState+ icon={Building2}+ title="No organization connections"+ description={`Organization connections are shared with every project in ${props.organizationName}.`}+ />+ }+ toolbarContent={+ <div className="flex items-center gap-1">+ <Header title="Organization connections" className="mb-0!" />+ <InfoTooltip label="About organization connections">+ Organization connections are inherited by this project. Project+ connections with the same provider name take precedence.+ </InfoTooltip>+ </div>+ }+ />+ </section>+ <section className="mt-6">+ <ConnectedLLMApiKeySettingsTable+ owner={{ scope: "project", projectId: props.projectId }}+ overriddenProviders={organizationProviders}+ emptyState={+ <LlmConnectionEmptyState+ icon={Plug}+ title="No project connections"+ description={`Project connections are only available in ${props.projectName} and take precedence over organization connections with the same provider.`}+ />+ }+ toolbarContent={+ <Header title="Project connections" className="mb-0!" />+ }+ />+ </section></div>);}diff --git a/web/src/features/public-api/components/LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable.tsx b/web/src/features/public-api/components/LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable.tsxindex 1bca9f1309..2459f2b280 100644--- a/web/src/features/public-api/components/LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable.tsx+++ b/web/src/features/public-api/components/LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable.tsx@@ -1,10 +1,14 @@-import { useMemo } from "react";+import { useMemo, type ReactNode } from "react";import { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";import { type AsyncTableData } from "@/src/components/design-system/table/Table";import { CreateLLMApiKeyDialogController } from "@/src/features/public-api/components/CreateLLMApiKeyDialogController";import { UpdateLLMApiKeyDialog } from "@/src/features/public-api/components/UpdateLLMApiKeyDialog";-import { useHasProjectAccess } from "@/src/features/rbac";+import type { LlmConnectionFormOwner } from "@/src/features/public-api/components/CreateLLMApiKeyForm";+import {+ useHasOrganizationAccess,+ useHasProjectAccess,+} from "@/src/features/rbac";import { usePostHogClientCapture } from "@/src/features/posthog-analytics";import { api, reportNonTrpcError } from "@/src/utils/api";import {@@ -13,31 +17,72 @@ import {} from "./LLMApiKeySettingsTable";export function ConnectedLLMApiKeySettingsTable({- projectId,+ data,+ emptyState,+ owner,+ overriddenProviders,+ toolbarContent,}: {- projectId: string;+ data?: AsyncTableData<LLMApiKeySettingsTableRow[]>;+ emptyState: ReactNode;+ owner: LlmConnectionFormOwner;+ overriddenProviders?: ReadonlySet<string>;+ toolbarContent?: ReactNode;}) {const capture = usePostHogClientCapture();const utils = api.useUtils();const hasDeleteAccess = useHasProjectAccess({- projectId,+ projectId: owner.scope === "project" ? owner.projectId : undefined,scope: "llmApiKeys:delete",});const hasUpdateAccess = useHasProjectAccess({- projectId,+ projectId: owner.scope === "project" ? owner.projectId : undefined,scope: "llmApiKeys:update",});-- const apiKeys = api.llmApiKey.all.useQuery({- projectId,- includeDecisionModels: true,+ const hasOrganizationAccess = useHasOrganizationAccess({+ organizationId:+ owner.scope === "organization" ? owner.organizationId : undefined,+ scope: "organizationLlmApiKeys:CUD",});- const deleteApiKey = api.llmApiKey.delete.useMutation({+ const canDelete =+ owner.scope === "project" ? hasDeleteAccess : hasOrganizationAccess;+ const canUpdate =+ owner.scope === "project" ? hasUpdateAccess : hasOrganizationAccess;++ const projectApiKeys = api.llmApiKey.all.useQuery(+ {+ projectId: owner.scope === "project" ? owner.projectId : "",+ includeDecisionModels: true,+ },+ { enabled: owner.scope === "project" && data === undefined },+ );+ const organizationApiKeys = api.organizationLlmApiKey.all.useQuery(+ {+ orgId: owner.scope === "organization" ? owner.organizationId : "",+ includeDecisionModels: true,+ },+ { enabled: owner.scope === "organization" && data === undefined },+ );+ const apiKeys =+ owner.scope === "project" ? projectApiKeys : organizationApiKeys;+ const deleteProjectApiKey = api.llmApiKey.delete.useMutation({onSuccess: () => utils.llmApiKey.invalidate(),});+ const deleteOrganizationApiKey = api.organizationLlmApiKey.delete.useMutation(+ {+ onSuccess: async () => {+ await Promise.all([+ utils.organizationLlmApiKey.invalidate(),+ utils.llmApiKey.invalidate(),+ ]);+ },+ },+ );- const tableData = useMemo<AsyncTableData<LLMApiKeySettingsTableRow[]>>(() => {+ const queriedTableData = useMemo<+ AsyncTableData<LLMApiKeySettingsTableRow[]>+ >(() => {if (apiKeys.isLoading) return { status: "loading" };if (apiKeys.isError) {return { status: "error", error: "Failed to load LLM connections" };@@ -48,46 +93,100 @@ export function ConnectedLLMApiKeySettingsTable({data: apiKeys.data?.data ?? [],};}, [apiKeys.data?.data, apiKeys.isError, apiKeys.isLoading]);+ const tableData = data ?? queriedTableData;+ const tableDataWithOverrides = useMemo<+ AsyncTableData<LLMApiKeySettingsTableRow[]>+ >(() => {+ if (tableData.status !== "success" || !overriddenProviders) {+ return tableData;+ }+ return {+ status: "success",+ data: tableData.data.map((apiKey) => ({+ ...apiKey,+ overriddenByProject: overriddenProviders.has(apiKey.provider),+ })),+ };+ }, [overriddenProviders, tableData]);++ const handleDeleteDismiss = () => {+ deleteProjectApiKey.reset();+ deleteOrganizationApiKey.reset();+ };++ const handleDeleteConfirm = async (apiKey: { id: string }) => {+ try {+ if (owner.scope === "project") {+ await deleteProjectApiKey.mutateAsync({+ projectId: owner.projectId,+ id: apiKey.id,+ });+ capture("project_settings:llm_api_key_delete");+ } else {+ await deleteOrganizationApiKey.mutateAsync({+ orgId: owner.organizationId,+ id: apiKey.id,+ });+ capture("organization_settings:llm_api_key_delete");+ }+ } catch (error) {+ reportNonTrpcError(error, "llm-api-keys");+ throw error;+ }+ };return (- <CreateLLMApiKeyDialogController projectId={projectId}>+ <CreateLLMApiKeyDialogController owner={owner}>{({ hasAccess: hasCreateAccess, openDialog: openCreateDialog }) => (- <UpdateLLMApiKeyDialog projectId={projectId}>+ <UpdateLLMApiKeyDialog owner={owner}>{({ openDialog: openUpdateDialog }) => (<ConfirmationDialogController<{ id: string }>title="Delete LLM Connection"text="Are you sure you want to delete this connection? This action cannot be undone."confirmLabel="Permanently delete"variant="destructive"- loading={deleteApiKey.isPending}- error={deleteApiKey.error?.message}- onAfterDismiss={deleteApiKey.reset}- onConfirm={async (apiKey) => {- try {- await deleteApiKey.mutateAsync({ projectId, id: apiKey.id });- capture("project_settings:llm_api_key_delete");- } catch (error) {- reportNonTrpcError(error, "llm-api-keys");- throw error;- }- }}+ loading={+ deleteProjectApiKey.isPending ||+ deleteOrganizationApiKey.isPending+ }+ error={+ deleteProjectApiKey.error?.message ??+ deleteOrganizationApiKey.error?.message+ }+ onAfterDismiss={handleDeleteDismiss}+ onConfirm={handleDeleteConfirm}>{({ openDialog: openDeleteDialog }) => (<LLMApiKeySettingsTable+ actionsDisabledReason={+ owner.scope === "organization" && !canUpdate && !canDelete+ ? "Only organization owners and admins can modify organization connections."+ : undefined+ }createAction={{hasAccess: hasCreateAccess,+ label:+ owner.scope === "project"+ ? "Project Connection"+ : "Organization Connection",onClick: openCreateDialog,}}deleteAction={{- hasAccess: hasDeleteAccess,+ hasAccess: canDelete,onClick: openDeleteDialog,}}updateAction={{- hasAccess: hasUpdateAccess,+ hasAccess: canUpdate,onClick: openUpdateDialog,}}- data={tableData}- noResultsMessage="None"+ data={tableDataWithOverrides}+ emptyState={emptyState}+ toolbarContent={toolbarContent}+ tableName={+ owner.scope === "project"+ ? "Project LLM connections"+ : "Organization LLM connections"+ }/>)}</ConfirmationDialogController>diff --git a/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.stories.tsx b/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.stories.tsxindex 7093d0570b..3940805e27 100644--- a/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.stories.tsx+++ b/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.stories.tsx@@ -1,6 +1,8 @@import { fn } from "storybook/test";+import { Plug } from "lucide-react";import preview from "../../../../../.storybook/preview";+import { LlmConnectionEmptyState } from "@/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState";import {LLMApiKeySettingsTable,type LLMApiKeySettingsTableRow,@@ -10,9 +12,20 @@ const meta = preview.meta({ component: LLMApiKeySettingsTable });export const Default = meta.story({args: {- createAction: { hasAccess: true, onClick: fn() },+ createAction: {+ hasAccess: true,+ label: "Project Connection",+ onClick: fn(),+ },deleteAction: { hasAccess: true, onClick: fn() },updateAction: { hasAccess: true, onClick: fn() },+ emptyState: (+ <LlmConnectionEmptyState+ icon={Plug}+ title="No project connections"+ description="Project connections are only available in llm-app and take precedence over organization connections with the same provider."+ />+ ),data: {status: "success",data: [@@ -55,11 +68,16 @@ export const Default = meta.story({},});+export const Empty = meta.story({+ args: {+ ...Default.input.args,+ data: { status: "success", data: [] },+ },+});+export const Loading = meta.story({args: {- createAction: { hasAccess: true, onClick: fn() },- deleteAction: { hasAccess: true, onClick: fn() },- updateAction: { hasAccess: true, onClick: fn() },+ ...Default.input.args,data: { status: "loading" },loadingRowCount: 5,},diff --git a/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.tsx b/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.tsxindex c48cd1ced3..3cfd3d3c19 100644--- a/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.tsx+++ b/web/src/features/public-api/components/LLMApiKeySettingsTable/LLMApiKeySettingsTable.tsx@@ -1,37 +1,47 @@-import { useCallback, useMemo } from "react";-import { Pencil, PlusIcon, Trash } from "lucide-react";+import { useCallback, useMemo, type ReactNode } from "react";+import { Pencil, PlusIcon, Route, Trash } from "lucide-react";+import { SiAnthropic, SiOpenai } from "react-icons/si";+import { LLMAdapter } from "@langfuse/shared";+import { Badge } from "@/src/components/design-system/Badge/Badge";+import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";import { type TableProps } from "@/src/components/design-system/table/Table";-import { createBadgeTableColumn } from "@/src/components/design-system/table/columns/createBadgeTableColumn";import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";import {SettingsTable,type SettingsTableProps,} from "@/src/components/SettingsTable/SettingsTable";import { type LangfuseColumnDef } from "@/src/components/table/types";-import { type RouterOutput } from "@/src/utils/types";+import type { LlmApiKeyListItem } from "@/src/features/public-api/components/CreateLLMApiKeyForm";-export type LLMApiKeySettingsTableRow =- RouterOutput["llmApiKey"]["all"]["data"][number];+export type LLMApiKeySettingsTableRow = LlmApiKeyListItem & {+ overriddenByProject?: boolean;+};export function LLMApiKeySettingsTable({+ actionsDisabledReason,createAction,deleteAction,+ emptyState,+ tableName = "LLM connections",updateAction,...tableProps}: Pick<TableProps<LLMApiKeySettingsTableRow>,- "data" | "loadingRowCount" | "noResultsMessage"+ "actionsDisabledReason" | "data" | "loadingRowCount"> & {- createAction: { hasAccess: boolean; onClick: () => void };+ createAction: { hasAccess: boolean; label: string; onClick: () => void };deleteAction: {hasAccess: boolean;onClick: (apiKey: LLMApiKeySettingsTableRow) => void;};+ emptyState: ReactNode;updateAction: {hasAccess: boolean;onClick: (apiKey: LLMApiKeySettingsTableRow) => void;};+ tableName?: string;+ toolbarContent?: ReactNode;}) {const showExtraHeaderKeys =tableProps.data.status === "success" &&@@ -39,16 +49,31 @@ export function LLMApiKeySettingsTable({const columns = useMemo<LangfuseColumnDef<LLMApiKeySettingsTableRow>[]>(() => [- createTextTableColumn<LLMApiKeySettingsTableRow>({+ {accessorKey: "provider",header: "Provider",enableResizing: false,- }),- createBadgeTableColumn<LLMApiKeySettingsTableRow>({+ cell: ({ row }) => (+ <div className="flex items-center gap-2">+ <span>{row.original.provider}</span>+ {row.original.overriddenByProject ? (+ <Tooltip label="This project connection overrides the organization secret with the same name.">+ {({ getTriggerProps }) => (+ <span {...getTriggerProps()}>+ <Badge text="Override" size="sm" color="filled" />+ </span>+ )}+ </Tooltip>+ ) : null}+ </div>+ ),+ },+ {accessorKey: "adapter",header: "Adapter",enableResizing: false,- }),+ cell: ({ row }) => <AdapterName adapter={row.original.adapter} />,+ },createTextTableColumn<LLMApiKeySettingsTableRow>({accessorKey: "baseURL",header: "Base URL",@@ -77,29 +102,36 @@ export function LLMApiKeySettingsTable({const actions = useCallback<NonNullable<TableProps<LLMApiKeySettingsTableRow>["actions"]>>(- (apiKey) => [- {- id: "edit",- type: "item",- title: "Edit connection",- icon: Pencil,- disabled: updateAction.hasAccess- ? undefined- : { reason: "You do not have permission to edit this connection" },- onClick: () => updateAction.onClick(apiKey),- },- {- id: "delete",- type: "item",- title: "Delete connection",- icon: Trash,- variant: "destructive",- disabled: deleteAction.hasAccess- ? undefined- : { reason: "You do not have permission to delete this connection" },- onClick: () => deleteAction.onClick(apiKey),- },- ],+ (apiKey) => {+ if (!updateAction.hasAccess && !deleteAction.hasAccess) {+ return [];+ }+ return [+ {+ id: "edit",+ type: "item",+ title: "Edit connection",+ icon: Pencil,+ disabled: updateAction.hasAccess+ ? undefined+ : { reason: "You do not have permission to edit this connection" },+ onClick: () => updateAction.onClick(apiKey),+ },+ {+ id: "delete",+ type: "item",+ title: "Delete connection",+ icon: Trash,+ variant: "destructive",+ disabled: deleteAction.hasAccess+ ? undefined+ : {+ reason: "You do not have permission to delete this connection",+ },+ onClick: () => deleteAction.onClick(apiKey),+ },+ ];+ },[deleteAction, updateAction],);@@ -108,7 +140,7 @@ export function LLMApiKeySettingsTable({? [{id: "add-connection",- label: "Add LLM Connection",+ label: createAction.label,variant: "secondary",icon: <PlusIcon className="icon-base" aria-hidden="true" />,onClick: createAction.onClick,@@ -118,12 +150,45 @@ export function LLMApiKeySettingsTable({return (<SettingsTable- tableName="LLM connections"+ tableName={tableName}columns={columns}actions={actions}+ actionsDisabledReason={actionsDisabledReason}+ emptyState={emptyState}toolbarActions={toolbarActions}onRowClick={updateAction.hasAccess ? updateAction.onClick : undefined}{...tableProps}/>);}++const adapterLabels: Record<LLMAdapter, string> = {+ [LLMAdapter.Anthropic]: "Anthropic",+ [LLMAdapter.OpenAI]: "OpenAI",+ [LLMAdapter.Azure]: "Azure OpenAI",+ [LLMAdapter.Bedrock]: "Amazon Bedrock",+ [LLMAdapter.VertexAI]: "Vertex AI",+ [LLMAdapter.GoogleAIStudio]: "Google AI Studio",+ [LLMAdapter.TypeSafe]: "Type-safe",+};++function AdapterName({ adapter }: { adapter: LLMAdapter }) {+ return (+ <div className="flex items-center gap-2">+ <span className="bg-muted flex size-7 items-center justify-center rounded-md border">+ <AdapterIcon adapter={adapter} />+ </span>+ <span>{adapterLabels[adapter]}</span>+ </div>+ );+}++function AdapterIcon({ adapter }: { adapter: LLMAdapter }) {+ if (adapter === LLMAdapter.OpenAI || adapter === LLMAdapter.Azure) {+ return <SiOpenai className="icon-base" aria-hidden="true" />;+ }+ if (adapter === LLMAdapter.Anthropic) {+ return <SiAnthropic className="icon-base" aria-hidden="true" />;+ }+ return <Route className="icon-base" aria-hidden="true" />;+}diff --git a/web/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState.stories.tsx b/web/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState.stories.tsxnew file mode 100644index 0000000000..63b58cf11e--- /dev/null+++ b/web/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState.stories.tsx@@ -0,0 +1,24 @@+import { Building2, Plug } from "lucide-react";++import preview from "@/.storybook/preview";+import { LlmConnectionEmptyState } from "./LlmConnectionEmptyState";++const meta = preview.meta({ component: LlmConnectionEmptyState });++export const Organization = meta.story({+ args: {+ icon: Building2,+ title: "No organization connections",+ description:+ "Organization connections are shared with every project in Seed Org.",+ },+});++export const Project = meta.story({+ args: {+ icon: Plug,+ title: "No project connections",+ description:+ "Project connections are only available in llm-app and take precedence over organization connections with the same provider.",+ },+});diff --git a/web/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState.tsx b/web/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState.tsxnew file mode 100644index 0000000000..513cbb92f0--- /dev/null+++ b/web/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState.tsx@@ -0,0 +1,23 @@+import type { LucideIcon } from "lucide-react";++export function LlmConnectionEmptyState({+ description,+ icon: Icon,+ title,+}: {+ description: string;+ icon: LucideIcon;+ title: string;+}) {+ return (+ <div className="border-border flex min-h-24 flex-col items-center justify-center gap-2 rounded-md border border-dashed p-4 text-center">+ <span className="flex size-8 items-center justify-center rounded-md border">+ <Icon className="icon-base text-muted-foreground" aria-hidden="true" />+ </span>+ <div className="flex flex-col gap-0.5">+ <p className="text-xs font-bold">{title}</p>+ <p className="text-muted-foreground text-xs">{description}</p>+ </div>+ </div>+ );+}diff --git a/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsx b/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsxindex af725470c3..078c9e6075 100644--- a/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsx+++ b/web/src/features/public-api/components/UpdateLLMApiKeyDialog.tsx@@ -5,17 +5,18 @@ import {DialogHeader,DialogTitle,} from "@/src/components/ui/dialog";-import { CreateLLMApiKeyForm } from "./CreateLLMApiKeyForm";+import {+ CreateLLMApiKeyForm,+ type LlmApiKeyListItem,+ type LlmConnectionFormOwner,+} from "./CreateLLMApiKeyForm";import { useUiCustomization } from "@/src/ee/features/ui-customization";-import { type RouterOutputs } from "@/src/utils/api";--type LlmApiKeyListItem = RouterOutputs["llmApiKey"]["all"]["data"][number];export function UpdateLLMApiKeyDialog({- projectId,+ owner,children,}: {- projectId: string;+ owner: LlmConnectionFormOwner;children: ComponentProps<typeof DialogController<LlmApiKeyListItem>>["children"];@@ -31,7 +32,7 @@ export function UpdateLLMApiKeyDialog({</DialogHeader><CreateLLMApiKeyFormkey={apiKey.id}- projectId={projectId}+ owner={owner}onSuccess={closeDialog}customization={uiCustomization}mode="update"diff --git a/web/src/features/public-api/index.ts b/web/src/features/public-api/index.tsindex 4f2daa4426..91364b8de3 100644--- a/web/src/features/public-api/index.ts+++ b/web/src/features/public-api/index.ts@@ -15,3 +15,4 @@ export {createStructuredPublicApiError,StructuredPublicApiError,} from "./types/structuredPublicApiError";+export { LlmConnectionEmptyState } from "./components/LlmConnectionEmptyState/LlmConnectionEmptyState";diff --git a/web/src/features/public-api/server/authenticateOrganizationApiRequest.ts b/web/src/features/public-api/server/authenticateOrganizationApiRequest.tsnew file mode 100644index 0000000000..812ebe8b8b--- /dev/null+++ b/web/src/features/public-api/server/authenticateOrganizationApiRequest.ts@@ -0,0 +1,46 @@+import type { NextApiRequest, NextApiResponse } from "next";+import type { Action } from "@langfuse/shared/rbac";++import { hasEntitlementBasedOnPlan } from "@/src/features/entitlements/server";+import { RateLimitService } from "./RateLimitService";+import { shadowAuth } from "./shadowAuth";+import { writeOrgError } from "./writeError";++export async function authenticateOrganizationApiRequest(+ req: NextApiRequest,+ res: NextApiResponse,+ action: Action,+) {+ const auth = await shadowAuth({+ req,+ action,+ allowedAccessLevels: ["organization"],+ });+ if (!auth.success) {+ writeOrgError(res, auth.error);+ return null;+ }++ if (+ !hasEntitlementBasedOnPlan({+ plan: auth.scope.plan,+ entitlement: "admin-api",+ })+ ) {+ res.status(403).json({+ error: "This feature is not available on your current plan.",+ });+ return null;+ }++ const rateLimit = await RateLimitService.getInstance().rateLimitRequest(+ auth.scope,+ "public-api",+ );+ if (rateLimit?.isRateLimited()) {+ rateLimit.sendRestResponseIfLimited(res);+ return null;+ }++ return auth.scope;+}diff --git a/web/src/features/public-api/server/clickHouseRequestTags.ts b/web/src/features/public-api/server/clickHouseRequestTags.tsindex 479b9e930c..b782d000c1 100644--- a/web/src/features/public-api/server/clickHouseRequestTags.ts+++ b/web/src/features/public-api/server/clickHouseRequestTags.ts@@ -22,6 +22,7 @@ export const DYNAMIC_API_ROUTE_TEMPLATES = ["/api/public/media/[mediaId]","/api/public/models/[modelId]","/api/public/observations/[observationId]",+ "/api/public/organizations/llm-connections/[id]","/api/public/projects/[projectId]","/api/public/projects/[projectId]/apiKeys","/api/public/projects/[projectId]/apiKeys/[apiKeyId]",diff --git a/web/src/features/public-api/server/index.ts b/web/src/features/public-api/server/index.tsindex 41c3008cd3..92583b7a76 100644--- a/web/src/features/public-api/server/index.ts+++ b/web/src/features/public-api/server/index.ts@@ -1,4 +1,5 @@export { ApiAuthService } from "./apiAuth";+export { authenticateOrganizationApiRequest } from "./authenticateOrganizationApiRequest";export { createAuthedProjectAPIRoute } from "./createAuthedProjectAPIRoute";export { createOrFetchDatasetRun } from "./dataset-runs";export {diff --git a/web/src/features/topics/TopicModelSettings.tsx b/web/src/features/topics/TopicModelSettings.tsxindex 37bb01bb40..ffec9e953e 100644--- a/web/src/features/topics/TopicModelSettings.tsx+++ b/web/src/features/topics/TopicModelSettings.tsx@@ -27,7 +27,7 @@ import { topicsSetupPayload } from "./topics-setup";import { useAutomaticProcessing } from "./useAutomaticProcessing";type StoredSettings = RouterOutputs["topics"]["modelSettings"];-type Connection = RouterOutputs["llmApiKey"]["all"]["data"][number];+type Connection = RouterOutputs["llmApiKey"]["effective"]["data"][number];type SlotValue = { llmApiKeyId: string; model: string } | null;/** Topics setup: one dialog for models, facets, and which traces to include. */@@ -47,7 +47,7 @@ export function useTopicModelSettings({projectId,});const rules = api.topics.rules.useQuery({ projectId });- const connections = api.llmApiKey.all.useQuery({ projectId });+ const connections = api.llmApiKey.effective.useQuery({ projectId });const configured = Boolean(settings.data?.summary &&settings.data.embedding &&diff --git a/web/src/pages/api/public/llm-connections/[id].ts b/web/src/pages/api/public/llm-connections/[id].tsindex 65cc606ea7..902e86430b 100644--- a/web/src/pages/api/public/llm-connections/[id].ts+++ b/web/src/pages/api/public/llm-connections/[id].ts@@ -1,23 +1,10 @@-import { prisma } from "@langfuse/shared/src/db";import { withMiddlewares } from "@/src/features/public-api/server/withMiddlewares";import { createAuthedProjectAPIRoute } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";+import { LlmConnectionService } from "@/src/features/llm-api-key/server/llmConnectionService";import {DeleteLlmConnectionV1Query,DeleteLlmConnectionV1Response,} from "@/src/features/public-api/types/llm-connections";-import { auditLog } from "@/src/features/audit-logs/server";-import {- invalidateTopicsProcessingActive,- pauseTopicsModelsUsingConnection,-} from "@langfuse/shared/topics/server";-import { EvaluatorBlockReason, LangfuseNotFoundError } from "@langfuse/shared";-import {- blockEvaluatorsUsingDefaultModel,- blockEvaluatorsUsingProvider,- EMPTY_EVALUATOR_BLOCK,- EvaluatorBlockSource,- finalizeEvaluatorBlocks,-} from "@langfuse/shared/src/server";export default withMiddlewares({DELETE: createAuthedProjectAPIRoute({@@ -27,73 +14,17 @@ export default withMiddlewares({responseSchema: DeleteLlmConnectionV1Response,isAdminApiKeyAuthAllowed: true,fn: async ({ query, auth }) => {- const projectId = auth.scope.projectId;-- const llmApiKey = await prisma.llmApiKeys.findFirst({- where: {- id: query.id,- projectId,+ await new LlmConnectionService().delete({+ owner: {+ type: "project",+ projectId: auth.scope.projectId,+ organizationId: auth.scope.orgId,},- });-- if (!llmApiKey) {- throw new LangfuseNotFoundError("LLM connection not found");- }-- const result = await prisma.$transaction(async (tx) => {- const defaultModel = await tx.defaultLlmModel.findFirst({- where: { projectId },- select: { llmApiKeyId: true },- });-- const providerBlock = llmApiKey.provider- ? await blockEvaluatorsUsingProvider({- tx,- projectId,- provider: llmApiKey.provider,- })- : EMPTY_EVALUATOR_BLOCK;-- const defaultModelBlock =- defaultModel && defaultModel.llmApiKeyId === llmApiKey.id- ? await blockEvaluatorsUsingDefaultModel({ tx, projectId })- : EMPTY_EVALUATOR_BLOCK;-- await pauseTopicsModelsUsingConnection(- {- projectId,- llmApiKeyId: llmApiKey.id,- provider: llmApiKey.provider,- },- tx,- );-- await tx.llmApiKeys.delete({- where: { id: llmApiKey.id, projectId },- });-- await auditLog({- action: "delete",- resourceType: "llmApiKey",- resourceId: llmApiKey.id,- projectId,+ id: query.id,+ actor: {+ projectId: auth.scope.projectId,orgId: auth.scope.orgId,apiKeyId: auth.scope.apiKeyId,- before: llmApiKey,- });-- return { providerBlock, defaultModelBlock };- });-- await invalidateTopicsProcessingActive(projectId);- await finalizeEvaluatorBlocks({- projectId,- source: EvaluatorBlockSource.LLM_API_KEY_DELETION,- evaluatorIdsByReason: {- [EvaluatorBlockReason.LLM_CONNECTION_MISSING]:- result.providerBlock.blockedEvaluatorIds,- [EvaluatorBlockReason.DEFAULT_EVAL_MODEL_MISSING]:- result.defaultModelBlock.blockedEvaluatorIds,},});diff --git a/web/src/pages/api/public/llm-connections/index.ts b/web/src/pages/api/public/llm-connections/index.tsindex b2bba797f6..901d9960d1 100644--- a/web/src/pages/api/public/llm-connections/index.ts+++ b/web/src/pages/api/public/llm-connections/index.ts@@ -1,6 +1,7 @@-import { prisma } from "@langfuse/shared/src/db";import { withMiddlewares } from "@/src/features/public-api/server/withMiddlewares";import { createAuthedProjectAPIRoute } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";+import { shadowAuthorize } from "@/src/features/public-api/server/shadowAuth";+import { LlmConnectionService } from "@/src/features/llm-api-key/server/llmConnectionService";import {GetLlmConnectionsV1Query,GetLlmConnectionsV1Response,@@ -8,16 +9,6 @@ import {PutLlmConnectionV1Response,transformDbLlmConnectionToAPI,} from "@/src/features/public-api/types/llm-connections";-import { encrypt } from "@langfuse/shared/encryption";-import { getDisplaySecretKey } from "@/src/features/llm-api-key/server/router";-import { auditLog } from "@/src/features/audit-logs/server";-import {- InvalidRequestError,- BEDROCK_USE_DEFAULT_CREDENTIALS,- LLMAdapter,-} from "@langfuse/shared";-import { validateLlmConnectionBaseURL } from "@langfuse/shared/src/server";-import { env } from "@/src/env.mjs";export default withMiddlewares({GET: createAuthedProjectAPIRoute({@@ -27,52 +18,24 @@ export default withMiddlewares({responseSchema: GetLlmConnectionsV1Response,isAdminApiKeyAuthAllowed: true,fn: async ({ query, auth }) => {- const { limit, page } = query;-- // Explicitly select only safe fields to prevent secret leakage- const llmConnections = await prisma.llmApiKeys.findMany({- select: {- id: true,- provider: true,- adapter: true,- displaySecretKey: true,- baseURL: true,- customModels: true,- withDefaultModels: true,- extraHeaderKeys: true,- config: true,- createdAt: true,- updatedAt: true,- // Explicitly exclude: secretKey, extraHeaders- },- where: {- projectId: auth.scope.projectId,- },- orderBy: {- createdAt: "desc",- },- take: limit,- skip: (page - 1) * limit,- });-- const totalItems = await prisma.llmApiKeys.count({- where: {+ const result = await new LlmConnectionService().list({+ owner: {+ type: "project",projectId: auth.scope.projectId,+ organizationId: auth.scope.orgId,},+ includeDecisionModels: true,+ page: query.page,+ limit: query.limit,});- // Transform and validate through strict schema- const transformedConnections = llmConnections.map(- transformDbLlmConnectionToAPI,- );-return {- data: transformedConnections,+ data: result.data.map(transformDbLlmConnectionToAPI),meta: {- page,- limit,- totalItems,- totalPages: Math.ceil(totalItems / limit),+ page: query.page,+ limit: query.limit,+ totalItems: result.totalCount,+ totalPages: Math.ceil(result.totalCount / query.limit),},};},@@ -84,102 +47,35 @@ export default withMiddlewares({bodySchema: PutLlmConnectionV1Body,responseSchema: PutLlmConnectionV1Response,isAdminApiKeyAuthAllowed: true,- fn: async ({ body, auth, res }) => {- const projectId = auth.scope.projectId;-- const existingConnection = await prisma.llmApiKeys.findUnique({- where: {- projectId_provider: {- projectId,- provider: body.provider,- },- },- select: { id: true, baseURL: true },- });-- const isUpdate = Boolean(existingConnection);-- if (body.baseURL && body.baseURL !== existingConnection?.baseURL) {- try {- await validateLlmConnectionBaseURL(body.baseURL);- } catch (error) {- throw new InvalidRequestError(- `Invalid baseURL: ${error instanceof Error ? error.message : "Unknown error"}`,- );- }- }-- const isLangfuseCloud = Boolean(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION);-- if (body.secretKey === BEDROCK_USE_DEFAULT_CREDENTIALS) {- if (isLangfuseCloud || body.adapter !== LLMAdapter.Bedrock) {- throw new InvalidRequestError(- "Default AWS credentials are only allowed for Bedrock in self-hosted deployments.",- );- }- }-- const llmConnectionBody = {- adapter: body.adapter,- secretKey: encrypt(body.secretKey),- displaySecretKey: getDisplaySecretKey(body.secretKey),- baseURL: body.baseURL || null,- customModels: body.customModels || [],- withDefaultModels: body.withDefaultModels,- extraHeaders: body.extraHeaders- ? encrypt(JSON.stringify(body.extraHeaders))- : null,- extraHeaderKeys: body.extraHeaders- ? Object.keys(body.extraHeaders)- : [],- config: body.config,+ fn: async ({ body, auth, ctx, res }) => {+ const service = new LlmConnectionService();+ const owner = {+ type: "project" as const,+ projectId: auth.scope.projectId,+ organizationId: auth.scope.orgId,};+ if (await service.exists({ owner, provider: body.provider })) {+ const decision = shadowAuthorize({+ ctx,+ action: "llmApiKeys:update",+ resource: { projectId: auth.scope.projectId },+ legacyDecision: { success: true, scope: auth.scope },+ });+ if (!decision.success) throw decision.error;+ }- // Perform upsert- const connection = await prisma.llmApiKeys.upsert({- where: {- projectId_provider: {- projectId,- provider: body.provider,- },- },- create: {- projectId,- provider: body.provider,- ...llmConnectionBody,- },- update: llmConnectionBody,- select: {- id: true,- provider: true,- adapter: true,- displaySecretKey: true,- baseURL: true,- customModels: true,- withDefaultModels: true,- extraHeaderKeys: true,- config: true,- createdAt: true,- updatedAt: true,- // Explicitly exclude: secretKey, extraHeaders+ const result = await service.upsert({+ owner,+ input: body,+ actor: {+ projectId: auth.scope.projectId,+ orgId: auth.scope.orgId,+ apiKeyId: auth.scope.apiKeyId,},});- // Set appropriate status code- res.status(isUpdate ? 200 : 201);-- // Add audit log entry- await auditLog({- action: isUpdate ? "update" : "create",- resourceType: "llmApiKey",- resourceId: connection.id,- projectId: auth.scope.projectId,- orgId: auth.scope.orgId,- apiKeyId: auth.scope.apiKeyId,- });-- // Transform and validate through strict schema- return transformDbLlmConnectionToAPI(connection);+ res.status(result.created ? 201 : 200);+ return transformDbLlmConnectionToAPI(result.connection);},}),});diff --git a/web/src/pages/api/public/organizations/llm-connections/[id].ts b/web/src/pages/api/public/organizations/llm-connections/[id].tsnew file mode 100644index 0000000000..05e8287d92--- /dev/null+++ b/web/src/pages/api/public/organizations/llm-connections/[id].ts@@ -0,0 +1,36 @@+import { LlmConnectionService } from "@/src/features/llm-api-key/server/llmConnectionService";+import {+ authenticateOrganizationApiRequest,+ withMiddlewares,+} from "@/src/features/public-api/server";+import {+ DeleteLlmConnectionV1Query,+ DeleteLlmConnectionV1Response,+} from "@/src/features/public-api/types/llm-connections";++export default withMiddlewares({+ DELETE: async (req, res) => {+ const scope = await authenticateOrganizationApiRequest(+ req,+ res,+ "organizationLlmApiKeys:CUD",+ );+ if (!scope) return;++ const query = DeleteLlmConnectionV1Query.parse(req.query);+ await new LlmConnectionService().delete({+ owner: { type: "organization", organizationId: scope.orgId },+ id: query.id,+ actor: {+ apiKeyId: scope.apiKeyId,+ orgId: scope.orgId,+ },+ });++ return res.status(200).json(+ DeleteLlmConnectionV1Response.parse({+ message: "LLM connection successfully deleted",+ }),+ );+ },+});diff --git a/web/src/pages/api/public/organizations/llm-connections/index.ts b/web/src/pages/api/public/organizations/llm-connections/index.tsnew file mode 100644index 0000000000..e621cd3d18--- /dev/null+++ b/web/src/pages/api/public/organizations/llm-connections/index.ts@@ -0,0 +1,69 @@+import { LlmConnectionService } from "@/src/features/llm-api-key/server/llmConnectionService";+import {+ authenticateOrganizationApiRequest,+ withMiddlewares,+} from "@/src/features/public-api/server";+import {+ GetLlmConnectionsV1Query,+ GetLlmConnectionsV1Response,+ PutLlmConnectionV1Body,+ PutLlmConnectionV1Response,+ transformDbLlmConnectionToAPI,+} from "@/src/features/public-api/types/llm-connections";++export default withMiddlewares({+ GET: async (req, res) => {+ const scope = await authenticateOrganizationApiRequest(+ req,+ res,+ "organizationLlmApiKeys:read",+ );+ if (!scope) return;++ const query = GetLlmConnectionsV1Query.parse(req.query);+ const result = await new LlmConnectionService().list({+ owner: { type: "organization", organizationId: scope.orgId },+ includeDecisionModels: true,+ page: query.page,+ limit: query.limit,+ });+ return res.status(200).json(+ GetLlmConnectionsV1Response.parse({+ data: result.data.map(transformDbLlmConnectionToAPI),+ meta: {+ page: query.page,+ limit: query.limit,+ totalItems: result.totalCount,+ totalPages: Math.ceil(result.totalCount / query.limit),+ },+ }),+ );+ },++ PUT: async (req, res) => {+ const scope = await authenticateOrganizationApiRequest(+ req,+ res,+ "organizationLlmApiKeys:CUD",+ );+ if (!scope) return;++ const input = PutLlmConnectionV1Body.parse(req.body);+ const result = await new LlmConnectionService().upsert({+ owner: { type: "organization", organizationId: scope.orgId },+ input,+ actor: {+ apiKeyId: scope.apiKeyId,+ orgId: scope.orgId,+ },+ });++ return res+ .status(result.created ? 201 : 200)+ .json(+ PutLlmConnectionV1Response.parse(+ transformDbLlmConnectionToAPI(result.connection),+ ),+ );+ },+});diff --git a/web/src/server/api/root.ts b/web/src/server/api/root.tsindex 092bafae46..d41cace360 100644--- a/web/src/server/api/root.ts+++ b/web/src/server/api/root.ts@@ -24,6 +24,7 @@ import { mixpanelIntegrationRouter } from "@/src/features/mixpanel-integration/mimport { blobStorageIntegrationRouter } from "@/src/features/blobstorage-integration/blobstorage-integration-router";import { externalMediaStorageRouter } from "@/src/features/external-media-storage/server";import { llmApiKeyRouter } from "@/src/features/llm-api-key/server/router";+import { organizationLlmApiKeyRouter } from "@/src/features/llm-api-key/server/organizationRouter";import { llmSchemaRouter } from "@/src/features/llm-schemas/server/router";import { llmToolRouter } from "@/src/features/llm-tools/server/router";import { organizationsRouter } from "@/src/features/organizations/server/organizationRouter";@@ -111,6 +112,7 @@ export const appRouter = createTRPCRouter({blobStorageIntegration: blobStorageIntegrationRouter,externalMediaStorage: externalMediaStorageRouter,llmApiKey: llmApiKeyRouter,+ organizationLlmApiKey: organizationLlmApiKeyRouter,llmSchemas: llmSchemaRouter,llmTools: llmToolRouter,public: publicRouter,diff --git a/worker/src/features/experiments/utils.ts b/worker/src/features/experiments/utils.tsindex 8def41d4d3..b2782d8dca 100644--- a/worker/src/features/experiments/utils.ts+++ b/worker/src/features/experiments/utils.ts@@ -24,6 +24,7 @@ import {ExperimentMetadataSchema,LLMApiKeySchema,PromptContentSchema,+ resolveLlmApiKey,} from "@langfuse/shared/src/server";import { prisma } from "@langfuse/shared/src/db";import z from "zod";@@ -218,9 +219,7 @@ export async function validateAndSetupExperiment(}// Fetch and validate API key- const apiKey = await prisma.llmApiKeys.findFirst({- where: { projectId, provider },- });+ const apiKey = await resolveLlmApiKey({ projectId, provider });if (!apiKey) {throw new UnrecoverableError(`API key for provider ${provider} not found`);}