mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 18:30:15 +01:00
🧩 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:
parent
927ce5395b
commit
39caeb2027
10 changed files with 328 additions and 113 deletions
|
|
@ -1,17 +1,48 @@
|
|||
const { getUserPluginAuthValue } = require('../../../../server/services/PluginService');
|
||||
const { getUserPluginAuthValue } = require('~/server/services/PluginService');
|
||||
const { availableTools } = require('../');
|
||||
|
||||
const loadToolSuite = async ({ pluginKey, tools, user, options }) => {
|
||||
/**
|
||||
* Loads a suite of tools with authentication values for a given user, supporting alternate authentication fields.
|
||||
* Authentication fields can have alternates separated by "||", and the first defined variable will be used.
|
||||
*
|
||||
* @param {Object} params Parameters for loading the tool suite.
|
||||
* @param {string} params.pluginKey Key identifying the plugin whose tools are to be loaded.
|
||||
* @param {Array<Function>} params.tools Array of tool constructor functions.
|
||||
* @param {Object} params.user User object for whom the tools are being loaded.
|
||||
* @param {Object} [params.options={}] Optional parameters to be passed to each tool constructor.
|
||||
* @returns {Promise<Array>} A promise that resolves to an array of instantiated tools.
|
||||
*/
|
||||
const loadToolSuite = async ({ pluginKey, tools, user, options = {} }) => {
|
||||
const authConfig = availableTools.find((tool) => tool.pluginKey === pluginKey).authConfig;
|
||||
const suite = [];
|
||||
const authValues = {};
|
||||
|
||||
for (const auth of authConfig) {
|
||||
let authValue = process.env[auth.authField];
|
||||
if (!authValue) {
|
||||
authValue = await getUserPluginAuthValue(user, auth.authField);
|
||||
const findAuthValue = async (authField) => {
|
||||
const fields = authField.split('||');
|
||||
for (const field of fields) {
|
||||
let value = process.env[field];
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
value = await getUserPluginAuthValue(user, field);
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error fetching plugin auth value for ${field}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
for (const auth of authConfig) {
|
||||
const authValue = await findAuthValue(auth.authField);
|
||||
if (authValue !== null) {
|
||||
authValues[auth.authField] = authValue;
|
||||
} else {
|
||||
console.warn(`No auth value found for ${auth.authField}`);
|
||||
}
|
||||
authValues[auth.authField] = authValue;
|
||||
}
|
||||
|
||||
for (const tool of tools) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue