"use strict";
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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenRouterClient = void 0;
const openai_1 = __importDefault(require("openai"));
class OpenRouterClient {
    constructor() {
        this.clientType = "direct-api";
        this.supportsTools = false;
        this.supportsStreaming = false;
        this.client = null;
        this.apiKey = null;
        this.apiKey =
            process.env.MYCONTEXT_OPENROUTER_API_KEY ||
                process.env.OPENROUTER_API_KEY ||
                process.env.OPENROUTER_KEY ||
                null;
        if (this.apiKey) {
            this.client = new openai_1.default({
                baseURL: "https://openrouter.ai/api/v1",
                apiKey: this.apiKey,
                defaultHeaders: {
                    "HTTP-Referer": "https://mycontext.dev",
                    "X-Title": "MyContext CLI",
                },
            });
        }
    }
    hasApiKey() {
        return !!this.apiKey;
    }
    setApiKey(apiKey) {
        this.apiKey = apiKey;
        this.client = new openai_1.default({
            baseURL: "https://openrouter.ai/api/v1",
            apiKey: this.apiKey,
            defaultHeaders: {
                "HTTP-Referer": "https://mycontext.dev",
                "X-Title": "MyContext CLI",
            },
        });
    }
    async checkConnection() {
        // Just check if client exists - don't make actual API call
        // (API calls in checkConnection cause false negatives)
        return !!this.client;
    }
    async generateText(prompt, options = {}) {
        if (!this.client)
            throw new Error("OpenRouter not configured");
        const model = process.env.OPENROUTER_MODEL ||
            process.env.MYCONTEXT_OPENROUTER_MODEL ||
            options.model ||
            "meta-llama/llama-3.3-70b-instruct:free";
        const response = await this.client.chat.completions.create({
            model,
            messages: [{ role: "user", content: prompt }],
            temperature: options.temperature || 0.7,
            max_tokens: options.maxTokens || 4000,
        });
        return response.choices[0]?.message?.content || "";
    }
    async generateComponent(prompt, context, options = {}) {
        return this.generateText(prompt, options);
    }
    async generateComponentRefinement(componentCode, prompt, context, options = {}) {
        const fullPrompt = `Refine this component: ${prompt}\n\nExisting Code:\n${componentCode}`;
        return this.generateText(fullPrompt, options);
    }
    async generateVisionText(prompt, imagePath, options = {}) {
        if (!this.client)
            throw new Error("OpenRouter not configured");
        try {
            const fs = await Promise.resolve().then(() => __importStar(require("fs")));
            const imageBuffer = await fs.promises.readFile(imagePath);
            const base64Image = imageBuffer.toString("base64");
            const mimeType = imagePath.toLowerCase().endsWith(".png") ? "image/png" : "image/jpeg";
            const model = process.env.OPENROUTER_VISION_MODEL ||
                process.env.MYCONTEXT_OPENROUTER_VISION_MODEL ||
                options.model ||
                "google/gemini-pro-1.5-vision";
            const response = await this.client.chat.completions.create({
                model,
                messages: [
                    {
                        role: "user",
                        content: [
                            { type: "text", text: prompt },
                            {
                                type: "image_url",
                                image_url: {
                                    url: `data:${mimeType};base64,${base64Image}`,
                                },
                            },
                        ],
                    },
                ],
                max_tokens: options.maxTokens || 4000,
            });
            return response.choices[0]?.message?.content || "";
        }
        catch (error) {
            throw new Error(`OpenRouter vision generation failed: ${error}`);
        }
    }
    async generateImage(prompt, outputPath, options = {}) {
        throw new Error("OpenRouter image generation not yet implemented in this client");
    }
    async listModels() {
        return ["deepseek/deepseek-r1", "google/gemini-pro-1.5-vision", "anthropic/claude-3-sonnet"];
    }
    async cleanup() {
        // No specific cleanup needed
    }
}
exports.OpenRouterClient = OpenRouterClient;
//# sourceMappingURL=openRouterClient.js.map