mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 21:08:17 +00:00
* fix: Mask Email Addresses and Drop Personal Identifiers from Logs * fix: Log User IDs in OpenID Admin Role and LDAP Domain Block Messages * fix: Mask Unicode Email Addresses in Log Redaction * test: Add Log Email Masking Scenarios * fix: Bound the Email Redaction Matcher and Handle Quoted Local Parts * fix: Mask Quoted and Special-Character Email Local Parts * fix: Redact Whole Email Tokens, Top-Level Log Fields and Metadata Keys * fix: Redact Top-Level Metadata Keys and Escaped Quotes in Email Local Parts * fix: Redact Truncated Emails, Address Literals and Request Paths in Logs * fix: Redact the Full Log String Before Truncating * fix: Preserve Colliding and Object Keys When Redacting Log Metadata * fix: Bound Log Redaction to a Window Past the Truncation Limit * fix: Redact the Truncation Window Without a Whitespace Cut * fix: Redact Serialized Quoted Addresses and Keep Key Collisions Linear * fix: Bound Every Log Redaction Path and the URL Credential Pattern * fix: Keep Long-Scheme Credential Redaction and Serialized Escaped Quotes * test: Cover Every Secret Shape Across the Log Truncation Cap * fix: Drop Unfinished Window Tokens, Mask Short TLDs and Redact Direct Error Stacks * fix: Treat Input End as a Token Boundary and Mask Parenthesized Identifiers * fix: Remove the Quoted Local Part Length Cap Without Quadratic Scans * fix: Drop an Unfinished Quoted Address at the Window Edge * fix: Check Quoted Locals Before Accepting a Window Delimiter * fix: Count Only Opening Quotes When Trimming the Window * fix: Track Unmatched Quoted Locals Instead of Counting Closing Quotes * fix: Lift Residual Email Caps, Keep Punctuated Credential URIs Masked and Move OBO Log Id to the API Package * fix: Mask Empty Quoted Locals and Tolerate Read-Only Log Fields * fix: Return a Sanitized Copy When a Log Field Cannot Be Replaced * fix: Treat a Failed Key Removal as a Failed Replacement and Guard Stack Writes * fix: Skip Heavy Error Graphs and Accept Escaped Backslashes in Serialized Locals * fix: Accept Lone Escaped Backslashes in Serialized Locals Without Rescans * fix: Compact Axios Request Info Before Redaction and Skip Heavy Splat Error Graphs * fix: Redact Address Keys on Nested Error Objects * fix: Materialize and Redact Enumerable Getters on Nested Errors * fix: Guard Throwing Top-Level Getters and Skip Heavy Fields Only on Error Records * fix: Skip Serialized Quotes When Trimming an Unfinished Address * fix: Bound Redaction With a Per-Record Node Budget and Accept JSON Control Escapes * fix: Match Credential URIs From the Separator So Scheme Length Is Unbounded * fix: Count Strings Against the Redaction Budget and Guard Nested Getters * fix: Rebuild the Log Record When a Top-Level Accessor Ignores Writes * fix: Rebuild the Log Record When a Stack Accessor Ignores Writes * fix: Guard the Initial Stack Read on Direct Error Logs * fix: Accept Escaped Solidus and Preserve the Outer Budget on Reentrant Logging * fix: Guard the Axios Marker and Error-Like Probes Against Throwing Getters * fix: Guard the Axios Marker on Nested Errors * fix: Replace Skipped Heavy Fields Up Front, Budget Top-Level Fields and Mask Encoded Emails * fix: Mask Encoded Address Keys and Guard Nested Axios Config Compaction * fix: Redact URL Objects in Compact Request Info * fix: Remove Encoded Email Caps and Strip Serializer Hooks From Clones * fix: Strip Serializer Hooks From Error Clones and Top-Level Records * fix: Neutralize Inherited Serializer Hooks on Log Records * fix: Guard the Serializer Hook Probe * fix: Replace Function Values in Log Records * fix: Mask Percent-Encoded Domain Separators in Logged URLs * fix: Bound the Encoded Email Domain at URL Delimiters * fix: Shadow Throwing Serializer Getters on Nested Errors * fix: Rebuild Accessor Messages and Mask Doubly Encoded Addresses * fix: Redact Multiply Encoded Address Keys * feat: Make the Log Redaction Node Budget Configurable * fix: Guard Message and Serializer Property Reads * fix: Contain Proxy Failures and Redact the Compact Request Method
256 lines
8.3 KiB
JavaScript
256 lines
8.3 KiB
JavaScript
const { logger } = require('@librechat/data-schemas');
|
|
const { isAssistantsEndpoint, ErrorTypes } = require('librechat-data-provider');
|
|
const {
|
|
isEnabled,
|
|
sendEvent,
|
|
countTokens,
|
|
isAbortError,
|
|
GenerationJobManager,
|
|
sanitizeMessageForTransmit,
|
|
buildAbortedResponseMetadata,
|
|
announceReply,
|
|
} = require('@librechat/api');
|
|
const { truncateText, smartTruncateText } = require('~/app/clients/prompts');
|
|
const clearPendingReq = require('~/cache/clearPendingReq');
|
|
const { sendError } = require('~/server/middleware/error');
|
|
const { abortRun } = require('./abortRun');
|
|
const db = require('~/models');
|
|
|
|
/**
|
|
* Abort an active message generation.
|
|
* Uses GenerationJobManager for all agent requests.
|
|
* Since streamId === conversationId, we can directly abort by conversationId.
|
|
*/
|
|
async function abortMessage(req, res) {
|
|
const { abortKey, endpoint } = req.body;
|
|
|
|
if (isAssistantsEndpoint(endpoint)) {
|
|
return await abortRun(req, res);
|
|
}
|
|
|
|
const conversationId = abortKey?.split(':')?.[0] ?? req.user.id;
|
|
const userId = req.user.id;
|
|
|
|
// Use GenerationJobManager to abort the job (streamId === conversationId)
|
|
const abortResult = await GenerationJobManager.abortJob(conversationId);
|
|
|
|
if (!abortResult.success) {
|
|
if (!res.headersSent) {
|
|
return res.status(204).send({ message: 'Request not found' });
|
|
}
|
|
return;
|
|
}
|
|
|
|
const { jobData, content, text } = abortResult;
|
|
|
|
const completionTokens = await countTokens(text);
|
|
|
|
const responseMessage = {
|
|
messageId: jobData?.responseMessageId,
|
|
parentMessageId: jobData?.userMessage?.messageId,
|
|
conversationId: jobData?.conversationId,
|
|
content,
|
|
text,
|
|
sender: jobData?.sender ?? 'AI',
|
|
finish_reason: 'incomplete',
|
|
endpoint: jobData?.endpoint,
|
|
iconURL: jobData?.iconURL,
|
|
model: jobData?.model,
|
|
unfinished: false,
|
|
error: false,
|
|
isCreatedByUser: false,
|
|
tokenCount: completionTokens,
|
|
/** The run publishes its calibration and fading tiers onto the job as it
|
|
* goes; a stopped response must carry them or the next turn re-derives its
|
|
* provider projection of history from scratch and loses the cached prefix.
|
|
* A job with none unsets what an earlier pause stored on this row. */
|
|
...(jobData != null && { contextMeta: jobData.contextMeta ?? null }),
|
|
};
|
|
|
|
/** Persist the usage/cost rollup + context breakdown for the stopped response
|
|
* so its branch/total cost and granular rows survive a reload, matching the
|
|
* normal completion path. */
|
|
const abortMetadata = buildAbortedResponseMetadata(jobData);
|
|
if (abortMetadata) {
|
|
responseMessage.metadata = abortMetadata;
|
|
}
|
|
|
|
/** The run that produced this response records its own usage on exit
|
|
* (`AgentClient` labels a stopped turn `'abort'`), so billing here would
|
|
* charge it a second time. This route only stops and persists. */
|
|
|
|
const savedMessage = await db.saveMessage(
|
|
{
|
|
userId: req?.user?.id,
|
|
isTemporary: req?.resolvedConversation?.isTemporary ?? req?.body?.isTemporary,
|
|
expiredAt: req?.resolvedConversation?.expiredAt,
|
|
interfaceConfig: req?.config?.interfaceConfig,
|
|
},
|
|
{ ...responseMessage, user: userId },
|
|
{ context: 'api/server/middleware/abortMiddleware.js' },
|
|
);
|
|
|
|
/* Mirrors the agents abort route: when Stop wins the terminal claim, the request controller
|
|
can return before its own save, making this direct write the only one that could reach
|
|
the unseen-reply indicator. The assistants path stamps inside `syncMessages`; this one
|
|
saves the message directly, so the stamp rides along here. Best-effort: the row is
|
|
already durable, and a missed stamp must not suppress the final event.
|
|
The temporary flag comes from the job: the client's abort request carries only the abort
|
|
key and endpoint, so the request body alone would stamp a stopped temporary chat.
|
|
An interrupt before the first real token still persists the unfinished assistant row, but
|
|
that row renders nothing: stamping it would raise a dot for a reply with nothing to read,
|
|
and opening the conversation could never clear it. */
|
|
await announceReply(db, {
|
|
userId,
|
|
conversationId: jobData?.conversationId,
|
|
reply: {
|
|
messageId: savedMessage?.messageId,
|
|
content,
|
|
text,
|
|
isTemporary: (jobData?.isTemporary ?? req?.body?.isTemporary) === true,
|
|
},
|
|
context: 'abortMessage',
|
|
});
|
|
|
|
// Get conversation for title
|
|
const conversation = await db.getConvo(userId, conversationId);
|
|
|
|
const finalEvent = {
|
|
title: conversation && !conversation.title ? null : conversation?.title || 'New Chat',
|
|
final: true,
|
|
conversation,
|
|
requestMessage: jobData?.userMessage
|
|
? sanitizeMessageForTransmit({
|
|
messageId: jobData.userMessage.messageId,
|
|
parentMessageId: jobData.userMessage.parentMessageId,
|
|
conversationId: jobData.userMessage.conversationId,
|
|
text: jobData.userMessage.text,
|
|
quotes: jobData.userMessage.quotes,
|
|
reasoningOverride: jobData.userMessage.reasoningOverride,
|
|
isCreatedByUser: true,
|
|
})
|
|
: null,
|
|
responseMessage,
|
|
};
|
|
|
|
logger.debug(`[abortMessage] ID: ${userId} | Aborted request: ${conversationId}`);
|
|
|
|
if (res.headersSent) {
|
|
return sendEvent(res, finalEvent);
|
|
}
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
res.send(JSON.stringify(finalEvent));
|
|
}
|
|
|
|
const handleAbort = function () {
|
|
return async function (req, res) {
|
|
try {
|
|
if (isEnabled(process.env.LIMIT_CONCURRENT_MESSAGES)) {
|
|
await clearPendingReq({ userId: req.user.id });
|
|
}
|
|
return await abortMessage(req, res);
|
|
} catch (err) {
|
|
logger.error('[abortMessage] handleAbort error', err);
|
|
}
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Handle abort errors during generation.
|
|
* @param {ServerResponse} res
|
|
* @param {ServerRequest} req
|
|
* @param {Error | unknown} error
|
|
* @param {Partial<TMessage> & { partialText?: string }} data
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const handleAbortError = async (res, req, error, data) => {
|
|
const { sender, conversationId, messageId, parentMessageId, userMessageId, partialText } = data;
|
|
|
|
if (error?.message?.includes('base64')) {
|
|
logger.error('[handleAbortError] Error in base64 encoding', {
|
|
...error,
|
|
stack: smartTruncateText(error?.stack, 1000),
|
|
message: truncateText(error.message, 350),
|
|
});
|
|
} else if (isAbortError(error)) {
|
|
logger.debug('[handleAbortError] AI response aborted by user', {
|
|
conversationId,
|
|
code: error?.code,
|
|
name: error?.name,
|
|
message: truncateText(error?.message ?? 'AbortError', 350),
|
|
});
|
|
} else {
|
|
logger.error('[handleAbortError] AI response error; aborting request:', error);
|
|
}
|
|
|
|
if (error?.stack && error.stack.includes('google')) {
|
|
logger.warn(
|
|
`AI Response error for conversation ${conversationId} likely caused by Google censor/filter`,
|
|
);
|
|
}
|
|
|
|
let errorText = error?.message?.includes('"type"')
|
|
? error.message
|
|
: 'An error occurred while processing your request. Please contact the Admin.';
|
|
|
|
if (error?.type === ErrorTypes.INVALID_REQUEST) {
|
|
errorText = `{"type":"${ErrorTypes.INVALID_REQUEST}"}`;
|
|
}
|
|
|
|
if (error?.message?.includes("does not support 'system'")) {
|
|
errorText = `{"type":"${ErrorTypes.NO_SYSTEM_MESSAGES}"}`;
|
|
}
|
|
|
|
/**
|
|
* @param {string} partialText
|
|
* @returns {Promise<void>}
|
|
*/
|
|
const respondWithError = async (partialText) => {
|
|
const endpointOption = req.body?.endpointOption;
|
|
let options = {
|
|
sender,
|
|
messageId,
|
|
conversationId,
|
|
parentMessageId,
|
|
text: errorText,
|
|
user: req.user.id,
|
|
spec: endpointOption?.spec,
|
|
iconURL: endpointOption?.iconURL,
|
|
modelLabel: endpointOption?.modelLabel,
|
|
shouldSaveMessage: userMessageId != null,
|
|
model: endpointOption?.modelOptions?.model || req.body?.model,
|
|
};
|
|
|
|
if (req.body?.agent_id) {
|
|
options.agent_id = req.body.agent_id;
|
|
}
|
|
|
|
if (partialText) {
|
|
options = {
|
|
...options,
|
|
error: false,
|
|
unfinished: true,
|
|
text: partialText,
|
|
};
|
|
}
|
|
|
|
await sendError(req, res, options);
|
|
};
|
|
|
|
if (partialText && partialText.length > 5) {
|
|
try {
|
|
return await abortMessage(req, res);
|
|
} catch (err) {
|
|
logger.error('[handleAbortError] error while trying to abort message', err);
|
|
return respondWithError(partialText);
|
|
}
|
|
} else {
|
|
return respondWithError();
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
handleAbort,
|
|
handleAbortError,
|
|
};
|