mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
* refactor: only remove conversation states from localStorage on login/logout but not on refresh * chore: add debugging log for azure completion url * chore: add api-key to redact regex * fix: do not show endpoint selector if endpoint is falsy * chore: remove logger from genAzureChatCompletion * feat(ci): mock fetchEventSource * refactor(ci): mock all model methods in BaseClient.test, as well as mock the implementation for getCompletion in FakeClient * fix(OpenAIClient): consider chatCompletion if model name includes `gpt` as opposed to `gpt-` * fix(ChatGPTClient/azureOpenAI): Remove 'model' option for Azure compatibility (cannot be sent in payload body) * feat(ci): write new test suite that significantly increase test coverage for OpenAIClient and BaseClient by covering most of the real implementation of the `sendMessage` method - test for the azure edge case where model option is appended to modelOptions, ensuring removal before sent to the azure endpoint - test for expected azure url being passed to SSE POST request - test for AZURE_OPENAI_DEFAULT_MODEL being set, but is not included in the URL deployment name as expected - test getCompletion method to have correct payload fix(ci/OpenAIClient.test.js): correctly mock hanging/async methods * refactor(addTitle): allow azure to title as it aborts signal on completion
128 lines
4.2 KiB
JavaScript
128 lines
4.2 KiB
JavaScript
const util = require('util');
|
|
const winston = require('winston');
|
|
const traverse = require('traverse');
|
|
const { klona } = require('klona/full');
|
|
|
|
const sensitiveKeys = [/^sk-\w+$/, /Bearer \w+/, /api-key: \w+/];
|
|
|
|
/**
|
|
* Determines if a given key string is sensitive.
|
|
*
|
|
* @param {string} keyStr - The key string to check.
|
|
* @returns {boolean} True if the key string matches known sensitive key patterns.
|
|
*/
|
|
function isSensitiveKey(keyStr) {
|
|
if (keyStr) {
|
|
return sensitiveKeys.some((regex) => regex.test(keyStr));
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Recursively redacts sensitive information from an object.
|
|
*
|
|
* @param {object} obj - The object to traverse and redact.
|
|
*/
|
|
function redactObject(obj) {
|
|
traverse(obj).forEach(function redactor() {
|
|
if (isSensitiveKey(this.key)) {
|
|
this.update('[REDACTED]');
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Deep copies and redacts sensitive information from an object.
|
|
*
|
|
* @param {object} obj - The object to copy and redact.
|
|
* @returns {object} The redacted copy of the original object.
|
|
*/
|
|
function redact(obj) {
|
|
const copy = klona(obj); // Making a deep copy to prevent side effects
|
|
redactObject(copy);
|
|
|
|
const splat = copy[Symbol.for('splat')];
|
|
redactObject(splat); // Specifically redact splat Symbol
|
|
|
|
return copy;
|
|
}
|
|
|
|
/**
|
|
* Truncates long strings, especially base64 image data, within log messages.
|
|
*
|
|
* @param {any} value - The value to be inspected and potentially truncated.
|
|
* @returns {any} - The truncated or original value.
|
|
*/
|
|
const truncateLongStrings = (value) => {
|
|
if (typeof value === 'string') {
|
|
return value.length > 100 ? value.substring(0, 100) + '... [truncated]' : value;
|
|
}
|
|
|
|
return value;
|
|
};
|
|
|
|
// /**
|
|
// * Processes each message in the messages array, specifically looking for and truncating
|
|
// * base64 image URLs in the content. If a base64 image URL is found, it replaces the URL
|
|
// * with a truncated message.
|
|
// *
|
|
// * @param {PayloadMessage} message - The payload message object to format.
|
|
// * @returns {PayloadMessage} - The processed message object with base64 image URLs truncated.
|
|
// */
|
|
// const truncateBase64ImageURLs = (message) => {
|
|
// // Create a deep copy of the message
|
|
// const messageCopy = JSON.parse(JSON.stringify(message));
|
|
|
|
// if (messageCopy.content && Array.isArray(messageCopy.content)) {
|
|
// messageCopy.content = messageCopy.content.map(contentItem => {
|
|
// if (contentItem.type === 'image_url' && contentItem.image_url && isBase64String(contentItem.image_url.url)) {
|
|
// return { ...contentItem, image_url: { ...contentItem.image_url, url: 'Base64 Image Data... [truncated]' } };
|
|
// }
|
|
// return contentItem;
|
|
// });
|
|
// }
|
|
// return messageCopy;
|
|
// };
|
|
|
|
// /**
|
|
// * Checks if a string is a base64 image data string.
|
|
// *
|
|
// * @param {string} str - The string to be checked.
|
|
// * @returns {boolean} - True if the string is base64 image data, otherwise false.
|
|
// */
|
|
// const isBase64String = (str) => /^data:image\/[a-zA-Z]+;base64,/.test(str);
|
|
|
|
/**
|
|
* Custom log format for Winston that handles deep object inspection.
|
|
* It specifically truncates long strings and handles nested structures within metadata.
|
|
*
|
|
* @param {Object} info - Information about the log entry.
|
|
* @returns {string} - The formatted log message.
|
|
*/
|
|
const deepObjectFormat = winston.format.printf(({ level, message, timestamp, ...metadata }) => {
|
|
let msg = `${timestamp} ${level}: ${message}`;
|
|
|
|
if (Object.keys(metadata).length) {
|
|
Object.entries(metadata).forEach(([key, value]) => {
|
|
let val = value;
|
|
if (key === 'modelOptions' && value && Array.isArray(value.messages)) {
|
|
// Create a shallow copy of the messages array
|
|
// val = { ...value, messages: value.messages.map(truncateBase64ImageURLs) };
|
|
val = { ...value, messages: `${value.messages.length} message(s) in payload` };
|
|
}
|
|
// Inspects each metadata value; applies special handling for 'messages'
|
|
const inspectedValue =
|
|
typeof val === 'string'
|
|
? truncateLongStrings(val)
|
|
: util.inspect(val, { depth: null, colors: false }); // Use 'val' here
|
|
msg += ` ${key}: ${inspectedValue}`;
|
|
});
|
|
}
|
|
|
|
return msg;
|
|
});
|
|
|
|
module.exports = {
|
|
redact,
|
|
deepObjectFormat,
|
|
};
|