mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-04 09:38:50 +01:00
refactor: decouple caching and DB operations from AppService, make part of consolidated getAppConfig
This commit is contained in:
parent
28cd234a6c
commit
f7ea232760
5 changed files with 58 additions and 33 deletions
|
|
@ -3,6 +3,7 @@ const {
|
|||
loadMemoryConfig,
|
||||
agentsConfigSetup,
|
||||
loadWebSearchConfig,
|
||||
loadDefaultInterface,
|
||||
} = require('@librechat/api');
|
||||
const {
|
||||
FileSources,
|
||||
|
|
@ -16,16 +17,14 @@ const {
|
|||
checkHealth,
|
||||
checkConfig,
|
||||
} = require('./start/checks');
|
||||
const { ensureDefaultCategories, seedDefaultRoles, initializeRoles } = require('~/models');
|
||||
const { setCachedTools, setAppConfig, loadCustomConfig } = require('./Config');
|
||||
const { initializeAzureBlobService } = require('./Files/Azure/initialize');
|
||||
const { initializeFirebase } = require('./Files/Firebase/initialize');
|
||||
const handleRateLimits = require('./Config/handleRateLimits');
|
||||
const { loadDefaultInterface } = require('./start/interface');
|
||||
const loadCustomConfig = require('./Config/loadCustomConfig');
|
||||
const { loadTurnstileConfig } = require('./start/turnstile');
|
||||
const { processModelSpecs } = require('./start/modelSpecs');
|
||||
const { initializeS3 } = require('./Files/S3/initialize');
|
||||
const { loadAndFormatTools } = require('./ToolService');
|
||||
const { loadAndFormatTools } = require('./start/tools');
|
||||
const { loadEndpoints } = require('./start/endpoints');
|
||||
const paths = require('~/config/paths');
|
||||
|
||||
|
|
@ -34,9 +33,6 @@ const paths = require('~/config/paths');
|
|||
* @function AppService
|
||||
*/
|
||||
const AppService = async () => {
|
||||
await initializeRoles();
|
||||
await seedDefaultRoles();
|
||||
await ensureDefaultCategories();
|
||||
/** @type {TCustomConfig} */
|
||||
const config = (await loadCustomConfig()) ?? {};
|
||||
const configDefaults = getConfigDefaults();
|
||||
|
|
@ -73,17 +69,11 @@ const AppService = async () => {
|
|||
adminFilter: filteredTools,
|
||||
adminIncluded: includedTools,
|
||||
directory: paths.structuredTools,
|
||||
imageOutputType,
|
||||
fileStrategy,
|
||||
});
|
||||
|
||||
await setCachedTools(availableTools, { isGlobal: true });
|
||||
|
||||
// Store MCP config for later initialization
|
||||
const mcpConfig = config.mcpServers || null;
|
||||
|
||||
const registration = config.registration ?? configDefaults.registration;
|
||||
const interfaceConfig = await loadDefaultInterface(config, configDefaults);
|
||||
const interfaceConfig = await loadDefaultInterface({ config, configDefaults });
|
||||
const turnstileConfig = loadTurnstileConfig(config, configDefaults);
|
||||
const speech = config.speech;
|
||||
|
||||
|
|
@ -100,6 +90,7 @@ const AppService = async () => {
|
|||
registration,
|
||||
filteredTools,
|
||||
includedTools,
|
||||
availableTools,
|
||||
imageOutputType,
|
||||
interfaceConfig,
|
||||
turnstileConfig,
|
||||
|
|
@ -115,8 +106,7 @@ const AppService = async () => {
|
|||
[EModelEndpoint.agents]: agentsDefaults,
|
||||
},
|
||||
};
|
||||
await setAppConfig(appConfig);
|
||||
return;
|
||||
return appConfig;
|
||||
}
|
||||
|
||||
checkConfig(config);
|
||||
|
|
@ -131,7 +121,7 @@ const AppService = async () => {
|
|||
endpoints: loadedEndpoints,
|
||||
};
|
||||
|
||||
await setAppConfig(appConfig);
|
||||
return appConfig;
|
||||
};
|
||||
|
||||
module.exports = AppService;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
const { logger } = require('@librechat/data-schemas');
|
||||
const { CacheKeys } = require('librechat-data-provider');
|
||||
const AppService = require('~/server/services/AppService');
|
||||
const { setCachedTools } = require('./getCachedTools');
|
||||
const getLogStores = require('~/cache/getLogStores');
|
||||
|
||||
/**
|
||||
|
|
@ -22,9 +24,20 @@ async function getAppConfig(options = {}) {
|
|||
}
|
||||
}
|
||||
|
||||
const baseConfig = await cache.get(CacheKeys.APP_CONFIG);
|
||||
let baseConfig = await cache.get(CacheKeys.APP_CONFIG);
|
||||
if (!baseConfig) {
|
||||
throw new Error('App configuration not initialized. Please ensure AppService has been called.');
|
||||
logger.info('[getAppConfig] App configuration not initialized. Initializing AppService...');
|
||||
baseConfig = await AppService();
|
||||
|
||||
if (!baseConfig) {
|
||||
throw new Error('Failed to initialize app configuration through AppService.');
|
||||
}
|
||||
|
||||
if (baseConfig.availableTools) {
|
||||
await setCachedTools(baseConfig.availableTools, { isGlobal: true });
|
||||
}
|
||||
|
||||
await cache.set(CacheKeys.APP_CONFIG, baseConfig);
|
||||
}
|
||||
|
||||
// For now, return the base config
|
||||
|
|
@ -49,19 +62,7 @@ async function clearAppConfigCache() {
|
|||
return await cache.delete(cacheKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the app configuration during startup
|
||||
* @param {AppConfig} config - The initial configuration to store
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async function setAppConfig(config) {
|
||||
const cache = getLogStores(CacheKeys.CONFIG_STORE);
|
||||
await cache.set(CacheKeys.APP_CONFIG, config);
|
||||
logger.debug('[getAppConfig] App configuration initialized');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getAppConfig,
|
||||
setAppConfig,
|
||||
clearAppConfigCache,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue