2025-06-29 17:09:37 -04:00
|
|
|
const path = require('path');
|
2023-12-15 02:18:07 -05:00
|
|
|
const { EModelEndpoint, AuthKeys } = require('librechat-data-provider');
|
2025-07-01 22:37:29 -04:00
|
|
|
const { getGoogleConfig, isEnabled, loadServiceKey } = require('@librechat/api');
|
2023-12-10 14:54:13 -05:00
|
|
|
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
|
|
|
|
|
|
2025-11-25 15:20:07 -05:00
|
|
|
const initializeClient = async ({ req, endpointOption, overrideModel }) => {
|
2025-03-03 19:09:22 -05:00
|
|
|
const { GOOGLE_KEY, GOOGLE_REVERSE_PROXY, GOOGLE_AUTH_HEADER, PROXY } = process.env;
|
2023-12-10 14:54:13 -05:00
|
|
|
const isUserProvided = GOOGLE_KEY === 'user_provided';
|
|
|
|
|
const { key: expiresAt } = req.body;
|
|
|
|
|
|
|
|
|
|
let userKey = null;
|
|
|
|
|
if (expiresAt && isUserProvided) {
|
2024-04-21 08:31:54 -04:00
|
|
|
checkUserKeyExpiry(expiresAt, EModelEndpoint.google);
|
2023-12-10 14:54:13 -05:00
|
|
|
userKey = await getUserKey({ userId: req.user.id, name: EModelEndpoint.google });
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 02:18:07 -05:00
|
|
|
let serviceKey = {};
|
2025-06-29 17:09:37 -04:00
|
|
|
|
2025-07-07 01:10:08 -04:00
|
|
|
/** 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 =
|
2025-07-08 21:07:33 -04:00
|
|
|
process.env.GOOGLE_SERVICE_KEY_FILE ||
|
2025-07-07 01:10:08 -04:00
|
|
|
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
|
2025-07-01 22:37:29 -04:00
|
|
|
serviceKey = {};
|
2025-06-29 17:09:37 -04:00
|
|
|
}
|
2023-12-15 02:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const credentials = isUserProvided
|
|
|
|
|
? userKey
|
|
|
|
|
: {
|
2025-06-04 00:13:28 -04:00
|
|
|
[AuthKeys.GOOGLE_SERVICE_KEY]: serviceKey,
|
|
|
|
|
[AuthKeys.GOOGLE_API_KEY]: GOOGLE_KEY,
|
|
|
|
|
};
|
2023-12-10 14:54:13 -05:00
|
|
|
|
2024-12-28 17:15:03 -05:00
|
|
|
let clientOptions = {};
|
2024-07-17 10:47:17 -04:00
|
|
|
|
2025-08-26 12:10:18 -04:00
|
|
|
const appConfig = req.config;
|
2024-07-17 10:47:17 -04:00
|
|
|
/** @type {undefined | TBaseEndpoint} */
|
2025-08-26 12:10:18 -04:00
|
|
|
const allConfig = appConfig.endpoints?.all;
|
2024-07-17 10:47:17 -04:00
|
|
|
/** @type {undefined | TBaseEndpoint} */
|
2025-08-26 12:10:18 -04:00
|
|
|
const googleConfig = appConfig.endpoints?.[EModelEndpoint.google];
|
2024-07-17 10:47:17 -04:00
|
|
|
|
|
|
|
|
if (googleConfig) {
|
|
|
|
|
clientOptions.streamRate = googleConfig.streamRate;
|
2025-03-03 19:09:22 -05:00
|
|
|
clientOptions.titleModel = googleConfig.titleModel;
|
2024-07-17 10:47:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (allConfig) {
|
|
|
|
|
clientOptions.streamRate = allConfig.streamRate;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-28 17:15:03 -05:00
|
|
|
clientOptions = {
|
2023-12-10 14:54:13 -05:00
|
|
|
reverseProxyUrl: GOOGLE_REVERSE_PROXY ?? null,
|
2024-12-18 12:13:16 -05:00
|
|
|
authHeader: isEnabled(GOOGLE_AUTH_HEADER) ?? null,
|
2023-12-10 14:54:13 -05:00
|
|
|
proxy: PROXY ?? null,
|
2024-07-17 10:47:17 -04:00
|
|
|
...clientOptions,
|
2023-12-10 14:54:13 -05:00
|
|
|
...endpointOption,
|
2024-12-28 17:15:03 -05:00
|
|
|
};
|
|
|
|
|
|
2025-11-25 15:20:07 -05:00
|
|
|
clientOptions = Object.assign(
|
|
|
|
|
{
|
|
|
|
|
modelOptions: endpointOption?.model_parameters ?? {},
|
|
|
|
|
},
|
|
|
|
|
clientOptions,
|
|
|
|
|
);
|
|
|
|
|
if (overrideModel) {
|
|
|
|
|
clientOptions.modelOptions.model = overrideModel;
|
2024-12-28 17:15:03 -05:00
|
|
|
}
|
2025-11-25 15:20:07 -05:00
|
|
|
return getGoogleConfig(credentials, clientOptions);
|
2023-12-10 14:54:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = initializeClient;
|