UNPKG

@plastichub/osr-ai-tools

Version:

CLI and library for LLM tools

148 lines (147 loc) 6.86 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.tools = void 0; const path = __importStar(require("path")); const child_process_1 = require("child_process"); const util_1 = require("util"); const execAsync = (0, util_1.promisify)(child_process_1.exec); const __1 = require("../.."); const tools = (target, options) => { const logger = (0, __1.toolLogger)(path.parse(__filename).name, options); return [ { type: 'function', function: { name: 'execute_command', description: 'Execute a terminal command and capture output', parameters: { type: 'object', properties: { command: { type: 'string', description: 'Command to execute' }, args: { type: 'array', items: { type: 'string' }, description: 'Command arguments', optional: true }, cwd: { type: 'string', description: 'Working directory for command execution', optional: true }, background: { type: 'boolean', description: 'Run command in background (non-blocking)', optional: true }, window: { type: 'boolean', description: 'Open command in new terminal window', optional: true }, detached: { type: 'boolean', description: 'Run process detached from parent', optional: true } }, required: ['command'] }, function: async (params) => { try { const workingDir = params.cwd ? path.join(target, params.cwd) : target; const args = params.args || []; logger.debug(`Tool::Terminal : ExecuteCommand Running '${params.command}' in ${workingDir}`, params); if (params.detached) { const isWindows = process.platform === 'win32'; if (isWindows) { (0, child_process_1.spawn)('cmd', ['/c', 'start', 'cmd', '/k', params.command, ...args], { cwd: workingDir, detached: true, stdio: 'ignore' }); } else { // For macOS/Linux (0, child_process_1.spawn)('x-terminal-emulator', ['-e', `${params.command} ${args.join(' ')}`], { cwd: workingDir, detached: true, stdio: 'ignore' }); } return { success: true, output: 'Command launched in new window', error: null }; } // Handle background/detached mode if (params.background || params.detached) { const child = (0, child_process_1.spawn)(params.command, args, { cwd: workingDir, detached: params.detached === true, stdio: 'ignore' }); if (params.detached) { child.unref(); } return { success: true, output: `Process started with PID: ${child.pid}`, error: null }; } // Regular synchronous execution const fullCommand = `${params.command} ${args.join(' ')}`.trim(); logger.debug(`Tool::ExecuteCommand Running '${fullCommand}' in ${workingDir}`); const { stdout, stderr } = await execAsync(fullCommand, { cwd: workingDir }); logger.debug(`Tool::ExecuteCommand Output: ${stdout}`); return { success: !stderr, output: stdout, error: stderr || null }; } catch (error) { logger.error('Error executing command', error); return { success: false, output: null, error: error.message }; } }, parse: JSON.parse } } ]; }; exports.tools = tools;