@just-every/ensemble
Version:
LLM provider abstraction layer with unified streaming interface
100 lines • 3.62 kB
JavaScript
import { ConfigurationError } from '../types/errors.js';
const DEFAULT_CONFIG = {
defaultToolTimeout: 300000,
maxConcurrentTools: 10,
pauseCheckInterval: 100,
historyCompactionThreshold: 0.7,
apiKeys: {},
};
class ConfigManager {
config;
isLoaded = false;
constructor() {
this.config = { ...DEFAULT_CONFIG };
}
loadConfig() {
if (this.isLoaded)
return;
this.config.apiKeys = {
openai: process.env.OPENAI_API_KEY,
anthropic: process.env.ANTHROPIC_API_KEY,
google: process.env.GOOGLE_API_KEY,
deepseek: process.env.DEEPSEEK_API_KEY,
xai: process.env.XAI_API_KEY,
openrouter: process.env.OPENROUTER_API_KEY,
};
if (process.env.ENSEMBLE_TOOL_TIMEOUT) {
const timeout = parseInt(process.env.ENSEMBLE_TOOL_TIMEOUT, 10);
if (isNaN(timeout) || timeout <= 0) {
throw new ConfigurationError('ENSEMBLE_TOOL_TIMEOUT must be a positive number', {
value: process.env.ENSEMBLE_TOOL_TIMEOUT,
});
}
this.config.defaultToolTimeout = timeout;
}
if (process.env.ENSEMBLE_MAX_CONCURRENT_TOOLS) {
const maxTools = parseInt(process.env.ENSEMBLE_MAX_CONCURRENT_TOOLS, 10);
if (isNaN(maxTools) || maxTools <= 0) {
throw new ConfigurationError('ENSEMBLE_MAX_CONCURRENT_TOOLS must be a positive number', {
value: process.env.ENSEMBLE_MAX_CONCURRENT_TOOLS,
});
}
this.config.maxConcurrentTools = maxTools;
}
if (process.env.ENSEMBLE_PAUSE_CHECK_INTERVAL) {
const interval = parseInt(process.env.ENSEMBLE_PAUSE_CHECK_INTERVAL, 10);
if (isNaN(interval) || interval <= 0) {
throw new ConfigurationError('ENSEMBLE_PAUSE_CHECK_INTERVAL must be a positive number', {
value: process.env.ENSEMBLE_PAUSE_CHECK_INTERVAL,
});
}
this.config.pauseCheckInterval = interval;
}
if (process.env.ENSEMBLE_HISTORY_COMPACTION_THRESHOLD) {
const threshold = parseFloat(process.env.ENSEMBLE_HISTORY_COMPACTION_THRESHOLD);
if (isNaN(threshold) || threshold <= 0 || threshold > 1) {
throw new ConfigurationError('ENSEMBLE_HISTORY_COMPACTION_THRESHOLD must be a number between 0 and 1', {
value: process.env.ENSEMBLE_HISTORY_COMPACTION_THRESHOLD,
});
}
this.config.historyCompactionThreshold = threshold;
}
this.isLoaded = true;
}
getConfig() {
if (!this.isLoaded) {
this.loadConfig();
}
return { ...this.config };
}
get(key) {
return this.getConfig()[key];
}
updateConfig(updates) {
this.config = { ...this.config, ...updates };
}
reset() {
this.config = { ...DEFAULT_CONFIG };
this.isLoaded = false;
}
hasApiKey(provider) {
return !!this.getConfig().apiKeys[provider];
}
getApiKey(provider) {
return this.getConfig().apiKeys[provider];
}
}
let configManager = null;
export function getConfigManager() {
if (!configManager) {
configManager = new ConfigManager();
}
return configManager;
}
export function getConfig() {
return getConfigManager().getConfig();
}
export function getConfigValue(key) {
return getConfigManager().get(key);
}
//# sourceMappingURL=config_manager.js.map