import { MAX_OUTPUT_TOKENS, MODEL_CAPABILITIES, } from "@superblocksteam/library-shared/types";
import { getLogger } from "../../util/logger.js";
/**
 * Processes LLM config and returns the configuration options for AI SDK calls.
 *
 * @param llmConfig - The LLM provider configuration
 * @param defaultBudgetTokens - Default thinking budget if not specified (default: 5000)
 * @param logContext - Optional context string for logging
 */
export function processLLMConfig(llmConfig, defaultBudgetTokens = 5000, logContext) {
    const logger = getLogger();
    // Get thinking configuration from context, with defaults
    const thinkingEnabled = llmConfig?.thinkingEnabled ?? true;
    let thinkingBudgetTokens = llmConfig?.thinkingBudgetTokens
        ? parseInt(llmConfig.thinkingBudgetTokens)
        : defaultBudgetTokens;
    const interleavedThinking = llmConfig?.interleavedThinking ?? true;
    const maxOutputTokens = MAX_OUTPUT_TOKENS;
    // Validate total output tokens don't exceed model limits
    if (thinkingEnabled) {
        const coreModel = llmConfig?.coreModel ?? "claude-sonnet-4-5";
        const modelCaps = MODEL_CAPABILITIES[coreModel];
        const modelMaxOutput = modelCaps?.maxOutputTokens ?? 64_000;
        const total = maxOutputTokens + thinkingBudgetTokens;
        if (total > modelMaxOutput) {
            const adjustedThinkingBudget = modelMaxOutput - maxOutputTokens;
            logger.warn(`maxOutputTokens (${maxOutputTokens}) + thinkingBudget (${thinkingBudgetTokens}) = ${total} ` +
                `exceeds model max (${modelMaxOutput}). Reducing thinkingBudget to ${adjustedThinkingBudget}.`);
            thinkingBudgetTokens = Math.max(0, adjustedThinkingBudget);
        }
    }
    // Log LLM provider configuration if context is provided
    if (logContext) {
        getLogger().info(`${logContext} LLM Provider Config: provider=${llmConfig?.provider || "bedrock (default)"}, ` +
            `thinking=${thinkingEnabled}, budget=${thinkingBudgetTokens}, ` +
            `interleaved=${interleavedThinking}`);
    }
    // Build provider options and headers based on configuration
    const providerOptions = thinkingEnabled
        ? {
            anthropic: {
                thinking: { type: "enabled", budgetTokens: thinkingBudgetTokens },
            },
            bedrock: {
                additionalModelRequestFields: {
                    thinking: { type: "enabled", budget_tokens: thinkingBudgetTokens },
                    anthropic_beta: ["interleaved-thinking-2025-05-14"],
                },
            },
        }
        : undefined;
    const headers = thinkingEnabled && interleavedThinking
        ? {
            "anthropic-beta": "interleaved-thinking-2025-05-14",
        }
        : {};
    return {
        thinkingEnabled,
        thinkingBudgetTokens,
        interleavedThinking,
        maxOutputTokens,
        providerOptions,
        headers,
    };
}
//# sourceMappingURL=llm-config-utils.js.map