UNPKG

@2501-ai/cli

Version:

[![npm version](https://img.shields.io/npm/v/@2501-ai/cli.svg)](https://www.npmjs.com/package/@2501-ai/cli) [![HumanEval Score](https://img.shields.io/badge/HumanEval-96.95%25-brightgreen.svg)](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic

175 lines (174 loc) 7.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SSHExecutor = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const ssh2_1 = require("ssh2"); const logger_1 = __importDefault(require("../../utils/logger")); const UNIX_COMMAND_WRAPPER = `source ~/.bashrc 2>/dev/null || true; source ~/.profile 2>/dev/null || true; source ~/.nvm/nvm.sh 2>/dev/null || true;`; const WINDOWS_CMD_WRAPPER = 'powershell '; class SSHExecutor { constructor() { this.client = null; this.connected = false; this.config = null; this.wrapper = ''; } static get instance() { if (!SSHExecutor._instance) { SSHExecutor._instance = new SSHExecutor(); } return SSHExecutor._instance; } init(config) { this.config = config; this.client = null; this.connected = false; if (config.platform === 'fortigate') { this.wrapper = ''; } else { this.wrapper = config.platform === 'windows' ? WINDOWS_CMD_WRAPPER : UNIX_COMMAND_WRAPPER; } } isConnected() { return this.connected; } getConnectionConfig() { if (!this.config || !this.config.enabled) { throw new Error('Remote execution not configured for this agent'); } const connectionConfig = { host: this.config.target, port: this.config.port, username: this.config.user, }; if (this.config.private_key) { logger_1.default.debug('Using PEM private key:', this.config.private_key); connectionConfig.privateKey = fs_1.default.readFileSync(this.config.private_key); if (this.config.password) { connectionConfig.passphrase = this.config.password; } } const rsaKeyPath = path_1.default.join(os_1.default.homedir(), '.ssh', 'id_rsa'); if (!connectionConfig.privateKey && fs_1.default.existsSync(rsaKeyPath)) { logger_1.default.debug('Using RSA private key:', rsaKeyPath); connectionConfig.privateKey = fs_1.default.readFileSync(rsaKeyPath); } if (this.config.password) { logger_1.default.debug('Using password authentication'); connectionConfig.password = this.config.password; } return connectionConfig; } connect() { return __awaiter(this, void 0, void 0, function* () { if (!this.config || !this.config.enabled) { throw new Error('Remote execution not configured'); } if (this.connected && this.client && this.config.target === this.config.target) { return; } if (this.connected && this.config.target !== this.config.target) { this.disconnect(); } return new Promise((resolve, reject) => { this.client = new ssh2_1.Client({ captureRejections: true, }); this.client.on('ready', () => { this.connected = true; logger_1.default.debug('SSH connection established'); resolve(); }); this.client.on('error', (err) => { this.connected = false; logger_1.default.debug('SSH connection error:', err); reject(err); }); this.client.on('close', () => { this.connected = false; logger_1.default.debug('SSH connection closed'); }); const connectionConfig = this.getConnectionConfig(); this.client.connect(connectionConfig); }); }); } executeCommand(command_1, stdin_1) { return __awaiter(this, arguments, void 0, function* (command, stdin, rawCmd = false) { try { yield this.connect(); if (!this.config || !this.config.enabled) { throw new Error('Remote execution not configured'); } return new Promise((resolve, reject) => { if (!this.client) { reject(new Error('SSH client not initialized')); return; } this.client.exec(rawCmd ? command : this.wrapper + command, (err, stream) => { if (err) { reject(err); return; } let stdout = ''; let stderr = ''; stream.on('close', (code) => { if (code !== 0) { reject(new Error(`Command failed with exit code ${code}: ${stderr}`)); } else { const result = stdout.replace(this.wrapper, '').trim(); resolve(result); } }); stream.on('data', (data) => { stdout += data.toString(); }); stream.stderr.on('data', (data) => { stderr += data.toString(); }); if (stdin) { stream.stdin.write(stdin); stream.stdin.end(); } }); }); } catch (error) { logger_1.default.error('Remote command execution failed:', error); throw error; } }); } disconnect() { return __awaiter(this, void 0, void 0, function* () { if (this.client) { this.client.end(); this.client = null; this.connected = false; this.config = null; } }); } } exports.SSHExecutor = SSHExecutor;