2025-08-26 12:10:18 -04:00
|
|
|
const { CacheKeys } = require('librechat-data-provider');
|
2025-10-05 06:37:57 -04:00
|
|
|
const { logger, AppService } = require('@librechat/data-schemas');
|
|
|
|
|
const { loadAndFormatTools } = require('~/server/services/start/tools');
|
|
|
|
|
const loadCustomConfig = require('./loadCustomConfig');
|
2025-08-26 12:10:18 -04:00
|
|
|
const { setCachedTools } = require('./getCachedTools');
|
|
|
|
|
const getLogStores = require('~/cache/getLogStores');
|
2025-10-05 06:37:57 -04:00
|
|
|
const paths = require('~/config/paths');
|
2025-08-26 12:10:18 -04:00
|
|
|
|
2025-09-10 16:46:54 -06:00
|
|
|
const BASE_CONFIG_KEY = '_BASE_';
|
|
|
|
|
|
2025-10-05 06:37:57 -04:00
|
|
|
const loadBaseConfig = async () => {
|
|
|
|
|
/** @type {TCustomConfig} */
|
|
|
|
|
const config = (await loadCustomConfig()) ?? {};
|
|
|
|
|
/** @type {Record<string, FunctionTool>} */
|
|
|
|
|
const systemTools = loadAndFormatTools({
|
|
|
|
|
adminFilter: config.filteredTools,
|
|
|
|
|
adminIncluded: config.includedTools,
|
|
|
|
|
directory: paths.structuredTools,
|
|
|
|
|
});
|
|
|
|
|
return AppService({ config, paths, systemTools });
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-26 12:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Get the app configuration based on user context
|
|
|
|
|
* @param {Object} [options]
|
|
|
|
|
* @param {string} [options.role] - User role for role-based config
|
|
|
|
|
* @param {boolean} [options.refresh] - Force refresh the cache
|
|
|
|
|
* @returns {Promise<AppConfig>}
|
|
|
|
|
*/
|
|
|
|
|
async function getAppConfig(options = {}) {
|
|
|
|
|
const { role, refresh } = options;
|
|
|
|
|
|
2025-09-10 16:46:54 -06:00
|
|
|
const cache = getLogStores(CacheKeys.APP_CONFIG);
|
|
|
|
|
const cacheKey = role ? role : BASE_CONFIG_KEY;
|
2025-08-26 12:10:18 -04:00
|
|
|
|
|
|
|
|
if (!refresh) {
|
|
|
|
|
const cached = await cache.get(cacheKey);
|
|
|
|
|
if (cached) {
|
|
|
|
|
return cached;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 16:46:54 -06:00
|
|
|
let baseConfig = await cache.get(BASE_CONFIG_KEY);
|
2025-08-26 12:10:18 -04:00
|
|
|
if (!baseConfig) {
|
|
|
|
|
logger.info('[getAppConfig] App configuration not initialized. Initializing AppService...');
|
2025-10-05 06:37:57 -04:00
|
|
|
baseConfig = await loadBaseConfig();
|
2025-08-26 12:10:18 -04:00
|
|
|
|
|
|
|
|
if (!baseConfig) {
|
|
|
|
|
throw new Error('Failed to initialize app configuration through AppService.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (baseConfig.availableTools) {
|
2025-09-21 07:56:40 -04:00
|
|
|
await setCachedTools(baseConfig.availableTools);
|
2025-08-26 12:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 16:46:54 -06:00
|
|
|
await cache.set(BASE_CONFIG_KEY, baseConfig);
|
2025-08-26 12:10:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For now, return the base config
|
|
|
|
|
// In the future, this is where we'll apply role-based modifications
|
|
|
|
|
if (role) {
|
|
|
|
|
// TODO: Apply role-based config modifications
|
|
|
|
|
// const roleConfig = await applyRoleBasedConfig(baseConfig, role);
|
|
|
|
|
// await cache.set(cacheKey, roleConfig);
|
|
|
|
|
// return roleConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear the app configuration cache
|
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
|
*/
|
|
|
|
|
async function clearAppConfigCache() {
|
|
|
|
|
const cache = getLogStores(CacheKeys.CONFIG_STORE);
|
|
|
|
|
const cacheKey = CacheKeys.APP_CONFIG;
|
|
|
|
|
return await cache.delete(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getAppConfig,
|
|
|
|
|
clearAppConfigCache,
|
|
|
|
|
};
|