Skip to content

Commit

Permalink
Now returning resource in static resource function
Browse files Browse the repository at this point in the history
  • Loading branch information
overmode committed Jan 7, 2025
1 parent 45631a9 commit 3aa78fe
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 100 deletions.
48 changes: 39 additions & 9 deletions front/lib/api/assistant/feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,55 @@ export type AgentMessageFeedbackType = {
isConversationShared: boolean;
};

export type AgentMessageFeedbackWithMetadataType = AgentMessageFeedbackType & {
conversationId: string | null;
export type FeedbackUserInfo = {
userName: string;
userEmail: string;
userImageUrl: string | null;
};

export type FeedbackConversationInfo = {
conversationId: string | null;
};

export type AgentMessageFeedbackWithMetadataType = AgentMessageFeedbackType &
FeedbackConversationInfo &
FeedbackUserInfo;

export async function getConversationFeedbacksForUser(
auth: Authenticator,
conversation: ConversationType | ConversationWithoutContentType
): Promise<Result<AgentMessageFeedbackType[], ConversationError | Error>> {
) {
if (!canAccessConversation(auth, conversation)) {
return new Err(new ConversationError("conversation_access_restricted"));
}

const feedbacks =
const feedbacksRes =
await AgentMessageFeedbackResource.getConversationFeedbacksForUser(
auth,
conversation
);

return feedbacks;
const feedbacks = feedbacksRes
.filter(
(feedback): feedback is AgentMessageFeedbackResource => feedback !== null
)
.map((feedback) => {
const jsonFeedback = feedback.toJSON();
return {
id: jsonFeedback.id,
messageId: jsonFeedback.messageId,
agentMessageId: jsonFeedback.agentMessageId,
userId: jsonFeedback.userId,
thumbDirection: jsonFeedback.thumbDirection,
content: jsonFeedback.content,
isConversationShared: jsonFeedback.isConversationShared,
createdAt: jsonFeedback.createdAt,
agentConfigurationId: jsonFeedback.agentConfigurationId,
agentConfigurationVersion: jsonFeedback.agentConfigurationVersion,
} as AgentMessageFeedbackType;
});

return new Ok(feedbacks);
}

/**
Expand Down Expand Up @@ -204,20 +231,23 @@ export async function getAgentFeedbacks({
workspace: owner,
agentConfiguration,
paginationParams,
withMetadata,
}
);

const feedbacks = feedbacksRes.map((feedback) => feedback.toJSON());

if (!withMetadata) {
return new Ok(feedbacksRes);
return new Ok(feedbacks as AgentMessageFeedbackType[]);
}

const feedbacksWithHiddenConversationId = feedbacksRes.map((feedback) => ({
const feedbacksWithHiddenConversationId = feedbacks.map((feedback) => ({
...feedback,
// Redact the conversationId if user did not share the conversation.
conversationId: feedback.isConversationShared
? (feedback as AgentMessageFeedbackWithMetadataType).conversationId
: null,
}));
return new Ok(feedbacksWithHiddenConversationId);
return new Ok(
feedbacksWithHiddenConversationId as AgentMessageFeedbackWithMetadataType[]
);
}
170 changes: 84 additions & 86 deletions front/lib/resources/agent_message_feedback_resource.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
AgentConfigurationType,
AgentMessageType,
ConversationError,
ConversationType,
ConversationWithoutContentType,
LightAgentConfigurationType,
Expand All @@ -16,21 +15,18 @@ 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";
import {
AgentMessage,
AgentMessage as AgentMessageModel,
AgentMessageFeedback,
Conversation,
Message,
} from "@app/lib/models/assistant/conversation";
import { BaseResource } from "@app/lib/resources/base_resource";
import type { UserModel } from "@app/lib/resources/storage/models/user";
import type { ReadonlyAttributesType } from "@app/lib/resources/storage/types";
import { UserResource } from "@app/lib/resources/user_resource";

Expand All @@ -44,23 +40,44 @@ export interface AgentMessageFeedbackResource
export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedback> {
static model: ModelStatic<AgentMessageFeedback> = AgentMessageFeedback;

readonly message?: Attributes<Message>;
readonly user?: Attributes<UserModel>;
readonly conversationId?: string;

constructor(
model: ModelStatic<AgentMessageFeedback>,
blob: Attributes<AgentMessageFeedback>
blob: Attributes<AgentMessageFeedback>,
{
message,
user,
conversationId,
}: {
message?: Attributes<Message>;
user?: Attributes<UserModel>;
conversationId?: string;
} = {}
) {
super(AgentMessageFeedback, blob);

this.message = message;
this.user = user;
this.conversationId = conversationId;
}

static async makeNew(
blob: CreationAttributes<AgentMessageFeedback>
blob: CreationAttributes<AgentMessageFeedback>,
message?: Attributes<Message>,
user?: Attributes<UserModel>,
conversationId?: string
): Promise<AgentMessageFeedbackResource> {
const agentMessageFeedback = await AgentMessageFeedback.create({
...blob,
});

return new AgentMessageFeedbackResource(
AgentMessageFeedback,
agentMessageFeedback.get()
agentMessageFeedback.get(),
{ message, user, conversationId }
);
}

Expand Down Expand Up @@ -94,17 +111,13 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb

static async getAgentConfigurationFeedbacksByDescVersion({
workspace,
withMetadata,
agentConfiguration,
paginationParams,
}: {
workspace: WorkspaceType;
withMetadata: boolean;
agentConfiguration: LightAgentConfigurationType;
paginationParams: PaginationParams;
}): Promise<
(AgentMessageFeedbackType | AgentMessageFeedbackWithMetadataType)[]
> {
}) {
const where: WhereOptions<AgentMessageFeedback> = {
// Safety check: global models share ids across workspaces and some have had feedbacks.
workspaceId: workspace.id,
Expand Down Expand Up @@ -157,45 +170,17 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
limit: paginationParams.limit,
});

return (
agentMessageFeedback
// Typeguard needed because of TypeScript limitations
.filter(
(
feedback
): feedback is AgentMessageFeedback & {
agentMessage: {
message: Message & { conversation: Conversation };
};
} => !!feedback.agentMessage?.message?.conversation
)
.map((feedback) => {
return {
id: feedback.id,
messageId: feedback.agentMessage.message.sId,
agentMessageId: feedback.agentMessageId,
userId: feedback.userId,
thumbDirection: feedback.thumbDirection,
content: feedback.content
? feedback.content.replace(/\r?\n/g, "\\n")
: null,
isConversationShared: feedback.isConversationShared,
createdAt: feedback.createdAt,
agentConfigurationId: feedback.agentConfigurationId,
agentConfigurationVersion: feedback.agentConfigurationVersion,

...(withMetadata && {
// This field is sensitive, it allows accessing the conversation
conversationId: feedback.isConversationShared
? feedback.agentMessage.message.conversation.sId
: null,
userName: feedback.user.name,
userEmail: feedback.user.email,
userImageUrl: feedback.user.imageUrl,
}),
};
})
);
return agentMessageFeedback.map((feedback) => {
return new AgentMessageFeedbackResource(
AgentMessageFeedback,
feedback.get(),
{
message: feedback.agentMessage?.message,
user: feedback.user,
conversationId: feedback.agentMessage?.message?.conversation?.sId,
}
);
});
}

static async getFeedbackUsageDataForWorkspace({
Expand Down Expand Up @@ -231,24 +216,23 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
order: [["id", "ASC"]],
});

return agentMessageFeedback.map((feedback) => {
return {
id: feedback.id,
createdAt: feedback.createdAt,
userName: feedback.user.name,
userEmail: feedback.user.email,
agentConfigurationId: feedback.agentConfigurationId,
agentConfigurationVersion: feedback.agentConfigurationVersion,
thumb: feedback.thumbDirection,
content: feedback.content?.replace(/\r?\n/g, "\\n") || null,
};
});
return agentMessageFeedback
.filter((feedback) => !!feedback.user)
.map((feedback) => {
return new AgentMessageFeedbackResource(
AgentMessageFeedback,
feedback.get(),
{
user: feedback.user,
}
);
});
}

static async getConversationFeedbacksForUser(
auth: Authenticator,
conversation: ConversationType | ConversationWithoutContentType
): Promise<Result<AgentMessageFeedbackType[], ConversationError | Error>> {
) {
const user = auth.getNonNullableUser();

const feedbackForMessages = await Message.findAll({
Expand Down Expand Up @@ -276,36 +260,27 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
},
],
});

const feedbacksWithMessageId = feedbackForMessages
// typeguard needed because of TypeScript limitations
const feedbacks = feedbackForMessages
.filter(
(
message
): message is Message & {
agentMessage: { feedbacks: AgentMessageFeedbackResource[] };
agentMessage: { feedbacks: AgentMessageFeedback[] };
} =>
!!message.agentMessage?.feedbacks &&
message.agentMessage.feedbacks.length > 0
)
.map((message) => {
// Only one feedback can be associated with a message
const feedback = message.agentMessage.feedbacks[0];
return {
id: feedback.id,
messageId: message.sId,
agentMessageId: feedback.agentMessageId,
userId: feedback.userId,
thumbDirection: feedback.thumbDirection,
content: feedback.content,
isConversationShared: feedback.isConversationShared,
createdAt: feedback.createdAt,
agentConfigurationId: feedback.agentConfigurationId,
agentConfigurationVersion: feedback.agentConfigurationVersion,
} as AgentMessageFeedbackType;
const feedback = message.agentMessage?.feedbacks?.[0];
return new AgentMessageFeedbackResource(
AgentMessageFeedback,
feedback.get(),
{
message,
}
);
});

return new Ok(feedbacksWithMessageId);
return feedbacks;
}

static async getFeedbackWithConversationContext({
Expand Down Expand Up @@ -436,4 +411,27 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
isGlobalAgent,
});
}

toJSON() {
return {
id: this.id,
messageId: this.message?.sId,
agentMessageId: this.agentMessageId,
userId: this.userId,
thumbDirection: this.thumbDirection,
content: this.content ? this.content.replace(/\r?\n/g, "\\n") : null,
isConversationShared: this.isConversationShared,
createdAt: this.createdAt,
agentConfigurationId: this.agentConfigurationId,
agentConfigurationVersion: this.agentConfigurationVersion,
conversationId: this.conversationId,
...(this.user
? {
userName: this.user.name,
userEmail: this.user.email,
userImageUrl: this.user.imageUrl,
}
: {}),
};
}
}
1 change: 0 additions & 1 deletion front/lib/swr/assistants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ export function useAgentConfigurationFeedbacksByDescVersion({
const { data, error, mutate, size, setSize, isLoading, isValidating } =
useSWRInfiniteWithDefaults(
(pageIndex: number, previousPageData) => {
console.log("previousPageData", previousPageData);
if (!agentConfigurationId) {
return null;
}
Expand Down
Loading

0 comments on commit 3aa78fe

Please sign in to comment.