🪢 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(web): add name search and gateway key filter to organization API keys (#19108)
Tobias Wochinger9cfcc835872c
diff --git a/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts b/web/src/__tests__/server/organizations-api-key-trpc.servertest.tsindex e56db20949..2a6a82b9d2 100644--- a/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts+++ b/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts@@ -20,6 +20,8 @@ import {import { getRolesForPrincipal } from "@/src/features/rbac/getRolesForPrincipal";import { env } from "@/src/env.mjs";import { randomUUID } from "crypto";+import { GatewayResolveCache } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";+import { gatewayApiFormats } from "@/src/features/ai-gateway/server/provider/registry";vi.mock("@/src/env.mjs", async (importOriginal) => {const actual = await importOriginal<{ env: typeof env }>();@@ -27,6 +29,8 @@ vi.mock("@/src/env.mjs", async (importOriginal) => {});const originalRoleConfig = {+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED:+ env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED,API_AUTH_MIGRATION: env.API_AUTH_MIGRATION,API_KEY_ORG_ROLES_ENABLE: env.API_KEY_ORG_ROLES_ENABLE,API_KEY_PROJECT_ROLES_ENABLE: env.API_KEY_PROJECT_ROLES_ENABLE,@@ -263,6 +267,14 @@ describe("organization API keys trpc", () => {expect(ids).not.toContain(gatewayKey.id);expect(ids).toContain(ingestKey.id);++ const allApiKeys = await ownerCaller.organizationApiKeys.byOrganizationId(+ { orgId: organizationId, includeGatewayKeys: true },+ );+ const allIds = allApiKeys.map((key) => key.id);++ expect(allIds).toContain(gatewayKey.id);+ expect(allIds).toContain(ingestKey.id);});it("regular member cannot fetch organization API keys", async () => {@@ -514,6 +526,44 @@ describe("organization API keys trpc", () => {expect(deletedKey).toBeUndefined();});+ it("stops a deleted gateway key from resolving through the gateway cache", async () => {+ Object.assign(env, { LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED: "true" });+ const gatewayKey = await createApiKey(prisma, {+ owner: OrganizationId(organizationId),+ role: SystemRoleId("AI_GATEWAY"),+ createdBy: UserId("user-1"),+ name: "Cached gateway key",+ });+ const { fastHashedSecretKey } = await prisma.apiKey.findUniqueOrThrow({+ where: { id: gatewayKey.id },+ select: { fastHashedSecretKey: true },+ });+ const cache = new GatewayResolveCache();+ const cacheEntry = {+ fastHashedSecretKey: fastHashedSecretKey!,+ apiFormat: gatewayApiFormats[0],+ };+ await cache.set({+ ...cacheEntry,+ context: {+ organizationId,+ apiKeyId: gatewayKey.id,+ keyMetadata: {},+ ingestionProjectId: null,+ ingestionMode: null,+ connections: [],+ },+ });+ expect(await cache.get(cacheEntry)).not.toBeNull();++ await ownerCaller.organizationApiKeys.delete({+ orgId: organizationId,+ id: gatewayKey.id,+ });++ expect(await cache.get(cacheEntry)).toBeNull();+ });+it("returns NOT_FOUND for a missing API key", async () => {await expect(ownerCaller.organizationApiKeys.delete({diff --git a/web/src/components/SettingsTable/SettingsTable.tsx b/web/src/components/SettingsTable/SettingsTable.tsxindex 7501c91476..4d1bc801af 100644--- a/web/src/components/SettingsTable/SettingsTable.tsx+++ b/web/src/components/SettingsTable/SettingsTable.tsx@@ -13,6 +13,7 @@ import {} from "@/src/components/design-system/table/Table";import { MultiSelectInput } from "@/src/components/design-system/MultiSelectInput/MultiSelectInput";import { SearchInput } from "@/src/components/design-system/SearchInput/SearchInput";+import { SelectInput } from "@/src/components/design-system/SelectInput/SelectInput";import { SettingsTableCard } from "@/src/components/layouts/settings-table-card";import { DataTableColumnVisibilityFilter } from "@/src/components/table/data-table-column-visibility-filter";import { type RowHeight } from "@/src/components/table/data-table-row-height-switch";@@ -38,13 +39,24 @@ export type SettingsTableToolbarAction = ActionButtonProps extends infer TAction: never: never;-type SettingsTableFilter = {+type SettingsTableFilter =+ | (SettingsTableFilterBase & {+ type?: "multi-select";+ value: string[];+ onChange: (value: string[]) => void;+ })+ | (SettingsTableFilterBase & {+ type: "select";+ value: string;+ onChange: (value: string) => void;+ muted?: boolean;+ });++type SettingsTableFilterBase = {id: string;label: string;placeholder: string;options: { value: string; label: string }[];- value: string[];- onChange: (value: string[]) => void;};export type SettingsTableProps<TData extends object> = Omit<@@ -148,21 +160,7 @@ export function SettingsTable<TData extends object>({</div>)}{filters?.map((filter) => (- <div key={filter.id} className="w-44 shrink-0">- <MultiSelectInput- aria-label={`Filter by ${filter.label.toLowerCase()}`}- value={filter.value}- options={filter.options}- onValueChange={filter.onChange}- placeholder={filter.placeholder}- selectedLabel={filter.options- .filter((option) => filter.value.includes(option.value))- .map((option) => option.label)- .join(", ")}- searchPlaceholder="Search..."- emptyMessage="No options found."- />- </div>+ <SettingsTableFilterInput key={filter.id} filter={filter} />))}</div>@@ -257,3 +255,59 @@ export function SettingsTable<TData extends object>({</div>);}++function SettingsTableFilterInput({ filter }: { filter: SettingsTableFilter }) {+ const ariaLabel = `Filter by ${filter.label.toLowerCase()}`;++ if (filter.type === "select") {+ return (+ <div className="grid shrink-0">+ {/* The open menu is as wide as the trigger, so the trigger is sized to+ its longest option rather than to the current selection. */}+ {filter.options.map((option) => (+ <div+ key={option.value}+ inert+ aria-hidden="true"+ className="invisible col-start-1 row-start-1"+ >+ <SelectInput+ value={option.value}+ options={filter.options}+ onValueChange={filter.onChange}+ placeholder={filter.placeholder}+ />+ </div>+ ))}+ <div className="col-start-1 row-start-1">+ <SelectInput+ aria-label={ariaLabel}+ value={filter.value}+ options={filter.options}+ onValueChange={filter.onChange}+ placeholder={filter.placeholder}+ muted={filter.muted}+ />+ </div>+ </div>+ );+ }++ return (+ <div className="w-44 shrink-0">+ <MultiSelectInput+ aria-label={ariaLabel}+ value={filter.value}+ options={filter.options}+ onValueChange={filter.onChange}+ placeholder={filter.placeholder}+ selectedLabel={filter.options+ .filter((option) => filter.value.includes(option.value))+ .map((option) => option.label)+ .join(", ")}+ searchPlaceholder="Search..."+ emptyMessage="No options found."+ />+ </div>+ );+}diff --git a/web/src/features/ai-gateway/server/index.ts b/web/src/features/ai-gateway/server/index.tsindex 8c15fe7119..f26431ac5b 100644--- a/web/src/features/ai-gateway/server/index.ts+++ b/web/src/features/ai-gateway/server/index.ts@@ -5,4 +5,5 @@ export {} from "./auth";export { gatewayModelsApiHandler } from "./provider/models/gatewayModelsApiHandler";export { gatewayResolveApiHandler } from "./resolve/gatewayResolveApiHandler";+export { invalidateGatewayResolveCacheForApiKey } from "./resolve/gatewayResolveCache";export { aiGatewayRouter } from "./router";diff --git a/web/src/features/public-api/components/ApiKeyList.clienttest.tsx b/web/src/features/public-api/components/ApiKeyList.clienttest.tsxindex 3c890132b0..300817707c 100644--- a/web/src/features/public-api/components/ApiKeyList.clienttest.tsx+++ b/web/src/features/public-api/components/ApiKeyList.clienttest.tsx@@ -72,6 +72,7 @@ const PROJECT_ID = "project-1";function renderAsProjectRole(role: Role) {mockSession.data = {+ environment: {},user: {admin: false,organizations: [{ id: "org-1", projects: [{ id: PROJECT_ID, role }] }],diff --git a/web/src/features/public-api/components/ApiKeyList.tsx b/web/src/features/public-api/components/ApiKeyList.tsxindex a432e6c676..f5523260b5 100644--- a/web/src/features/public-api/components/ApiKeyList.tsx+++ b/web/src/features/public-api/components/ApiKeyList.tsx@@ -78,7 +78,9 @@ export function ApiKeyList(props: { entityId: string; scope: ApiKeyScope }) {copiedMessage="Secrets are not included, create a new key to copy them."content={envCode}/>+ {/* Remount per entity so search, filter and retained query rows never carry over to another org or project. */}<ConnectedApiKeySettingsTable+ key={entityId}entityId={entityId}scope={scope}hasWriteAccess={hasWriteAccess}diff --git a/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.stories.tsx b/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.stories.tsxindex 25f17d2649..95174a9732 100644--- a/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.stories.tsx+++ b/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.stories.tsx@@ -27,6 +27,7 @@ export const Default = meta.story({editNameAction: { hasAccess: true, onClick: fn() },hasWriteAccess: true,onDelete: fn(),+ search: { value: "", onChange: fn() },data: {status: "success",data: [@@ -53,7 +54,18 @@ export const Loading = meta.story({editNameAction: { hasAccess: true, onClick: fn() },hasWriteAccess: true,onDelete: fn(),+ search: { value: "", onChange: fn() },data: { status: "loading" },loadingRowCount: 5,},});++export const WithGatewayKeyFilter = meta.story({+ args: {+ ...Default.input.args,+ gatewayKeyFilter: {+ includeGatewayKeys: false,+ onIncludeGatewayKeysChange: fn(),+ },+ },+});diff --git a/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.tsx b/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.tsxindex 2a8bc6e0dc..66a0feeee2 100644--- a/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.tsx+++ b/web/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable.tsx@@ -5,11 +5,22 @@ import { type TableProps } from "@/src/components/design-system/table/Table";import { createDateTableColumn } from "@/src/components/design-system/table/columns/createDateTableColumn";import { createTextTableColumn } from "@/src/components/design-system/table/columns/createTextTableColumn";import { createUserTableColumn } from "@/src/components/design-system/table/columns/createUserTableColumn";-import { SettingsTable } from "@/src/components/SettingsTable/SettingsTable";+import {+ SettingsTable,+ type SettingsTableProps,+} from "@/src/components/SettingsTable/SettingsTable";import { type LangfuseColumnDef } from "@/src/components/table/types";import { isApiKeyExpired } from "@/src/features/apiKey";import { type RouterOutput } from "@/src/utils/types";+const EXCLUDE_GATEWAY_KEYS = "exclude-gateway";+const ALL_KEYS = "all";++const GATEWAY_KEY_FILTER_OPTIONS = [+ { value: EXCLUDE_GATEWAY_KEYS, label: "Exclude gateway keys" },+ { value: ALL_KEYS, label: "All API keys" },+];+export type ApiKeySettingsTableRow =| RouterOutput["projectApiKeys"]["byProjectId"][number]| RouterOutput["organizationApiKeys"]["byOrganizationId"][number];@@ -18,18 +29,10 @@ export function ApiKeySettingsTable({editNameAction,hasWriteAccess,onDelete,+ search,+ gatewayKeyFilter,...tableProps-}: Pick<- TableProps<ApiKeySettingsTableRow>,- "data" | "loadingRowCount" | "noResultsMessage"-> & {- editNameAction: {- hasAccess: boolean;- onClick: (apiKey: ApiKeySettingsTableRow) => void;- };- hasWriteAccess: boolean;- onDelete: (apiKey: ApiKeySettingsTableRow) => void;-}) {+}: ApiKeySettingsTableProps) {const columns = useMemo<LangfuseColumnDef<ApiKeySettingsTableRow>[]>(() => [createTextTableColumn<ApiKeySettingsTableRow>({@@ -126,7 +129,52 @@ export function ApiKeySettingsTable({columnOrderKey="apiKeysColumnOrder-v2"columns={columns}actions={actions}+ search={{ ...search, placeholder: "Search by name" }}+ filters={+ gatewayKeyFilter+ ? [toGatewayKeySettingsFilter(gatewayKeyFilter)]+ : undefined+ }{...tableProps}/>);}++function toGatewayKeySettingsFilter({+ includeGatewayKeys,+ onIncludeGatewayKeysChange,+}: GatewayKeyFilter): SettingsTableFilter {+ return {+ type: "select",+ id: "gatewayKeys",+ label: "Gateway keys",+ placeholder: "Exclude gateway keys",+ options: GATEWAY_KEY_FILTER_OPTIONS,+ value: includeGatewayKeys ? ALL_KEYS : EXCLUDE_GATEWAY_KEYS,+ onChange: (value) => onIncludeGatewayKeysChange(value === ALL_KEYS),+ muted: !includeGatewayKeys,+ };+}++type ApiKeySettingsTableProps = Pick<+ TableProps<ApiKeySettingsTableRow>,+ "data" | "loadingRowCount" | "noResultsMessage"+> & {+ editNameAction: {+ hasAccess: boolean;+ onClick: (apiKey: ApiKeySettingsTableRow) => void;+ };+ hasWriteAccess: boolean;+ onDelete: (apiKey: ApiKeySettingsTableRow) => void;+ search: { value: string; onChange: (value: string) => void };+ gatewayKeyFilter?: GatewayKeyFilter;+};++type GatewayKeyFilter = {+ includeGatewayKeys: boolean;+ onIncludeGatewayKeysChange: (includeGatewayKeys: boolean) => void;+};++type SettingsTableFilter = NonNullable<+ SettingsTableProps<ApiKeySettingsTableRow>["filters"]+>[number];diff --git a/web/src/features/public-api/components/ApiKeySettingsTable/ConnectedApiKeySettingsTable.tsx b/web/src/features/public-api/components/ApiKeySettingsTable/ConnectedApiKeySettingsTable.tsxindex 5200768231..559d402889 100644--- a/web/src/features/public-api/components/ApiKeySettingsTable/ConnectedApiKeySettingsTable.tsx+++ b/web/src/features/public-api/components/ApiKeySettingsTable/ConnectedApiKeySettingsTable.tsx@@ -1,11 +1,15 @@import { useId, useMemo, useRef, useState, type ReactNode } from "react";+import { keepPreviousData } from "@tanstack/react-query";import { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";import { Dialog } from "@/src/components/design-system/Dialog/Dialog";import { DialogController } from "@/src/components/design-system/DialogController/DialogController";import { Input } from "@/src/components/design-system/Input/Input";+import { SEPARATE_GATEWAY_API_KEYS } from "@/src/features/ai-gateway/fns/separateGatewayApiKeys";import { type AsyncTableData } from "@/src/components/design-system/table/Table";+import { useIsFeatureEnabled } from "@/src/features/feature-flags";import { usePostHogClientCapture } from "@/src/features/posthog-analytics";+import { filterApiKeysByName } from "@/src/features/public-api/components/ApiKeySettingsTable/fns/filterApiKeysByName";import { api, reportNonTrpcError } from "@/src/utils/api";import {ApiKeySettingsTable,@@ -26,15 +30,24 @@ export function ConnectedApiKeySettingsTable({const capture = usePostHogClientCapture();const utils = api.useUtils();- const projectApiKeys = api.projectApiKeys.byProjectId.useQuery(- { projectId: entityId },- { enabled: scope === "project" },- );- const organizationApiKeys = api.organizationApiKeys.byOrganizationId.useQuery(- { orgId: entityId },- { enabled: scope === "organization" },- );- const apiKeys = scope === "project" ? projectApiKeys : organizationApiKeys;+ const [searchQuery, setSearchQuery] = useState("");+ const [includeGatewayKeys, setIncludeGatewayKeys] = useState(false);+ const isAiGatewayEnabled = useIsFeatureEnabled("aiGateway", {+ enableForAdmins: false,+ organizationId: scope === "organization" ? entityId : undefined,+ });+ const showGatewayKeyFilter =+ SEPARATE_GATEWAY_API_KEYS && scope === "organization" && isAiGatewayEnabled;+ const tableData = useApiKeySettingsTableData({+ entityId,+ scope,+ includeGatewayKeys,+ searchQuery,+ });+ const noResultsMessage =+ searchQuery.trim().length > 0+ ? "No API keys match the current search."+ : "None";const deleteProjectApiKey = api.projectApiKeys.delete.useMutation({onSuccess: () => utils.projectApiKeys.invalidate(),@@ -42,13 +55,6 @@ export function ConnectedApiKeySettingsTable({const deleteOrganizationApiKey = api.organizationApiKeys.delete.useMutation({onSuccess: () => utils.organizationApiKeys.invalidate(),});- const tableData = useMemo<AsyncTableData<ApiKeySettingsTableRow[]>>(() => {- if (apiKeys.isLoading) return { status: "loading" };- if (apiKeys.isError) {- return { status: "error", error: "Failed to load API keys" };- }- return { status: "success", data: apiKeys.data ?? [] };- }, [apiKeys.data, apiKeys.isError, apiKeys.isLoading]);const deleteApiKey =scope === "project" ? deleteProjectApiKey : deleteOrganizationApiKey;@@ -86,6 +92,15 @@ export function ConnectedApiKeySettingsTable({>{({ openDialog: openDeleteDialog }) => (<ApiKeySettingsTable+ search={{ value: searchQuery, onChange: setSearchQuery }}+ gatewayKeyFilter={+ showGatewayKeyFilter+ ? {+ includeGatewayKeys,+ onIncludeGatewayKeysChange: setIncludeGatewayKeys,+ }+ : undefined+ }data={tableData}editNameAction={{hasAccess: hasWriteAccess,@@ -93,7 +108,7 @@ export function ConnectedApiKeySettingsTable({}}hasWriteAccess={hasWriteAccess}onDelete={openDeleteDialog}- noResultsMessage="None"+ noResultsMessage={noResultsMessage}/>)}</ConfirmationDialogController>@@ -213,3 +228,37 @@ function ApiKeyNameDialog({</Dialog>);}++function useApiKeySettingsTableData({+ entityId,+ scope,+ includeGatewayKeys,+ searchQuery,+}: {+ entityId: string;+ scope: ApiKeyScope;+ includeGatewayKeys: boolean;+ searchQuery: string;+}) {+ const projectApiKeys = api.projectApiKeys.byProjectId.useQuery(+ { projectId: entityId },+ { enabled: scope === "project" },+ );+ // keepPreviousData keeps the current rows visible while the gateway filter refetches.+ const organizationApiKeys = api.organizationApiKeys.byOrganizationId.useQuery(+ { orgId: entityId, includeGatewayKeys },+ { enabled: scope === "organization", placeholderData: keepPreviousData },+ );+ const apiKeys = scope === "project" ? projectApiKeys : organizationApiKeys;++ return useMemo<AsyncTableData<ApiKeySettingsTableRow[]>>(() => {+ if (apiKeys.isLoading) return { status: "loading" };+ if (apiKeys.isError) {+ return { status: "error", error: "Failed to load API keys" };+ }+ return {+ status: "success",+ data: filterApiKeysByName(apiKeys.data ?? [], searchQuery),+ };+ }, [apiKeys.data, apiKeys.isError, apiKeys.isLoading, searchQuery]);+}diff --git a/web/src/features/public-api/components/ApiKeySettingsTable/fns/filterApiKeysByName.ts b/web/src/features/public-api/components/ApiKeySettingsTable/fns/filterApiKeysByName.tsnew file mode 100644index 0000000000..f420cd21c3--- /dev/null+++ b/web/src/features/public-api/components/ApiKeySettingsTable/fns/filterApiKeysByName.ts@@ -0,0 +1,12 @@+import type { ApiKeySettingsTableRow } from "@/src/features/public-api/components/ApiKeySettingsTable/ApiKeySettingsTable";++export function filterApiKeysByName(+ apiKeys: ApiKeySettingsTableRow[],+ searchQuery: string,+) {+ const normalizedSearch = searchQuery.trim().toLowerCase();+ if (normalizedSearch.length === 0) return apiKeys;+ return apiKeys.filter((apiKey) =>+ apiKey.note?.toLowerCase().includes(normalizedSearch),+ );+}diff --git a/web/src/features/public-api/server/organizationApiKeyRouter.ts b/web/src/features/public-api/server/organizationApiKeyRouter.tsindex f42c4c51e1..8adcc451c7 100644--- a/web/src/features/public-api/server/organizationApiKeyRouter.ts+++ b/web/src/features/public-api/server/organizationApiKeyRouter.ts@@ -13,12 +13,14 @@ import { createApiKey } from "@langfuse/shared/src/server/auth/apiKeys";import { OrganizationId, SystemRoleId, UserId } from "@langfuse/shared/rbac";import { apiKeyCreationRoleSchema } from "@/src/features/public-api/server/apiKeyCreationRoleSchema";import { excludeGatewayApiKeys } from "@/src/features/ai-gateway/server/apiKey/excludeGatewayApiKeys";+import { invalidateGatewayResolveCacheForApiKey } from "@/src/features/ai-gateway/server";export const organizationApiKeysRouter = createTRPCRouter({byOrganizationId: protectedOrganizationProcedure.input(z.object({orgId: z.string(),+ includeGatewayKeys: z.boolean().optional(),}),).query(async ({ input, ctx }) => {@@ -33,7 +35,7 @@ export const organizationApiKeysRouter = createTRPCRouter({orgId: input.orgId,scope: "ORGANIZATION",isInAppAgentKey: false,- ...excludeGatewayApiKeys,+ ...(input.includeGatewayKeys ? {} : excludeGatewayApiKeys),},select: {id: true,@@ -195,10 +197,18 @@ export const organizationApiKeysRouter = createTRPCRouter({action: "delete",});- return await new ApiAuthService(ctx.prisma, redis).deleteApiKey(+ const deleted = await new ApiAuthService(ctx.prisma, redis).deleteApiKey(input.id,input.orgId,"ORGANIZATION",);+ // Any organization key whose role grants gateway:invoke may sit in the+ // gateway resolve cache, which would otherwise keep it usable until the TTL.+ if (deleted && apiKey.fastHashedSecretKey) {+ await invalidateGatewayResolveCacheForApiKey(+ apiKey.fastHashedSecretKey,+ );+ }+ return deleted;}),});