mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
🔧 refactor: Prevent Unnecessary Google Service Key Loading (#8287)
* 🔧 refactor: Improve Google Key Handling in `loadAsyncEndpoints` - Enhanced logic to check if GOOGLE_KEY is provided, including user-provided checks. - Updated service key loading mechanism to only attempt loading if GOOGLE_KEY is not provided. - Added error logging for service key loading failures. * 🔧 refactor: Enhance service key loading logic in `initializeClient`
This commit is contained in:
parent
aecf8f19a6
commit
035fa081c1
2 changed files with 36 additions and 27 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { loadServiceKey } = require('@librechat/api');
|
const { logger } = require('@librechat/data-schemas');
|
||||||
|
const { loadServiceKey, isUserProvided } = require('@librechat/api');
|
||||||
const { EModelEndpoint } = require('librechat-data-provider');
|
const { EModelEndpoint } = require('librechat-data-provider');
|
||||||
const { isUserProvided } = require('~/server/utils');
|
|
||||||
const { config } = require('./EndpointService');
|
const { config } = require('./EndpointService');
|
||||||
|
|
||||||
const { openAIApiKey, azureOpenAIApiKey, useAzurePlugins, userProvidedOpenAI, googleKey } = config;
|
const { openAIApiKey, azureOpenAIApiKey, useAzurePlugins, userProvidedOpenAI, googleKey } = config;
|
||||||
|
|
@ -11,28 +11,29 @@ const { openAIApiKey, azureOpenAIApiKey, useAzurePlugins, userProvidedOpenAI, go
|
||||||
* @param {Express.Request} req - The request object
|
* @param {Express.Request} req - The request object
|
||||||
*/
|
*/
|
||||||
async function loadAsyncEndpoints(req) {
|
async function loadAsyncEndpoints(req) {
|
||||||
let i = 0;
|
|
||||||
let serviceKey, googleUserProvides;
|
let serviceKey, googleUserProvides;
|
||||||
const serviceKeyPath =
|
|
||||||
process.env.GOOGLE_SERVICE_KEY_FILE_PATH ||
|
|
||||||
path.join(__dirname, '../../..', 'data', 'auth.json');
|
|
||||||
|
|
||||||
try {
|
/** Check if GOOGLE_KEY is provided at all(including 'user_provided') */
|
||||||
serviceKey = await loadServiceKey(serviceKeyPath);
|
const isGoogleKeyProvided = googleKey && googleKey.trim() !== '';
|
||||||
} catch {
|
|
||||||
if (i === 0) {
|
if (isGoogleKeyProvided) {
|
||||||
i++;
|
/** If GOOGLE_KEY is provided, check if it's user_provided */
|
||||||
|
googleUserProvides = isUserProvided(googleKey);
|
||||||
|
} else {
|
||||||
|
/** Only attempt to load service key if GOOGLE_KEY is not provided */
|
||||||
|
const serviceKeyPath =
|
||||||
|
process.env.GOOGLE_SERVICE_KEY_FILE_PATH ||
|
||||||
|
path.join(__dirname, '../../..', 'data', 'auth.json');
|
||||||
|
|
||||||
|
try {
|
||||||
|
serviceKey = await loadServiceKey(serviceKeyPath);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('Error loading service key', error);
|
||||||
|
serviceKey = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUserProvided(googleKey)) {
|
const google = serviceKey || isGoogleKeyProvided ? { userProvide: googleUserProvides } : false;
|
||||||
googleUserProvides = true;
|
|
||||||
if (i <= 1) {
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const google = serviceKey || googleKey ? { userProvide: googleUserProvides } : false;
|
|
||||||
|
|
||||||
const useAzure = req.app.locals[EModelEndpoint.azureOpenAI]?.plugins;
|
const useAzure = req.app.locals[EModelEndpoint.azureOpenAI]?.plugins;
|
||||||
const gptPlugins =
|
const gptPlugins =
|
||||||
|
|
|
||||||
|
|
@ -17,16 +17,24 @@ const initializeClient = async ({ req, res, endpointOption, overrideModel, optio
|
||||||
|
|
||||||
let serviceKey = {};
|
let serviceKey = {};
|
||||||
|
|
||||||
try {
|
/** Check if GOOGLE_KEY is provided at all (including 'user_provided') */
|
||||||
const serviceKeyPath =
|
const isGoogleKeyProvided =
|
||||||
process.env.GOOGLE_SERVICE_KEY_FILE_PATH ||
|
(GOOGLE_KEY && GOOGLE_KEY.trim() !== '') || (isUserProvided && userKey != null);
|
||||||
path.join(__dirname, '../../../..', 'data', 'auth.json');
|
|
||||||
serviceKey = await loadServiceKey(serviceKeyPath);
|
if (!isGoogleKeyProvided) {
|
||||||
if (!serviceKey) {
|
/** Only attempt to load service key if GOOGLE_KEY is not provided */
|
||||||
|
try {
|
||||||
|
const serviceKeyPath =
|
||||||
|
process.env.GOOGLE_SERVICE_KEY_FILE_PATH ||
|
||||||
|
path.join(__dirname, '../../../..', 'data', 'auth.json');
|
||||||
|
serviceKey = await loadServiceKey(serviceKeyPath);
|
||||||
|
if (!serviceKey) {
|
||||||
|
serviceKey = {};
|
||||||
|
}
|
||||||
|
} catch (_e) {
|
||||||
|
// Service key loading failed, but that's okay if not required
|
||||||
serviceKey = {};
|
serviceKey = {};
|
||||||
}
|
}
|
||||||
} catch (_e) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const credentials = isUserProvided
|
const credentials = isUserProvided
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue