🧩 feat: Support Alternate API Keys for Plugins (#1760)

* refactor(DALL-E): retrieve env variables at runtime and not from memory

* feat(plugins): add alternate env variable handling to allow setting one api key for multiple plugins

* docs: update docs
This commit is contained in:
Danny Avila 2024-02-09 10:38:50 -05:00 committed by GitHub
parent 927ce5395b
commit 39caeb2027
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 328 additions and 113 deletions

View file

@ -4,6 +4,12 @@ const { CacheKeys } = require('librechat-data-provider');
const { addOpenAPISpecs } = require('~/app/clients/tools/util/addOpenAPISpecs');
const { getLogStores } = require('~/cache');
/**
* Filters out duplicate plugins from the list of plugins.
*
* @param {TPlugin[]} plugins The list of plugins to filter.
* @returns {TPlugin[]} The list of plugins with duplicates removed.
*/
const filterUniquePlugins = (plugins) => {
const seen = new Set();
return plugins.filter((plugin) => {
@ -13,17 +19,31 @@ const filterUniquePlugins = (plugins) => {
});
};
/**
* Determines if a plugin is authenticated by checking if all required authentication fields have non-empty values.
* Supports alternate authentication fields, allowing validation against multiple possible environment variables.
*
* @param {TPlugin} plugin The plugin object containing the authentication configuration.
* @returns {boolean} True if the plugin is authenticated for all required fields, false otherwise.
*/
const isPluginAuthenticated = (plugin) => {
if (!plugin.authConfig || plugin.authConfig.length === 0) {
return false;
}
return plugin.authConfig.every((authFieldObj) => {
const envValue = process.env[authFieldObj.authField];
if (envValue === 'user_provided') {
return false;
const authFieldOptions = authFieldObj.authField.split('||');
let isFieldAuthenticated = false;
for (const fieldOption of authFieldOptions) {
const envValue = process.env[fieldOption];
if (envValue && envValue.trim() !== '' && envValue !== 'user_provided') {
isFieldAuthenticated = true;
break;
}
}
return envValue && envValue.trim() !== '';
return isFieldAuthenticated;
});
};