UNPKG

@measey/mycoder-agent

Version:

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

110 lines 3.55 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 shellId = uuidv4(); const shell = { shellId, status: ShellStatus.RUNNING, startTime: new Date(), metadata: { command, }, }; this.shells.set(shellId, shell); return shellId; } // Update the status of a shell process updateShellStatus(shellId, status, metadata) { const shell = this.shells.get(shellId); 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(shellId) { return this.shells.get(shellId); } /** * Cleans up a shell process * @param shellId The ID of the shell process to clean up */ async cleanupShellProcess(shellId) { try { const shell = this.shells.get(shellId); if (!shell) { return; } const processState = this.processStates.get(shellId); 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(shellId, ShellStatus.TERMINATED); } catch (error) { this.updateShellStatus(shellId, 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.shellId)); await Promise.all(cleanupPromises); } } //# sourceMappingURL=ShellTracker.js.map