// tools/configure-llm-providers-handler.ts — Handler for configure_llm_providers tool.
// SPEC-083 AC-08
import providersData from '../config/llm-providers.json' with { type: 'json' };
/** In-memory config state. In production, persisted to data/global/llm-provider-config.json. */
let currentConfig = {
    providers: providersData.providers.map((p) => p.id),
    failoverOrder: providersData.providers.map((p) => p.id),
    strategy: 'cost-based',
    customProviders: [],
};
/**
 * Get the current LLM provider configuration.
 */
export function getCurrentConfig() {
    return { ...currentConfig };
}
/**
 * Get all provider definitions (built-in + custom).
 */
export function getAllProviderDefinitions() {
    const builtIn = providersData.providers;
    return [...builtIn, ...currentConfig.customProviders];
}
/**
 * Handle the configure_llm_providers tool call.
 */
export function handleConfigureLLMProviders(args) {
    switch (args.action) {
        case 'add':
            return handleAdd(args);
        case 'remove':
            return handleRemove(args);
        case 'update':
            return handleUpdate(args);
        case 'set_priority':
            return handleSetPriority(args);
        case 'set_strategy':
            return handleSetStrategy(args);
    }
}
function handleAdd(args) {
    if (!args.provider) {
        return errorResult('Provider definition is required for "add" action.');
    }
    const existing = getAllProviderDefinitions().find((p) => p.id === args.provider?.id);
    if (existing) {
        return errorResult(`Provider "${String(args.provider.id)}" already exists.`);
    }
    currentConfig.customProviders.push(args.provider);
    currentConfig.providers.push(args.provider.id);
    currentConfig.failoverOrder.push(args.provider.id);
    return successResult(`Provider "${args.provider.name}" added successfully.`);
}
function handleRemove(args) {
    if (!args.providerId) {
        return errorResult('providerId is required for "remove" action.');
    }
    const remaining = currentConfig.providers.filter((p) => p !== args.providerId);
    if (remaining.length === 0) {
        return errorResult('Cannot remove the last provider. At least 1 must remain configured.');
    }
    currentConfig.providers = remaining;
    currentConfig.failoverOrder = currentConfig.failoverOrder.filter((p) => p !== args.providerId);
    currentConfig.customProviders = currentConfig.customProviders.filter((p) => p.id !== args.providerId);
    return successResult(`Provider "${args.providerId}" removed.`);
}
function handleUpdate(args) {
    if (!args.provider) {
        return errorResult('Provider definition is required for "update" action.');
    }
    const idx = currentConfig.customProviders.findIndex((p) => p.id === args.provider?.id);
    if (idx >= 0) {
        currentConfig.customProviders[idx] = args.provider;
    }
    else {
        // If updating a built-in, add as custom override
        currentConfig.customProviders.push(args.provider);
    }
    return successResult(`Provider "${args.provider.name}" updated.`);
}
function handleSetPriority(args) {
    if (!args.failoverOrder || args.failoverOrder.length === 0) {
        return errorResult('failoverOrder is required for "set_priority" action.');
    }
    currentConfig.failoverOrder = args.failoverOrder;
    return successResult(`Failover priority updated: ${args.failoverOrder.join(' > ')}`);
}
function handleSetStrategy(args) {
    if (!args.strategy) {
        return errorResult('strategy is required for "set_strategy" action.');
    }
    currentConfig.strategy = args.strategy;
    return successResult(`Load balancing strategy set to "${args.strategy}".`);
}
function successResult(message) {
    const lines = [
        `## LLM Provider Configuration`,
        '',
        message,
        '',
        `**Active providers:** ${currentConfig.providers.join(', ')}`,
        `**Failover order:** ${currentConfig.failoverOrder.join(' > ')}`,
        `**Strategy:** ${currentConfig.strategy}`,
    ];
    if (currentConfig.customProviders.length > 0) {
        lines.push(`**Custom providers:** ${currentConfig.customProviders.map((p) => p.name).join(', ')}`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
function errorResult(message) {
    return {
        content: [{ type: 'text', text: `Error: ${message}` }],
        isError: true,
    };
}
/**
 * Reset config to defaults (for testing).
 */
export function resetConfig() {
    currentConfig = {
        providers: providersData.providers.map((p) => p.id),
        failoverOrder: providersData.providers.map((p) => p.id),
        strategy: 'cost-based',
        customProviders: [],
    };
}
//# sourceMappingURL=configure-llm-providers-handler.js.map