Skip to content

Commit

Permalink
API: posting new conversation: await message created (#2239)
Browse files Browse the repository at this point in the history
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](dust-tt/tasks#128) (plan limits -- do not create the conversation if the
message cannot be created because the message limit has been reached)
  • Loading branch information
philipperolet authored Oct 24, 2023
1 parent 81c1b7c commit a0426f9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 4 additions & 2 deletions front/pages/api/v1/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions front/pages/api/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -32,6 +33,7 @@ export type GetConversationsResponseBody = {
};
export type PostConversationsResponseBody = {
conversation: ConversationType;
message?: UserMessageType;
};

async function handler(
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit a0426f9

Please sign in to comment.