UNPKG

@tehreet/conduit

Version:

LLM API gateway with intelligent routing, robust process management, and health monitoring

181 lines 6.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateSynapseConfig = validateSynapseConfig; exports.countTokens = countTokens; exports.estimateTokensFromContext = estimateTokensFromContext; const tiktoken_1 = require("tiktoken"); const enc = (0, tiktoken_1.get_encoding)('cl100k_base'); function validateSynapseConfig(config) { const errors = []; const warnings = []; const models = []; try { // Check if it's a model config or a full preset if (config.enabled !== undefined || config.defaultModel !== undefined || config.useProjectDefaults !== undefined) { return validateModelConfig(config); } // Validate as preset config if (!config.name || typeof config.name !== 'string') { errors.push('Config must have a valid "name" field'); } if (!config.providers || !Array.isArray(config.providers)) { errors.push('Config must have a "providers" array'); } else { for (let i = 0; i < config.providers.length; i++) { const provider = config.providers[i]; const providerErrors = validateProvider(provider, i); errors.push(...providerErrors); if (provider.models && Array.isArray(provider.models)) { provider.models.forEach((model) => { if (model.id) models.push(model.id); if (model.aliases) models.push(...model.aliases); }); } } } if (config.defaultRouting) { const routingErrors = validateDefaultRouting(config.defaultRouting); errors.push(...routingErrors); } if (config.synapseIntegration) { const synapseErrors = validateSynapseIntegration(config.synapseIntegration); errors.push(...synapseErrors); } return { valid: errors.length === 0, errors, warnings, models: [...new Set(models)], // Remove duplicates }; } catch (error) { return { valid: false, errors: [`Configuration parsing error: ${error}`], }; } } function validateModelConfig(config) { const errors = []; if (config.enabled !== undefined && typeof config.enabled !== 'boolean') { errors.push('enabled must be a boolean'); } if (config.defaultModel !== undefined && typeof config.defaultModel !== 'string') { errors.push('defaultModel must be a string'); } if (config.useProjectDefaults !== undefined && typeof config.useProjectDefaults !== 'boolean') { errors.push('useProjectDefaults must be a boolean'); } if (config.overrideModel !== undefined && typeof config.overrideModel !== 'string') { errors.push('overrideModel must be a string'); } const models = []; if (config.defaultModel) models.push(config.defaultModel); if (config.overrideModel) models.push(config.overrideModel); return { valid: errors.length === 0, errors, models, }; } function validateProvider(provider, index) { const errors = []; const prefix = `Provider ${index}:`; if (!provider.name || typeof provider.name !== 'string') { errors.push(`${prefix} must have a valid "name" field`); } if (!provider.type || typeof provider.type !== 'string') { errors.push(`${prefix} must have a valid "type" field`); } if (!provider.models || !Array.isArray(provider.models)) { errors.push(`${prefix} must have a "models" array`); } else { provider.models.forEach((model, modelIndex) => { if (!model.id || typeof model.id !== 'string') { errors.push(`${prefix} Model ${modelIndex} must have a valid "id" field`); } if (model.aliases && !Array.isArray(model.aliases)) { errors.push(`${prefix} Model ${modelIndex} "aliases" must be an array`); } if (model.contextWindow && typeof model.contextWindow !== 'number') { errors.push(`${prefix} Model ${modelIndex} "contextWindow" must be a number`); } if (model.capabilities && !Array.isArray(model.capabilities)) { errors.push(`${prefix} Model ${modelIndex} "capabilities" must be an array`); } }); } return errors; } function validateDefaultRouting(routing) { const errors = []; if (routing.rules && !Array.isArray(routing.rules)) { errors.push('defaultRouting.rules must be an array'); } else if (routing.rules) { routing.rules.forEach((rule, index) => { if (!rule.name || typeof rule.name !== 'string') { errors.push(`Routing rule ${index} must have a valid "name" field`); } if (!rule.condition || typeof rule.condition !== 'string') { errors.push(`Routing rule ${index} must have a valid "condition" field`); } if (!rule.model || typeof rule.model !== 'string') { errors.push(`Routing rule ${index} must have a valid "model" field`); } }); } if (routing.fallback && typeof routing.fallback !== 'string') { errors.push('defaultRouting.fallback must be a string'); } return errors; } function validateSynapseIntegration(integration) { const errors = []; if (typeof integration.enabled !== 'boolean') { errors.push('synapseIntegration.enabled must be a boolean'); } if (integration.telemetryEndpoint && typeof integration.telemetryEndpoint !== 'string') { errors.push('synapseIntegration.telemetryEndpoint must be a string'); } if (integration.configSync !== undefined && typeof integration.configSync !== 'boolean') { errors.push('synapseIntegration.configSync must be a boolean'); } return errors; } function countTokens(text) { try { return enc.encode(text).length; } catch (error) { console.error('Error counting tokens:', error); return 0; } } function estimateTokensFromContext(context) { let total = 0; if (context.projectConfig) { total += countTokens(JSON.stringify(context.projectConfig)); } if (context.agentConfig) { total += countTokens(JSON.stringify(context.agentConfig)); } if (context.messageContent) { total += countTokens(context.messageContent); } return total; } //# sourceMappingURL=config-validator.js.map