UNPKG

mycoder-agent

Version:

Agent module for mycoder - an AI-powered software development assistant

110 lines 3.47 kB
import { v4 as uuidv4 } from 'uuid'; // Status of a shell process export var ShellStatus; (function (ShellStatus) { ShellStatus["RUNNING"] = "running"; ShellStatus["COMPLETED"] = "completed"; ShellStatus["ERROR"] = "error"; ShellStatus["TERMINATED"] = "terminated"; })(ShellStatus || (ShellStatus = {})); /** * Registry to keep track of shell processes */ export class ShellTracker { ownerAgentId; shells = new Map(); processStates = new Map(); constructor(ownerAgentId) { this.ownerAgentId = ownerAgentId; } // Register a new shell process registerShell(command) { const id = uuidv4(); const shell = { id, status: ShellStatus.RUNNING, startTime: new Date(), metadata: { command, }, }; this.shells.set(id, shell); return id; } // Update the status of a shell process updateShellStatus(id, status, metadata) { const shell = this.shells.get(id); if (!shell) { return false; } shell.status = status; if (status === ShellStatus.COMPLETED || status === ShellStatus.ERROR || status === ShellStatus.TERMINATED) { shell.endTime = new Date(); } if (metadata) { shell.metadata = { ...shell.metadata, ...metadata }; } return true; } // Get all shell processes getShells(status) { const result = []; for (const shell of this.shells.values()) { if (!status || shell.status === status) { result.push(shell); } } return result; } // Get a specific shell process by ID getShellById(id) { return this.shells.get(id); } /** * Cleans up a shell process * @param id The ID of the shell process to clean up */ async cleanupShellProcess(id) { try { const shell = this.shells.get(id); if (!shell) { return; } const processState = this.processStates.get(id); if (processState && !processState.state.completed) { processState.process.kill('SIGTERM'); // Force kill after a short timeout if still running await new Promise((resolve) => { setTimeout(() => { try { if (!processState.state.completed) { processState.process.kill('SIGKILL'); } } catch { // Ignore errors on forced kill } resolve(); }, 500); }); } this.updateShellStatus(id, ShellStatus.TERMINATED); } catch (error) { this.updateShellStatus(id, ShellStatus.ERROR, { error: error instanceof Error ? error.message : String(error), }); } } /** * Cleans up all running shell processes */ async cleanup() { const runningShells = this.getShells(ShellStatus.RUNNING); const cleanupPromises = runningShells.map((shell) => this.cleanupShellProcess(shell.id)); await Promise.all(cleanupPromises); } } //# sourceMappingURL=ShellTracker.js.map