mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
* feat: add global teardown to remove test data and add registration/log-out to auth flow * refactor(models/Conversation): index user field and add JSDoc to deleteConvos * refactor: add user index to message schema and ensure user is saved to each Message * refactor: add user to each saveMessage call * fix: handle case where title is null in zod schema * feat(e2e): ensure messages are deleted on cleanUp * fix: set last convo for all endpoints on conversation update * fix: enable registration for CI env
64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
const crypto = require('crypto');
|
|
const { saveMessage } = require('../../models');
|
|
|
|
/**
|
|
* Sends error data in Server Sent Events format and ends the response.
|
|
* @param {object} res - The server response.
|
|
* @param {string} message - The error message.
|
|
*/
|
|
const handleError = (res, message) => {
|
|
res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`);
|
|
res.end();
|
|
};
|
|
|
|
/**
|
|
* 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'.
|
|
*/
|
|
const sendMessage = (res, message, event = 'message') => {
|
|
if (message.length === 0) {
|
|
return;
|
|
}
|
|
res.write(`event: ${event}\ndata: ${JSON.stringify(message)}\n\n`);
|
|
};
|
|
|
|
/**
|
|
* Processes an error with provided options, saves the error message and sends a corresponding SSE response
|
|
* @async
|
|
* @param {object} res - The server response.
|
|
* @param {object} options - The options for handling the error containing message properties.
|
|
* @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 errorMessage = {
|
|
sender,
|
|
messageId: messageId ?? crypto.randomUUID(),
|
|
conversationId,
|
|
parentMessageId,
|
|
unfinished: false,
|
|
cancelled: false,
|
|
error: true,
|
|
final: true,
|
|
text,
|
|
isCreatedByUser: false,
|
|
};
|
|
if (callback && typeof callback === 'function') {
|
|
await callback();
|
|
}
|
|
|
|
if (shouldSaveMessage) {
|
|
await saveMessage({ ...errorMessage, user });
|
|
}
|
|
|
|
handleError(res, errorMessage);
|
|
};
|
|
|
|
module.exports = {
|
|
handleError,
|
|
sendMessage,
|
|
sendError,
|
|
};
|