From 14110681d6f5ee25338c4d56cff1eb50c29b2501 Mon Sep 17 00:00:00 2001 From: Harish Mohan Raj Date: Mon, 27 Nov 2023 11:46:32 +0530 Subject: [PATCH] Add functionality to respond to agent questions --- src/client/components/ConversationWrapper.tsx | 18 ++++------ src/server/actions.ts | 34 ++++++++++++++++++- src/server/webSocket.js | 1 - 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/client/components/ConversationWrapper.tsx b/src/client/components/ConversationWrapper.tsx index be5bea6..4b34872 100644 --- a/src/client/components/ConversationWrapper.tsx +++ b/src/client/components/ConversationWrapper.tsx @@ -89,31 +89,27 @@ export default function ConversationWrapper() { try { // 1. add new conversation to table const payload = { + // @ts-ignore conversation_id: conversations.id, - conversations: [ - // Todo: remove the below ignore comment - // @ts-ignore - ...conversations.conversation, - ...[{ role: "user", content: userQuery }], - ], + conversations: [{ role: "user", content: userQuery }], }; - await updateConversation(payload); + const updatedConversation = await updateConversation(payload); // 2. call backend python server to get agent response setIsLoading(true); const response = await getAgentResponse({ - message: payload.conversations, + message: updatedConversation["conversation"], conv_id: payload.conversation_id, + // @ts-ignore is_answer_to_agent_question: conversations.status === "pause", }); // 3. add agent response as new conversation in the table const openAIResponse = { + // @ts-ignore conversation_id: conversations.id, conversations: [ - ...payload.conversations, - // Todo: remove the below ignore comment // @ts-ignore - ...[{ role: "assistant", content: response.content }], + { role: "assistant", content: response.content }, ], // @ts-ignore ...(response.team_status && { status: response.team_status }), diff --git a/src/server/actions.ts b/src/server/actions.ts index 8718760..730f6b1 100644 --- a/src/server/actions.ts +++ b/src/server/actions.ts @@ -204,6 +204,21 @@ type UpdateConversationPayload = { status?: string; }; +type ConversationItem = { + role: string; + content: string; +}; + +function convertConversationList( + currentConversation: Conversation +): Array { + const conversationList: Array = Object.entries( + // @ts-ignore + currentConversation.conversation + ); + return conversationList.map((item) => item[1]); +} + export const updateConversation: UpdateConversation< UpdateConversationPayload, Conversation @@ -211,10 +226,27 @@ export const updateConversation: UpdateConversation< if (!context.user) { throw new HttpError(401); } + const currentConversation = + await context.entities.Conversation.findFirstOrThrow({ + where: { id: args.conversation_id }, + }); + + let currentConversationList = convertConversationList(currentConversation); + const existingRole = + currentConversationList[currentConversationList.length - 1]["role"]; + const openAIResponseRole = args.conversations[0]["role"]; + + if (!(existingRole === "assistant" && existingRole === openAIResponseRole)) { + currentConversationList = [ + ...currentConversationList, + ...args.conversations, + ]; + } + return context.entities.Conversation.update({ where: { id: args.conversation_id }, data: { - conversation: args.conversations, + conversation: currentConversationList, ...(args.status && { status: args.status }), }, }); diff --git a/src/server/webSocket.js b/src/server/webSocket.js index d273948..48f8ac6 100644 --- a/src/server/webSocket.js +++ b/src/server/webSocket.js @@ -11,7 +11,6 @@ export const webSocketFn = (io, context) => { // Check for updates every 3 seconds const updateInterval = setInterval(async () => { - console.log("Checking database for inprogress tasks"); const conversations = await context.entities.Conversation.findMany({ where: { userId: socket.data.user.id, status: "inprogress" }, });