Skip to content

Commit

Permalink
Client-side conversation participants (#1935)
Browse files Browse the repository at this point in the history
  • Loading branch information
PopDaph authored Oct 3, 2023
1 parent e690e55 commit 88d6133
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Avatar } from "@dust-tt/sparkle";
import React from "react";

import { ConversationType } from "@app/types/assistant/conversation";

export function ConversationParticipants({
conversation,
}: {
conversation: ConversationType;
}) {
type UserParticipant = {
username: string;
fullName: string | null;
pictureUrl: string | null;
};
type AgentParticipant = {
configurationId: string;
name: string;
pictureUrl: string;
};
const userParticipantsMap = new Map<string, UserParticipant>();
const agentParticipantsMap = new Map<string, AgentParticipant>();
conversation.content.map((messages) => {
messages.map((m) => {
if (m.type === "user_message") {
const key = `${m.context.username}-${m.context.profilePictureUrl}`;
if (!userParticipantsMap.has(key)) {
userParticipantsMap.set(key, {
username: m.context.username,
fullName: m.context.fullName,
pictureUrl: m.context.profilePictureUrl,
});
}
} else if (m.type === "agent_message") {
const key = `${m.configuration.sId}`;
if (!agentParticipantsMap.has(key)) {
agentParticipantsMap.set(key, {
configurationId: m.configuration.sId,
name: m.configuration.name,
pictureUrl: m.configuration.pictureUrl,
});
}
}
});
});
const userParticipants = Array.from(userParticipantsMap.values());
const agentParticipants = Array.from(agentParticipantsMap.values());

return (
<div className="flex gap-6">
<Avatar.Stack
size="sm"
nbMoreItems={
agentParticipants.length > 4 ? agentParticipants.length - 4 : 0
}
>
{agentParticipants.slice(0, 4).map((agent) => (
<Avatar
name={agent.name}
visual={agent.pictureUrl}
size="md"
key={agent.configurationId}
/>
))}
</Avatar.Stack>
<Avatar.Stack
size="sm"
nbMoreItems={
userParticipants.length > 4 ? userParticipants.length - 4 : 0
}
>
{userParticipants.slice(0, 4).map((user, i) => (
<Avatar
name={user.fullName || user.username}
visual={user.pictureUrl}
size="md"
key={i}
/>
))}
</Avatar.Stack>
</div>
);
}
44 changes: 4 additions & 40 deletions front/components/assistant/conversation/ConversationTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ArrowUpOnSquareIcon,
Avatar,
Button,
CheckIcon,
ClipboardCheckIcon,
Expand All @@ -14,6 +13,7 @@ import {
import React, { MouseEvent, useRef, useState } from "react";
import { useSWRConfig } from "swr";

import { ConversationParticipants } from "@app/components/assistant/conversation/ConversationParticipants";
import { useConversation } from "@app/lib/swr";
import { WorkspaceType } from "@app/types/user";

Expand Down Expand Up @@ -161,46 +161,10 @@ export function ConversationTitle({
/>
)}
</div>
<div className="flex items-center gap-6">
<div className="hidden lg:flex">
<Avatar.Stack
size="sm"
nbMoreItems={
conversation.participants.agents.length > 4
? conversation.participants.agents.length - 4
: 0
}
>
{conversation.participants.agents.slice(0, 4).map((agent) => (
<Avatar
name={agent.name}
visual={agent.pictureUrl}
size="md"
key={agent.configurationId}
/>
))}
</Avatar.Stack>
</div>
<div className="hidden lg:flex">
<Avatar.Stack
size="sm"
nbMoreItems={
conversation.participants.users.length > 4
? conversation.participants.users.length - 4
: 0
}
>
{conversation.participants.users.slice(0, 4).map((user, i) => (
<Avatar
name={user.fullName || user.username}
visual={user.pictureUrl}
size="md"
key={i}
/>
))}
</Avatar.Stack>
<div className="flex items-center">
<div className="hidden pr-6 lg:flex">
<ConversationParticipants conversation={conversation} />
</div>

<Button.List>
<div className="hidden lg:flex">
{onDelete && (
Expand Down
25 changes: 0 additions & 25 deletions front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export async function createConversation(
title: conversation.title,
visibility: conversation.visibility,
content: [],
participants: { users: [], agents: [] },
};
}

Expand Down Expand Up @@ -401,23 +400,11 @@ export async function getConversation(
],
});

const userParticipants = new Map();
const agentParticipants = new Map();

const render = await Promise.all(
messages.map((message) => {
return (async () => {
if (message.userMessage) {
const m = await renderUserMessage(message, message.userMessage);

const key = `${m.context.username}-${m.context.profilePictureUrl}`;
if (!userParticipants.has(key)) {
userParticipants.set(key, {
username: m.context.username,
fullName: m.context.fullName,
pictureUrl: m.context.profilePictureUrl,
});
}
return { m, rank: message.rank, version: message.version };
}
if (message.agentMessage) {
Expand All @@ -426,14 +413,6 @@ export async function getConversation(
agentMessage: message.agentMessage,
messages,
});
const configurationId = m.configuration.sId;
if (!agentParticipants.has(configurationId)) {
agentParticipants.set(configurationId, {
configurationId: configurationId,
name: m.configuration.name,
pictureUrl: m.configuration.pictureUrl,
});
}
return { m, rank: message.rank, version: message.version };
}
throw new Error("Unreachable: message must be either user or agent");
Expand Down Expand Up @@ -468,10 +447,6 @@ export async function getConversation(
title: conversation.title,
visibility: conversation.visibility,
content,
participants: {
users: Array.from(userParticipants.values()),
agents: Array.from(agentParticipants.values()),
},
};
}

Expand Down
4 changes: 0 additions & 4 deletions front/types/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ export type ConversationType = {
title: string | null;
visibility: ConversationVisibility;
content: (UserMessageType[] | AgentMessageType[])[];
participants: {
users: UserParticipant[];
agents: AgentParticipant[];
};
};

export type UserParticipant = {
Expand Down

0 comments on commit 88d6133

Please sign in to comment.