LibreChat/api/server/controllers/PluginController.js
Danny Avila e5336039fc
ci(backend-review.yml): add linter step to the backend review workflow (#625)
* ci(backend-review.yml): add linter step to the backend review workflow

* chore(backend-review.yml): remove prettier from lint-action configuration

* chore: apply new linting workflow

* chore(lint-staged.config.js): reorder lint-staged tasks for JavaScript and TypeScript files

* chore(eslint): update ignorePatterns in .eslintrc.js
chore(lint-action): remove prettier option in backend-review.yml
chore(package.json): add lint and lint:fix scripts

* chore(lint-staged.config.js): remove prettier --write command for js, jsx, ts, tsx files

* chore(titleConvo.js): remove unnecessary console.log statement
chore(titleConvo.js): add missing comma in options object

* chore: apply linting to all files

* chore(lint-staged.config.js): update lint-staged configuration to include prettier formatting
2023-07-14 09:36:49 -04:00

57 lines
1.6 KiB
JavaScript

// const { getAvailableToolsService } = require('../services/PluginService');
const fs = require('fs');
const path = require('path');
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 envValue = process.env[authFieldObj.authField];
if (envValue === 'user_provided') {
return false;
}
return envValue && envValue.trim() !== '';
});
};
const getAvailablePluginsController = async (req, res) => {
try {
fs.readFile(
path.join(__dirname, '..', '..', 'app', 'clients', 'tools', 'manifest.json'),
'utf8',
(err, data) => {
if (err) {
res.status(500).json({ message: err.message });
} else {
const jsonData = JSON.parse(data);
const uniquePlugins = filterUniquePlugins(jsonData);
const authenticatedPlugins = uniquePlugins.map((plugin) => {
if (isPluginAuthenticated(plugin)) {
return { ...plugin, authenticated: true };
} else {
return plugin;
}
});
res.status(200).json(authenticatedPlugins);
}
},
);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
module.exports = {
getAvailablePluginsController,
};