"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XAIClient = void 0;
/**
 * XAI Client - Fallback provider using X AI API (Grok)
 *
 * This is a simplified client that uses the X AI API directly
 * as a secondary fallback when MyContext AI and Claude SDK are not available.
 */
class XAIClient {
    constructor() {
        this.clientType = "direct-api";
        this.supportsTools = false;
        this.supportsStreaming = false;
        this.apiKey = null;
        this.baseUrl = "https://api.x.ai/v1";
        this.apiKey = process.env.XAI_API_KEY || null;
    }
    /**
     * Check if API key is available
     */
    hasApiKey() {
        return !!this.apiKey;
    }
    /**
     * Set API key for X AI API
     */
    setApiKey(apiKey) {
        this.apiKey = apiKey;
    }
    /**
     * Check connection to X AI API
     */
    async checkConnection() {
        if (!this.apiKey) {
            return false;
        }
        try {
            const response = await fetch(`${this.baseUrl}/chat/completions`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    Authorization: `Bearer ${this.apiKey}`,
                },
                body: JSON.stringify({
                    model: "grok-2-1212",
                    max_tokens: 10,
                    messages: [
                        {
                            role: "user",
                            content: "test",
                        },
                    ],
                }),
            });
            if (!response.ok) {
                console.warn(`XAI connection check failed: HTTP ${response.status}`);
                return false;
            }
            return true;
        }
        catch (error) {
            console.warn("XAI connection check failed:", error);
            return false;
        }
    }
    /**
     * Generate text using X AI API
     */
    async generateText(prompt, options = {}) {
        if (!this.apiKey) {
            throw new Error("XAI API key not configured");
        }
        try {
            const response = await fetch(`${this.baseUrl}/chat/completions`, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    Authorization: `Bearer ${this.apiKey}`,
                },
                body: JSON.stringify({
                    model: options.model || "grok-beta",
                    max_tokens: options.maxTokens || 4000,
                    temperature: options.temperature || 0.7,
                    messages: [
                        {
                            role: "user",
                            content: prompt,
                        },
                    ],
                }),
            });
            if (!response.ok) {
                throw new Error(`XAI API error: ${response.status} ${response.statusText}`);
            }
            const data = await response.json();
            return data.choices[0].message.content || "";
        }
        catch (error) {
            throw new Error(`XAI generation failed: ${error}`);
        }
    }
    /**
     * Generate React component using X AI API
     */
    async generateComponent(prompt, context, options = {}) {
        const systemPrompt = `You are a React component generator. Generate production-ready React components with TypeScript.

Requirements:
- Use functional components with hooks
- Include proper TypeScript types
- Add accessibility attributes
- Use Tailwind CSS for styling
- Include proper imports and exports
- Generate complete, runnable code

${context?.prd ? `Project Requirements: ${context.prd}` : ""}
${context?.types ? `Type Definitions: ${context.types}` : ""}
${context?.brand ? `Brand Guidelines: ${context.brand}` : ""}`;
        const fullPrompt = `${systemPrompt}\n\nGenerate a React component for: ${prompt}`;
        return this.generateText(fullPrompt, options);
    }
    /**
     * Generate component refinement using X AI API
     */
    async generateComponentRefinement(componentCode, prompt, context, options = {}) {
        const systemPrompt = `You are a React component refiner. Improve and modify existing React components.

Requirements:
- Maintain existing functionality
- Preserve TypeScript types
- Keep accessibility attributes
- Follow React best practices
- Provide complete refined component

${context?.prd ? `Project Requirements: ${context.prd}` : ""}
${context?.types ? `Type Definitions: ${context.types}` : ""}
${context?.brand ? `Brand Guidelines: ${context.brand}` : ""}`;
        const fullPrompt = `${systemPrompt}\n\nComponent to refine:\n\`\`\`tsx\n${componentCode}\n\`\`\n\nRefinement request: ${prompt}`;
        return this.generateText(fullPrompt, options);
    }
    /**
     * Generate text from image using X AI API (Multimodal)
     */
    async generateVisionText(prompt, imagePath, options = {}) {
        // XAI vision models are not yet fully integrated in this client's multimodal format
        // For now we'll throw an error to trigger fallback in AICore
        throw new Error("XAI vision generation not yet implemented in this client");
    }
    /**
     * Generate image using X AI API
     */
    async generateImage(prompt, outputPath, options = {}) {
        throw new Error("XAI image generation not yet implemented in this client");
    }
    /**
     * List available models
     */
    async listModels() {
        return ["grok-beta", "grok-2-1212", "grok-2-vision-1212"];
    }
    /**
     * Cleanup resources
     */
    async cleanup() {
        // No cleanup needed for fetch-based client
    }
}
exports.XAIClient = XAIClient;
//# sourceMappingURL=XAIClient.js.map