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.

109 lines (108 loc) 4.21 kB
import Redis from 'ioredis'; import { readFileSync } from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; // ESM-compatible __dirname const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); export class RedisWaitingMode { redis; config; constructor(){ const configPath = path.resolve(__dirname, '../../.claude/skills/redis-coordination/config.json'); const configFile = readFileSync(configPath, 'utf-8'); this.config = JSON.parse(configFile); this.redis = new Redis({ host: this.config.host, port: this.config.port, db: this.config.db }); } async enterWaitingMode(taskId, agentId, context) { try { const wakeChannel = this.config.waitingMode.wakeChannelPattern.replace('{taskId}', taskId).replace('{agentId}', agentId); const result = await this.redis.blpop(wakeChannel, this.config.waitingMode.defaultTimeout / 1000); return { status: result ? 'woken' : 'timeout', payload: result ? JSON.parse(result[1]) : null, context }; } catch (error) { return { status: 'error', message: error instanceof Error ? error.message : 'Unknown error', context }; } } async wakeAgent(taskId, agentId, payload) { try { const wakeChannel = this.config.waitingMode.wakeChannelPattern.replace('{taskId}', taskId).replace('{agentId}', agentId); await this.redis.lpush(wakeChannel, JSON.stringify(payload)); return { status: 'success', taskId, agentId, payload }; } catch (error) { return { status: 'error', message: error instanceof Error ? error.message : 'Unknown error', taskId, agentId }; } } async reportResult(taskId, agentId, result, confidence) { try { const resultKey = this.config.waitingMode.resultKeyPattern.replace('{taskId}', taskId).replace('{agentId}', agentId); await this.redis.set(resultKey, JSON.stringify({ result, confidence, timestamp: Date.now() })); return { status: 'success', taskId, agentId, confidence }; } catch (error) { return { status: 'error', message: error instanceof Error ? error.message : 'Unknown error', taskId, agentId }; } } async collectConsensus(taskId, agentIds) { try { const results = await Promise.all(agentIds.map(async (agentId)=>{ const resultKey = this.config.waitingMode.resultKeyPattern.replace('{taskId}', taskId).replace('{agentId}', agentId); const resultStr = await this.redis.get(resultKey); return resultStr ? JSON.parse(resultStr) : null; })); const validResults = results.filter((r)=>r && r.confidence); const avgConfidence = validResults.length ? validResults.reduce((sum, r)=>sum + r.confidence, 0) / validResults.length : 0; const consensusReached = avgConfidence >= this.config.waitingMode.consensusThreshold; return { status: consensusReached ? 'consensus' : 'insufficient_consensus', taskId, results: validResults, avgConfidence, consensusThreshold: this.config.waitingMode.consensusThreshold }; } catch (error) { return { status: 'error', message: error instanceof Error ? error.message : 'Unknown error', taskId }; } } } // Export a singleton instance for easier use export const redisWaitingMode = new RedisWaitingMode(); //# sourceMappingURL=redis-waiting-mode.js.map