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

176 lines (174 loc) 9.12 kB
#!/usr/bin/env node "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 }); const commander_1 = require("commander"); const os_1 = __importDefault(require("os")); const agents_1 = require("./commands/agents"); const config_1 = require("./commands/config"); const init_1 = require("./commands/init"); const query_1 = require("./commands/query"); const set_1 = require("./commands/set"); const startTask_1 = require("./commands/startTask"); const tasks_1 = require("./commands/tasks"); const auth_1 = require("./middleware/auth"); const errorHandler_1 = require("./middleware/errorHandler"); const remoteExecutor_1 = require("./remoteExecution/remoteExecutor"); const cliUpdate_1 = require("./utils/cliUpdate"); const credentials_1 = require("./utils/credentials"); const logger_1 = __importDefault(require("./utils/logger")); const platform_1 = require("./utils/platform"); const plugins_1 = require("./utils/plugins"); const package_json_1 = require("../package.json"); errorHandler_1.errorHandler.initializeGlobalHandlers(); process.on('SIGINT', () => __awaiter(void 0, void 0, void 0, function* () { remoteExecutor_1.RemoteExecutor.instance.disconnect(); process.exit(0); })); process.on('SIGTERM', () => __awaiter(void 0, void 0, void 0, function* () { remoteExecutor_1.RemoteExecutor.instance.disconnect(); process.exit(0); })); const program = new commander_1.Command(); const programName = os_1.default.platform() === 'win32' ? 'a2501' : '@2501'; let timeout; program .name(programName) .description(` ░▒▓███████▓▒░░▒▓████████▓▒░▒▓████████▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓████▓▒░ ░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓██████▓▒░░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓████████▓▒░▒▓███████▓▒░░▒▓████████▓▒░ ░▒▓█▓▒░ ---- AI Autonomous Systems ---- `) .version(package_json_1.version) .option('--remote-exec <connection>', 'Enable remote execution (user@host:port)') .option('--remote-private-key <privateKey>', 'Path to private key for remote execution') .option('--remote-exec-type <type>', 'Type of remote execution: ssh or winrm. (defaults to ssh)', 'ssh') .option('--remote-exec-password <password>', 'Password for remote execution') .option('--remote-skip-test <skipTest>', 'Skip the remote connection test') .option('--raw-ssh', 'Execute SSH commands without automatic wrapper (skip shell initialization)') .option('--workspace <path>', 'Specify a different workspace path') .option('--config <configKey>', 'Specify the configuration Key to use') .hook('preAction', (cmd) => { const TWENTY_MINUTES = 20 * 60 * 1000; timeout = setTimeout(() => { logger_1.default.error(`Command ${cmd.name()} timed out`); process.exit(1); }, TWENTY_MINUTES); }) .hook('postAction', () => { logger_1.default.debug('Post-action hook clearing timeout'); clearTimeout(timeout); remoteExecutor_1.RemoteExecutor.instance.disconnect(); }) .on('command:*', (args) => __awaiter(void 0, void 0, void 0, function* () { const query = args === null || args === void 0 ? void 0 : args.join(' '); if (!query) { logger_1.default.log('Please provide a query or use --help to see available commands'); return; } const options = program.opts(); logger_1.default.debug('Args:', { args, options }); try { yield (0, auth_1.authMiddleware)(); yield (0, query_1.queryCommand)(query, options); yield remoteExecutor_1.RemoteExecutor.instance.disconnect(); process.exit(); } catch (error) { yield errorHandler_1.errorHandler.handleCommandError(error, 'fallback-query'); process.exit(1); } })); program .command('config') .description('Fetch configuration from API') .hook('preAction', auth_1.authMiddleware) .action(() => __awaiter(void 0, void 0, void 0, function* () { yield (0, config_1.configCommand)(); })); program .command('start-task') .argument('<taskId>', 'Task ID to start') .description('Start a task') .option('--workspace <path>', 'Specify a different workspace path') .option('--agent-id <agentId>', 'Specify the agent ID') .option('--stream [stream]', 'Stream the output of the query', true) .hook('preAction', auth_1.authMiddleware) .hook('preAction', plugins_1.initPlugins) .hook('preAction', credentials_1.initPluginCredentials) .action((taskId, cmdOptions) => __awaiter(void 0, void 0, void 0, function* () { const options = program.opts(); const allOptions = Object.assign(Object.assign(Object.assign({}, cmdOptions), options), { taskId }); logger_1.default.debug(`Starting task ${taskId}`); yield (0, startTask_1.startTaskCommand)(allOptions); })); program .command('init') .description('Initializes a new agent') .option('--name <name>', 'Specify the name of the agent') .option('--workspace <path>', 'Specify a different workspace path') .option('--no-workspace', `Will not sync the current workspace and will create a temporary one in ${(0, platform_1.getTempPath2501)()}`) .option('--config <configKey>', 'Specify the configuration Key to use') .option('--agent-id <agentId>', 'Specify the agent ID') .option('--task-id <taskId>', 'Specify the task ID') .hook('preAction', auth_1.authMiddleware) .action((cmdOptions) => __awaiter(void 0, void 0, void 0, function* () { const options = program.opts(); const allOptions = Object.assign(Object.assign({}, cmdOptions), options); logger_1.default.debug('Init options:', Object.assign(Object.assign({}, allOptions), { remoteExecPassword: (allOptions.remoteExecPassword && '***') || '(not provided)' })); yield (0, init_1.initCommand)(allOptions); })); program .command('agents') .description('List agents in the current workspace or all agents on the machine') .option('--workspace <path>', 'Specify a different workspace path') .option('--all', 'Parameter to target all agents during list or flush action') .option('--flush', 'Flush all agents from the configuration') .action((options) => __awaiter(void 0, void 0, void 0, function* () { yield (0, agents_1.agentsCommand)(options); })); program .command('tasks') .description('Fetch tasks from API') .option('--workspace <path>', 'Specify a different workspace path') .option('--subscribe', 'Subscribe to the API for new tasks on the current workspace (updated every minute)') .option('--unsubscribe', 'Unsubscribe to the API for new tasks on the current workspace') .option('--listen', 'Listen for new tasks from the API and execute them') .hook('preAction', auth_1.authMiddleware) .hook('preAction', plugins_1.initPlugins) .hook('preAction', credentials_1.initPluginCredentials) .action((options) => __awaiter(void 0, void 0, void 0, function* () { yield (0, tasks_1.tasksSubscriptionCommand)(options); })); program .command('set') .description('Set a configuration value') .argument('<key>', 'The key to set') .argument('<value>', 'The value to set') .action((key, value) => __awaiter(void 0, void 0, void 0, function* () { return yield (0, set_1.setCommand)(key, value); })); (() => __awaiter(void 0, void 0, void 0, function* () { try { yield (0, cliUpdate_1.handleAutoUpdate)(); yield program.parseAsync(process.argv); } catch (error) { yield errorHandler_1.errorHandler.handleCommandError(error, program.args[0]); process.exit(1); } }))();