Spaces:
Paused
Paused
File size: 4,068 Bytes
529090e 1d28c11 529090e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | import { EventEmitter } from 'events';
import { persistentEventBus } from '../services/EventBus.js';
export type EventType =
| 'system.alert'
| 'security.alert'
| 'agent.decision'
| 'agent.log'
| 'mcp.tool.executed'
| 'autonomous.task.executed'
| 'taskrecorder.suggestion.created'
| 'taskrecorder.suggestion.approved'
| 'taskrecorder.execution.started'
| 'data:ingested'
| 'widget:invoke'
| 'osint:investigation:start'
| 'threat:hunt:start'
| 'orchestrator:coordinate:start'
| 'docgen:powerpoint:create'
| 'docgen:word:create'
| 'docgen:excel:create'
| 'docgen:powerpoint:completed'
| 'docgen:powerpoint:failed'
| 'docgen:word:completed'
| 'docgen:word:failed'
| 'docgen:excel:completed'
| 'docgen:excel:failed'
| 'docgen:powerpoint:created'
| 'docgen:powerpoint:error'
| 'devtools:scan:started'
| 'devtools:scan:completed'
| 'devtools:scan:failed'
// Data ingestion events
| 'ingestion:emails'
| 'ingestion:news'
| 'ingestion:documents'
| 'ingestion:assets'
| 'email:new'
| 'email:fetched'
| 'threat:detected'
| 'system:heartbeat'
| 'system:force-refresh'
// WebSocket events
| 'ws:connected'
| 'ws:disconnected'
// HansPedder agent events
| 'hanspedder:test-results'
| 'hanspedder:nudge'
| 'hanspedder:fix-reported'
// System health events
| 'system:backend-unhealthy'
| 'system:health-check'
| 'mcp:reconnect-requested'
// Prototype generation events
| 'prototype.generation.started'
| 'prototype.generation.completed'
| 'prototype.generation.error'
| 'prototype.saved'
| 'prototype.deleted'
// MCP tool events
| 'mcp.tool.call'
| 'mcp.tool.result'
| 'mcp.tool.error'
// NudgeService events
| 'nudge.system_metrics'
| 'nudge.cycle_complete'
| 'agent.ping'
| 'system.activity'
| 'data.push'
// Autonomous memory events
| 'pattern:recorded'
| 'failure:recorded'
| 'failure:recurring'
| 'recovery:completed'
| 'health:recorded'
| 'source:health'
| 'decision:made'
// Email events
| 'email:refresh'
| 'email:new';
export interface BaseEvent {
type: EventType;
timestamp: string;
source: string;
payload: any;
}
/**
* Unified Event Bus Interface
* Uses RedisEventBus in production for persistence and scalability
* Falls back to in-memory EventEmitter in development
*/
class MCPEventBus extends EventEmitter {
constructor() {
super();
// init persistent bus (no await needed; it will fallback if unavailable)
persistentEventBus.init().catch((err) => {
console.warn('⚠️ Redis Streams not ready, using in-memory bus:', err?.message || err);
});
}
async initialize(): Promise<void> {
// persistent bus handles its own initialization
await persistentEventBus.init();
}
emitEvent(event: BaseEvent) {
// Publish to stream (persistent) or fallback to memory
persistentEventBus.publish(event.type, event);
if (!persistentEventBus.isReady()) {
// Immediate local delivery for dev/fallback
super.emit(event.type, event);
super.emit('*', event);
}
}
// Direct emit for convenience (for non-BaseEvent objects)
emit(type: EventType | '*', ...args: any[]): boolean {
if (type !== '*') {
persistentEventBus.publish(type, args[0]);
}
if (!persistentEventBus.isReady()) {
return super.emit(type, ...args);
}
return true;
}
onEvent(type: EventType | '*', listener: (event: BaseEvent | any) => void) {
if (type !== '*') {
persistentEventBus.subscribe(type, listener);
}
// Always listen locally for fallback
this.on(type, listener);
}
async shutdown(): Promise<void> {
await persistentEventBus.shutdown?.();
}
}
export const eventBus = new MCPEventBus();
|