@tehreet/conduit
Version:
LLM API gateway with intelligent routing, robust process management, and health monitoring
167 lines • 6.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigPresetManager = void 0;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const fs_1 = require("fs");
const log_1 = require("./log");
class ConfigPresetManager {
static async loadPreset(presetPath) {
// Check cache first
if (this.presetCache.has(presetPath)) {
return this.presetCache.get(presetPath);
}
try {
if (!(0, fs_1.existsSync)(presetPath)) {
throw new Error(`Preset file not found: ${presetPath}`);
}
const presetData = await (0, promises_1.readFile)(presetPath, 'utf-8');
const preset = JSON.parse(presetData);
// Validate preset
this.validatePreset(preset);
// Cache the preset
this.presetCache.set(presetPath, preset);
(0, log_1.log)(`Loaded configuration preset: ${preset.name}`);
return preset;
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to load preset from ${presetPath}: ${message}`);
}
}
static async loadBuiltinPreset(presetName) {
// Look for preset in the config directory
const presetPath = (0, path_1.join)(process.cwd(), 'config', `${presetName}.json`);
return this.loadPreset(presetPath);
}
static async applyPresetToConfig(config) {
let preset = null;
// Load preset if specified
if (config.preset) {
try {
if (config.preset.endsWith('.json') || config.preset.includes('/')) {
// Assume it's a file path
preset = await this.loadPreset(config.preset);
}
else {
// Assume it's a built-in preset name
preset = await this.loadBuiltinPreset(config.preset);
}
}
catch (error) {
(0, log_1.log)(`Warning: Failed to load preset '${config.preset}':`, error);
}
}
// If no preset loaded, return config as-is
if (!preset) {
return config;
}
// Merge preset into config
const mergedConfig = {
...config,
presetConfig: preset,
};
// Convert preset providers to legacy format for backward compatibility
if (preset.providers && preset.providers.length > 0) {
mergedConfig.Providers = this.convertProvidersToLegacyFormat(preset.providers);
mergedConfig.providers = mergedConfig.Providers; // Ensure both formats exist
}
// Set up routing configuration
if (preset.defaultRouting) {
mergedConfig.Router = {
...mergedConfig.Router,
default: preset.defaultRouting.fallback || mergedConfig.Router?.default,
// Keep existing routing overrides
longContext: mergedConfig.Router?.longContext,
background: mergedConfig.Router?.background,
think: mergedConfig.Router?.think,
};
}
return mergedConfig;
}
static validatePreset(preset) {
if (!preset.name) {
throw new Error('Preset must have a name');
}
if (!preset.providers ||
!Array.isArray(preset.providers) ||
preset.providers.length === 0) {
throw new Error('Preset must have at least one provider');
}
for (const provider of preset.providers) {
if (!provider.name || !provider.type) {
throw new Error('Each provider must have a name and type');
}
if (!provider.models ||
!Array.isArray(provider.models) ||
provider.models.length === 0) {
throw new Error(`Provider ${provider.name} must have at least one model`);
}
for (const model of provider.models) {
if (!model.id) {
throw new Error(`All models in provider ${provider.name} must have an id`);
}
}
}
}
static convertProvidersToLegacyFormat(providers) {
return providers.map(provider => {
const legacyProvider = {
name: provider.name,
type: provider.type,
};
// Handle different provider types
if (provider.type === 'claude-cli') {
legacyProvider.binary = provider.binary || 'claude';
legacyProvider.models = provider.models.map(m => m.id);
}
else {
// HTTP-based provider
if (provider.baseUrl)
legacyProvider.api_base_url = provider.baseUrl;
if (provider.apiKey)
legacyProvider.api_key = provider.apiKey;
legacyProvider.models = provider.models.map(m => m.id);
}
return legacyProvider;
});
}
static getAvailableModels(config) {
const models = [];
// Get models from preset
if (config.presetConfig?.providers) {
for (const provider of config.presetConfig.providers) {
for (const model of provider.models) {
models.push(model.id);
if (model.aliases) {
models.push(...model.aliases);
}
}
}
}
// Get models from legacy providers
if (config.Providers || config.providers) {
const providers = config.Providers || config.providers || [];
for (const provider of providers) {
if (provider.models) {
if (Array.isArray(provider.models)) {
models.push(...provider.models);
}
else {
models.push(provider.models);
}
}
}
}
return [...new Set(models)]; // Remove duplicates
}
static getRoutingRules(config) {
return config.presetConfig?.defaultRouting?.rules || [];
}
static clearCache() {
this.presetCache.clear();
}
}
exports.ConfigPresetManager = ConfigPresetManager;
ConfigPresetManager.presetCache = new Map();
//# sourceMappingURL=config-presets.js.map