/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import OpenAI from 'openai';
/**
 * OpenAI provider implementation
 */
export class OpenAIProvider {
    name = 'openai';
    client;
    constructor(config) {
        this.client = new OpenAI({ apiKey: config.apiKey });
    }
    async execute(prompt, options = {}) {
        const completion = await this.client.chat.completions.create({
            model: options.model || 'gpt-4',
            messages: [{ role: 'user', content: prompt }],
            temperature: options.temperature ?? 0.7,
            max_tokens: options.maxTokens,
            stop: options.stopSequences,
            stream: false, // We'll implement streaming later
        });
        const choice = completion.choices[0];
        const usage = completion.usage;
        return {
            text: choice.message.content || '',
            usage: {
                promptTokens: usage?.prompt_tokens || 0,
                completionTokens: usage?.completion_tokens || 0,
                totalTokens: usage?.total_tokens || 0,
            },
            model: completion.model,
            finishReason: choice.finish_reason || 'stop',
            metadata: {
                created: completion.created,
                id: completion.id,
            },
        };
    }
    async getModels() {
        try {
            const models = await this.client.models.list();
            return models.data
                .filter((model) => model.id.startsWith('gpt-'))
                .map((model) => ({
                id: model.id,
                name: this.formatModelName(model.id),
                provider: 'openai',
                contextWindow: this.getContextWindow(model.id),
                supportsVision: model.id.includes('vision'),
                supportsFunctionCalling: true, // Most GPT models support functions
                inputPricing: this.getPricing(model.id, 'input'),
                outputPricing: this.getPricing(model.id, 'output'),
            }));
        }
        catch (error) {
            console.warn('Failed to fetch OpenAI models:', error);
            // Return known models as fallback
            return this.getKnownModels();
        }
    }
    async checkLimits() {
        // OpenAI doesn't provide usage API, so we return configured limits
        // In a real implementation, you might track usage locally
        return {
            requestsPerMinute: 60,
            requestsPerDay: 1000,
            tokensPerMinute: 10000,
            tokensPerDay: 100000,
            currentUsage: {
                requestsToday: 0, // Would be tracked locally
                tokensToday: 0,
                lastReset: new Date().toISOString().split('T')[0],
            },
        };
    }
    async validateConnection() {
        try {
            await this.client.models.list();
            return true;
        }
        catch {
            return false;
        }
    }
    formatModelName(modelId) {
        const nameMap = {
            'gpt-4': 'GPT-4',
            'gpt-4-turbo': 'GPT-4 Turbo',
            'gpt-4o': 'GPT-4o',
            'gpt-4o-mini': 'GPT-4o Mini',
            'gpt-3.5-turbo': 'GPT-3.5 Turbo',
        };
        return nameMap[modelId] || modelId;
    }
    getContextWindow(modelId) {
        const contextWindows = {
            'gpt-4': 8192,
            'gpt-4-turbo': 128000,
            'gpt-4o': 128000,
            'gpt-4o-mini': 128000,
            'gpt-3.5-turbo': 16385,
        };
        return contextWindows[modelId] || 4096;
    }
    getPricing(modelId, type) {
        // Pricing in USD per 1K tokens (approximate, as of 2024)
        const pricing = {
            'gpt-4': { input: 0.03, output: 0.06 },
            'gpt-4-turbo': { input: 0.01, output: 0.03 },
            'gpt-4o': { input: 0.005, output: 0.015 },
            'gpt-4o-mini': { input: 0.00015, output: 0.0006 },
            'gpt-3.5-turbo': { input: 0.0015, output: 0.002 },
        };
        return pricing[modelId]?.[type] || 0.01;
    }
    getKnownModels() {
        return [
            {
                id: 'gpt-4',
                name: 'GPT-4',
                provider: 'openai',
                contextWindow: 8192,
                supportsFunctionCalling: true,
                inputPricing: 0.03,
                outputPricing: 0.06,
            },
            {
                id: 'gpt-4-turbo',
                name: 'GPT-4 Turbo',
                provider: 'openai',
                contextWindow: 128000,
                supportsFunctionCalling: true,
                inputPricing: 0.01,
                outputPricing: 0.03,
            },
            {
                id: 'gpt-4o',
                name: 'GPT-4o',
                provider: 'openai',
                contextWindow: 128000,
                supportsFunctionCalling: true,
                inputPricing: 0.005,
                outputPricing: 0.015,
            },
            {
                id: 'gpt-3.5-turbo',
                name: 'GPT-3.5 Turbo',
                provider: 'openai',
                contextWindow: 16385,
                supportsFunctionCalling: true,
                inputPricing: 0.0015,
                outputPricing: 0.002,
            },
        ];
    }
}
//# sourceMappingURL=openai.js.map