2023-12-10 14:54:13 -05:00
|
|
|
const { GoogleClient } = require('~/app');
|
2023-12-15 02:18:07 -05:00
|
|
|
const { EModelEndpoint, AuthKeys } = require('librechat-data-provider');
|
2023-12-10 14:54:13 -05:00
|
|
|
const { getUserKey, checkUserKeyExpiry } = require('~/server/services/UserService');
|
|
|
|
|
|
|
|
|
|
const initializeClient = async ({ req, res, endpointOption }) => {
|
|
|
|
|
const { GOOGLE_KEY, GOOGLE_REVERSE_PROXY, PROXY } = process.env;
|
|
|
|
|
const isUserProvided = GOOGLE_KEY === 'user_provided';
|
|
|
|
|
const { key: expiresAt } = req.body;
|
|
|
|
|
|
|
|
|
|
let userKey = null;
|
|
|
|
|
if (expiresAt && isUserProvided) {
|
|
|
|
|
checkUserKeyExpiry(
|
|
|
|
|
expiresAt,
|
2023-12-15 02:18:07 -05:00
|
|
|
'Your Google Credentials have expired. Please provide your Service Account JSON Key or Generative Language API Key again.',
|
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 = {};
|
|
|
|
|
try {
|
|
|
|
|
serviceKey = require('~/data/auth.json');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const credentials = isUserProvided
|
|
|
|
|
? userKey
|
|
|
|
|
: {
|
|
|
|
|
[AuthKeys.GOOGLE_SERVICE_KEY]: serviceKey,
|
|
|
|
|
[AuthKeys.GOOGLE_API_KEY]: GOOGLE_KEY,
|
|
|
|
|
};
|
2023-12-10 14:54:13 -05:00
|
|
|
|
2023-12-15 02:18:07 -05:00
|
|
|
const client = new GoogleClient(credentials, {
|
2023-12-10 14:54:13 -05:00
|
|
|
req,
|
|
|
|
|
res,
|
|
|
|
|
reverseProxyUrl: GOOGLE_REVERSE_PROXY ?? null,
|
|
|
|
|
proxy: PROXY ?? null,
|
|
|
|
|
...endpointOption,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
client,
|
2023-12-15 02:18:07 -05:00
|
|
|
credentials,
|
2023-12-10 14:54:13 -05:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = initializeClient;
|