UNPKG

claude-flow-novice

Version:

Claude Flow Novice - Advanced orchestration platform for multi-agent AI workflows with CFN Loop architecture Includes Local RuVector Accelerator and all CFN skills for complete functionality.

209 lines (208 loc) 8.05 kB
import { EventEmitter } from "events"; import { readFile } from "fs/promises"; import { join, dirname } from "path"; import { fileURLToPath } from "url"; // ESM-compatible __dirname const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export var ProcessType = /*#__PURE__*/ function(ProcessType) { ProcessType["EVENT_BUS"] = "EVENT_BUS"; ProcessType["ORCHESTRATOR"] = "ORCHESTRATOR"; ProcessType["MEMORY_MANAGER"] = "MEMORY_MANAGER"; ProcessType["TERMINAL_POOL"] = "TERMINAL_POOL"; ProcessType["COORDINATOR"] = "COORDINATOR"; ProcessType["MCP_SERVER"] = "MCP_SERVER"; return ProcessType; }({}); export var ProcessStatus = /*#__PURE__*/ function(ProcessStatus) { ProcessStatus["STOPPED"] = "STOPPED"; ProcessStatus["STARTING"] = "STARTING"; ProcessStatus["RUNNING"] = "RUNNING"; ProcessStatus["STOPPING"] = "STOPPING"; ProcessStatus["ERROR"] = "ERROR"; return ProcessStatus; }({}); export class ProcessLifecycleManager extends EventEmitter { processes = new Map(); config; configPath; constructor(configPath = join(__dirname, "..", "..", ".claude", "skills", "process-lifecycle", "config.json")){ super(); this.configPath = configPath; } async initialize() { try { const configData = await readFile(this.configPath, "utf-8"); this.config = JSON.parse(configData); this.initializeProcesses(); this.emit("initialized", { config: this.config }); } catch (error) { this.emit("error", { component: "ProcessLifecycleManager", error }); throw error; } } initializeProcesses() { const processDefinitions = Object.entries(this.config.processTypes).map(([type, details])=>({ id: type, name: this.formatProcessName(type), type: type, status: "STOPPED" })); for (const process of processDefinitions){ this.processes.set(process.id, process); } } formatProcessName(type) { return type.split("_").map((word)=>word[0] + word.slice(1).toLowerCase()).join(" "); } async startProcess(processId) { const process = this.processes.get(processId); if (!process) { throw new Error(`Unknown process: ${processId}`); } if (process.status === "RUNNING") { throw new Error(`Process ${processId} is already running`); } // Validate dependencies const processConfig = this.config.processTypes[process.type]; const missingDependencies = processConfig.dependencies.filter((dep)=>this.processes.get(dep)?.status !== "RUNNING"); if (missingDependencies.length > 0) { throw new Error(`Missing dependencies for ${processId}: ${missingDependencies.join(", ")}`); } this.updateProcessStatus(processId, "STARTING"); try { // Simulated process start - in real implementation, this would start actual processes process.startTime = Date.now(); process.pid = process.id.charCodeAt(0); // Simulated PID this.updateProcessStatus(processId, "RUNNING"); this.emit("processStarted", { processId, process }); } catch (error) { this.updateProcessStatus(processId, "ERROR"); process.metrics = { ...process.metrics, lastError: error.message }; this.emit("processError", { processId, error }); throw error; } } async stopProcess(processId) { const process = this.processes.get(processId); if (!process || process.status !== "RUNNING") { throw new Error(`Process ${processId} is not running`); } // Check if any running processes depend on this one const dependentProcesses = Array.from(this.processes.values()).filter((p)=>this.config.processTypes[p.type].dependencies.includes(process.type) && p.status === "RUNNING"); if (dependentProcesses.length > 0) { throw new Error(`Cannot stop ${processId}, dependent processes are running: ${dependentProcesses.map((p)=>p.id).join(", ")}`); } this.updateProcessStatus(processId, "STOPPING"); try { // Simulated process stop this.updateProcessStatus(processId, "STOPPED"); this.emit("processStopped", { processId }); } catch (error) { this.updateProcessStatus(processId, "ERROR"); this.emit("processError", { processId, error }); throw error; } } async restartProcess(processId) { await this.stopProcess(processId); await new Promise((resolve)=>setTimeout(resolve, 1000)); // Brief delay await this.startProcess(processId); } async startAll() { // Start in dependency order const startOrder = Object.values(ProcessType); for (const processId of startOrder){ try { await this.startProcess(processId); } catch (error) { console.error(`Failed to start ${processId}:`, error.message); // Continue with other processes } } } async stopAll() { // Stop in reverse dependency order const stopOrder = Object.values(ProcessType).reverse(); for (const processId of stopOrder){ const process = this.processes.get(processId); if (process && process.status === "RUNNING") { try { await this.stopProcess(processId); } catch (error) { console.error(`Failed to stop ${processId}:`, error.message); } } } } getProcess(processId) { return this.processes.get(processId); } getAllProcesses() { return Array.from(this.processes.values()); } getSystemStats() { const processes = this.getAllProcesses(); const runningProcesses = processes.filter((p)=>p.status === "RUNNING"); const stoppedProcesses = processes.filter((p)=>p.status === "STOPPED"); const errorProcesses = processes.filter((p)=>p.status === "ERROR"); return { totalProcesses: processes.length, runningProcesses: runningProcesses.length, stoppedProcesses: stoppedProcesses.length, errorProcesses: errorProcesses.length, systemUptime: this.getSystemUptime(), totalMemory: this.getTotalMemoryUsage(), totalCpu: this.getTotalCpuUsage() }; } updateProcessStatus(processId, status) { const process = this.processes.get(processId); if (process) { process.status = status; this.emit("statusChanged", { processId, status }); } } getSystemUptime() { const firstRunningProcess = Array.from(this.processes.values()).find((p)=>p.status === "RUNNING" && p.startTime); return firstRunningProcess && firstRunningProcess.startTime ? Date.now() - firstRunningProcess.startTime : 0; } getTotalMemoryUsage() { // Placeholder for actual memory monitoring return 0; } getTotalCpuUsage() { // Placeholder for actual CPU monitoring return 0; } async getProcessLogs(processId, lines = 50) { // Placeholder for actual logging system return [ `[${new Date().toISOString()}] Process ${processId} started`, `[${new Date().toISOString()}] Process ${processId} is running normally` ]; } } //# sourceMappingURL=process-lifecycle.js.map