mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* 🚀 feat: Add automatic refill settings to balance schema * 🚀 feat: Refactor balance feature to use global interface configuration * 🚀 feat: Implement auto-refill functionality for balance management * 🚀 feat: Enhance auto-refill logic and configuration for balance management * 🚀 chore: Bump version to 0.7.74 in package.json and package-lock.json * 🚀 chore: Bump version to 0.0.5 in package.json and package-lock.json * 🚀 docs: Update comment for balance settings in librechat.example.yaml * chore: space in `.env.example` * 🚀 feat: Implement balance configuration loading and refactor related components * 🚀 test: Refactor tests to use custom config for balance feature * 🚀 fix: Update balance response handling in Transaction.js to use Balance model * 🚀 test: Update AppService tests to include balance configuration in mock setup * 🚀 test: Enhance AppService tests with complete balance configuration scenarios * 🚀 refactor: Rename balanceConfig to balance and update related tests for clarity * 🚀 refactor: Remove loadDefaultBalance and update balance handling in AppService * 🚀 test: Update AppService tests to reflect new balance structure and defaults * 🚀 test: Mock getCustomConfig in BaseClient tests to control balance configuration * 🚀 test: Add get method to mockCache in OpenAIClient tests for improved cache handling * 🚀 test: Mock getCustomConfig in OpenAIClient tests to control balance configuration * 🚀 test: Remove mock for getCustomConfig in OpenAIClient tests to streamline configuration handling * 🚀 fix: Update balance configuration reference in config.js for consistency * refactor: Add getBalanceConfig function to retrieve balance configuration * chore: Comment out example balance settings in librechat.example.yaml * refactor: Replace getCustomConfig with getBalanceConfig for balance handling * fix: tests * refactor: Replace getBalanceConfig call with balance from request locals * refactor: Update balance handling to use environment variables for configuration * refactor: Replace getBalanceConfig calls with balance from request locals * refactor: Simplify balance configuration logic in getBalanceConfig --------- Co-authored-by: Danny Avila <danny@librechat.ai>
154 lines
4.7 KiB
JavaScript
154 lines
4.7 KiB
JavaScript
const {
|
|
FileSources,
|
|
EModelEndpoint,
|
|
loadOCRConfig,
|
|
processMCPEnv,
|
|
getConfigDefaults,
|
|
} = require('librechat-data-provider');
|
|
const { checkVariables, checkHealth, checkConfig, checkAzureVariables } = require('./start/checks');
|
|
const { azureAssistantsDefaults, assistantsConfigSetup } = require('./start/assistants');
|
|
const { initializeAzureBlobService } = require('./Files/Azure/initialize');
|
|
const { initializeFirebase } = require('./Files/Firebase/initialize');
|
|
const loadCustomConfig = require('./Config/loadCustomConfig');
|
|
const handleRateLimits = require('./Config/handleRateLimits');
|
|
const { loadDefaultInterface } = require('./start/interface');
|
|
const { azureConfigSetup } = require('./start/azureOpenAI');
|
|
const { processModelSpecs } = require('./start/modelSpecs');
|
|
const { initializeS3 } = require('./Files/S3/initialize');
|
|
const { loadAndFormatTools } = require('./ToolService');
|
|
const { agentsConfigSetup } = require('./start/agents');
|
|
const { initializeRoles } = require('~/models/Role');
|
|
const { isEnabled } = require('~/server/utils');
|
|
const { getMCPManager } = require('~/config');
|
|
const paths = require('~/config/paths');
|
|
|
|
/**
|
|
*
|
|
* Loads custom config and initializes app-wide variables.
|
|
* @function AppService
|
|
* @param {Express.Application} app - The Express application object.
|
|
*/
|
|
const AppService = async (app) => {
|
|
await initializeRoles();
|
|
/** @type {TCustomConfig} */
|
|
const config = (await loadCustomConfig()) ?? {};
|
|
const configDefaults = getConfigDefaults();
|
|
|
|
const ocr = loadOCRConfig(config.ocr);
|
|
const filteredTools = config.filteredTools;
|
|
const includedTools = config.includedTools;
|
|
const fileStrategy = config.fileStrategy ?? configDefaults.fileStrategy;
|
|
const startBalance = process.env.START_BALANCE;
|
|
const balance = config.balance ?? {
|
|
enabled: isEnabled(process.env.CHECK_BALANCE),
|
|
startBalance: startBalance ? parseInt(startBalance, 10) : undefined,
|
|
};
|
|
const imageOutputType = config?.imageOutputType ?? configDefaults.imageOutputType;
|
|
|
|
process.env.CDN_PROVIDER = fileStrategy;
|
|
|
|
checkVariables();
|
|
await checkHealth();
|
|
|
|
if (fileStrategy === FileSources.firebase) {
|
|
initializeFirebase();
|
|
} else if (fileStrategy === FileSources.azure) {
|
|
initializeAzureBlobService();
|
|
} else if (fileStrategy === FileSources.s3) {
|
|
initializeS3();
|
|
}
|
|
|
|
/** @type {Record<string, FunctionTool>} */
|
|
const availableTools = loadAndFormatTools({
|
|
adminFilter: filteredTools,
|
|
adminIncluded: includedTools,
|
|
directory: paths.structuredTools,
|
|
});
|
|
|
|
if (config.mcpServers != null) {
|
|
const mcpManager = await getMCPManager();
|
|
await mcpManager.initializeMCP(config.mcpServers, processMCPEnv);
|
|
await mcpManager.mapAvailableTools(availableTools);
|
|
}
|
|
|
|
const socialLogins =
|
|
config?.registration?.socialLogins ?? configDefaults?.registration?.socialLogins;
|
|
const interfaceConfig = await loadDefaultInterface(config, configDefaults);
|
|
|
|
const defaultLocals = {
|
|
ocr,
|
|
paths,
|
|
fileStrategy,
|
|
socialLogins,
|
|
filteredTools,
|
|
includedTools,
|
|
availableTools,
|
|
imageOutputType,
|
|
interfaceConfig,
|
|
balance,
|
|
};
|
|
|
|
if (!Object.keys(config).length) {
|
|
app.locals = defaultLocals;
|
|
return;
|
|
}
|
|
|
|
checkConfig(config);
|
|
handleRateLimits(config?.rateLimits);
|
|
|
|
const endpointLocals = {};
|
|
const endpoints = config?.endpoints;
|
|
|
|
if (endpoints?.[EModelEndpoint.azureOpenAI]) {
|
|
endpointLocals[EModelEndpoint.azureOpenAI] = azureConfigSetup(config);
|
|
checkAzureVariables();
|
|
}
|
|
|
|
if (endpoints?.[EModelEndpoint.azureOpenAI]?.assistants) {
|
|
endpointLocals[EModelEndpoint.azureAssistants] = azureAssistantsDefaults();
|
|
}
|
|
|
|
if (endpoints?.[EModelEndpoint.azureAssistants]) {
|
|
endpointLocals[EModelEndpoint.azureAssistants] = assistantsConfigSetup(
|
|
config,
|
|
EModelEndpoint.azureAssistants,
|
|
endpointLocals[EModelEndpoint.azureAssistants],
|
|
);
|
|
}
|
|
|
|
if (endpoints?.[EModelEndpoint.assistants]) {
|
|
endpointLocals[EModelEndpoint.assistants] = assistantsConfigSetup(
|
|
config,
|
|
EModelEndpoint.assistants,
|
|
endpointLocals[EModelEndpoint.assistants],
|
|
);
|
|
}
|
|
|
|
if (endpoints?.[EModelEndpoint.agents]) {
|
|
endpointLocals[EModelEndpoint.agents] = agentsConfigSetup(config);
|
|
}
|
|
|
|
const endpointKeys = [
|
|
EModelEndpoint.openAI,
|
|
EModelEndpoint.google,
|
|
EModelEndpoint.bedrock,
|
|
EModelEndpoint.anthropic,
|
|
EModelEndpoint.gptPlugins,
|
|
];
|
|
|
|
endpointKeys.forEach((key) => {
|
|
if (endpoints?.[key]) {
|
|
endpointLocals[key] = endpoints[key];
|
|
}
|
|
});
|
|
|
|
app.locals = {
|
|
...defaultLocals,
|
|
fileConfig: config?.fileConfig,
|
|
secureImageLinks: config?.secureImageLinks,
|
|
modelSpecs: processModelSpecs(endpoints, config.modelSpecs),
|
|
...endpointLocals,
|
|
};
|
|
};
|
|
|
|
module.exports = AppService;
|