UNPKG

snow-flow

Version:

Snow-Flow v3.2.0: Complete ServiceNow Enterprise Suite with 180+ MCP Tools. ATF Testing, Knowledge Management, Service Catalog, Change Management with CAB scheduling, Virtual Agent chatbots with NLU, Performance Analytics KPIs, Flow Designer automation, A

405 lines 15.8 kB
"use strict"; /** * Snow-Flow Main Integration Layer * Coordinates all subsystems: Agents, Memory, MCPs, and ServiceNow */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.snowFlowSystem = exports.SnowFlowSystem = void 0; const events_1 = require("events"); const snow_flow_config_1 = require("./config/snow-flow-config"); const servicenow_queen_1 = require("./queen/servicenow-queen"); const memory_system_1 = require("./memory/memory-system"); const mcp_server_manager_1 = require("./utils/mcp-server-manager"); const performance_tracker_1 = require("./monitoring/performance-tracker"); const system_health_1 = require("./health/system-health"); const error_recovery_1 = require("./utils/error-recovery"); const logger_1 = require("./utils/logger"); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); class SnowFlowSystem extends events_1.EventEmitter { constructor(config) { super(); this.sessions = new Map(); this.initialized = false; this.config = new snow_flow_config_1.SnowFlowConfig(config); this.logger = new logger_1.Logger('SnowFlowSystem'); this.errorRecovery = new error_recovery_1.ErrorRecovery(this.logger); } /** * Initialize the entire Snow-Flow system */ async initialize() { if (this.initialized) { this.logger.info('System already initialized'); return; } try { this.logger.info('Initializing Snow-Flow System...'); // 1. Initialize Memory System await this.initializeMemory(); // 2. Initialize MCP Servers await this.initializeMCPServers(); // 3. Initialize Queen Agent System await this.initializeQueen(); // 4. Initialize Performance Tracking await this.initializePerformanceTracking(); // 5. Initialize System Health Monitoring await this.initializeHealthMonitoring(); this.initialized = true; this.emit('system:initialized'); this.logger.info('Snow-Flow System initialized successfully'); } catch (error) { this.logger.error('System initialization failed:', error); await this.errorRecovery.handleCriticalError(error, { operation: 'system_initialization', fallbackStrategies: ['retry_initialization', 'partial_initialization'] }); throw error; } } /** * Initialize Memory System with SQLite */ async initializeMemory() { this.logger.info('Initializing Memory System...'); const dbPath = path_1.default.join(os_1.default.homedir(), '.snow-flow', 'memory', 'snow-flow.db'); this.memory = new memory_system_1.MemorySystem({ dbPath, schema: { version: '1.0.0', autoMigrate: true, ...(this.config.memory.schema || {}) }, ttl: { default: 86400000, session: 3600000, artifact: 86400000, metric: 3600000, ...(this.config.memory.ttl || {}) } }); await this.memory.initialize(); this.emit('memory:initialized'); } /** * Initialize MCP Server Manager */ async initializeMCPServers() { this.logger.info('Initializing MCP Servers...'); this.mcpManager = new mcp_server_manager_1.MCPServerManager(JSON.stringify(this.config.mcp)); // Start all required MCP servers const servers = [ 'servicenow-deployment', 'servicenow-intelligent', 'servicenow-operations', 'servicenow-platform-development', 'servicenow-integration', 'servicenow-automation', 'servicenow-security-compliance', 'servicenow-reporting-analytics', 'servicenow-graph-memory', 'servicenow-update-set' ]; for (const server of servers) { try { await this.mcpManager.startServer(server); this.logger.info(`Started MCP server: ${server}`); } catch (error) { this.logger.error(`Failed to start MCP server ${server}:`, error); // Continue with other servers even if one fails } } this.emit('mcp:initialized'); } /** * Initialize Queen Agent System */ async initializeQueen() { this.logger.info('Initializing Queen Agent System...'); if (!this.memory || !this.mcpManager) { throw new Error('Memory and MCP must be initialized before Queen'); } this.queen = new servicenow_queen_1.ServiceNowQueen({ memoryPath: this.config.memory?.dbPath, maxConcurrentAgents: this.config.agents?.queen?.maxConcurrentAgents || 5, learningRate: this.config.agents?.queen?.learningRate || 0.1, debugMode: this.config.debugMode || false, autoPermissions: this.config.agents?.queen?.autoPermissions || false }); // ServiceNowQueen is ready to use after construction // No initialize method or event handlers available this.emit('queen:initialized'); } /** * Initialize Performance Tracking */ async initializePerformanceTracking() { this.logger.info('Initializing Performance Tracking...'); if (!this.memory) { throw new Error('Memory must be initialized before Performance Tracking'); } this.performanceTracker = new performance_tracker_1.PerformanceTracker({ memory: this.memory, config: { sampleRate: 100, metricsRetention: 86400000, aggregationInterval: 60000, ...(this.config.monitoring.performance || {}) } }); await this.performanceTracker.initialize(); // Set up performance monitoring this.performanceTracker.on('metric:recorded', (metric) => { this.emit('performance:metric', metric); }); this.emit('performance:initialized'); } /** * Initialize System Health Monitoring */ async initializeHealthMonitoring() { this.logger.info('Initializing System Health Monitoring...'); if (!this.memory || !this.mcpManager) { throw new Error('Memory and MCP must be initialized before Health Monitoring'); } this.systemHealth = new system_health_1.SystemHealth({ memory: this.memory, mcpManager: this.mcpManager, config: { checks: { memory: true, mcp: true, servicenow: true, queen: true }, thresholds: { memoryUsage: 85, responseTime: 5000, queueSize: 100, cpuUsage: 80, errorRate: 5 } } }); await this.systemHealth.initialize(); // Set up health monitoring this.systemHealth.on('health:check', (status) => { this.emit('health:status', status); }); // Start periodic health checks await this.systemHealth.startMonitoring(); this.emit('health:initialized'); } /** * Execute a swarm objective */ async executeSwarm(objective, options = {}) { if (!this.initialized) { throw new Error('System not initialized. Call initialize() first.'); } const sessionId = this.generateSessionId(); const session = { id: sessionId, objective, startedAt: new Date(), status: 'initializing', queenAgentId: '', activeAgents: new Map(), completedTasks: 0, totalTasks: 0, errors: [] }; this.sessions.set(sessionId, session); try { // Track swarm execution await this.performanceTracker?.startOperation('swarm_execution', { sessionId, objective }); // Execute objective using Queen's main method const queenResult = await this.queen.executeObjective(objective); const _analysis = { complexity: 0.5, type: 'unknown', estimatedDuration: 30000, queenId: 'main-queen', estimatedTasks: 1 }; session.queenAgentId = _analysis.queenId; session.totalTasks = _analysis.estimatedTasks; session.status = 'active'; // Execute swarm with Queen coordination (MCP-FIRST workflow) console.log(`🚨 SWARM EXECUTING WITH MCP-FIRST WORKFLOW`); console.log(`🎯 Objective: ${objective}`); const executionResult = await this.queen.executeObjective(objective); // Update session with swarm-specific progress tracking session.completedTasks = 1; // Queen completed the objective session.status = 'completed'; this.emit('swarm:progress', { sessionId, progress: { completed: 1, total: 1, status: 'completed', mcpFirst: true, realServiceNow: true } }); session.status = 'completed'; await this.performanceTracker?.endOperation('swarm_execution', { success: true }); return { sessionId, success: true, artifacts: queenResult.artifacts || [], summary: queenResult.deploymentResult || queenResult, metrics: await this.performanceTracker?.getSessionMetrics(sessionId) || {} }; } catch (error) { session.status = 'failed'; session.errors.push(error); await this.performanceTracker?.endOperation('swarm_execution', { success: false, error: error.message }); // Attempt recovery const recovery = await this.errorRecovery.attemptSwarmRecovery(sessionId, error); if (recovery.success) { return recovery.result; } throw error; } } /** * Get system status */ async getStatus() { if (!this.initialized) { return { initialized: false, status: 'not_initialized', components: {} }; } const healthStatus = await this.systemHealth.getFullStatus(); const activeSessions = Array.from(this.sessions.values()) .filter(s => s.status === 'active'); return { initialized: true, status: healthStatus.healthy ? 'healthy' : 'degraded', components: { memory: healthStatus.components.memory, mcp: healthStatus.components.mcp, queen: healthStatus.components.queen, performance: healthStatus.components.performance }, activeSessions: activeSessions.length, metrics: { totalSessions: this.sessions.size, successRate: await this.calculateSuccessRate(), averageExecutionTime: await this.calculateAverageExecutionTime() } }; } /** * Shutdown the system gracefully */ async shutdown() { this.logger.info('Shutting down Snow-Flow System...'); try { // Complete active sessions for (const session of this.sessions.values()) { if (session.status === 'active') { await this.gracefullyCompleteSession(session.id); } } // Shutdown components in reverse order await this.systemHealth?.stopMonitoring(); await this.performanceTracker?.shutdown(); await this.queen?.shutdown(); // Use available shutdown method if (this.mcpManager && typeof this.mcpManager.shutdownAll === 'function') { await this.mcpManager.shutdownAll(); } else if (this.mcpManager && typeof this.mcpManager.shutdown === 'function') { await this.mcpManager.shutdown(); } await this.memory?.close(); this.initialized = false; this.emit('system:shutdown'); this.logger.info('Snow-Flow System shutdown complete'); } catch (error) { this.logger.error('Error during shutdown:', error); throw error; } } /** * Get session information */ getSession(sessionId) { return this.sessions.get(sessionId); } /** * List all sessions */ listSessions(filter) { const sessions = Array.from(this.sessions.values()); if (filter?.status) { return sessions.filter(s => s.status === filter.status); } return sessions; } /** * Get memory system instance */ getMemory() { return this.memory; } /** * Get MCP manager instance */ getMCPManager() { return this.mcpManager; } /** * Private helper methods */ generateSessionId() { return `swarm_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } updateAgentInfo(session, update) { const agent = session.activeAgents.get(update.agentId) || { id: update.agentId, type: update.agentType, status: 'spawned', assignedTasks: [], progress: 0, lastActivity: new Date() }; Object.assign(agent, update); session.activeAgents.set(update.agentId, agent); } async gracefullyCompleteSession(sessionId) { const session = this.sessions.get(sessionId); if (!session) return; this.logger.info(`Gracefully completing session ${sessionId}`); // Notify all active agents to wrap up for (const agent of session.activeAgents.values()) { if (agent.status === 'active') { // Use available shutdown method // await this.queen?.shutdown(); // No per-agent shutdown available } } // Wait for agents to complete (max 30 seconds) const timeout = setTimeout(() => { session.status = 'completed'; }, 30000); while (session.status === 'active') { await new Promise(resolve => setTimeout(resolve, 1000)); const activeAgents = Array.from(session.activeAgents.values()) .filter(a => a.status === 'active'); if (activeAgents.length === 0) { session.status = 'completed'; clearTimeout(timeout); break; } } } async calculateSuccessRate() { const sessions = Array.from(this.sessions.values()); const completed = sessions.filter(s => s.status === 'completed').length; const total = sessions.filter(s => ['completed', 'failed'].includes(s.status)).length; return total > 0 ? (completed / total) * 100 : 0; } async calculateAverageExecutionTime() { const metrics = await this.performanceTracker?.getAggregateMetrics('swarm_execution'); return metrics?.averageDuration || 0; } } exports.SnowFlowSystem = SnowFlowSystem; // Export singleton instance exports.snowFlowSystem = new SnowFlowSystem(); //# sourceMappingURL=snow-flow-system.js.map