mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* feat: add GOOGLE_MODELS env var * feat: add gemini vision support * refactor(GoogleClient): adjust clientOptions handling depending on model * fix(logger): fix redact logic and redact errors only * fix(GoogleClient): do not allow non-multiModal messages when gemini-pro-vision is selected * refactor(OpenAIClient): use `isVisionModel` client property to avoid calling validateVisionModel multiple times * refactor: better debug logging by correctly traversing, redacting sensitive info, and logging condensed versions of long values * refactor(GoogleClient): allow response errors to be thrown/caught above client handling so user receives meaningful error message debug orderedMessages, parentMessageId, and buildMessages result * refactor(AskController): use model from client.modelOptions.model when saving intermediate messages, which requires for the progress callback to be initialized after the client is initialized * feat(useSSE): revert to previous model if the model was auto-switched by backend due to message attachments * docs: update with google updates, notes about Gemini Pro Vision * fix: redis should not be initialized without USE_REDIS and increase max listeners to 20
160 lines
5 KiB
JavaScript
160 lines
5 KiB
JavaScript
const winston = require('winston');
|
|
const traverse = require('traverse');
|
|
const { klona } = require('klona/full');
|
|
|
|
const SPLAT_SYMBOL = Symbol.for('splat');
|
|
const MESSAGE_SYMBOL = Symbol.for('message');
|
|
|
|
const sensitiveKeys = [/^(sk-)[^\s]+/, /(Bearer )[^\s]+/, /(api-key:? )[^\s]+/, /(key=)[^\s]+/];
|
|
|
|
/**
|
|
* Determines if a given value string is sensitive and returns matching regex patterns.
|
|
*
|
|
* @param {string} valueStr - The value string to check.
|
|
* @returns {Array<RegExp>} An array of regex patterns that match the value string.
|
|
*/
|
|
function getMatchingSensitivePatterns(valueStr) {
|
|
if (valueStr) {
|
|
// Filter and return all regex patterns that match the value string
|
|
return sensitiveKeys.filter((regex) => regex.test(valueStr));
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* Redacts sensitive information from a console message.
|
|
*
|
|
* @param {string} str - The console message to be redacted.
|
|
* @returns {string} - The redacted console message.
|
|
*/
|
|
function redactMessage(str) {
|
|
const patterns = getMatchingSensitivePatterns(str);
|
|
|
|
if (patterns.length === 0) {
|
|
return str;
|
|
}
|
|
|
|
patterns.forEach((pattern) => {
|
|
str = str.replace(pattern, '$1[REDACTED]');
|
|
});
|
|
|
|
return str;
|
|
}
|
|
|
|
/**
|
|
* Redacts sensitive information from log messages if the log level is 'error'.
|
|
* Note: Intentionally mutates the object.
|
|
* @param {Object} info - The log information object.
|
|
* @returns {Object} - The modified log information object.
|
|
*/
|
|
const redactFormat = winston.format((info) => {
|
|
if (info.level === 'error') {
|
|
info.message = redactMessage(info.message);
|
|
if (info[MESSAGE_SYMBOL]) {
|
|
info[MESSAGE_SYMBOL] = redactMessage(info[MESSAGE_SYMBOL]);
|
|
}
|
|
}
|
|
return info;
|
|
});
|
|
|
|
/**
|
|
* Truncates long strings, especially base64 image data, within log messages.
|
|
*
|
|
* @param {any} value - The value to be inspected and potentially truncated.
|
|
* @param {number} [length] - The length at which to truncate the value. Default: 100.
|
|
* @returns {any} - The truncated or original value.
|
|
*/
|
|
const truncateLongStrings = (value, length = 100) => {
|
|
if (typeof value === 'string') {
|
|
return value.length > length ? value.substring(0, length) + '... [truncated]' : value;
|
|
}
|
|
|
|
return value;
|
|
};
|
|
|
|
/**
|
|
* An array mapping function that truncates long strings (objects converted to JSON strings).
|
|
* @param {any} item - The item to be condensed.
|
|
* @returns {any} - The condensed item.
|
|
*/
|
|
const condenseArray = (item) => {
|
|
if (typeof item === 'string') {
|
|
return truncateLongStrings(JSON.stringify(item));
|
|
} else if (typeof item === 'object') {
|
|
return truncateLongStrings(JSON.stringify(item));
|
|
}
|
|
return item;
|
|
};
|
|
|
|
/**
|
|
* Formats log messages for debugging purposes.
|
|
* - Truncates long strings within log messages.
|
|
* - Condenses arrays by truncating long strings and objects as strings within array items.
|
|
* - Redacts sensitive information from log messages if the log level is 'error'.
|
|
* - Converts log information object to a formatted string.
|
|
*
|
|
* @param {Object} options - The options for formatting log messages.
|
|
* @param {string} options.level - The log level.
|
|
* @param {string} options.message - The log message.
|
|
* @param {string} options.timestamp - The timestamp of the log message.
|
|
* @param {Object} options.metadata - Additional metadata associated with the log message.
|
|
* @returns {string} - The formatted log message.
|
|
*/
|
|
const debugTraverse = winston.format.printf(({ level, message, timestamp, ...metadata }) => {
|
|
let msg = `${timestamp} ${level}: ${truncateLongStrings(message?.trim(), 150)}`;
|
|
|
|
if (level !== 'debug') {
|
|
return msg;
|
|
}
|
|
|
|
if (!metadata) {
|
|
return msg;
|
|
}
|
|
|
|
const debugValue = metadata[SPLAT_SYMBOL]?.[0];
|
|
|
|
if (!debugValue) {
|
|
return msg;
|
|
}
|
|
|
|
if (debugValue && Array.isArray(debugValue)) {
|
|
msg += `\n${JSON.stringify(debugValue.map(condenseArray))}`;
|
|
return msg;
|
|
}
|
|
|
|
if (typeof debugValue !== 'object') {
|
|
return (msg += ` ${debugValue}`);
|
|
}
|
|
|
|
msg += '\n{';
|
|
|
|
const copy = klona(metadata);
|
|
traverse(copy).forEach(function (value) {
|
|
const parent = this.parent;
|
|
const parentKey = `${parent && parent.notRoot ? parent.key + '.' : ''}`;
|
|
const tabs = `${parent && parent.notRoot ? '\t\t' : '\t'}`;
|
|
if (this.isLeaf && typeof value === 'string') {
|
|
const truncatedText = truncateLongStrings(value);
|
|
msg += `\n${tabs}${parentKey}${this.key}: ${JSON.stringify(truncatedText)},`;
|
|
} else if (this.notLeaf && Array.isArray(value) && value.length > 0) {
|
|
const currentMessage = `\n${tabs}// ${value.length} ${this.key.replace(/s$/, '')}(s)`;
|
|
this.update(currentMessage, true);
|
|
msg += currentMessage;
|
|
const stringifiedArray = value.map(condenseArray);
|
|
msg += `\n${tabs}${parentKey}${this.key}: [${stringifiedArray}],`;
|
|
} else if (this.isLeaf && typeof value === 'function') {
|
|
msg += `\n${tabs}${parentKey}${this.key}: function,`;
|
|
} else if (this.isLeaf) {
|
|
msg += `\n${tabs}${parentKey}${this.key}: ${value},`;
|
|
}
|
|
});
|
|
|
|
msg += '\n}';
|
|
return msg;
|
|
});
|
|
|
|
module.exports = {
|
|
redactFormat,
|
|
redactMessage,
|
|
debugTraverse,
|
|
};
|