Skip to content

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

feat(web): page-based pagination for gateway API keys (#18965)

Tobias Wochingerb76dc4f92896
diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
index e89ff35ee3..60a368a992 100644
--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
@@ -717,7 +717,7 @@ describe("AI gateway control plane", () => {
expect(remainingConnections.nextCursor).toBeNull();
});
- it("cursor-paginates gateway keys", async () => {
+ it("offset-paginates gateway keys with a total count", async () => {
const { caller, org } = await prepare();
for (const name of ["First", "Second", "Third"]) {
@@ -733,14 +733,21 @@ describe("AI gateway control plane", () => {
limit: 2,
});
expect(firstKeys.data).toHaveLength(2);
- expect(firstKeys.nextCursor).toBeTruthy();
+ expect(firstKeys.totalCount).toBe(3);
const remainingKeys = await caller.aiGateway.listApiKeys({
orgId: org.id,
+ page: 1,
limit: 2,
- cursor: firstKeys.nextCursor ?? undefined,
});
expect(remainingKeys.data).toHaveLength(1);
- expect(remainingKeys.nextCursor).toBeNull();
+ expect(remainingKeys.totalCount).toBe(3);
+ expect(
+ new Set(
+ [...firstKeys.data, ...remainingKeys.data].map(
+ ({ apiKey }) => apiKey.note,
+ ),
+ ),
+ ).toEqual(new Set(["First", "Second", "Third"]));
});
it("rejects gateway page limits above 100", async () => {
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.tsx
index d2f89cf5ff..5b5fe69852 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,8 +1,8 @@
-import { useState } from "react";
import { Alert } from "@/src/components/design-system/Alert/Alert";
import { Button } from "@/src/components/ui/button";
import { CreateGatewayApiKeyDialogController } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/components/CreateGatewayApiKeyDialogController/CreateGatewayApiKeyDialogController";
import { ConfirmationDialogController } from "@/src/components/design-system/ConfirmationDialogController/ConfirmationDialogController";
+import { usePaginationState } from "@/src/hooks/usePaginationState";
import { api } from "@/src/utils/api";
import { GatewayApiKeysTable } from "./GatewayApiKeysTable";
@@ -11,11 +11,15 @@ export function ConnectedGatewayApiKeysTable({
}: {
organizationId: string;
}) {
- const apiKeysQuery = api.aiGateway.listApiKeys.useInfiniteQuery(
- { orgId: organizationId, limit: 50 },
- { getNextPageParam: (lastPage) => lastPage.nextCursor ?? undefined },
- );
- const [pageIndex, setPageIndex] = useState(0);
+ const [paginationState, setPaginationState] = usePaginationState(0, 10, {
+ page: "pageIndex",
+ limit: "pageSize",
+ });
+ const apiKeysQuery = api.aiGateway.listApiKeys.useQuery({
+ orgId: organizationId,
+ page: paginationState.pageIndex,
+ limit: paginationState.pageSize,
+ });
const utils = api.useUtils();
const revoke = api.aiGateway.revokeApiKey.useMutation();
@@ -39,9 +43,6 @@ export function ConnectedGatewayApiKeysTable({
);
}
- const pages = apiKeysQuery.data?.pages ?? [];
- const currentPageIndex = Math.min(pageIndex, Math.max(0, pages.length - 1));
-
return (
<ConfirmationDialogController<string>
title="Revoke gateway API key"
@@ -53,6 +54,14 @@ export function ConnectedGatewayApiKeysTable({
onAfterDismiss={revoke.reset}
onConfirm={async (apiKeyId) => {
await revoke.mutateAsync({ orgId: organizationId, id: apiKeyId });
+ const remainingCount = (apiKeysQuery.data?.totalCount ?? 1) - 1;
+ const lastPageIndex = Math.max(
+ 0,
+ Math.ceil(remainingCount / paginationState.pageSize) - 1,
+ );
+ if (paginationState.pageIndex > lastPageIndex) {
+ setPaginationState({ ...paginationState, pageIndex: lastPageIndex });
+ }
await utils.aiGateway.listApiKeys.invalidate({ orgId: organizationId });
}}
>
@@ -66,29 +75,14 @@ export function ConnectedGatewayApiKeysTable({
? { status: "loading" }
: {
status: "success",
- data: pages[currentPageIndex]?.data ?? [],
+ data: apiKeysQuery.data.data,
}
}
pagination={{
- mode: "cursor",
- state: { pageIndex: currentPageIndex, pageSize: 50 },
- pageSizeOptions: [50],
- hasNextPage:
- currentPageIndex < pages.length - 1 ||
- Boolean(apiKeysQuery.hasNextPage),
- isLoadingNextPage: apiKeysQuery.isFetchingNextPage,
- onChange: (state) => {
- if (state.pageIndex < pages.length) {
- setPageIndex(state.pageIndex);
- return;
- }
- if (!apiKeysQuery.hasNextPage) return;
- apiKeysQuery.fetchNextPage().then((result) => {
- if (result.data?.pages[state.pageIndex]) {
- setPageIndex(state.pageIndex);
- }
- });
- },
+ mode: "offset",
+ totalCount: apiKeysQuery.data?.totalCount ?? null,
+ state: paginationState,
+ onChange: setPaginationState,
}}
createAction={openDialog}
onRevoke={openRevokeDialog}
diff --git a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx
index c56b585ef3..51165325ab 100644
--- a/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx
+++ b/web/src/features/ai-gateway/components/GatewayApiKeysPage/components/GatewayApiKeysTable/GatewayApiKeysTable.stories.tsx
@@ -50,10 +50,9 @@ const actions = {
createAction: onCreate,
onRevoke,
pagination: {
- mode: "cursor",
- state: { pageIndex: 0, pageSize: 50 },
- pageSizeOptions: [50],
- hasNextPage: false,
+ mode: "offset",
+ totalCount: 1,
+ state: { pageIndex: 0, pageSize: 10 },
onChange: onPageChange,
},
} satisfies Pick<
@@ -112,11 +111,15 @@ export const OverflowingMetadata = meta.story({
},
});
-export const ManyRowsWithMoreAvailable = meta.story({
+export const ManyRows = meta.story({
args: {
data: { status: "success", data: manyApiKeys },
...actions,
- pagination: { ...actions.pagination, hasNextPage: true },
+ pagination: {
+ ...actions.pagination,
+ totalCount: 75,
+ state: { pageIndex: 0, pageSize: 30 },
+ },
},
});
@@ -149,32 +152,12 @@ export const MissingDescription = meta.story({
},
});
-export const MoreAvailable = meta.story({
- args: {
- data: { status: "success", data: apiKeys },
- ...actions,
- pagination: { ...actions.pagination, hasNextPage: true },
- },
-});
-
-export const LoadingMore = meta.story({
- args: {
- data: { status: "success", data: apiKeys },
- ...actions,
- pagination: {
- ...actions.pagination,
- hasNextPage: true,
- isLoadingNextPage: true,
- },
- },
-});
-
export const NextPage = meta.story({
name: "(Test) Next page",
args: {
data: { status: "success", data: apiKeys },
...actions,
- pagination: { ...actions.pagination, hasNextPage: true },
+ pagination: { ...actions.pagination, totalCount: 25 },
},
play: async ({ canvas }) => {
onPageChange.mockClear();
@@ -183,7 +166,7 @@ export const NextPage = meta.story({
);
await expect(onPageChange).toHaveBeenCalledWith({
pageIndex: 1,
- pageSize: 50,
+ pageSize: 10,
});
},
});
diff --git a/web/src/features/ai-gateway/components/GatewayConnectionModelsPage/components/GatewayConnectionModelsTable/GatewayConnectionModelsTable.tsx b/web/src/features/ai-gateway/components/GatewayConnectionModelsPage/components/GatewayConnectionModelsTable/GatewayConnectionModelsTable.tsx
index 11a20725be..a3fce1b222 100644
--- a/web/src/features/ai-gateway/components/GatewayConnectionModelsPage/components/GatewayConnectionModelsTable/GatewayConnectionModelsTable.tsx
+++ b/web/src/features/ai-gateway/components/GatewayConnectionModelsPage/components/GatewayConnectionModelsTable/GatewayConnectionModelsTable.tsx
@@ -82,6 +82,8 @@ export function GatewayConnectionModelsTable({
minSize: 180,
isFlexWidth: true,
enableSorting: true,
+ headerClassName: "pl-2",
+ cellClassName: "pl-2",
cell: ({ row }) => (
<span className="block truncate" title={row.original.id}>
{row.original.id}
diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts
index f8c9462c67..52bbbb5357 100644
--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts
+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.servertest.ts
@@ -20,38 +20,34 @@ function gatewayApiKey(id: string) {
}
describe("GatewayApiKeyRepository pagination", () => {
- it("bounds API key pages with stable cursors", async () => {
+ it("returns the requested offset page with the organization's total count", async () => {
const findMany = vi
.fn()
- .mockResolvedValue([
- gatewayApiKey("key-1"),
- gatewayApiKey("key-2"),
- gatewayApiKey("key-3"),
- ]);
+ .mockResolvedValue([gatewayApiKey("key-3"), gatewayApiKey("key-4")]);
+ const count = vi.fn().mockResolvedValue(5);
const repository = new GatewayApiKeyRepository({
- gatewayApiKeyAssociation: { findMany },
+ gatewayApiKeyAssociation: { findMany, count },
} as unknown as PrismaClient);
await expect(
repository.listGatewayApiKeys({
organizationId: "org-1",
+ page: 1,
limit: 2,
}),
).resolves.toMatchObject({
- data: [{ apiKey: { id: "key-1" } }, { apiKey: { id: "key-2" } }],
- nextCursor: "key-2",
+ data: [{ apiKey: { id: "key-3" } }, { apiKey: { id: "key-4" } }],
+ totalCount: 5,
});
+ const where = { apiKey: { orgId: "org-1", scope: "ORGANIZATION" } };
expect(findMany).toHaveBeenCalledWith(
expect.objectContaining({
orderBy: [{ apiKey: { createdAt: "asc" } }, { apiKeyId: "asc" }],
- take: 3,
- where: {
- apiKey: {
- orgId: "org-1",
- scope: "ORGANIZATION",
- },
- },
+ take: 2,
+ skip: 2,
+ where,
}),
);
+ expect(count).toHaveBeenCalledWith({ where });
});
});
diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts
index 8a6172a35c..28e965960e 100644
--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts
+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyRepository.ts
@@ -5,16 +5,17 @@ export class GatewayApiKeyRepository {
async listGatewayApiKeys(params: {
organizationId: string;
- cursor?: string;
+ page: number;
limit: number;
}) {
- const rows = await this.prisma.gatewayApiKeyAssociation.findMany({
- where: {
- apiKey: {
- orgId: params.organizationId,
- scope: "ORGANIZATION",
- },
+ const where = {
+ apiKey: {
+ orgId: params.organizationId,
+ scope: "ORGANIZATION",
},
+ } as const;
+ const findPage = this.prisma.gatewayApiKeyAssociation.findMany({
+ where,
select: {
metadata: true,
apiKey: {
@@ -31,17 +32,14 @@ export class GatewayApiKeyRepository {
},
},
orderBy: [{ apiKey: { createdAt: "asc" } }, { apiKeyId: "asc" }],
- take: params.limit + 1,
- ...(params.cursor
- ? { cursor: { apiKeyId: params.cursor }, skip: 1 }
- : undefined),
+ take: params.limit,
+ skip: params.page * params.limit,
});
- const hasMore = rows.length > params.limit;
- const data = hasMore ? rows.slice(0, params.limit) : rows;
- return {
- data,
- nextCursor: hasMore ? (data.at(-1)?.apiKey.id ?? null) : null,
- };
+ const [data, totalCount] = await Promise.all([
+ findPage,
+ this.prisma.gatewayApiKeyAssociation.count({ where }),
+ ]);
+ return { data, totalCount };
}
getGatewayApiKey(params: { organizationId: string; apiKeyId: string }) {
diff --git a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts
index 62ba613aec..6c462aa51d 100644
--- a/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts
+++ b/web/src/features/ai-gateway/server/apiKey/gatewayApiKeyService.ts
@@ -23,7 +23,7 @@ export class GatewayApiKeyService {
this.repository = new GatewayApiKeyRepository(prisma);
}
- list(params: { organizationId: string; cursor?: string; limit: number }) {
+ list(params: { organizationId: string; page: number; limit: number }) {
return this.repository.listGatewayApiKeys(params);
}
diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.ts
index 548eaa8524..ef941974e4 100644
--- a/web/src/features/ai-gateway/server/router.ts
+++ b/web/src/features/ai-gateway/server/router.ts
@@ -34,9 +34,14 @@ import {
const organizationInput = z.object({ orgId: z.string() });
/** Naming rules live in `parseGatewayConnectionName` so the dialog can show them on the field. */
const connectionNameSchema = z.string().max(1_000);
+const pageLimitSchema = z.number().int().min(1).max(100).default(50);
const paginatedOrganizationInput = organizationInput.extend({
cursor: z.string().optional(),
- limit: z.number().int().min(1).max(100).default(50),
+ limit: pageLimitSchema,
+});
+const offsetPaginatedOrganizationInput = organizationInput.extend({
+ page: z.number().int().min(0).default(0),
+ limit: pageLimitSchema,
});
const connectionInput = organizationInput.extend({ id: z.string() });
const allowedModelsSchema = z
@@ -315,12 +320,12 @@ export const aiGatewayRouter = createTRPCRouter({
}),
listApiKeys: protectedOrganizationProcedure
- .input(paginatedOrganizationInput)
+ .input(offsetPaginatedOrganizationInput)
.query(async ({ input, ctx }) => {
requireGatewayAdmin({ session: ctx.session, orgId: input.orgId });
return new GatewayApiKeyService(ctx.prisma, redis).list({
organizationId: input.orgId,
- cursor: input.cursor,
+ page: input.page,
limit: input.limit,
});
}),