"use strict";
/**
 * Model-agnostic LLM service.
 *
 * Single entry point for all LLM calls in the application.
 * Automatically detects the provider from the model name and
 * routes to the appropriate SDK (OpenAI, Anthropic, or any
 * OpenAI-compatible provider).
 *
 * Usage:
 *   import { callLLM, hasLLMApiKey } from '../services/llm';
 *   const response = await callLLM({ model, messages, tools, max_tokens });
 *   console.log(response.content, response.tool_calls);
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectProvider = void 0;
exports.callLLM = callLLM;
exports.hasLLMApiKey = hasLLMApiKey;
const detect_1 = require("./detect");
var detect_2 = require("./detect");
Object.defineProperty(exports, "detectProvider", { enumerable: true, get: function () { return detect_2.detectProvider; } });
// ── Provider cache (lazy singletons) ──────────────────────────────────────────
const providers = new Map();
async function getProvider(model) {
    const name = (0, detect_1.detectProvider)(model);
    let provider = providers.get(name);
    if (!provider) {
        const apiKey = (0, detect_1.resolveApiKey)(name);
        switch (name) {
            case 'openai':
            case 'openai-compatible': {
                const { OpenAIProvider } = await Promise.resolve().then(() => __importStar(require('./providers/openai')));
                const baseURL = process.env.LT_LLM_BASE_URL;
                provider = new OpenAIProvider(apiKey, name === 'openai-compatible' ? baseURL : undefined);
                break;
            }
            case 'anthropic': {
                const { AnthropicProvider } = await Promise.resolve().then(() => __importStar(require('./providers/anthropic')));
                provider = new AnthropicProvider(apiKey);
                break;
            }
        }
        providers.set(name, provider);
    }
    return provider;
}
// ── Public API ────────────────────────────────────────────────────────────────
/**
 * Call an LLM with model-agnostic options.
 *
 * The provider is auto-detected from the model name:
 * - gpt-*, o1-*, o3-*, o4-* → OpenAI
 * - claude-* → Anthropic
 * - others → OpenAI-compatible (respects LT_LLM_BASE_URL)
 *
 * Messages, tools, and responses use OpenAI's format as the canonical
 * representation. Translation to/from other providers happens internally.
 */
async function callLLM(options) {
    const provider = await getProvider(options.model);
    return provider.call(options);
}
/**
 * Check if the required API key is available for a given model.
 * Useful for conditional feature enablement (e.g., skip LLM compilation
 * when no key is configured).
 */
function hasLLMApiKey(model) {
    const m = model || process.env.LT_LLM_MODEL_PRIMARY || 'claude-sonnet-4-6';
    const provider = (0, detect_1.detectProvider)(m);
    const key = (0, detect_1.resolveApiKey)(provider);
    return !!key && key !== 'xxx';
}
