Skip to content

Commit

Permalink
Added code for paginating feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
overmode committed Dec 30, 2024
1 parent fc40537 commit f3a3363
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
11 changes: 4 additions & 7 deletions front/lib/api/assistant/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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)[],
Expand All @@ -204,7 +201,7 @@ export async function getAgentFeedbacks({
const feedbacksRes = await AgentMessageFeedbackResource.fetch({
workspace: owner,
agentConfiguration,
filters,
paginationParams,
withMetadata,
});

Expand Down
44 changes: 25 additions & 19 deletions front/lib/resources/agent_message_feedback_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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";

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";
Expand Down Expand Up @@ -94,29 +95,32 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
workspace,
withMetadata,
agentConfiguration,
filters,
paginationParams,
}: {
workspace: WorkspaceType;
withMetadata: boolean;
agentConfiguration?: AgentConfigurationType;
filters?: {
limit?: number;
};
paginationParams: PaginationParams;
}): Promise<
(AgentMessageFeedbackType | AgentMessageFeedbackWithMetadataType)[]
> {
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<AgentMessageFeedback> = {
// 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,
Expand All @@ -143,10 +147,12 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
},
],
order: [
["agentConfigurationVersion", "DESC"],
["createdAt", "DESC"],
[
paginationParams.orderColumn,
paginationParams.orderDirection === "desc" ? "DESC" : "ASC",
],
],
limit: filters?.limit,
limit: paginationParams.limit,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { apiErrorForConversation } from "@app/lib/api/assistant/conversation/hel
import type { AgentMessageFeedbackType } from "@app/lib/api/assistant/feedback";
import { getAgentFeedbacks } from "@app/lib/api/assistant/feedback";
import { withSessionAuthenticationForWorkspace } from "@app/lib/api/auth_wrappers";
import { getPaginationParams } from "@app/lib/api/pagination";
import type { Authenticator } from "@app/lib/auth";
import { apiError } from "@app/logger/withlogging";

Expand All @@ -16,7 +17,7 @@ async function handler(
>,
auth: Authenticator
): Promise<void> {
const { aId, limit } = req.query;
const { aId } = req.query;

if (typeof aId !== "string") {
return apiError(req, res, {
Expand All @@ -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()) {
Expand Down

0 comments on commit f3a3363

Please sign in to comment.