diff --git a/api/server/services/Config/loadCustomConfig.js b/api/server/services/Config/loadCustomConfig.js index db25049957..053380ded2 100644 --- a/api/server/services/Config/loadCustomConfig.js +++ b/api/server/services/Config/loadCustomConfig.js @@ -11,6 +11,7 @@ const { agentParamSettings, validateSettingDefinitions, } = require('librechat-data-provider'); +const { syncCategories } = require('~/server/utils/agentCategory'); const projectRoot = path.resolve(__dirname, '..', '..', '..', '..'); const defaultConfigPath = path.resolve(projectRoot, 'librechat.yaml'); @@ -119,6 +120,24 @@ https://www.librechat.ai/docs/configuration/stt_tts`); } } + // Injecting custom marketplace categories + logger.info('Checking for custom marketplace categories to sync.'); + if (customConfig.interface?.marketplace?.use) { + logger.info('Marketplace is enabled in config.'); + const marketplaceConfig = customConfig.interface.marketplace; + if (marketplaceConfig.categories) { + logger.info('Marketplace categories configuration found.'); + // enableDefaultCategories should be set as a boolean, defaulting to true if not specified + const enableDefaultCategories = + typeof marketplaceConfig.categories.enableDefaultCategories === 'boolean' + ? marketplaceConfig.categories.enableDefaultCategories + : true; + const customCategoriesList = marketplaceConfig.categories.list || []; + logger.info(`Found ${customCategoriesList.length} custom categories to sync.`); + await syncCategories(customCategoriesList, enableDefaultCategories); + } + } + (customConfig.endpoints?.custom ?? []) .filter((endpoint) => endpoint.customParams) .forEach((endpoint) => parseCustomParams(endpoint.name, endpoint.customParams)); diff --git a/api/server/utils/agentCategory.js b/api/server/utils/agentCategory.js new file mode 100644 index 0000000000..1d40193a57 --- /dev/null +++ b/api/server/utils/agentCategory.js @@ -0,0 +1,82 @@ +const { logger } = require('@librechat/data-schemas'); + +/** + * Synchronizes custom agent categories with the database. + * @param {Array} customCategoriesList - List of custom categories to be synchronized. + * @param {boolean} enableDefaultCategories - Flag indicating whether to enable default categories. + */ +async function syncCategories(customCategoriesList, enableDefaultCategories) { + const { getAllCategories, createCategory, updateCategory, deleteCategory } = require('~/models'); + logger.info('Syncing custom agent categories...'); + + // Get all categories from DB + const dbCategories = await getAllCategories(); + logger.info(`Found ${dbCategories.length} categories in the database.`); + + logger.info( + `${enableDefaultCategories ? 'Enabling' : 'Disabling'} default agent categories as per configuration.`, + ); + for (const cat of dbCategories.filter((c) => !c.custom)) { + logger.info(`${enableDefaultCategories ? 'Enabling' : 'Disabling'} category: ${cat.value}`); + await updateCategory(cat.value, { isActive: enableDefaultCategories }); + } + + // Initial order takes the max value of existing default categories + 1 + const defaultCategoriesInDb = dbCategories.filter((cat) => !cat.custom); + const initialOrder = + defaultCategoriesInDb.reduce((max, cat) => Math.max(max, cat.order || 0), 0) + 1; + logger.info(`Current order for new custom categories starts at ${initialOrder}.`); + + // Inject order to custom categories + logger.info('Assigning order to custom categories.'); + customCategoriesList = customCategoriesList.map((cat, index) => ({ + ...cat, + order: initialOrder + index, + })); + + // Delete custom categories not in the config file + const customCategoryValues = new Set(customCategoriesList.map((cat) => cat.value.toLowerCase())); + const categoriesToDelete = dbCategories.filter( + (cat) => cat.custom && !customCategoryValues.has(cat.value.toLowerCase()), + ); + for (const cat of categoriesToDelete) { + logger.info(`Deleting custom category not in config: ${cat.value}`); + await deleteCategory(cat.value); + } + + // Sync custom categories + for (const category of customCategoriesList) { + let formattedCategory; + try { + formattedCategory = { + value: category.value.toLowerCase() || 'unnamed', + label: category.value || 'Unnamed', + description: category.description || '', + isActive: true, + custom: true, + order: category.order, + }; + } catch (error) { + logger.error('Error formatting category: ', category, error); + continue; + } + + // Check if category exists by value + const existing = dbCategories.find( + (cat) => cat.value.toLowerCase() === formattedCategory.value, + ); + if (existing) { + logger.info(`Updating category: ${formattedCategory.value}`); + await updateCategory(existing.value, formattedCategory); + } else { + logger.info(`Creating category: ${formattedCategory.value}`); + await createCategory(formattedCategory); + } + } + + logger.info('Custom categories synchronized successfully.'); +} + +module.exports = { + syncCategories, +}; diff --git a/librechat.example.yaml b/librechat.example.yaml index 92206c4b6e..96dd42b998 100644 --- a/librechat.example.yaml +++ b/librechat.example.yaml @@ -100,7 +100,15 @@ interface: groups: true roles: true marketplace: - use: false + use: false + categories: + enableDefaultCategories: true + # list: + # - value: "Education" + # description: "Educational agents." + # - value: "Productivity" + # description: "Productivity agents." + fileCitations: true # Remote Agents configuration # Controls user permissions for remote agents with external API support