export class ZenProvider {
    name = 'zen';
    config;
    constructor(config) {
        this.config = config;
    }
    async execute(prompt, options = {}) {
        const model = options.model || this.config.models?.default || 'gpt-5.1';
        const isAnthropicModel = model.startsWith('claude-');
        // Use appropriate endpoint based on model type
        const endpoint = isAnthropicModel
            ? 'https://opencode.ai/zen/v1/messages'
            : 'https://opencode.ai/zen/v1/chat/completions';
        const headers = {
            'Authorization': `Bearer ${this.config.apiKey}`,
            'Content-Type': 'application/json',
        };
        let body;
        if (isAnthropicModel) {
            // Anthropic-compatible format
            body = {
                model: model,
                max_tokens: options.maxTokens || 4096,
                temperature: options.temperature || 0.7,
                messages: [
                    {
                        role: 'user',
                        content: prompt,
                    },
                ],
            };
        }
        else {
            // OpenAI-compatible format
            body = {
                model: model,
                messages: [
                    {
                        role: 'user',
                        content: prompt,
                    },
                ],
                max_tokens: options.maxTokens || 4096,
                temperature: options.temperature || 0.7,
            };
        }
        try {
            const response = await fetch(endpoint, {
                method: 'POST',
                headers,
                body: JSON.stringify(body),
            });
            if (!response.ok) {
                throw new Error(`Zen API error: ${response.status} ${response.statusText}`);
            }
            const data = await response.json();
            let content;
            if (isAnthropicModel) {
                content = data.content?.[0]?.text || '';
            }
            else {
                content = data.choices?.[0]?.message?.content || '';
            }
            return {
                text: content,
                usage: {
                    promptTokens: data.usage?.input_tokens || data.usage?.prompt_tokens || 0,
                    completionTokens: data.usage?.output_tokens || data.usage?.completion_tokens || 0,
                    totalTokens: data.usage?.total_tokens || 0,
                },
                model,
                finishReason: 'stop',
            };
        }
        catch (error) {
            throw new Error(`Zen provider error: ${error instanceof Error ? error.message : String(error)}`);
        }
    }
    async checkLimits() {
        // Zen doesn't have strict rate limits in the same way as other providers
        // They use pay-per-request model, so we return generous limits
        return {
            requestsPerMinute: 100,
            requestsPerDay: 10000,
            tokensPerMinute: 100000,
            tokensPerDay: 1000000,
        };
    }
    async getModels() {
        try {
            const response = await fetch('https://opencode.ai/zen/v1/models', {
                headers: {
                    'Authorization': `Bearer ${this.config.apiKey}`,
                },
            });
            if (!response.ok) {
                // Fallback to known models if API call fails
                return this.getKnownModels();
            }
            const data = await response.json();
            return data.data?.map((model) => ({
                id: model.id,
                name: model.id,
                provider: 'zen',
                contextWindow: 128000, // Default context window
            })) || this.getKnownModels();
        }
        catch (error) {
            // Return known models as fallback
            return this.getKnownModels();
        }
    }
    getKnownModels() {
        const models = [
            'gpt-5.1',
            'gpt-5.1-codex',
            'gpt-5',
            'gpt-5-codex',
            'gpt-5-nano',
            'claude-sonnet-4-5',
            'claude-sonnet-4',
            'claude-haiku-4-5',
            'claude-3-5-haiku',
            'claude-opus-4-1',
            'glm-4.6',
            'kimi-k2',
            'qwen3-coder',
            'grok-code',
            'big-pickle',
        ];
        return models.map(id => ({
            id,
            name: id,
            provider: 'zen',
            contextWindow: 128000,
        }));
    }
    async validateConnection() {
        try {
            const response = await fetch('https://opencode.ai/zen/v1/models', {
                headers: {
                    'Authorization': `Bearer ${this.config.apiKey}`,
                },
            });
            return response.ok;
        }
        catch (error) {
            return false;
        }
    }
}
//# sourceMappingURL=zen.js.map