UNPKG

automagik-genie

Version:

Self-evolving AI agent orchestration framework with Model Context Protocol support

237 lines (236 loc) • 8.13 kB
"use strict"; /** * Executor Management - Verification, Installation, and Authentication */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getOSType = getOSType; exports.commandExists = commandExists; exports.verifyExecutor = verifyExecutor; exports.installExecutor = installExecutor; exports.authenticateExecutor = authenticateExecutor; exports.selectExecutorInteractive = selectExecutorInteractive; exports.setupExecutor = setupExecutor; exports.getAllExecutorKeys = getAllExecutorKeys; exports.getExecutorInfo = getExecutorInfo; const child_process_1 = require("child_process"); const os_1 = require("os"); const executor_definitions_js_1 = require("./executor-definitions.js"); /** * OS detection utilities */ function getOSType() { const p = (0, os_1.platform)(); if (p === 'darwin') return 'macos'; if (p === 'linux') return 'linux'; if (p === 'win32') return 'windows'; return 'unknown'; } /** * Execute shell command and return stdout */ function execCommand(command, ignoreError = false) { try { return (0, child_process_1.execSync)(command, { encoding: 'utf8', stdio: ignoreError ? 'pipe' : 'inherit' }).trim(); } catch (error) { if (ignoreError) return null; throw error; } } /** * Check if a command exists in PATH */ function commandExists(command) { try { (0, child_process_1.execSync)(`command -v ${command}`, { stdio: 'ignore' }); return true; } catch { return false; } } /** * Verify if an executor is installed */ async function verifyExecutor(executorKey) { const def = (0, executor_definitions_js_1.getExecutorDefinition)(executorKey); if (!def) { return { installed: false, error: `Unknown executor: ${executorKey}` }; } try { // For VSCode extensions, check both VSCode and extension if (def.key === 'cline' || def.key === 'continue') { if (!commandExists('code')) { return { installed: false, error: 'VSCode is not installed' }; } // Check if extension is installed const extensions = execCommand('code --list-extensions', true); if (!extensions) { return { installed: false, error: 'Could not list VSCode extensions' }; } const extensionId = def.key === 'cline' ? 'saoudrizwan.claude-dev' : 'continue.continue'; const installed = extensions.includes(extensionId); return { installed, version: installed ? 'installed' : undefined, error: installed ? undefined : `VSCode extension ${extensionId} not found` }; } // For regular CLI tools, check command existence if (!commandExists(def.command)) { return { installed: false, error: `Command '${def.command}' not found in PATH` }; } // Try to get version const version = execCommand(def.verifyCommand, true); return { installed: true, version: version || 'installed' }; } catch (error) { return { installed: false, error: error.message || 'Verification failed' }; } } /** * Install an executor */ async function installExecutor(executorKey) { const def = (0, executor_definitions_js_1.getExecutorDefinition)(executorKey); if (!def) { return { success: false, error: `Unknown executor: ${executorKey}` }; } const osType = getOSType(); const installCommands = osType === 'macos' ? def.installCommands.macos : def.installCommands.linux; if (!installCommands || installCommands.length === 0) { return { success: false, error: `No installation method defined for ${def.friendlyName} on ${osType}` }; } try { console.log(`šŸ“¦ Installing ${def.friendlyName}...`); // Execute installation commands sequentially for (const cmd of installCommands) { console.log(` Running: ${cmd}`); execCommand(cmd, false); } // Verify installation succeeded const verification = await verifyExecutor(executorKey); if (!verification.installed) { return { success: false, error: `Installation completed but verification failed: ${verification.error}` }; } console.log(`āœ… ${def.friendlyName} installed successfully!`); return { success: true }; } catch (error) { return { success: false, error: `Installation failed: ${error.message}` }; } } /** * Authenticate/login to an executor */ async function authenticateExecutor(executorKey) { const def = (0, executor_definitions_js_1.getExecutorDefinition)(executorKey); if (!def) { return { success: false, error: `Unknown executor: ${executorKey}` }; } if (!def.requiresAuth) { return { success: true }; // No auth required } if (!def.loginCommand) { return { success: false, error: `${def.friendlyName} requires authentication but no login command defined` }; } try { console.log(`šŸ”‘ Authenticating ${def.friendlyName}...`); console.log(` Please follow the prompts to complete authentication.`); // Execute login command interactively execCommand(def.loginCommand, false); console.log(`āœ… ${def.friendlyName} authenticated successfully!`); return { success: true }; } catch (error) { return { success: false, error: `Authentication failed: ${error.message}` }; } } /** * Interactive executor selection menu */ async function selectExecutorInteractive() { const executors = Object.values(executor_definitions_js_1.EXECUTOR_DEFINITIONS); console.log('\nšŸ§ž ✨ SELECT YOUR AI EXECUTOR ✨'); console.log('━'.repeat(60)); console.log(''); executors.forEach((exec, index) => { console.log(`${index + 1}. ${exec.friendlyName.padEnd(20)} - ${exec.description}`); }); console.log(''); console.log('Enter number (1-' + executors.length + '): '); // Read from stdin (this will be implemented in the CLI command) // For now, return null to indicate interactive selection needed return null; } /** * Complete executor setup workflow */ async function setupExecutor(executorKey) { const def = (0, executor_definitions_js_1.getExecutorDefinition)(executorKey); if (!def) { return { success: false, needsAuth: false, error: `Unknown executor: ${executorKey}` }; } console.log(`\nšŸ” Checking if ${def.friendlyName} is installed...`); // Verify installation const verification = await verifyExecutor(executorKey); if (!verification.installed) { console.log(`āŒ ${def.friendlyName} is not installed`); console.log(` Error: ${verification.error}`); // Prompt for installation (in CLI command) console.log(`\nšŸ“¦ Would you like to install ${def.friendlyName} now? (y/n)`); // Return needsAuth flag for CLI to handle return { success: false, needsAuth: def.requiresAuth, error: 'Not installed' }; } console.log(`āœ… ${def.friendlyName} is already installed (${verification.version})`); // Check if authentication is needed if (def.requiresAuth) { return { success: true, needsAuth: true }; } return { success: true, needsAuth: false }; } /** * Get list of all executor keys */ function getAllExecutorKeys() { return Object.keys(executor_definitions_js_1.EXECUTOR_DEFINITIONS); } /** * Get executor display info */ function getExecutorInfo(executorKey) { const def = (0, executor_definitions_js_1.getExecutorDefinition)(executorKey); if (!def) return null; return { key: def.key, name: def.friendlyName, description: def.description, website: def.website }; }