| const { promises: fs } = require('fs'); |
| const { CacheKeys } = require('librechat-data-provider'); |
| const { addOpenAPISpecs } = require('~/app/clients/tools/util/addOpenAPISpecs'); |
| const { getLogStores } = require('~/cache'); |
|
|
| |
| |
| |
| |
| |
| |
| const filterUniquePlugins = (plugins) => { |
| const seen = new Set(); |
| return plugins.filter((plugin) => { |
| const duplicate = seen.has(plugin.pluginKey); |
| seen.add(plugin.pluginKey); |
| return !duplicate; |
| }); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| const isPluginAuthenticated = (plugin) => { |
| if (!plugin.authConfig || plugin.authConfig.length === 0) { |
| return false; |
| } |
|
|
| return plugin.authConfig.every((authFieldObj) => { |
| 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 isFieldAuthenticated; |
| }); |
| }; |
|
|
| const getAvailablePluginsController = async (req, res) => { |
| try { |
| const cache = getLogStores(CacheKeys.CONFIG_STORE); |
| const cachedPlugins = await cache.get(CacheKeys.PLUGINS); |
| if (cachedPlugins) { |
| res.status(200).json(cachedPlugins); |
| return; |
| } |
|
|
| |
| const { filteredTools = [], includedTools = [] } = req.app.locals; |
| const pluginManifest = await fs.readFile(req.app.locals.paths.pluginManifest, 'utf8'); |
| const jsonData = JSON.parse(pluginManifest); |
|
|
| const uniquePlugins = filterUniquePlugins(jsonData); |
| let authenticatedPlugins = []; |
| for (const plugin of uniquePlugins) { |
| authenticatedPlugins.push( |
| isPluginAuthenticated(plugin) ? { ...plugin, authenticated: true } : plugin, |
| ); |
| } |
|
|
| let plugins = await addOpenAPISpecs(authenticatedPlugins); |
|
|
| if (includedTools.length > 0) { |
| plugins = plugins.filter((plugin) => includedTools.includes(plugin.pluginKey)); |
| } else { |
| plugins = plugins.filter((plugin) => !filteredTools.includes(plugin.pluginKey)); |
| } |
|
|
| await cache.set(CacheKeys.PLUGINS, plugins); |
| res.status(200).json(plugins); |
| } catch (error) { |
| res.status(500).json({ message: error.message }); |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const getAvailableTools = async (req, res) => { |
| try { |
| const cache = getLogStores(CacheKeys.CONFIG_STORE); |
| const cachedTools = await cache.get(CacheKeys.TOOLS); |
| if (cachedTools) { |
| res.status(200).json(cachedTools); |
| return; |
| } |
|
|
| const pluginManifest = await fs.readFile(req.app.locals.paths.pluginManifest, 'utf8'); |
|
|
| const jsonData = JSON.parse(pluginManifest); |
| |
| const uniquePlugins = filterUniquePlugins(jsonData); |
|
|
| const authenticatedPlugins = uniquePlugins.map((plugin) => { |
| if (isPluginAuthenticated(plugin)) { |
| return { ...plugin, authenticated: true }; |
| } else { |
| return plugin; |
| } |
| }); |
|
|
| const tools = authenticatedPlugins.filter( |
| (plugin) => req.app.locals.availableTools[plugin.pluginKey] !== undefined, |
| ); |
|
|
| await cache.set(CacheKeys.TOOLS, tools); |
| res.status(200).json(tools); |
| } catch (error) { |
| res.status(500).json({ message: error.message }); |
| } |
| }; |
|
|
| module.exports = { |
| getAvailableTools, |
| getAvailablePluginsController, |
| }; |
|
|