@2501-ai/cli
Version:
[](https://www.npmjs.com/package/@2501-ai/cli) [](https://www.2501.ai/research/full-humaneval-benchmark) [![Lic
88 lines (87 loc) • 3.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseConnectionString = parseConnectionString;
exports.configureRemoteExecution = configureRemoteExecution;
const logger_1 = __importDefault(require("../utils/logger"));
const sshConfig_1 = require("../utils/sshConfig");
const types_1 = require("../utils/types");
const DEFAULT_PORTS = {
ssh: '22',
winrm: '5985',
};
function isRemoteExecType(type) {
return types_1.REMOTE_EXEC_TYPES.includes(type);
}
function getDefaultPort(remoteExecType) {
return DEFAULT_PORTS[remoteExecType];
}
const HOST_PATTERNS = {
WITH_PORT: /^((?:\d{1,3}\.){3}\d{1,3}|[a-zA-Z0-9.-]+):(\d+)$/,
WITHOUT_PORT: /^((?:\d{1,3}\.){3}\d{1,3}|[a-zA-Z0-9.-]+)$/,
};
function tryParseExplicitConnection(connectionString, defaultPort) {
const lastAtIndex = connectionString.lastIndexOf('@');
if (lastAtIndex === -1)
return null;
const user = connectionString.substring(0, lastAtIndex);
const hostPortPart = connectionString.substring(lastAtIndex + 1);
const portMatch = hostPortPart.match(HOST_PATTERNS.WITH_PORT);
if (portMatch)
return { user, host: portMatch[1], port: portMatch[2] };
const hostMatch = hostPortPart.match(HOST_PATTERNS.WITHOUT_PORT);
if (hostMatch)
return { user, host: hostMatch[1], port: defaultPort };
return null;
}
function generateConnectionErrorMessage(connectionString) {
let message = 'Invalid connection format. Use: user@host:port or user@host';
if ((0, sshConfig_1.hasSSHConfig)()) {
message += `, or add '${connectionString}' to your SSH config file`;
}
else {
message += `, or create an SSH config file at ${(0, sshConfig_1.getSSHConfigPath)()}`;
}
return message;
}
function parseConnectionString(connectionString, remoteExecType = 'ssh') {
const defaultPort = getDefaultPort(remoteExecType);
const explicitResult = tryParseExplicitConnection(connectionString, defaultPort);
if (explicitResult) {
return explicitResult;
}
if (remoteExecType === 'ssh') {
const sshConfigResult = (0, sshConfig_1.lookupConnectionInSSHConfig)(connectionString, defaultPort);
logger_1.default.log('sshConfigResult', { sshConfigResult });
if (sshConfigResult) {
return sshConfigResult;
}
}
throw new Error(generateConnectionErrorMessage(connectionString));
}
function configureRemoteExecution(options) {
var _a;
if (!options.remoteExec) {
throw new Error('Remote execution is not enabled');
}
const remoteExecType = (_a = options.remoteExecType) !== null && _a !== void 0 ? _a : 'ssh';
if (!isRemoteExecType(remoteExecType)) {
throw new Error('Invalid remote execution type. Use: ssh or winrm');
}
const connectionDetails = parseConnectionString(options.remoteExec, remoteExecType);
logger_1.default.debug('connectionDetails', { connectionDetails });
return {
enabled: true,
target: connectionDetails.host,
port: parseInt(connectionDetails.port),
type: remoteExecType,
user: connectionDetails.user,
platform: remoteExecType === 'winrm' ? 'windows' : 'unix',
password: options.remoteExecPassword,
private_key: connectionDetails.identityFile || options.remotePrivateKey,
remote_workspace: options.remoteWorkspace || '',
raw_ssh: options.rawSsh,
};
}