UNPKG

@vladimirdukelic/revolutionary-ui-factory

Version:

Revolutionary UI Factory System v2 - Generate ANY UI component for ANY framework with 60-95% code reduction

491 lines 16.1 kB
"use strict"; /** * AI Configuration Manager for Revolutionary UI Factory * Manages custom AI provider configurations */ 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AIConfigManager = void 0; const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const os = __importStar(require("os")); const auth_manager_1 = require("./auth-manager"); class AIConfigManager { static CONFIG_DIR = path.join(os.homedir(), '.revolutionary-ui'); static AI_CONFIG_FILE = 'ai-config.json'; static AI_CONFIG_PATH = path.join(AIConfigManager.CONFIG_DIR, AIConfigManager.AI_CONFIG_FILE); static providers = { openai: { name: 'OpenAI', models: ['gpt-4', 'gpt-4-turbo-preview', 'gpt-3.5-turbo', 'gpt-3.5-turbo-16k'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true, placeholder: 'sk-...' }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['gpt-4', 'gpt-4-turbo-preview', 'gpt-3.5-turbo'], default: 'gpt-4' }, { name: 'temperature', type: 'number', label: 'Temperature (0-2)', required: false, default: 0.7 }, { name: 'maxTokens', type: 'number', label: 'Max Tokens', required: false, default: 2000 } ] }, anthropic: { name: 'Anthropic (Claude)', models: ['claude-3-opus-20240229', 'claude-3-sonnet-20240229', 'claude-3-haiku-20240307'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true, placeholder: 'sk-ant-...' }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['claude-3-opus-20240229', 'claude-3-sonnet-20240229', 'claude-3-haiku-20240307'], default: 'claude-3-opus-20240229' }, { name: 'maxTokens', type: 'number', label: 'Max Tokens', required: false, default: 4000 } ] }, google: { name: 'Google (Gemini)', models: ['gemini-pro', 'gemini-pro-vision'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true, placeholder: 'AI...' }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['gemini-pro', 'gemini-pro-vision'], default: 'gemini-pro' } ] }, mistral: { name: 'Mistral AI', models: ['mistral-large-latest', 'mistral-medium-latest', 'mistral-small-latest'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['mistral-large-latest', 'mistral-medium-latest', 'mistral-small-latest'], default: 'mistral-large-latest' } ] }, groq: { name: 'Groq', models: ['mixtral-8x7b-32768', 'llama2-70b-4096'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['mixtral-8x7b-32768', 'llama2-70b-4096'], default: 'mixtral-8x7b-32768' } ] }, deepseek: { name: 'DeepSeek', models: ['deepseek-coder', 'deepseek-chat'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['deepseek-coder', 'deepseek-chat'], default: 'deepseek-coder' } ] }, cohere: { name: 'Cohere', models: ['command', 'command-light'], requiresApiKey: true, supportsStreaming: true, configFields: [ { name: 'apiKey', type: 'password', label: 'API Key', required: true }, { name: 'model', type: 'select', label: 'Model', required: true, options: ['command', 'command-light'], default: 'command' } ] }, custom: { name: 'Custom API Endpoint', models: [], requiresApiKey: false, supportsStreaming: false, configFields: [ { name: 'endpoint', type: 'text', label: 'API Endpoint URL', required: true, placeholder: 'https://api.example.com/v1/chat' }, { name: 'apiKey', type: 'password', label: 'API Key (if required)', required: false }, { name: 'model', type: 'text', label: 'Model Name', required: false }, { name: 'customHeaders', type: 'textarea', label: 'Custom Headers (JSON)', required: false, placeholder: '{"X-Custom-Header": "value"}' } ] }, local: { name: 'Local Model (Ollama, etc.)', models: ['llama2', 'codellama', 'mistral', 'mixtral'], requiresApiKey: false, supportsStreaming: true, configFields: [ { name: 'endpoint', type: 'text', label: 'Local API Endpoint', required: true, default: 'http://localhost:11434/api/generate', placeholder: 'http://localhost:11434/api/generate' }, { name: 'model', type: 'text', label: 'Model Name', required: true, placeholder: 'llama2' } ] } }; /** * Initialize config directory */ static async init() { try { await fs.mkdir(this.CONFIG_DIR, { recursive: true }); } catch (error) { // Directory might already exist } } /** * Get current AI configuration */ static async getConfig() { try { const data = await fs.readFile(this.AI_CONFIG_PATH, 'utf-8'); return JSON.parse(data); } catch { return null; } } /** * Save AI configuration */ static async saveConfig(config) { await this.init(); // Validate config const provider = this.providers[config.provider]; if (!provider) { throw new Error(`Unknown AI provider: ${config.provider}`); } // Don't save sensitive data to Revolutionary UI servers // Only save locally await fs.writeFile(this.AI_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf-8'); // Set restrictive permissions await fs.chmod(this.AI_CONFIG_PATH, 0o600); // Notify server about provider choice (not the key) try { await auth_manager_1.AuthManager.apiRequest('/ai/config', { method: 'POST', body: JSON.stringify({ provider: config.provider, model: config.model // Don't send API keys }) }); } catch { // Server notification is optional } } /** * Test AI configuration */ static async testConfig(config) { try { // Test based on provider switch (config.provider) { case 'openai': return await this.testOpenAI(config); case 'anthropic': return await this.testAnthropic(config); case 'google': return await this.testGoogle(config); case 'local': return await this.testLocal(config); case 'custom': return await this.testCustom(config); default: // For other providers, assume valid if API key is present return !!config.apiKey; } } catch (error) { return false; } } /** * Test OpenAI configuration */ static async testOpenAI(config) { if (!config.apiKey) return false; try { const response = await fetch('https://api.openai.com/v1/models', { headers: { 'Authorization': `Bearer ${config.apiKey}` } }); return response.ok; } catch { return false; } } /** * Test Anthropic configuration */ static async testAnthropic(config) { if (!config.apiKey) return false; try { const response = await fetch('https://api.anthropic.com/v1/messages', { method: 'POST', headers: { 'x-api-key': config.apiKey, 'anthropic-version': '2023-06-01', 'content-type': 'application/json' }, body: JSON.stringify({ model: config.model || 'claude-3-opus-20240229', messages: [{ role: 'user', content: 'test' }], max_tokens: 10 }) }); // Anthropic returns 200 for valid requests return response.ok || response.status === 400; // 400 might mean quota exceeded } catch { return false; } } /** * Test Google configuration */ static async testGoogle(config) { if (!config.apiKey) return false; try { const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models?key=${config.apiKey}`); return response.ok; } catch { return false; } } /** * Test local configuration */ static async testLocal(config) { if (!config.endpoint) return false; try { // Try to connect to local endpoint const response = await fetch(config.endpoint.replace('/generate', '/tags')); return response.ok; } catch { return false; } } /** * Test custom configuration */ static async testCustom(config) { if (!config.endpoint) return false; try { const headers = {}; if (config.apiKey) { headers['Authorization'] = `Bearer ${config.apiKey}`; } if (config.customHeaders) { const customHeaders = typeof config.customHeaders === 'string' ? JSON.parse(config.customHeaders) : config.customHeaders; Object.assign(headers, customHeaders); } const response = await fetch(config.endpoint, { method: 'GET', headers }); return response.ok; } catch { return false; } } /** * Get provider info */ static getProviderInfo(provider) { return this.providers[provider]; } /** * Get all providers */ static getAllProviders() { return Object.entries(this.providers).map(([key, info]) => ({ key: key, info })); } /** * Generate component with custom AI */ static async generateWithCustomAI(prompt, config) { // This would be implemented based on the provider // For now, return a placeholder throw new Error('Custom AI generation not yet implemented'); } } exports.AIConfigManager = AIConfigManager; //# sourceMappingURL=ai-config-manager.js.map