/**
 * Model resolver — picks the right LanguageModel implementation based on
 * environment configuration. One call site, four providers:
 *
 *   SPECVERSE_AI_PROVIDER=claude-cli          Max user, spawns claude --print
 *   SPECVERSE_AI_PROVIDER=anthropic           API-keyed Claude (metered)
 *   SPECVERSE_AI_PROVIDER=openai-compatible   DeepSeek/Groq/Together/Ollama/etc.
 *   SPECVERSE_AI_PROVIDER=stub                No LLM; emit prompt for ambient executor
 *
 * Defaults:
 *   - Inside an MCP server context (MCP_SERVER=1) → stub
 *   - Claude CLI available on PATH → claude-cli
 *   - ANTHROPIC_API_KEY set → anthropic
 *   - Else → stub (with a clear diagnostic comment)
 */
import { createAnthropic } from '@ai-sdk/anthropic';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { claudeCli, isClaudeCliAvailable } from './providers/claude-cli.js';
import { stubModel } from './providers/stub.js';
/** Return the configured provider id, resolving defaults if unset. */
export function resolveProviderId() {
    const explicit = process.env.SPECVERSE_AI_PROVIDER;
    if (explicit) {
        if (['claude-cli', 'anthropic', 'openai-compatible', 'stub'].includes(explicit)) {
            return explicit;
        }
        throw new Error(`SPECVERSE_AI_PROVIDER="${explicit}" is not recognized. Use one of: claude-cli, anthropic, openai-compatible, stub.`);
    }
    if (process.env.MCP_SERVER === '1')
        return 'stub';
    if (isClaudeCliAvailable())
        return 'claude-cli';
    if (process.env.ANTHROPIC_API_KEY)
        return 'anthropic';
    return 'stub';
}
/** Resolve an AI SDK v3 LanguageModel based on environment + options. */
export function resolveModel(options = {}) {
    const provider = resolveProviderId();
    const modelOverride = options.model ?? process.env.SPECVERSE_AI_MODEL;
    const sessionId = options.sessionId ?? process.env.SPECVERSE_AI_SESSION_ID;
    switch (provider) {
        case 'claude-cli':
            return claudeCli({ model: modelOverride, timeout: options.timeout, sessionId });
        case 'anthropic': {
            const apiKey = process.env.ANTHROPIC_API_KEY;
            if (!apiKey) {
                throw new Error('SPECVERSE_AI_PROVIDER=anthropic requires ANTHROPIC_API_KEY. ' +
                    'Set it or pick a different provider (claude-cli, openai-compatible, stub).');
            }
            const anthropic = createAnthropic({ apiKey });
            // Default to current Sonnet (4.6 as of 2026-05-06). Was previously
            // pinned to claude-3-5-sonnet-latest from an earlier model generation.
            // Override per-run via SPECVERSE_AI_MODEL or options.model.
            return anthropic(modelOverride ?? 'claude-sonnet-4-6');
        }
        case 'openai-compatible': {
            const baseURL = process.env.SPECVERSE_AI_BASE_URL;
            if (!baseURL) {
                throw new Error('SPECVERSE_AI_PROVIDER=openai-compatible requires SPECVERSE_AI_BASE_URL ' +
                    '(e.g. https://api.together.xyz/v1 or http://localhost:11434/v1 for Ollama).');
            }
            const apiKey = process.env.SPECVERSE_AI_API_KEY;
            const openaiCompatible = createOpenAICompatible({
                name: 'specverse-ai',
                baseURL,
                apiKey,
            });
            return openaiCompatible(modelOverride ?? 'deepseek-chat');
        }
        case 'stub':
            return stubModel();
    }
}
/** Best-effort diagnostic description of the active provider + model. */
export function describeActiveProvider() {
    const provider = resolveProviderId();
    const model = process.env.SPECVERSE_AI_MODEL;
    const modelStr = model ? ` (${model})` : '';
    switch (provider) {
        case 'claude-cli':
            return `claude-cli${modelStr} — local Claude CLI, using your Max subscription auth`;
        case 'anthropic':
            return `anthropic${modelStr} — Anthropic API (metered tokens)`;
        case 'openai-compatible':
            return `openai-compatible${modelStr} — ${process.env.SPECVERSE_AI_BASE_URL ?? '(no BASE_URL)'}`;
        case 'stub':
            return 'stub — no LLM calls; prompt emitted verbatim for ambient executor';
    }
}
//# sourceMappingURL=model-resolver.js.map