/**
 * SpecVerse LLM Provider Interface
 * Abstracts different LLM providers (OpenAI, Anthropic, Azure, local)
 */
/**
 * Abstract base class for LLM providers
 */
export class LLMProvider {
    config;
    constructor(config) {
        this.config = config;
    }
    /**
     * Update configuration
     */
    configure(newConfig) {
        this.config = { ...this.config, ...newConfig };
    }
    /**
     * Get current configuration (without sensitive data)
     */
    getConfig() {
        const { apiKey, ...safeConfig } = this.config;
        return safeConfig;
    }
    /**
     * Validate configuration
     */
    validateConfig() {
        if (!this.config.model) {
            throw new Error('Model is required in provider configuration');
        }
    }
    /**
     * Merge completion options with defaults
     */
    mergeOptions(options) {
        return {
            max_tokens: 4000,
            temperature: 0.7,
            top_p: 1.0,
            ...this.config.defaultOptions,
            ...options
        };
    }
    /**
     * Handle provider errors
     */
    handleError(error) {
        if (error.response?.status === 401) {
            throw new Error(`Authentication failed for ${this.getInfo().name}: Check API key`);
        }
        else if (error.response?.status === 429) {
            throw new Error(`Rate limit exceeded for ${this.getInfo().name}: Try again later`);
        }
        else if (error.response?.status >= 500) {
            throw new Error(`${this.getInfo().name} service error: ${error.message}`);
        }
        else {
            throw new Error(`${this.getInfo().name} error: ${error.message}`);
        }
    }
}
/**
 * Provider registry for managing multiple providers
 */
export class LLMProviderRegistry {
    providers = new Map();
    defaultProvider;
    /**
     * Register a provider
     */
    register(name, provider) {
        this.providers.set(name, provider);
        if (!this.defaultProvider) {
            this.defaultProvider = name;
        }
    }
    /**
     * Get a provider by name
     */
    get(name) {
        const providerName = name || this.defaultProvider;
        if (!providerName) {
            throw new Error('No LLM provider configured');
        }
        const provider = this.providers.get(providerName);
        if (!provider) {
            throw new Error(`LLM provider '${providerName}' not found`);
        }
        return provider;
    }
    /**
     * Set default provider
     */
    setDefault(name) {
        if (!this.providers.has(name)) {
            throw new Error(`Provider '${name}' not registered`);
        }
        this.defaultProvider = name;
    }
    /**
     * List all registered providers
     */
    list() {
        return Array.from(this.providers.entries()).map(([name, provider]) => ({
            name,
            info: provider.getInfo()
        }));
    }
    /**
     * Test all providers
     */
    async testAll() {
        const results = [];
        for (const [name, provider] of this.providers) {
            try {
                const success = await provider.test();
                results.push({ name, success });
            }
            catch (error) {
                results.push({
                    name,
                    success: false,
                    error: error instanceof Error ? error.message : String(error)
                });
            }
        }
        return results;
    }
    /**
     * Clear all providers
     */
    clear() {
        this.providers.clear();
        this.defaultProvider = undefined;
    }
}
// Export singleton registry
export const llmRegistry = new LLMProviderRegistry();
//# sourceMappingURL=llm-provider.js.map