🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(ai-gateway): sort gateway keys by created, name, and expiration (#19571)
Tobias Wochinger7b6e8936cfcb
diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.tsindex 94ae6cd659..2ce83f3a8f 100644--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts@@ -1535,6 +1535,34 @@ describe("AI gateway control plane", () => {).toEqual(new Set(["First", "Second", "Third"]));});+ it("sorts gateway keys by expiration with never-expiring keys as the latest", async () => {+ const { caller, org } = await prepare();+ const day = 24 * 60 * 60 * 1000;++ for (const [name, expiresAt] of [+ ["Never", null],+ ["Later", new Date(Date.now() + 30 * day)],+ ["Sooner", new Date(Date.now() + day)],+ ] as const) {+ await caller.aiGateway.createApiKey({+ orgId: org.id,+ name,+ metadata: {},+ expiresAt,+ });+ }++ const listNames = async (order: "ASC" | "DESC") =>+ (+ await caller.aiGateway.listApiKeys({+ orgId: org.id,+ orderBy: { column: "expiresAt", order },+ })+ ).data.map(({ apiKey }) => apiKey.note);+ expect(await listNames("ASC")).toEqual(["Sooner", "Later", "Never"]);+ expect(await listNames("DESC")).toEqual(["Never", "Later", "Sooner"]);+ });+it("rejects gateway page limits above 100", async () => {const { caller, org } = await prepare();diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsxindex 87b8de99ad..931ee48d98 100644--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsx+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/ConnectedGatewayApiKeysTable.tsx@@ -1,4 +1,5 @@import { useState } from "react";+import type { OrderByState } from "@langfuse/shared";import { Alert } from "@/src/components/design-system/Alert/Alert";import { Button } from "@/src/components/ui/button";@@ -8,6 +9,7 @@ import { TeamInviteDialogController } from "@/src/features/ai-gateway/componentsimport { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import { usePaginationState } from "@/src/hooks/usePaginationState";+import { useOrderByState } from "@/src/features/orderBy";import { useGatewayApiKeysTableData } from "@/src/features/ai-gateway/hooks/useGatewayApiKeysTableData";import { api } from "@/src/utils/api";import { GatewayApiKeysTable } from "./GatewayApiKeysTable";@@ -25,9 +27,11 @@ export function ConnectedGatewayApiKeysTable({limit: "pageSize",});const [searchQuery, setSearchQuery] = useState("");+ const [orderBy, setOrderBy] = useOrderByState();const { apiKeysQuery, rows, lastUsed } = useGatewayApiKeysTableData({organizationId,searchQuery,+ orderBy,pageIndex: paginationState.pageIndex,pageSize: paginationState.pageSize,});@@ -40,6 +44,11 @@ export function ConnectedGatewayApiKeysTable({setPaginationState({ ...paginationState, pageIndex: 0 });};+ const handleOrderByChange = (value: OrderByState) => {+ setOrderBy(value);+ setPaginationState({ ...paginationState, pageIndex: 0 });+ };+if (apiKeysQuery.isError) {return (<div className="flex flex-col gap-4">@@ -100,6 +109,8 @@ export function ConnectedGatewayApiKeysTable({: { status: "success", data: rows }}lastUsed={lastUsed}+ orderBy={orderBy}+ setOrderBy={handleOrderByChange}pagination={{mode: "offset",totalCount: apiKeysQuery.data?.totalCount ?? null,diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsxindex 74090acbe7..21eb5d3dae 100644--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsx+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.tsx@@ -10,7 +10,10 @@ import { createUserTableColumn } from "@/src/components/design-system/table/coluimport { createTableColumn } from "@/src/components/design-system/table/columns/utils/createTableColumn";import { Skeleton } from "@/src/components/ui/skeleton";import { buildLocalIsoDatePresentation } from "@/src/utils/dates";-import type { AsyncTableData } from "@/src/components/design-system/table/Table";+import type {+ AsyncTableData,+ TableProps,+} from "@/src/components/design-system/table/Table";import type { LangfuseColumnDef } from "@/src/components/table/types";import { isApiKeyExpired } from "@/src/features/apiKey";import { GATEWAY_API_KEY_LAST_USED_WINDOW_DAYS } from "@/src/features/ai-gateway/constants/gatewayApiKeyLastUsed";@@ -52,7 +55,9 @@ export function GatewayApiKeysTable({onRevoke,pagination,search,-}: {+ orderBy,+ setOrderBy,+}: Pick<TableProps<GatewayApiKey>, "orderBy" | "setOrderBy"> & {data: AsyncTableData<GatewayApiKey[]>;lastUsed: GatewayApiKeysLastUsedState;createAction: () => void;@@ -72,6 +77,7 @@ export function GatewayApiKeysTable({id: "createdAt",header: "Created",enableResizing: false,+ enableSorting: true,}),createTextTableColumn<GatewayApiKey>({accessorFn: (row) => row.apiKey.displaySecretKey,@@ -86,6 +92,7 @@ export function GatewayApiKeysTable({header: "Name",nullValue: "-",enableResizing: false,+ enableSorting: true,}),createUserTableColumn<GatewayApiKey>({accessorFn: (row) => row.apiKey.user,@@ -102,6 +109,7 @@ export function GatewayApiKeysTable({header: "Expiration",hideBelowMd: true,enableResizing: false,+ enableSorting: true,emptyValue: "No expiration",getPrefix: (expiresAt) =>isApiKeyExpired(expiresAt) ? "Expired on " : null,@@ -148,6 +156,8 @@ export function GatewayApiKeysTable({tableName="gatewayApiKeys"columns={columns}data={data}+ orderBy={orderBy}+ setOrderBy={setOrderBy}actions={(row) => [{id: "revoke",diff --git a/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.ts b/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.tsindex bb2527cad0..bdc8bc0f80 100644--- a/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.ts+++ b/web/src/features/ai-gateway/hooks/useGatewayApiKeysTableData.ts@@ -1,3 +1,4 @@+import type { OrderByState } from "@langfuse/shared";import { keepPreviousData } from "@tanstack/react-query";import { joinTableCoreAndMetrics } from "@/src/components/table/utils/joinTableCoreAndMetrics";@@ -11,16 +12,24 @@ import { api } from "@/src/utils/api";export function useGatewayApiKeysTableData({organizationId,searchQuery,+ orderBy,pageIndex,pageSize,}: {organizationId: string;searchQuery: string;+ orderBy: OrderByState;pageIndex: number;pageSize: number;}) {const apiKeysQuery = api.aiGateway.listApiKeys.useQuery(- { orgId: organizationId, searchQuery, page: pageIndex, limit: pageSize },+ {+ orgId: organizationId,+ searchQuery,+ orderBy,+ page: pageIndex,+ limit: pageSize,+ },{ placeholderData: keepPreviousData },);const lastUsedQuery = api.aiGateway.getApiKeysLastUsed.useQuery(diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.tsindex 09c7706c2d..5440825eb1 100644--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts@@ -1,4 +1,5 @@-import type { PrismaClient } from "@langfuse/shared/src/db";+import type { OrderByState } from "@langfuse/shared";+import type { Prisma, PrismaClient } from "@langfuse/shared/src/db";import { escapeSqlLikePattern } from "@langfuse/shared/src/server";const GATEWAY_API_KEY_SELECT = {@@ -25,6 +26,7 @@ export class GatewayApiKeyRepository {organizationId: string;ownerUserId?: string;searchQuery?: string;+ orderBy?: OrderByState;page: number;limit: number;}) {@@ -37,7 +39,7 @@ export class GatewayApiKeyRepository {const findPage = this.prisma.gatewayApiKeyAssociation.findMany({where,select: GATEWAY_API_KEY_SELECT,- orderBy: [{ apiKey: { createdAt: "asc" } }, { apiKeyId: "asc" }],+ orderBy: [gatewayApiKeyOrderBy(params.orderBy), { apiKeyId: "asc" }],take: params.limit,skip: params.page * params.limit,});@@ -103,3 +105,27 @@ function gatewayApiKeySearch(searchQuery: string) {],};}++/**+ * gatewayApiKeyOrderBy maps a gateway keys table sort to the key list order; other columns keep the default oldest-first order.+ * A key without expiration never expires, so it sorts after every expiration date ascending and before them descending.+ */+function gatewayApiKeyOrderBy(+ orderBy: OrderByState | undefined,+): Prisma.GatewayApiKeyAssociationOrderByWithRelationInput {+ const sort = orderBy?.order === "DESC" ? "desc" : "asc";+ switch (orderBy?.column) {+ case "createdAt":+ return { apiKey: { createdAt: sort } };+ case "description":+ return { apiKey: { note: { sort, nulls: "last" } } };+ case "expiresAt":+ return {+ apiKey: {+ expiresAt: { sort, nulls: sort === "asc" ? "last" : "first" },+ },+ };+ default:+ return { apiKey: { createdAt: "asc" } };+ }+}diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.tsindex 3a091582c4..7e465a9390 100644--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts@@ -2,6 +2,7 @@ import {ForbiddenError,InternalServerError,LangfuseNotFoundError,+ type OrderByState,} from "@langfuse/shared";import { Prisma, type PrismaClient, type Role } from "@langfuse/shared/src/db";import {@@ -47,6 +48,7 @@ export class GatewayApiKeyService {organizationId: string;ownerUserId?: string;searchQuery?: string;+ orderBy?: OrderByState;page: number;limit: number;}) {diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.tsindex 26b5a9ac85..7c5a1ff970 100644--- a/web/src/features/ai-gateway/server/router.ts+++ b/web/src/features/ai-gateway/server/router.ts@@ -21,6 +21,7 @@ import {GatewayProvider,} from "@langfuse/shared/src/db";import { redis } from "@langfuse/shared/src/server";+import { orderBy } from "@langfuse/shared";import {GatewayApiKeyService,@@ -461,6 +462,7 @@ export const aiGatewayRouter = createTRPCRouter({.input(offsetPaginatedOrganizationInput.extend({searchQuery: z.string().trim().optional(),+ orderBy: orderBy.optional(),}),).query(async ({ input, ctx }) => {@@ -472,6 +474,7 @@ export const aiGatewayRouter = createTRPCRouter({organizationId: input.orgId,ownerUserId,searchQuery: input.searchQuery,+ orderBy: input.orderBy,page: input.page,limit: input.limit,});