refactor: original changes

This commit is contained in:
Danny Avila 2025-05-30 04:28:22 -04:00
parent fa9177180f
commit f9c0e9853f
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
83 changed files with 413 additions and 505 deletions

View file

@ -1,6 +1,7 @@
const jwt = require('jsonwebtoken');
const { nanoid } = require('nanoid');
const { tool } = require('@langchain/core/tools');
const { findToken } = require('@librechat/data-schemas');
const { GraphEvents, sleep } = require('@librechat/agents');
const {
Time,
@ -231,10 +232,9 @@ async function createActionTool({
};
const tokenPromises = [];
const { Token } = db.models;
tokenPromises.push(Token.findToken({ userId, type: 'oauth', identifier }));
tokenPromises.push(findToken({ userId, type: 'oauth', identifier }));
tokenPromises.push(
Token.findToken({
findToken({
userId,
type: 'oauth_refresh',
identifier: `${identifier}:refresh`,

View file

@ -1,11 +1,11 @@
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const { updateUser, logger } = require('@librechat/data-schemas');
const { resizeImageBuffer } = require('../images/resize');
const { updateFile } = require('~/models/File');
const { logger } = require('~/config');
const { saveBufferToAzure } = require('./crud');
const db = require('~/lib/db/connectDb');
/**
* Uploads an image file to Azure Blob Storage.
* It resizes and converts the image similar to your Firebase implementation.
@ -107,7 +107,7 @@ async function processAzureAvatar({ buffer, userId, manual, basePath = 'images',
const isManual = manual === 'true';
const url = `${downloadURL}?manual=${isManual}`;
if (isManual) {
await db.models?.User.updateUser(userId, { avatar: url });
await updateUser(userId, { avatar: url });
}
return url;
} catch (error) {

View file

@ -4,8 +4,7 @@ const sharp = require('sharp');
const { resizeImageBuffer } = require('../images/resize');
const { saveBufferToFirebase } = require('./crud');
const { updateFile } = require('~/models/File');
const { logger } = require('~/config');
const db = require('~/lib/db/connectDb');
const { logger, updateUser } = require('@librechat/data-schemas');
/**
* Converts an image file to the target format. The function first resizes the image based on the specified
@ -99,7 +98,7 @@ async function processFirebaseAvatar({ buffer, userId, manual }) {
const url = `${downloadURL}?manual=${isManual}`;
if (isManual) {
await db.models.User.updateUser(userId, { avatar: url });
await updateUser(userId, { avatar: url });
}
return url;

View file

@ -1,9 +1,9 @@
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const { updateUser } = require('@librechat/data-schemas');
const { resizeImageBuffer } = require('../images/resize');
const { updateFile } = require('~/models/File');
const db = require('~/lib/db/connectDb');
/**
* Converts an image file to the target format. The function first resizes the image based on the specified
@ -141,7 +141,7 @@ async function processLocalAvatar({ buffer, userId, manual }) {
let url = `${urlRoute}?manual=${isManual}`;
if (isManual) {
await db.models?.User.updateUser(userId, { avatar: url });
await updateUser(userId, { avatar: url });
}
return url;

View file

@ -1,11 +1,10 @@
const fs = require('fs');
const path = require('path');
const sharp = require('sharp');
const { logger, updateUser } = require('@librechat/data-schemas');
const { resizeImageBuffer } = require('../images/resize');
const { saveBufferToS3 } = require('./crud');
const { updateFile } = require('~/models/File');
const { logger } = require('~/config');
const db = require('~/lib/db/connectDb');
const { saveBufferToS3 } = require('./crud');
const defaultBasePath = 'images';
@ -102,7 +101,7 @@ async function processS3Avatar({ buffer, userId, manual, basePath = defaultBaseP
try {
const downloadURL = await saveBufferToS3({ userId, buffer, fileName: 'avatar.png', basePath });
if (manual === 'true') {
await db.models?.User.updateUser(userId, { avatar: downloadURL });
await updateUser(userId, { avatar: downloadURL });
}
return downloadURL;
} catch (error) {

View file

@ -1,7 +1,7 @@
const { ErrorTypes } = require('librechat-data-provider');
const { encrypt, decrypt } = require('~/server/utils');
const { logger } = require('~/config');
const db = require('~/lib/db/connectDb');
const { Key, logger, updateUser } = require('@librechat/data-schemas');
const { encrypt, decrypt } = require('~/server/utils/crypto');
/**
* Updates the plugins for a user based on the action specified (install/uninstall).
* @async
@ -16,11 +16,10 @@ const db = require('~/lib/db/connectDb');
const updateUserPluginsService = async (user, pluginKey, action) => {
try {
const userPlugins = user.plugins || [];
const { User } = db.models;
if (action === 'install') {
return await User.updateUser(user._id, { plugins: [...userPlugins, pluginKey] });
return await updateUser(user._id, { plugins: [...userPlugins, pluginKey] });
} else if (action === 'uninstall') {
return await User.updateUser(user._id, {
return await updateUser(user._id, {
plugins: userPlugins.filter((plugin) => plugin !== pluginKey),
});
}
@ -42,7 +41,7 @@ const updateUserPluginsService = async (user, pluginKey, action) => {
* an error indicating that there is no user key available.
*/
const getUserKey = async ({ userId, name }) => {
const keyValue = await db.models.Key.findOne({ userId, name }).lean();
const keyValue = await Key.findOne({ userId, name }).lean();
if (!keyValue) {
throw new Error(
JSON.stringify({
@ -89,7 +88,7 @@ const getUserKeyValues = async ({ userId, name }) => {
* returns its expiry date. If the key is not found, it returns null for the expiry date.
*/
const getUserKeyExpiry = async ({ userId, name }) => {
const keyValue = await db.models.Key.findOne({ userId, name }).lean();
const keyValue = await Key.findOne({ userId, name }).lean();
if (!keyValue) {
return { expiresAt: null };
}
@ -123,7 +122,7 @@ const updateUserKey = async ({ userId, name, value, expiresAt = null }) => {
// make sure to remove if already present
updateQuery.$unset = { expiresAt };
}
return await db.models.Key.findOneAndUpdate({ userId, name }, updateQuery, {
return await Key.findOneAndUpdate({ userId, name }, updateQuery, {
upsert: true,
new: true,
}).lean();
@ -143,10 +142,10 @@ const updateUserKey = async ({ userId, name, value, expiresAt = null }) => {
*/
const deleteUserKey = async ({ userId, name, all = false }) => {
if (all) {
return await db.models.Key.deleteMany({ userId });
return await Key.deleteMany({ userId });
}
await db.models.Key.findOneAndDelete({ userId, name }).lean();
await Key.findOneAndDelete({ userId, name }).lean();
};
/**

View file

@ -1,7 +1,6 @@
const { webcrypto } = require('node:crypto');
const { decryptV3, decryptV2 } = require('../utils/crypto');
const { hashBackupCode } = require('~/server/utils/crypto');
const db = require('~/lib/db/connectDb');
const { User } = require('@librechat/data-schemas');
const { hashBackupCode, decryptV3, decryptV2 } = require('~/server/utils/crypto');
// Base32 alphabet for TOTP secret encoding.
const BASE32_ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
@ -173,7 +172,7 @@ const verifyBackupCode = async ({ user, backupCode }) => {
: codeObj,
);
// Update the user record with the marked backup code.
await db.models.User.updateUser(user._id, { backupCodes: updatedBackupCodes });
await User.updateUser(user._id, { backupCodes: updatedBackupCodes });
return true;
}
return false;