mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
* WIP: initial logging changes add several transports in ~/config/winston omit messages in logs, truncate long strings add short blurb in dotenv for debug logging GoogleClient: using logger OpenAIClient: using logger, handleOpenAIErrors Adding typedef for payload message bumped winston and using winston-daily-rotate-file moved config for server paths to ~/config dir Added `DEBUG_LOGGING=true` to .env.example * WIP: Refactor logging statements in code * WIP: Refactor logging statements and import configurations * WIP: Refactor logging statements and import configurations * refactor: broadcast Redis initialization message with `info` not `debug` * refactor: complete Refactor logging statements and import configurations * chore: delete unused tools * fix: circular dependencies due to accessing logger * refactor(handleText): handle booleans and write tests * refactor: redact sensitive values, better formatting * chore: improve log formatting, avoid passing strings to 2nd arg * fix(ci): fix jest tests due to logger changes * refactor(getAvailablePluginsController): cache plugins as they are static and avoids async addOpenAPISpecs call every time * chore: update docs * chore: update docs * chore: create separate meiliSync logger, clean up logs to avoid being unnecessarily verbose * chore: spread objects where they are commonly logged to allow string truncation * chore: improve error log formatting
78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
const { User, Key } = require('~/models');
|
|
const { encrypt, decrypt } = require('~/server/utils');
|
|
const { logger } = require('~/config');
|
|
|
|
const updateUserPluginsService = async (user, pluginKey, action) => {
|
|
try {
|
|
if (action === 'install') {
|
|
return await User.updateOne(
|
|
{ _id: user._id },
|
|
{ $set: { plugins: [...user.plugins, pluginKey] } },
|
|
);
|
|
} else if (action === 'uninstall') {
|
|
return await User.updateOne(
|
|
{ _id: user._id },
|
|
{ $set: { plugins: user.plugins.filter((plugin) => plugin !== pluginKey) } },
|
|
);
|
|
}
|
|
} catch (err) {
|
|
logger.error('[updateUserPluginsService]', err);
|
|
return err;
|
|
}
|
|
};
|
|
|
|
const getUserKey = async ({ userId, name }) => {
|
|
const keyValue = await Key.findOne({ userId, name }).lean();
|
|
if (!keyValue) {
|
|
throw new Error('User-provided key not found');
|
|
}
|
|
return decrypt(keyValue.value);
|
|
};
|
|
|
|
const getUserKeyExpiry = async ({ userId, name }) => {
|
|
const keyValue = await Key.findOne({ userId, name }).lean();
|
|
if (!keyValue) {
|
|
return { expiresAt: null };
|
|
}
|
|
return { expiresAt: keyValue.expiresAt };
|
|
};
|
|
|
|
const updateUserKey = async ({ userId, name, value, expiresAt }) => {
|
|
const encryptedValue = encrypt(value);
|
|
return await Key.findOneAndUpdate(
|
|
{ userId, name },
|
|
{
|
|
userId,
|
|
name,
|
|
value: encryptedValue,
|
|
expiresAt: new Date(expiresAt),
|
|
},
|
|
{ upsert: true, new: true },
|
|
).lean();
|
|
};
|
|
|
|
const deleteUserKey = async ({ userId, name, all = false }) => {
|
|
if (all) {
|
|
return await Key.deleteMany({ userId });
|
|
}
|
|
|
|
await Key.findOneAndDelete({ userId, name }).lean();
|
|
};
|
|
|
|
const checkUserKeyExpiry = (expiresAt, message) => {
|
|
const expiresAtDate = new Date(expiresAt);
|
|
if (expiresAtDate < new Date()) {
|
|
const expiryStr = `User-provided key expired at ${expiresAtDate.toLocaleString()}`;
|
|
const errorMessage = message ? `${message}\n${expiryStr}` : expiryStr;
|
|
throw new Error(errorMessage);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
updateUserPluginsService,
|
|
getUserKey,
|
|
getUserKeyExpiry,
|
|
updateUserKey,
|
|
deleteUserKey,
|
|
checkUserKeyExpiry,
|
|
};
|