"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LLMProvider = void 0;
exports.getAISDKLanguageModel = getAISDKLanguageModel;
const sdkErrors_js_1 = require("../types/public/sdkErrors.js");
const aisdk_js_1 = require("./aisdk.js");
const AnthropicClient_js_1 = require("./AnthropicClient.js");
const CerebrasClient_js_1 = require("./CerebrasClient.js");
const GoogleClient_js_1 = require("./GoogleClient.js");
const GroqClient_js_1 = require("./GroqClient.js");
const OpenAIClient_js_1 = require("./OpenAIClient.js");
const openai_1 = require("@ai-sdk/openai");
const amazon_bedrock_1 = require("@ai-sdk/amazon-bedrock");
const google_vertex_1 = require("@ai-sdk/google-vertex");
const anthropic_1 = require("@ai-sdk/anthropic");
const google_1 = require("@ai-sdk/google");
const xai_1 = require("@ai-sdk/xai");
const azure_1 = require("@ai-sdk/azure");
const groq_1 = require("@ai-sdk/groq");
const cerebras_1 = require("@ai-sdk/cerebras");
const togetherai_1 = require("@ai-sdk/togetherai");
const mistral_1 = require("@ai-sdk/mistral");
const deepseek_1 = require("@ai-sdk/deepseek");
const perplexity_1 = require("@ai-sdk/perplexity");
const ollama_ai_provider_v2_1 = require("ollama-ai-provider-v2");
const ai_1 = require("ai");
const AISDKProviders = {
    openai: openai_1.openai,
    bedrock: amazon_bedrock_1.bedrock,
    anthropic: anthropic_1.anthropic,
    google: google_1.google,
    xai: xai_1.xai,
    azure: azure_1.azure,
    groq: groq_1.groq,
    cerebras: cerebras_1.cerebras,
    togetherai: togetherai_1.togetherai,
    mistral: mistral_1.mistral,
    deepseek: deepseek_1.deepseek,
    perplexity: perplexity_1.perplexity,
    ollama: ollama_ai_provider_v2_1.ollama,
    vertex: google_vertex_1.vertex,
    gateway: ai_1.gateway,
};
const AISDKProvidersWithAPIKey = {
    openai: openai_1.createOpenAI,
    bedrock: amazon_bedrock_1.createAmazonBedrock,
    anthropic: anthropic_1.createAnthropic,
    google: google_1.createGoogleGenerativeAI,
    vertex: google_vertex_1.createVertex,
    xai: xai_1.createXai,
    azure: azure_1.createAzure,
    groq: groq_1.createGroq,
    cerebras: cerebras_1.createCerebras,
    togetherai: togetherai_1.createTogetherAI,
    mistral: mistral_1.createMistral,
    deepseek: deepseek_1.createDeepSeek,
    perplexity: perplexity_1.createPerplexity,
    ollama: ollama_ai_provider_v2_1.createOllama,
    gateway: ai_1.createGateway,
};
const modelToProviderMap = {
    "gpt-4.1": "openai",
    "gpt-4.1-mini": "openai",
    "gpt-4.1-nano": "openai",
    "o4-mini": "openai",
    //prettier-ignore
    "o3": "openai",
    "o3-mini": "openai",
    //prettier-ignore
    "o1": "openai",
    "o1-mini": "openai",
    "gpt-4o": "openai",
    "gpt-4o-mini": "openai",
    "gpt-4o-2024-08-06": "openai",
    "gpt-4.5-preview": "openai",
    "o1-preview": "openai",
    "claude-3-5-sonnet-latest": "anthropic",
    "claude-3-5-sonnet-20240620": "anthropic",
    "claude-3-5-sonnet-20241022": "anthropic",
    "claude-3-7-sonnet-20250219": "anthropic",
    "claude-3-7-sonnet-latest": "anthropic",
    "cerebras-llama-3.3-70b": "cerebras",
    "cerebras-llama-3.1-8b": "cerebras",
    "groq-llama-3.3-70b-versatile": "groq",
    "groq-llama-3.3-70b-specdec": "groq",
    "moonshotai/kimi-k2-instruct": "groq",
    "gemini-1.5-flash": "google",
    "gemini-1.5-pro": "google",
    "gemini-1.5-flash-8b": "google",
    "gemini-2.0-flash-lite": "google",
    "gemini-2.0-flash": "google",
    "gemini-2.5-flash-preview-04-17": "google",
    "gemini-2.5-pro-preview-03-25": "google",
};
function getAISDKLanguageModel(subProvider, subModelName, clientOptions) {
    const hasValidOptions = clientOptions &&
        Object.values(clientOptions).some((v) => v !== undefined && v !== null);
    if (hasValidOptions) {
        const creator = AISDKProvidersWithAPIKey[subProvider];
        if (!creator) {
            throw new sdkErrors_js_1.UnsupportedAISDKModelProviderError(subProvider, Object.keys(AISDKProvidersWithAPIKey));
        }
        const provider = creator(clientOptions);
        // Get the specific model from the provider
        return provider(subModelName);
    }
    else {
        const provider = AISDKProviders[subProvider];
        if (!provider) {
            throw new sdkErrors_js_1.UnsupportedAISDKModelProviderError(subProvider, Object.keys(AISDKProviders));
        }
        return provider(subModelName);
    }
}
class LLMProvider {
    logger;
    constructor(logger) {
        this.logger = logger;
    }
    getClient(modelName, clientOptions, options) {
        if (modelName.includes("/")) {
            const firstSlashIndex = modelName.indexOf("/");
            const subProvider = modelName.substring(0, firstSlashIndex);
            const subModelName = modelName.substring(firstSlashIndex + 1);
            if (subProvider === "vertex" &&
                !options?.disableAPI &&
                !options?.experimental) {
                throw new sdkErrors_js_1.ExperimentalNotConfiguredError("Vertex provider");
            }
            const languageModel = getAISDKLanguageModel(subProvider, subModelName, clientOptions);
            return new aisdk_js_1.AISdkClient({
                model: languageModel,
                logger: this.logger,
            });
        }
        // Model name doesn't include "/" - this format is deprecated
        const provider = modelToProviderMap[modelName];
        if (!provider) {
            throw new sdkErrors_js_1.UnsupportedModelError(Object.keys(modelToProviderMap));
        }
        this.logger({
            category: "llm",
            message: `Deprecation warning: Model format "${modelName}" is deprecated. Please use the provider/model format (e.g., "openai/gpt-5" or "anthropic/claude-sonnet-4").`,
            level: 0,
        });
        const availableModel = modelName;
        switch (provider) {
            case "openai":
                return new OpenAIClient_js_1.OpenAIClient({
                    logger: this.logger,
                    modelName: availableModel,
                    clientOptions,
                });
            case "anthropic":
                return new AnthropicClient_js_1.AnthropicClient({
                    logger: this.logger,
                    modelName: availableModel,
                    clientOptions,
                });
            case "cerebras":
                return new CerebrasClient_js_1.CerebrasClient({
                    logger: this.logger,
                    modelName: availableModel,
                    clientOptions,
                });
            case "groq":
                return new GroqClient_js_1.GroqClient({
                    logger: this.logger,
                    modelName: availableModel,
                    clientOptions,
                });
            case "google":
                return new GoogleClient_js_1.GoogleClient({
                    logger: this.logger,
                    modelName: availableModel,
                    clientOptions,
                });
            default:
                // This default case handles unknown providers that exist in modelToProviderMap
                // but aren't implemented in the switch. This is an internal consistency issue.
                throw new sdkErrors_js_1.UnsupportedModelProviderError([
                    ...new Set(Object.values(modelToProviderMap)),
                ]);
        }
    }
    static getModelProvider(modelName) {
        if (modelName.includes("/")) {
            const firstSlashIndex = modelName.indexOf("/");
            const subProvider = modelName.substring(0, firstSlashIndex);
            if (AISDKProviders[subProvider]) {
                return "aisdk";
            }
        }
        const provider = modelToProviderMap[modelName];
        return provider;
    }
}
exports.LLMProvider = LLMProvider;
//# sourceMappingURL=LLMProvider.js.map