2023-12-10 14:54:13 -05:00
|
|
|
const { GoogleClient } = require('~/app');
|
2023-12-11 14:48:40 -05:00
|
|
|
const { EModelEndpoint } = 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,
|
|
|
|
|
'Your Google key has expired. Please provide your JSON credentials again.',
|
|
|
|
|
);
|
|
|
|
|
userKey = await getUserKey({ userId: req.user.id, name: EModelEndpoint.google });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const apiKey = isUserProvided ? userKey : require('~/data/auth.json');
|
|
|
|
|
|
|
|
|
|
const client = new GoogleClient(apiKey, {
|
|
|
|
|
req,
|
|
|
|
|
res,
|
|
|
|
|
reverseProxyUrl: GOOGLE_REVERSE_PROXY ?? null,
|
|
|
|
|
proxy: PROXY ?? null,
|
|
|
|
|
...endpointOption,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
client,
|
|
|
|
|
apiKey,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = initializeClient;
|