From f3a3363be5874f06fdc3250117ad01123e11e857 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 30 Dec 2024 18:48:07 +0100 Subject: [PATCH] Added code for paginating feedbacks --- front/lib/api/assistant/feedback.ts | 11 ++--- .../agent_message_feedback_resource.ts | 44 +++++++++++-------- .../agent_configurations/[aId]/feedbacks.ts | 32 ++++++++++---- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/front/lib/api/assistant/feedback.ts b/front/lib/api/assistant/feedback.ts index bff31b63e6ad..49873e8da27c 100644 --- a/front/lib/api/assistant/feedback.ts +++ b/front/lib/api/assistant/feedback.ts @@ -9,6 +9,7 @@ import { ConversationError, Err, Ok } from "@dust-tt/types"; import { getAgentConfiguration } from "@app/lib/api/assistant/configuration"; import { canAccessConversation } from "@app/lib/api/assistant/conversation/auth"; import type { AgentMessageFeedbackDirection } from "@app/lib/api/assistant/conversation/feedbacks"; +import type { PaginationParams } from "@app/lib/api/pagination"; import type { Authenticator } from "@app/lib/auth"; import { AgentMessageFeedbackResource } from "@app/lib/resources/agent_message_feedback_resource"; @@ -174,16 +175,12 @@ export async function getAgentFeedbacks({ auth, agentConfigurationId, withMetadata, - filters, + paginationParams, }: { auth: Authenticator; withMetadata: boolean; agentConfigurationId: string; - filters?: { - limit?: number; - olderThan?: Date; - earlierThan?: Date; - }; + paginationParams: PaginationParams; }): Promise< Result< (AgentMessageFeedbackType | AgentMessageFeedbackWithMetadataType)[], @@ -204,7 +201,7 @@ export async function getAgentFeedbacks({ const feedbacksRes = await AgentMessageFeedbackResource.fetch({ workspace: owner, agentConfiguration, - filters, + paginationParams, withMetadata, }); diff --git a/front/lib/resources/agent_message_feedback_resource.ts b/front/lib/resources/agent_message_feedback_resource.ts index 5269a905c8a7..98ce6ed540d9 100644 --- a/front/lib/resources/agent_message_feedback_resource.ts +++ b/front/lib/resources/agent_message_feedback_resource.ts @@ -11,7 +11,7 @@ import type { } from "@dust-tt/types"; import { GLOBAL_AGENTS_SID } from "@dust-tt/types"; import { Err, Ok } from "@dust-tt/types"; -import type { Attributes, ModelStatic } from "sequelize"; +import type { Attributes, ModelStatic, WhereOptions } from "sequelize"; import type { CreationAttributes, Transaction } from "sequelize"; import { Op } from "sequelize"; @@ -19,6 +19,7 @@ import type { AgentMessageFeedbackType, AgentMessageFeedbackWithMetadataType, } from "@app/lib/api/assistant/feedback"; +import type { PaginationParams } from "@app/lib/api/pagination"; import type { Authenticator } from "@app/lib/auth"; import { AgentConfiguration } from "@app/lib/models/assistant/agent"; import { AgentMessage } from "@app/lib/models/assistant/conversation"; @@ -94,29 +95,32 @@ export class AgentMessageFeedbackResource extends BaseResource { - const agentMessageFeedback = await AgentMessageFeedback.findAll({ - where: { - // IMPORTANT: Necessary for global models who share ids across workspaces. - workspaceId: workspace.id, - // These clauses are optional - ...(agentConfiguration - ? { - agentConfigurationId: agentConfiguration.sId.toString(), - } - : {}), - }, + const where: WhereOptions = { + // IMPORTANT: Necessary for global models who share ids across workspaces. + workspaceId: workspace.id, + }; + + if (paginationParams.lastValue) { + const op = paginationParams.orderDirection === "desc" ? Op.lt : Op.gt; + where[paginationParams.orderColumn as any] = { + [op]: paginationParams.lastValue, + }; + } + if (agentConfiguration) { + where.agentConfigurationId = agentConfiguration.sId.toString(); + } + const agentMessageFeedback = await AgentMessageFeedback.findAll({ + where, include: [ { model: AgentMessageModel, @@ -143,10 +147,12 @@ export class AgentMessageFeedbackResource extends BaseResource, auth: Authenticator ): Promise { - const { aId, limit } = req.query; + const { aId } = req.query; if (typeof aId !== "string") { return apiError(req, res, { @@ -42,17 +43,32 @@ async function handler( switch (req.method) { case "GET": + // asc id is equivalent to desc createdAt + const paginationRes = getPaginationParams(req, { + defaultLimit: 50, + defaultOrderColumn: "id", + defaultOrderDirection: "asc", + supportedOrderColumn: ["id"], + }); + if (paginationRes.isErr()) { + return apiError( + req, + res, + { + status_code: 400, + api_error: { + type: "invalid_pagination_parameters", + message: "Invalid pagination parameters", + }, + }, + paginationRes.error + ); + } const feedbacksRes = await getAgentFeedbacks({ auth, agentConfigurationId: aId, withMetadata: req.query.withMetadata === "true", - filters: { - // Limit the number of feedbacks to retrieve. - limit: limit ? parseInt(limit as string) : 50, - olderThan: req.query.olderThan - ? new Date(req.query.olderThan as string) - : undefined, - }, + paginationParams: paginationRes.value, }); if (feedbacksRes.isErr()) {