/** * V3 Event Interfaces * Domain-Driven Design - Event Sourcing Pattern (ADR-007) */ /** * Event priority levels */ export type EventPriority = 'critical' | 'high' | 'normal' | 'low'; /** * Core event structure */ export interface IEvent { readonly id: string; readonly type: string; readonly timestamp: Date; readonly source: string; payload: T; priority?: EventPriority; correlationId?: string; causationId?: string; metadata?: { version?: number; userId?: string; sessionId?: string; [key: string]: unknown; }; } /** * Event creation parameters */ export interface IEventCreate { type: string; payload: T; source?: string; priority?: EventPriority; correlationId?: string; causationId?: string; metadata?: IEvent['metadata']; } /** * Event handler function type */ export type IEventHandler = (event: IEvent) => void | Promise; /** * Event filter for subscriptions */ export interface IEventFilter { types?: string[]; sources?: string[]; priority?: EventPriority[]; correlationId?: string; } /** * Event subscription handle */ export interface IEventSubscription { readonly id: string; readonly filter: IEventFilter; /** * Unsubscribe from events */ unsubscribe(): void; /** * Pause subscription */ pause(): void; /** * Resume subscription */ resume(): void; /** * Check if subscription is active */ isActive(): boolean; } /** * Event bus interface for pub/sub communication */ export interface IEventBus { /** * Emit an event to all subscribers */ emit(type: string, payload: T, options?: Partial>): void; /** * Emit an event and wait for all handlers */ emitAsync(type: string, payload: T, options?: Partial>): Promise; /** * Subscribe to events matching a type pattern */ on(type: string, handler: IEventHandler): IEventSubscription; /** * Subscribe to events with filter */ subscribe(filter: IEventFilter, handler: IEventHandler): IEventSubscription; /** * Subscribe to a single event occurrence */ once(type: string, handler: IEventHandler): IEventSubscription; /** * Remove a specific handler */ off(type: string, handler: IEventHandler): void; /** * Remove all handlers for a type */ removeAllListeners(type?: string): void; /** * Get count of listeners for a type */ listenerCount(type: string): number; /** * Get all event types with active listeners */ eventNames(): string[]; } /** * System event types enumeration */ export const SystemEventTypes = { // System lifecycle SYSTEM_READY: 'system:ready', SYSTEM_SHUTDOWN: 'system:shutdown', SYSTEM_ERROR: 'system:error', SYSTEM_HEALTHCHECK: 'system:healthcheck', // Agent lifecycle AGENT_SPAWNED: 'agent:spawned', AGENT_TERMINATED: 'agent:terminated', AGENT_ERROR: 'agent:error', AGENT_IDLE: 'agent:idle', AGENT_BUSY: 'agent:busy', AGENT_HEALTH_CHANGED: 'agent:health:changed', // Task lifecycle TASK_CREATED: 'task:created', TASK_ASSIGNED: 'task:assigned', TASK_STARTED: 'task:started', TASK_COMPLETED: 'task:completed', TASK_FAILED: 'task:failed', TASK_CANCELLED: 'task:cancelled', TASK_TIMEOUT: 'task:timeout', TASK_RETRY: 'task:retry', // Session lifecycle SESSION_CREATED: 'session:created', SESSION_RESTORED: 'session:restored', SESSION_TERMINATED: 'session:terminated', SESSION_PERSISTED: 'session:persisted', // Memory events MEMORY_STORED: 'memory:stored', MEMORY_RETRIEVED: 'memory:retrieved', MEMORY_CLEARED: 'memory:cleared', // Coordination events COORDINATION_STARTED: 'coordination:started', COORDINATION_COMPLETED: 'coordination:completed', DEADLOCK_DETECTED: 'coordination:deadlock', // Metrics events METRICS_COLLECTED: 'metrics:collected', } as const; export type SystemEventType = typeof SystemEventTypes[keyof typeof SystemEventTypes]; /** * Event store interface for event sourcing */ export interface IEventStore { /** * Append an event to the store */ append(event: IEvent): Promise; /** * Get events by aggregate ID */ getByAggregateId(aggregateId: string, fromVersion?: number): Promise; /** * Get events by type */ getByType(type: string, options?: { limit?: number; offset?: number }): Promise; /** * Get events in time range */ getByTimeRange(start: Date, end: Date): Promise; /** * Get events by correlation ID */ getByCorrelationId(correlationId: string): Promise; /** * Get all events (paginated) */ getAll(options?: { limit?: number; offset?: number }): Promise; /** * Get event count */ count(filter?: IEventFilter): Promise; /** * Clear old events */ prune(olderThan: Date): Promise; } /** * Event coordinator for routing and orchestration */ export interface IEventCoordinator { /** * Initialize the coordinator */ initialize(): Promise; /** * Shutdown the coordinator */ shutdown(): Promise; /** * Route an event to appropriate handlers */ route(event: IEvent): Promise; /** * Register a handler for event routing */ registerHandler(type: string, handler: IEventHandler): void; /** * Unregister a handler */ unregisterHandler(type: string, handler: IEventHandler): void; /** * Get event bus instance */ getEventBus(): IEventBus; }