Skip to content

Commit

Permalink
Can get conversationsId in the feedback card
Browse files Browse the repository at this point in the history
  • Loading branch information
overmode committed Dec 6, 2024
1 parent 412c157 commit f48174c
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 22 deletions.
51 changes: 30 additions & 21 deletions front/components/assistant/AssistantDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import type {
AgentConfigurationScope,
LightAgentConfigurationType,
LightWorkspaceType,
WorkspaceType,
} from "@dust-tt/types";
import { ExternalLinkIcon } from "lucide-react";
Expand All @@ -33,6 +34,7 @@ import {
useAgentConfigurationHistory,
useUpdateAgentScope,
} from "@app/lib/swr/assistants";
import { useFeedbackConversation } from "@app/lib/swr/feedbacks";
import { useUserDetails } from "@app/lib/swr/user";
import { classNames, timeAgoFrom } from "@app/lib/utils";

Expand Down Expand Up @@ -103,19 +105,15 @@ export function AssistantDetails({
);

const TabsSection = () => (
<Tabs defaultValue="performance">
<Tabs defaultValue="feedback">
<TabsList>
<TabsTrigger value="info" label="Info" icon={InformationCircleIcon} />
<TabsTrigger
value="performance"
label="Performance"
icon={HandThumbUpIcon}
/>
<TabsTrigger value="feedback" label="Feedback" icon={HandThumbUpIcon} />
</TabsList>
<TabsContent value="info">
<InfoSection />
</TabsContent>
<TabsContent value="performance">
<TabsContent value="feedback">
<FeedbacksSection />
</TabsContent>
</Tabs>
Expand Down Expand Up @@ -191,7 +189,7 @@ export function AssistantDetails({
{!agentConfigurationFeedbacks ||
agentConfigurationFeedbacks.length === 0 ||
!assistantId ? (
<div>No feedbacks.</div>
<div className="mt-3 text-sm text-element-900">No feedbacks.</div>
) : (
<div className="mt-3">
<ConfigVersionHeader
Expand All @@ -215,7 +213,7 @@ export function AssistantDetails({
isLatestVersion={false}
/>
)}
<FeedbackCard feedback={feedback} />
<FeedbackCard owner={owner} feedback={feedback} />
</div>
))}
</div>
Expand Down Expand Up @@ -278,9 +276,18 @@ function ConfigVersionHeader({
);
}

function FeedbackCard({ feedback }: { feedback: AgentMessageFeedbackType }) {
function FeedbackCard({
owner,
feedback,
}: {
owner: LightWorkspaceType;
feedback: AgentMessageFeedbackType;
}) {
const { userDetails } = useUserDetails(feedback.userId);
const conversationUrl = `https://dust.tt/w/0ec9852c2f/assistant/${feedback.c}`;
const { conversationId } = useFeedbackConversation({
workspaceId: owner.sId,
feedbackId: feedback.id.toString(),
});

return (
<ContentMessage variant="slate" className="my-2">
Expand Down Expand Up @@ -319,16 +326,18 @@ function FeedbackCard({ feedback }: { feedback: AgentMessageFeedbackType }) {
)}
</div>
</div>
<div className="mt-2">
<Button
variant="outline"
size="xs"
href={`https://google.com`}
label="Conversation"
icon={ExternalLinkIcon}
target="_blank"
/>
</div>
{conversationId && (
<div className="mt-2">
<Button
variant="outline"
size="xs"
href={`https://dust.tt/w/${owner.sId}/assistant/${conversationId}`}
label="Conversation"
icon={ExternalLinkIcon}
target="_blank"
/>
</div>
)}
</ContentMessage>
);
}
2 changes: 2 additions & 0 deletions front/lib/models/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ AgentMessage.hasMany(AgentMessageFeedback, {
User.hasMany(AgentMessageFeedback, {
onDelete: "SET NULL",
});
AgentMessageFeedback.belongsTo(User);
AgentMessageFeedback.belongsTo(AgentMessage);

export class Message extends Model<
InferAttributes<Message>,
Expand Down
45 changes: 44 additions & 1 deletion front/lib/resources/agent_message_feedback_resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { Op } from "sequelize";
import type { AgentMessageFeedbackDirection } from "@app/lib/api/assistant/conversation/feedbacks";
import type { Authenticator } from "@app/lib/auth";
import type { AgentMessage } from "@app/lib/models/assistant/conversation";
import { AgentMessageFeedback } from "@app/lib/models/assistant/conversation";
import {
AgentMessage as AgentMessageModel,
AgentMessageFeedback,
Conversation,
Message,
} from "@app/lib/models/assistant/conversation";
import type { Workspace } from "@app/lib/models/workspace";
import { BaseResource } from "@app/lib/resources/base_resource";
import type { ReadonlyAttributesType } from "@app/lib/resources/storage/types";
Expand Down Expand Up @@ -140,6 +145,44 @@ export class AgentMessageFeedbackResource extends BaseResource<AgentMessageFeedb
return feedbacks;
}

static async fetchConversationId(
agentMessageFeedbackId: string
): Promise<string | null> {
const agentMessageFeedback = await AgentMessageFeedback.findByPk(
agentMessageFeedbackId,
{
// Feedback -> AgentMessage -> Message -> Conversation
include: [
{
model: AgentMessageModel,
attributes: ["id"],
include: [
{
model: Message,
as: "agentMessage",
attributes: ["id"],
include: [
{
model: Conversation,
as: "conversation",
attributes: ["sId"],
},
],
},
],
},
],
}
);

if (!agentMessageFeedback) {
return null;
}

// @ts-expect-error: sequelize cannot handle include easily
return agentMessageFeedback.agent_message.agentMessage.conversation.sId;
}

async fetchUser(): Promise<UserResource | null> {
const users = await UserResource.fetchByModelIds([this.userId]);
return users[0] ?? null;
Expand Down
26 changes: 26 additions & 0 deletions front/lib/swr/feedbacks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Fetcher } from "swr";

import { fetcher, useSWRWithDefaults } from "@app/lib/swr/swr";

export function useFeedbackConversation({
workspaceId,
feedbackId,
}: {
feedbackId: string;
workspaceId: string;
}) {
const feedbackFetcher: Fetcher<{
conversationId: string;
}> = fetcher;

const { data, error } = useSWRWithDefaults(
`/api/w/${workspaceId}/assistant/feedbacks/${feedbackId}/conversation`,
feedbackFetcher
);

return {
conversationId: data ? data.conversationId : null,
isLoading: !error && !data,
isError: error,
};
}
59 changes: 59 additions & 0 deletions front/pages/api/w/[wId]/assistant/feedbacks/[fId]/conversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { WithAPIErrorResponse } from "@dust-tt/types";
import type { NextApiRequest, NextApiResponse } from "next";

import { withSessionAuthenticationForWorkspace } from "@app/lib/api/auth_wrappers";
import type { Authenticator } from "@app/lib/auth";
import { AgentMessageFeedbackResource } from "@app/lib/resources/agent_message_feedback_resource";
import { apiError } from "@app/logger/withlogging";

export type GetAgentConfigurationsResponseBody = {
conversationId: string;
};

async function handler(
req: NextApiRequest,
res: NextApiResponse<
WithAPIErrorResponse<GetAgentConfigurationsResponseBody | void>
>,
auth: Authenticator
): Promise<void> {
switch (req.method) {
case "GET":
if (typeof req.query.fId !== "string" || req.query.fId === "") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: "Invalid query parameters, `fId` (string) is required.",
},
});
}

const conversationId =
await AgentMessageFeedbackResource.fetchConversationId(req.query.fId);

if (!conversationId) {
return apiError(req, res, {
status_code: 404,
api_error: {
type: "conversation_not_found",
message: `Conversation not found for feedback ${req.query.fId}`,
},
});
}

return res.status(200).json({
conversationId,
});
default:
return apiError(req, res, {
status_code: 405,
api_error: {
type: "method_not_supported_error",
message: "The method passed is not supported, GET is expected.",
},
});
}
}

export default withSessionAuthenticationForWorkspace(handler);

0 comments on commit f48174c

Please sign in to comment.