mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
🧹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:
parent
f19f5dca8e
commit
379e470e38
19 changed files with 529 additions and 207 deletions
|
|
@ -7,17 +7,26 @@ const spendTokens = require('~/models/spendTokens');
|
|||
const { logger } = require('~/config');
|
||||
|
||||
async function abortMessage(req, res) {
|
||||
const { abortKey } = req.body;
|
||||
let { abortKey, conversationId } = req.body;
|
||||
|
||||
if (!abortKey && conversationId) {
|
||||
abortKey = conversationId;
|
||||
}
|
||||
|
||||
if (!abortControllers.has(abortKey) && !res.headersSent) {
|
||||
return res.status(404).send({ message: 'Request not found' });
|
||||
}
|
||||
|
||||
const { abortController } = abortControllers.get(abortKey);
|
||||
const ret = await abortController.abortCompletion();
|
||||
const finalEvent = await abortController.abortCompletion();
|
||||
logger.debug('[abortMessage] Aborted request', { abortKey });
|
||||
abortControllers.delete(abortKey);
|
||||
res.send(JSON.stringify(ret));
|
||||
|
||||
if (res.headersSent && finalEvent) {
|
||||
return sendMessage(res, finalEvent);
|
||||
}
|
||||
|
||||
res.send(JSON.stringify(finalEvent));
|
||||
}
|
||||
|
||||
const handleAbort = () => {
|
||||
|
|
@ -58,7 +67,6 @@ const createAbortController = (req, res, getAbortData) => {
|
|||
finish_reason: 'incomplete',
|
||||
model: endpointOption.modelOptions.model,
|
||||
unfinished: false,
|
||||
cancelled: true,
|
||||
error: false,
|
||||
isCreatedByUser: false,
|
||||
tokenCount: completionTokens,
|
||||
|
|
@ -84,10 +92,16 @@ const createAbortController = (req, res, getAbortData) => {
|
|||
};
|
||||
|
||||
const handleAbortError = async (res, req, error, data) => {
|
||||
logger.error('[handleAbortError] response error and aborting request', error);
|
||||
logger.error('[handleAbortError] AI response error; aborting request:', error);
|
||||
const { sender, conversationId, messageId, parentMessageId, partialText } = data;
|
||||
|
||||
const respondWithError = async () => {
|
||||
if (error.stack && error.stack.includes('google')) {
|
||||
logger.warn(
|
||||
`AI Response error for conversation ${conversationId} likely caused by Google censor/filter`,
|
||||
);
|
||||
}
|
||||
|
||||
const respondWithError = async (partialText) => {
|
||||
const options = {
|
||||
sender,
|
||||
messageId,
|
||||
|
|
@ -97,6 +111,15 @@ const handleAbortError = async (res, req, error, data) => {
|
|||
shouldSaveMessage: true,
|
||||
user: req.user.id,
|
||||
};
|
||||
|
||||
if (partialText) {
|
||||
options.overrideProps = {
|
||||
error: false,
|
||||
unfinished: true,
|
||||
text: partialText,
|
||||
};
|
||||
}
|
||||
|
||||
const callback = async () => {
|
||||
if (abortControllers.has(conversationId)) {
|
||||
const { abortController } = abortControllers.get(conversationId);
|
||||
|
|
@ -113,7 +136,7 @@ const handleAbortError = async (res, req, error, data) => {
|
|||
return await abortMessage(req, res);
|
||||
} catch (err) {
|
||||
logger.error('[handleAbortError] error while trying to abort message', err);
|
||||
return respondWithError();
|
||||
return respondWithError(partialText);
|
||||
}
|
||||
} else {
|
||||
return respondWithError();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue