From a0426f9fa60f0fe7eec6842e0cae36e74e8e48f2 Mon Sep 17 00:00:00 2001 From: Philippe Rolet Date: Tue, 24 Oct 2023 15:40:49 +0200 Subject: [PATCH] API: posting new conversation: await message created (#2239) This PR ensures a conversation is not created if we cannot create its first message, and allows error handling before creating the conversation Namely, when creating a new conversation with a message, the message creation event (or error) is awaited. This is fast because we do not wait for the full message posting (with all redis events, etc.), only the creation event. In doing so, it aligns the behaviour of `front/pages/api/w/[wId]/assistant/conversations/index.ts` with the ones of `front/pages/api/v1/w/[wId]/assistant/conversations/index.ts`, `front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts` and `front/pages/api/v1/w/[wId]/assistant/conversations/[cId]/messages/index.ts`, who were already awaiting Required for [this task](https://github.com/dust-tt/tasks/issues/128) (plan limits -- do not create the conversation if the message cannot be created because the message limit has been reached) --- .../w/[wId]/assistant/conversations/index.ts | 6 ++++-- .../conversations/[cId]/messages/index.ts | 4 ++-- .../w/[wId]/assistant/conversations/index.ts | 20 ++++++++++++++----- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/front/pages/api/v1/w/[wId]/assistant/conversations/index.ts b/front/pages/api/v1/w/[wId]/assistant/conversations/index.ts index 3408e5ec16f9..aca1154eb4e2 100644 --- a/front/pages/api/v1/w/[wId]/assistant/conversations/index.ts +++ b/front/pages/api/v1/w/[wId]/assistant/conversations/index.ts @@ -138,8 +138,10 @@ async function handler( } if (message) { - // If a message was provided we do await for the message to be posted before returning the - // conversation along with the message. + // If a message was provided we do await for the message to be created + // before returning the conversation along with the message. + // PostUserMessageWithPubSub returns swiftly since it only waits for the + // initial message creation event (or error) const messageRes = await postUserMessageWithPubSub(auth, { conversation, content: message.content, diff --git a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts index 124c4f0fc6de..363bf2001018 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/[cId]/messages/index.ts @@ -109,8 +109,8 @@ async function handler( const { content, context, mentions } = bodyValidation.right; - // Not awaiting this promise on prupose. We want to answer "OK" to the client ASAP and process - // the events in the background. + /* postUserMessageWithPubSub returns swiftly since it only waits for the + initial message creation event (or error) */ const messageRes = await postUserMessageWithPubSub(auth, { conversation, content, diff --git a/front/pages/api/w/[wId]/assistant/conversations/index.ts b/front/pages/api/w/[wId]/assistant/conversations/index.ts index 79c1ebdee3e7..e3e84f62dad1 100644 --- a/front/pages/api/w/[wId]/assistant/conversations/index.ts +++ b/front/pages/api/w/[wId]/assistant/conversations/index.ts @@ -15,6 +15,7 @@ import { PostMessagesRequestBodySchema } from "@app/pages/api/w/[wId]/assistant/ import { ConversationType, ConversationWithoutContentType, + UserMessageType, } from "@app/types/assistant/conversation"; export const PostConversationsRequestBodySchema = t.type({ @@ -32,6 +33,7 @@ export type GetConversationsResponseBody = { }; export type PostConversationsResponseBody = { conversation: ConversationType; + message?: UserMessageType; }; async function handler( @@ -122,11 +124,14 @@ async function handler( visibility, }); + let newMessage: UserMessageType | null = null; + if (message) { - // Not awaiting this promise on purpose. We want to answer "OK" to the client ASAP and - // process the events in the background. So that the client gets updated as soon as - // possible. - void postUserMessageWithPubSub(auth, { + /* If a message was provided we do await for the message to be created + before returning the conversation along with the message. + PostUserMessageWithPubSub returns swiftly since it only waits for the + initial message creation event (or error) */ + const messageRes = await postUserMessageWithPubSub(auth, { conversation, content: message.content, mentions: message.mentions, @@ -138,9 +143,14 @@ async function handler( profilePictureUrl: message.context.profilePictureUrl, }, }); + if (messageRes.isErr()) { + return apiError(req, res, messageRes.error); + } + + newMessage = messageRes.value; } - res.status(200).json({ conversation }); + res.status(200).json({ conversation, message: newMessage ?? undefined }); return; default: