From 9fca1c8559fc9308d3a8cc6402a7890e0bcc7663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Brabant?= Date: Tue, 10 Oct 2023 14:28:52 +0200 Subject: [PATCH] Fix frontend errors --- .../chat/chat-message/ChatMessage.svelte | 3 +-- frontend/src/lib/components/chat/chat.svelte | 24 +++++++++++++------ .../common/navigation/left-nav/LeftNav.svelte | 2 +- .../common/navigation/top-nav/TopNav.svelte | 2 +- .../conversations/fetch-conversation.ts | 7 ------ .../conversations/fetch-conversations.ts | 3 +-- .../frontend-connection.gateway.ts | 4 +--- 7 files changed, 22 insertions(+), 23 deletions(-) delete mode 100644 frontend/src/lib/usecases/conversations/fetch-conversation.ts diff --git a/frontend/src/lib/components/chat/chat-message/ChatMessage.svelte b/frontend/src/lib/components/chat/chat-message/ChatMessage.svelte index ff846a7..9e45b38 100644 --- a/frontend/src/lib/components/chat/chat-message/ChatMessage.svelte +++ b/frontend/src/lib/components/chat/chat-message/ChatMessage.svelte @@ -8,7 +8,6 @@ export let time: string; export let body: string; export let from: "USER" | "AGENT" | "SYSTEM"; - export let typewriter = false; let bubbleClass: string; @@ -25,7 +24,7 @@
{#if from === "USER"} - + {:else if from === "AGENT"} Ag {:else} diff --git a/frontend/src/lib/components/chat/chat.svelte b/frontend/src/lib/components/chat/chat.svelte index 992d503..5b275eb 100644 --- a/frontend/src/lib/components/chat/chat.svelte +++ b/frontend/src/lib/components/chat/chat.svelte @@ -11,6 +11,7 @@ import { afterNavigate, goto } from "$app/navigation"; import { agentStore } from "$lib/stores/agent"; import { authStore } from "$lib/stores/auth"; + import { v4 as uuidv4 } from "uuid"; let chatElement: HTMLDivElement; let chatInputElement: HTMLInputElement; @@ -24,8 +25,6 @@ $: messages = $chatStore.messages; $: conversationId = $conversationStore.selectedConversationId; - - let shouldRedirectToConversation = false; @@ -45,7 +44,9 @@ const actualConversationId = conversationId ?? generateConversationId(); + const timestamp = new Date().toISOString(); const payload = { + timestamp, data: { text: chatInputValue, conversationId: actualConversationId, @@ -55,10 +56,15 @@ chatElement.scrollTop = chatElement.scrollHeight; - addMessage(payload.data); + addMessage({ + id: uuidv4(), // this is a fake id, the real id will be set by the server + ...payload.data, + createdAt: timestamp + }); isWaitingForAnswer = true; $conversationStore.selectedConversationId = actualConversationId; + con.emit('chat-message', payload); chatInputValue = ""; @@ -103,7 +109,12 @@ } isWaitingForAnswer = false; - addMessage(payload.data); + addMessage({ + id: payload.data.messageId, + text: payload.data.text, + source: payload.data.source, + createdAt: payload.timestamp + }); } const listenToStreamedChatMessageToken = (payload: any) => { @@ -170,12 +181,11 @@ class="absolute top-0 bottom-[80px] left-0 right-0 overflow-y-scroll bg-background-primary dark:bg-background-secondary-dark"> {#if messages?.length > 0}
- {#each messages as message, idx} + {#each messages as message(message.id)}
{/each} diff --git a/frontend/src/lib/components/common/navigation/left-nav/LeftNav.svelte b/frontend/src/lib/components/common/navigation/left-nav/LeftNav.svelte index d74f1f8..ad68b5a 100644 --- a/frontend/src/lib/components/common/navigation/left-nav/LeftNav.svelte +++ b/frontend/src/lib/components/common/navigation/left-nav/LeftNav.svelte @@ -16,7 +16,7 @@ return; } - await fetchConversations(agent.id, member.id); + await fetchConversations(agent.id); }); diff --git a/frontend/src/lib/components/common/navigation/top-nav/TopNav.svelte b/frontend/src/lib/components/common/navigation/top-nav/TopNav.svelte index c5f24a3..b10bc8f 100644 --- a/frontend/src/lib/components/common/navigation/top-nav/TopNav.svelte +++ b/frontend/src/lib/components/common/navigation/top-nav/TopNav.svelte @@ -39,7 +39,7 @@ on:keydown={() => {}} tabindex="0" class="relative flex gap-4 border-l border-stroke-base dark:border-stroke-base-dark h-full items-center justify-center px-3 cursor-pointer"> - +
{member.email} {projectConfig.name} diff --git a/frontend/src/lib/usecases/conversations/fetch-conversation.ts b/frontend/src/lib/usecases/conversations/fetch-conversation.ts deleted file mode 100644 index 332188b..0000000 --- a/frontend/src/lib/usecases/conversations/fetch-conversation.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { AgentChatConversationsService } from "$lib/services/gen-api" - -export const fetchConversation = async (conversationId: string) => { - const conversationWithMessages = await AgentChatConversationsService.getConversationById({ conversationId }) - - return conversationWithMessages -} diff --git a/frontend/src/lib/usecases/conversations/fetch-conversations.ts b/frontend/src/lib/usecases/conversations/fetch-conversations.ts index b949f27..86a7680 100644 --- a/frontend/src/lib/usecases/conversations/fetch-conversations.ts +++ b/frontend/src/lib/usecases/conversations/fetch-conversations.ts @@ -3,9 +3,8 @@ import { AgentChatConversationsService } from "$lib/services/gen-api"; import { setConversationList } from "$lib/stores/conversation"; -export const fetchConversations = async (agentId: string, memberId: string): Promise => { +export const fetchConversations = async (agentId: string): Promise => { const conversations = await AgentChatConversationsService.getAllConversations({ - memberId, agentId }) diff --git a/server/src/frontend-connection/frontend-connection.gateway.ts b/server/src/frontend-connection/frontend-connection.gateway.ts index 109e4cd..91cceff 100644 --- a/server/src/frontend-connection/frontend-connection.gateway.ts +++ b/server/src/frontend-connection/frontend-connection.gateway.ts @@ -185,9 +185,6 @@ export class FrontendConnectionGateway ); } - // TODO: if the agent was never connected, assume it is the platform owner testing his app out. - // Send a link to the documentation on how to connect an agent. - await this.messagesService.createMessage({ conversationId: conversation.id, source: 'SYSTEM', @@ -200,6 +197,7 @@ export class FrontendConnectionGateway text, conversationId: conversation.id, source: 'SYSTEM', + messageId: message.id, }, message: 'Agent is offline', };