🧹fix: Handle Abort Message Edge Cases (#1462)

* chore: bump langchain to v0.0.213 from v0.0.186

* fix: handle abort edge cases:
- abort message server-side if response experienced error mid-generation
- attempt to recover message if aborting resulted in error
- if abortKey is not provided, use conversationId if it exists
- if headers were already sent, send an Event stream message
- issue warning for possible Google censor/filter

refactor(streamResponse): for `sendError`, allow passing overrides so that error can include partial generation, improve typing for `sendMessage`

* chore(MessageContent): remove eslint warning for unused `i`, rephrase unfinished message text

* fix(useSSE): avoid invoking cancelHandler if the abort response was 404

* chore(TMessage): remove unnecessary, unused legacy message property `submitting`

* chore(TMessage): remove unnecessary legacy message property `cancelled`

* chore(abortMiddleware): remove unused `errorText` property to avoid confusion
This commit is contained in:
Danny Avila 2023-12-30 12:34:23 -05:00 committed by GitHub
parent f19f5dca8e
commit 379e470e38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 529 additions and 207 deletions

View file

@ -1,5 +1,6 @@
const crypto = require('crypto');
const { saveMessage } = require('~/models/Message');
const { saveMessage, getMessages } = require('~/models/Message');
const { getConvo } = require('~/models/Conversation');
/**
* Sends error data in Server Sent Events format and ends the response.
@ -15,7 +16,7 @@ const handleError = (res, message) => {
* Sends message data in Server Sent Events format.
* @param {object} res - - The server response.
* @param {string} message - The message to be sent.
* @param {string} event - [Optional] The type of event. Default is 'message'.
* @param {'message' | 'error' | 'cancel'} event - [Optional] The type of event. Default is 'message'.
*/
const sendMessage = (res, message, event = 'message') => {
if (message.length === 0) {
@ -32,19 +33,27 @@ const sendMessage = (res, message, event = 'message') => {
* @param {function} callback - [Optional] The callback function to be executed.
*/
const sendError = async (res, options, callback) => {
const { user, sender, conversationId, messageId, parentMessageId, text, shouldSaveMessage } =
options;
const {
user,
sender,
conversationId,
messageId,
parentMessageId,
text,
shouldSaveMessage,
overrideProps = {},
} = options;
const errorMessage = {
sender,
messageId: messageId ?? crypto.randomUUID(),
conversationId,
parentMessageId,
unfinished: false,
cancelled: false,
error: true,
final: true,
text,
isCreatedByUser: false,
...overrideProps,
};
if (callback && typeof callback === 'function') {
await callback();
@ -54,6 +63,17 @@ const sendError = async (res, options, callback) => {
await saveMessage({ ...errorMessage, user });
}
if (!errorMessage.error) {
const requestMessage = { messageId: parentMessageId, conversationId };
const query = await getMessages(requestMessage);
return sendMessage(res, {
final: true,
requestMessage: query?.[0] ? query[0] : requestMessage,
responseMessage: errorMessage,
conversation: await getConvo(user, conversationId),
});
}
handleError(res, errorMessage);
};