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.
142 lines (141 loc) • 4.94 kB
JavaScript
/**
* Redis Coordination Layer for Dependency Resolution Engine
*
* Manages cross-functional dependency resolution with Redis pub/sub
* Supports distributed swarm coordination and state persistence
*/ import crypto from 'crypto';
/**
* Redis Coordination Configuration
* Supports authentication via REDIS_PASSWORD environment variable
*/ const REDIS_CONFIG = {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379'),
password: process.env.REDIS_PASSWORD || null,
db: parseInt(process.env.REDIS_DB || '0'),
retryDelayOnFailover: 100,
maxRetriesPerRequest: 3,
lazyConnect: true
};
/**
* Coordination Channels
*/ const CHANNELS = {
DEPENDENCIES: 'swarm:phase-2:dependencies',
CONFLICTS: 'swarm:phase-2:conflicts',
RESOLUTION: 'swarm:phase-2:resolution',
COORDINATION: 'swarm:phase-2:coordination',
HEARTBEAT: 'swarm:phase-2:heartbeat'
};
/**
* Message Types
*/ const MESSAGE_TYPES = {
TASK_ADDED: 'task_added',
TASK_UPDATED: 'task_updated',
TASK_COMPLETED: 'task_completed',
DEPENDENCY_ADDED: 'dependency_added',
DEPENDENCY_REMOVED: 'dependency_removed',
CONFLICT_DETECTED: 'conflict_detected',
CONFLICT_RESOLVED: 'conflict_resolved',
RESOLUTION_REQUEST: 'resolution_request',
COORDINATION_REQUEST: 'coordination_request',
HEARTBEAT: 'heartbeat'
};
/**
* Redis-backed Coordination Manager
*/ let RedisCoordinationManager = class RedisCoordinationManager {
options;
client = null;
subscriber = null;
publisher = null;
dependencyResolver = null;
conflictEngine = null;
isInitialized = false;
lastSync = null;
metrics;
startTime;
eventHandlers;
constructor(options = {}){
this.startTime = Date.now();
this.options = {
redisConfig: REDIS_CONFIG,
nodeId: `node_${crypto.randomBytes(8).toString('hex')}`,
enablePersistence: true,
syncInterval: 5000,
heartbeatInterval: 30000,
maxResolutionTime: 10,
...options
};
this.client = null;
this.subscriber = null;
this.publisher = null;
this.dependencyResolver = null;
this.conflictEngine = null;
this.isInitialized = false;
this.lastSync = null;
this.metrics = {
messagesPublished: 0,
messagesReceived: 0,
conflictsDetected: 0,
conflictsResolved: 0,
averageResolutionTime: 0,
totalNodes: 0
};
this.eventHandlers = new Map();
this.setupEventHandlers();
}
/**
* Setup default event handlers for different message types
* Provides a default implementation that can be overridden or extended
*/ setupEventHandlers() {
this.eventHandlers.set(MESSAGE_TYPES.TASK_ADDED, this.handleTaskAdded.bind(this));
this.eventHandlers.set(MESSAGE_TYPES.TASK_UPDATED, this.handleTaskUpdated.bind(this));
this.eventHandlers.set(MESSAGE_TYPES.TASK_COMPLETED, this.handleTaskCompleted.bind(this));
this.eventHandlers.set(MESSAGE_TYPES.DEPENDENCY_ADDED, this.handleDependencyAdded.bind(this));
this.eventHandlers.set(MESSAGE_TYPES.DEPENDENCY_REMOVED, this.handleDependencyRemoved.bind(this));
this.eventHandlers.set(MESSAGE_TYPES.CONFLICT_DETECTED, this.handleConflictDetected.bind(this));
}
// Stub implementations for event handlers
handleTaskAdded(message) {
console.log('Task Added:', message);
}
handleTaskUpdated(message) {
console.log('Task Updated:', message);
}
handleTaskCompleted(message) {
console.log('Task Completed:', message);
}
handleDependencyAdded(message) {
console.log('Dependency Added:', message);
}
handleDependencyRemoved(message) {
console.log('Dependency Removed:', message);
}
handleConflictDetected(message) {
console.log('Conflict Detected:', message);
this.metrics.conflictsDetected++;
}
/**
* Publish message to Redis channel
*/ async publishMessage(channel, message) {
try {
await this.publisher?.publish(channel, JSON.stringify(message));
this.metrics.messagesPublished++;
} catch (error) {
console.error(`❌ Failed to publish to ${channel}:`, error);
throw error;
}
}
/**
* Handle dependency messages
*/ handleDependencyMessage(message) {
this.metrics.messagesReceived++;
if (message.nodeId === this.options.nodeId) {
return; // Ignore own messages
}
const handler = this.eventHandlers.get(message.type);
if (handler) {
handler(message);
}
}
};
export { RedisCoordinationManager, CHANNELS, MESSAGE_TYPES, REDIS_CONFIG };
//# sourceMappingURL=redis-coordination.js.map