Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🔧 refactor: Prevent Unnecessary Google Service Key Loading (#8287)

Danny Avila035fa081c1a1
diff --git a/api/server/services/Config/loadAsyncEndpoints.js b/api/server/services/Config/loadAsyncEndpoints.js
index 56f693c779..bf19e2ea29 100644
--- a/api/server/services/Config/loadAsyncEndpoints.js
+++ b/api/server/services/Config/loadAsyncEndpoints.js
@@ -1,7 +1,7 @@
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 { isUserProvided } = require('~/server/utils');
const { config } = require('./EndpointService');
const { openAIApiKey, azureOpenAIApiKey, useAzurePlugins, userProvidedOpenAI, googleKey } = config;
@@ -11,28 +11,29 @@ const { openAIApiKey, azureOpenAIApiKey, useAzurePlugins, userProvidedOpenAI, go
* @param {Express.Request} req - The request object
*/
async function loadAsyncEndpoints(req) {
- let i = 0;
let serviceKey, googleUserProvides;
- const serviceKeyPath =
- process.env.GOOGLE_SERVICE_KEY_FILE_PATH ||
- path.join(__dirname, '../../..', 'data', 'auth.json');
-
- try {
- serviceKey = await loadServiceKey(serviceKeyPath);
- } catch {
- if (i === 0) {
- i++;
- }
- }
- if (isUserProvided(googleKey)) {
- googleUserProvides = true;
- if (i <= 1) {
- i++;
+ /** Check if GOOGLE_KEY is provided at all(including 'user_provided') */
+ const isGoogleKeyProvided = googleKey && googleKey.trim() !== '';
+
+ if (isGoogleKeyProvided) {
+ /** 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;
}
}
- const google = serviceKey || googleKey ? { userProvide: googleUserProvides } : false;
+ const google = serviceKey || isGoogleKeyProvided ? { userProvide: googleUserProvides } : false;
const useAzure = req.app.locals[EModelEndpoint.azureOpenAI]?.plugins;
const gptPlugins =
diff --git a/api/server/services/Endpoints/google/initialize.js b/api/server/services/Endpoints/google/initialize.js
index 871feda604..4e56cccb3b 100644
--- a/api/server/services/Endpoints/google/initialize.js
+++ b/api/server/services/Endpoints/google/initialize.js
@@ -17,16 +17,24 @@ const initializeClient = async ({ req, res, endpointOption, overrideModel, optio
let serviceKey = {};
- try {
- const serviceKeyPath =
- process.env.GOOGLE_SERVICE_KEY_FILE_PATH ||
- path.join(__dirname, '../../../..', 'data', 'auth.json');
- serviceKey = await loadServiceKey(serviceKeyPath);
- if (!serviceKey) {
+ /** Check if GOOGLE_KEY is provided at all (including 'user_provided') */
+ const isGoogleKeyProvided =
+ (GOOGLE_KEY && GOOGLE_KEY.trim() !== '') || (isUserProvided && userKey != null);
+
+ if (!isGoogleKeyProvided) {
+ /** 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 = {};
}
- } catch (_e) {
- // Do nothing
}
const credentials = isUserProvided