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

200 lines (199 loc) 9.8 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.initCommand = void 0; exports.getWorkspacePath = getWorkspacePath; exports.initRemoteExecution = initRemoteExecution; const axios_1 = __importDefault(require("axios")); const fs_1 = __importDefault(require("fs")); const api_1 = require("../helpers/api"); const security_1 = require("../helpers/security"); const workspace_1 = require("../helpers/workspace"); const configManager_1 = require("../managers/configManager"); const contextBuilder_1 = require("../telemetry/contextBuilder"); const connectionParser_1 = require("../remoteExecution/connectionParser"); const remoteWorkspace_1 = require("../remoteExecution/remoteWorkspace"); const remoteExecutor_1 = require("../remoteExecution/remoteExecutor"); const remoteSystemInfo_1 = require("../remoteExecution/remoteSystemInfo"); const conf_1 = require("../utils/conf"); const logger_1 = __importDefault(require("../utils/logger")); const platform_1 = require("../utils/platform"); const systemInfo_1 = require("../utils/systemInfo"); const logger = new logger_1.default(); function fetchConfiguration(configKey) { return __awaiter(this, void 0, void 0, function* () { const { data: configurations } = yield axios_1.default.get(`/configurations`); const selectedConfig = configurations.find((config) => config.key === configKey); if (!selectedConfig) { throw new Error(`Configuration not found: ${configKey}`); } return selectedConfig; }); } function getWorkspacePath(options) { return __awaiter(this, void 0, void 0, function* () { if (remoteExecutor_1.RemoteExecutor.instance.isEnabled()) { return (0, workspace_1.resolveWorkspacePath)({ workspace: typeof options.workspace === 'string' ? options.workspace : undefined, }); } if (options.workspace === false) { const path = (0, platform_1.getTempPath2501)(Date.now().toString()); fs_1.default.mkdirSync(path, { recursive: true }); logger.log(`Using workspace at ${path}`); return path; } let finalPath; if (typeof options.workspace === 'string' && !!options.workspace) { finalPath = (0, workspace_1.resolveWorkspacePath)({ workspace: options.workspace }); } else { finalPath = process.cwd(); } if (!options.ignoreUnsafe && (0, security_1.isDirUnsafe)(finalPath)) { logger.log(`Files in the workspace "${finalPath}" are considered sensitive`); const res = yield logger.prompt(`Are you sure you want to proceed with synchronization ? This will synchronize a sensitive directory and may overwrite or modify critical files. (y/n)`); if (res === false || res.toString() === 'Symbol(clack:cancel)') { logger.cancel('Operation cancelled'); process.exit(0); } logger.log(`Using workspace at ${finalPath}`); } return finalPath; }); } function initRemoteExecution(options, logger) { return __awaiter(this, void 0, void 0, function* () { if (!(options === null || options === void 0 ? void 0 : options.remoteExec)) { return; } let remoteExecConfig; try { remoteExecConfig = (0, connectionParser_1.configureRemoteExecution)(options); } catch (error) { throw new Error(`Remote connection configuration failed: ${error.message}`); } remoteExecutor_1.RemoteExecutor.instance.init(remoteExecConfig); const { target, type } = remoteExecConfig; logger.start(`Connecting to remote host ${target} using ${type}...`); const isValid = yield remoteExecutor_1.RemoteExecutor.instance.validateConnection(); if (!isValid) { throw new Error('Remote connection failed. Please check your settings.'); } const { platform } = remoteExecutor_1.RemoteExecutor.instance.getConfig(); logger.message(`Detected platform: ${platform} for ${target}`); logger.stop('Remote connection validated successfully'); const remoteWorkspace = yield (0, remoteWorkspace_1.setupRemoteWorkspace)(remoteExecConfig, options); remoteExecConfig.remote_workspace = remoteWorkspace; return remoteExecConfig; }); } function checkForExistingAgent(workspacePath) { var _a; const eligibleAgent = (0, conf_1.getEligibleAgent)(workspacePath); if ((_a = eligibleAgent === null || eligibleAgent === void 0 ? void 0 : eligibleAgent.remote_exec) === null || _a === void 0 ? void 0 : _a.enabled) { throw new Error('An agent is already initialized in this workspace.'); } } const initCommand = (options) => __awaiter(void 0, void 0, void 0, function* () { var _a; try { (0, contextBuilder_1.updateTelemetryContext)({ agentId: options.agentId }); const configManager = configManager_1.ConfigManager.instance; if (process.env.TFZO_DISABLE_SPINNER) { const shouldDisableSpinner = process.env.TFZO_DISABLE_SPINNER === 'true'; configManager.set('disable_spinner', shouldDisableSpinner); } const workspacePath = yield getWorkspacePath(options); const remoteExecConfig = yield initRemoteExecution(options, logger); checkForExistingAgent(workspacePath); const systemInfoPromise = remoteExecConfig ? (0, remoteSystemInfo_1.getRemoteSystemInfo)() : (0, systemInfo_1.getSystemInfo)(); const systemInfo = yield systemInfoPromise; logger_1.default.debug('systemInfo results:', systemInfo); logger.start('Creating agent'); const path = (_a = remoteExecConfig === null || remoteExecConfig === void 0 ? void 0 : remoteExecConfig.remote_workspace) !== null && _a !== void 0 ? _a : workspacePath; let id; let name; let configurationKey = options.config || ''; const hostInfo = yield (0, systemInfo_1.getHostInfo)(); const context = { agentId: options.agentId, }; if (options.agentId) { const agent = yield (0, api_1.getAgent)(options.agentId); id = agent.id; name = agent.name; logger_1.default.debug('Agent retrieved:', { agent }); if (agent.status !== 'idle') { const msg = `Agent ${id} is not idle. Please stop the agent before starting a new task.`; logger.cancel(msg); logger_1.default.debug(msg); throw new Error('Agent is not idle.'); } configurationKey = agent.configuration; if (remoteExecutor_1.RemoteExecutor.instance.isEnabled()) { delete hostInfo.public_ip; delete hostInfo.public_ip_note; delete hostInfo.private_ip; delete hostInfo.name; } yield (0, api_1.updateHostInfo)(id, hostInfo); yield (0, api_1.updateAgent)(id, { workspace: path, cli_data: Object.assign({}, systemInfo), }); context.orgId = agent.organization.id; context.tenantId = agent.organization.tenant_id; context.agentId = agent.id; } else { if (!options.config) { throw new Error('A configuration Key is required to create an agent.'); } const agentConfig = yield fetchConfiguration(options.config); if (!agentConfig) { throw new Error(`Invalid Agent configuration: ${options.config}`); } const createdAgent = yield (0, api_1.createAgent)(path, agentConfig, systemInfo, configManager.get('engine'), hostInfo); logger_1.default.debug('Agent created:', { agent: createdAgent }); id = createdAgent.id; name = createdAgent.name; context.orgId = createdAgent.organization.id; context.tenantId = createdAgent.organization.tenant_id; context.hostId = createdAgent.host_id; context.agentId = createdAgent.id; } (0, contextBuilder_1.updateTelemetryContext)(context); (0, conf_1.addAgent)({ id, name, workspace: workspacePath, configuration: configurationKey, engine: configManager.get('engine'), remote_exec: remoteExecConfig !== null && remoteExecConfig !== void 0 ? remoteExecConfig : undefined, org_id: context.orgId, tenant_id: context.tenantId, host_id: context.hostId, }); logger.stop(`Agent ${id} ${options.agentId ? 'retrieved' : 'created'}`); } catch (error) { logger.stop('Failed to initialize agent', 1); throw error; } }); exports.initCommand = initCommand;