mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
* wip: Add Instructions component for agent configuration
* ✨ feat: Implement DropdownPopup for variable insertion in instructions
* refactor: Enhance variable handling by exporting specialVariables and updating Markdown components
* feat: Add special variable support for current date and user in Instructions component
* refactor: Update handleAddVariable to include localized label
* feat: replace special variables in instructions presets
* chore: update parameter type for user in getListAgents function
* refactor: integrate dayjs for date handling and move replaceSpecialVars function to data-provider
* feat: enhance replaceSpecialVars to include day number in current date format
* feat: integrate replaceSpecialVars for processing agent instructions
* feat: add support for current date & time in replaceSpecialVars function
* feat: add iso_datetime support in replaceSpecialVars function
* fix: enforce text parameter to be a required field in replaceSpecialVars function
* feat: add ISO datetime support in translation file
* fix: disable eslint warning for autoFocus in TextareaAutosize component
* feat: add VariablesDropdown component and integrate it into CreatePromptForm and PromptEditor; update translation for special variables
* fix: CategorySelector and related localizations
* fix: add z-index class to LanguageSTTDropdown for proper stacking context
* fix: add max-height and overflow styles to OGDialogContent in VariableDialog and PreviewPrompt components
* fix: update variable detection logic to exclude special variables and improve regex matching
* fix: improve accessibility text for actions menu in ChatGroupItem component
* fix: adjust max-width and height styles for dialog components and improve markdown rendering for light vs. dark, height/widths, etc.
* fix: remove commented-out code for better readability in PromptVariableGfm component
* fix: handle undefined input parameter in setParams function call
* fix: update variable label types to use TSpecialVarLabel for consistency
* fix: remove outdated information from special variables description in translation file
* fix: enhance unused i18next keys detection for special variable keys
* fix: update color classes for consistency/a11y in category and prompt variable components
* fix: update PromptVariableGfm component and special variable styles for consistency
* fix: improve variable highlighting logic in VariableForm component
* fix: update background color classes for consistency in VariableForm component
* fix: add missing ref parameter to Dialog component in OriginalDialog
* refactor: move navigate call for new conversation to after setConversation update
* refactor: move message query hook to client workspace; fix: handle edge case for navigation from finalHandler creating race condition for response message DB save
* chore: bump librechat-data-provider to 0.7.793
* ci: add unit tests for replaceSpecialVars function
* fix: implement getToolkitKey function for image_gen_oai toolkit filtering/including
* ci: enhance dayjs mock for consistent date/time values in tests
* fix: MCP stdio server fail to start when passing env property
* fix: use optional chaining for clientRef dereferencing in AskController and EditController
feat: add context to saveMessage call in streamResponse utility
* fix: only save error messages if the userMessageId was initialized
* refactor: add isNotAppendable check to disable inputs in ChatForm and useTextarea
* feat: enhance error handling in useEventHandlers and update conversation state in useNewConvo
* refactor: prepend underscore to conversationId in newConversation template
* feat: log aborted conversations with minimal messages and use consistent conversationId generation
---------
Co-authored-by: Olivier Schiavo <olivier.schiavo@wengo.com>
Co-authored-by: aka012 <aka012@neowiz.com>
Co-authored-by: jiasheng <jiashengguo@outlook.com>
131 lines
3.8 KiB
JavaScript
131 lines
3.8 KiB
JavaScript
const crypto = require('crypto');
|
|
const { parseConvo } = require('librechat-data-provider');
|
|
const { saveMessage, getMessages } = require('~/models/Message');
|
|
const { getConvo } = require('~/models/Conversation');
|
|
const { logger } = require('~/config');
|
|
|
|
/**
|
|
* 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 {Express.Response} res - - The server response.
|
|
* @param {string | Object} message - The message to be sent.
|
|
* @param {'message' | 'error' | 'cancel'} event - [Optional] The type of event. Default is 'message'.
|
|
*/
|
|
const sendMessage = (res, message, event = 'message') => {
|
|
if (typeof message === 'string' && 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} req - The request.
|
|
* @param {object} res - The response.
|
|
* @param {object} options - The options for handling the error containing message properties.
|
|
* @param {object} options.user - The user ID.
|
|
* @param {string} options.sender - The sender of the message.
|
|
* @param {string} options.conversationId - The conversation ID.
|
|
* @param {string} options.messageId - The message ID.
|
|
* @param {string} options.parentMessageId - The parent message ID.
|
|
* @param {string} options.text - The error message.
|
|
* @param {boolean} options.shouldSaveMessage - [Optional] Whether the message should be saved. Default is true.
|
|
* @param {function} callback - [Optional] The callback function to be executed.
|
|
*/
|
|
const sendError = async (req, res, options, callback) => {
|
|
const {
|
|
user,
|
|
sender,
|
|
conversationId,
|
|
messageId,
|
|
parentMessageId,
|
|
text,
|
|
shouldSaveMessage,
|
|
...rest
|
|
} = options;
|
|
const errorMessage = {
|
|
sender,
|
|
messageId: messageId ?? crypto.randomUUID(),
|
|
conversationId,
|
|
parentMessageId,
|
|
unfinished: false,
|
|
error: true,
|
|
final: true,
|
|
text,
|
|
isCreatedByUser: false,
|
|
...rest,
|
|
};
|
|
if (callback && typeof callback === 'function') {
|
|
await callback();
|
|
}
|
|
|
|
if (shouldSaveMessage) {
|
|
await saveMessage(
|
|
req,
|
|
{ ...errorMessage, user },
|
|
{
|
|
context: 'api/server/utils/streamResponse.js - sendError',
|
|
},
|
|
);
|
|
}
|
|
|
|
if (!errorMessage.error) {
|
|
const requestMessage = { messageId: parentMessageId, conversationId };
|
|
let query = [],
|
|
convo = {};
|
|
try {
|
|
query = await getMessages(requestMessage);
|
|
convo = await getConvo(user, conversationId);
|
|
} catch (err) {
|
|
logger.error('[sendError] Error retrieving conversation data:', err);
|
|
convo = parseConvo(errorMessage);
|
|
}
|
|
|
|
return sendMessage(res, {
|
|
final: true,
|
|
requestMessage: query?.[0] ? query[0] : requestMessage,
|
|
responseMessage: errorMessage,
|
|
conversation: convo,
|
|
});
|
|
}
|
|
|
|
handleError(res, errorMessage);
|
|
};
|
|
|
|
/**
|
|
* Sends the response based on whether headers have been sent or not.
|
|
* @param {Express.Request} req - The server response.
|
|
* @param {Express.Response} res - The server response.
|
|
* @param {Object} data - The data to be sent.
|
|
* @param {string} [errorMessage] - The error message, if any.
|
|
*/
|
|
const sendResponse = (req, res, data, errorMessage) => {
|
|
if (!res.headersSent) {
|
|
if (errorMessage) {
|
|
return res.status(500).json({ error: errorMessage });
|
|
}
|
|
return res.json(data);
|
|
}
|
|
|
|
if (errorMessage) {
|
|
return sendError(req, res, { ...data, text: errorMessage });
|
|
}
|
|
return sendMessage(res, data);
|
|
};
|
|
|
|
module.exports = {
|
|
sendResponse,
|
|
handleError,
|
|
sendMessage,
|
|
sendError,
|
|
};
|