Skip to content

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

fix(comments): add pagination meta object to API response (#9902)

marliessophieb36cc5309dba
diff --git a/web/src/__tests__/async/comments-api.servertest.ts b/web/src/__tests__/async/comments-api.servertest.ts
index 9417689d77..0702fc1d2d 100644
--- a/web/src/__tests__/async/comments-api.servertest.ts
+++ b/web/src/__tests__/async/comments-api.servertest.ts
@@ -209,6 +209,12 @@ describe("GET /api/public/comments API Endpoint", () => {
"/api/public/comments",
);
expect(comments.body.data).toHaveLength(5);
+ expect(comments.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 5,
+ totalPages: 1,
+ });
});
it("should return comments for a specific objectId and objectType", async () => {
@@ -222,6 +228,12 @@ describe("GET /api/public/comments API Endpoint", () => {
);
expect(comments.body.data).toHaveLength(4);
+ expect(comments.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 4,
+ totalPages: 1,
+ });
expect(comments.body.data.map((comment) => comment.id)).toEqual([
"comment-2021-01-01",
"comment-2021-03-01",
@@ -242,6 +254,12 @@ describe("GET /api/public/comments API Endpoint", () => {
);
expect(comments.body.data).toHaveLength(2);
+ expect(comments.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 2,
+ totalPages: 1,
+ });
expect(comments.body.data.map((comment) => comment.id)).toEqual([
"comment-2021-01-01",
"comment-2021-03-01",
@@ -256,6 +274,12 @@ describe("GET /api/public/comments API Endpoint", () => {
);
expect(comments.body.data).toHaveLength(0);
+ expect(comments.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 0,
+ totalPages: 0,
+ });
});
it("should throw 400 error with descriptive error message if objectType is provided but invalid", async () => {
@@ -282,6 +306,12 @@ describe("GET /api/public/comments API Endpoint", () => {
"/api/public/comments?objectType=TRACE",
);
expect(comments.body.data).toHaveLength(4);
+ expect(comments.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 4,
+ totalPages: 1,
+ });
expect(comments.body.data.map((comment) => comment.id)).toEqual([
"comment-2021-01-01",
"comment-2021-03-01",
diff --git a/web/src/features/public-api/types/comments.ts b/web/src/features/public-api/types/comments.ts
index b234d77ac1..0ced2c71e9 100644
--- a/web/src/features/public-api/types/comments.ts
+++ b/web/src/features/public-api/types/comments.ts
@@ -1,6 +1,7 @@
import {
CommentObjectType,
CreateCommentData,
+ paginationMetaResponseZod,
publicApiPaginationZod,
} from "@langfuse/shared";
import { z } from "zod/v4";
@@ -54,6 +55,7 @@ export const GetCommentsV1Query = z
export const GetCommentsV1Response = z
.object({
data: z.array(APIComment),
+ meta: paginationMetaResponseZod,
})
.strict();
diff --git a/web/src/pages/api/public/comments/index.ts b/web/src/pages/api/public/comments/index.ts
index f9ad32a44c..b9f5acd36a 100644
--- a/web/src/pages/api/public/comments/index.ts
+++ b/web/src/pages/api/public/comments/index.ts
@@ -66,7 +66,24 @@ export default withMiddlewares({
skip: (page - 1) * limit,
});
- return { data: comments };
+ const totalItems = await prisma.comment.count({
+ where: {
+ projectId: auth.scope.projectId,
+ objectType: objectType ?? undefined,
+ objectId: objectId ?? undefined,
+ authorUserId: authorUserId ?? undefined,
+ },
+ });
+
+ return {
+ data: comments,
+ meta: {
+ page: query.page,
+ limit: query.limit,
+ totalItems,
+ totalPages: Math.ceil(totalItems / query.limit),
+ },
+ };
},
}),
});