UNPKG

crewai-ts

Version:

TypeScript port of crewAI for agent-based workflows

198 lines 5.8 kB
/** * Event priority levels */ export declare enum EventPriority { LOW = 0, NORMAL = 1, HIGH = 2, CRITICAL = 3 } /** * Base flow event interface */ export interface FlowEvent { type: string; timestamp: number; priority: EventPriority; metadata?: Record<string, any>; } /** * Flow state change event */ export interface FlowStateChangeEvent extends FlowEvent { type: 'flow:state:changed'; flowId: string; stateId: string; changes: Record<string, any>; } /** * Flow execution event */ export interface FlowExecutionEvent extends FlowEvent { type: 'flow:execution:started' | 'flow:execution:completed' | 'flow:execution:failed'; flowId: string; executionId: string; duration?: number; error?: Error; } /** * Flow method execution event */ export interface FlowMethodEvent extends FlowEvent { type: 'flow:method:before' | 'flow:method:after' | 'flow:method:error'; flowId: string; methodName: string; executionId: string; duration?: number; result?: any; error?: Error; } /** * Flow routing event */ export interface FlowRoutingEvent extends FlowEvent { type: 'flow:routing:evaluation' | 'flow:routing:selected'; flowId: string; routeId?: string; matches?: string[]; evaluationTimeMs?: number; } /** * Combined event types */ export type AllFlowEvents = FlowStateChangeEvent | FlowExecutionEvent | FlowMethodEvent | FlowRoutingEvent; /** * Event handler function */ export type EventHandler<T extends FlowEvent = FlowEvent> = (event: T) => void | Promise<void>; /** * Event subscription options */ export interface EventSubscriptionOptions { filter?: (event: FlowEvent) => boolean; maxListenerCount?: number; priorityThreshold?: EventPriority; queueSize?: number; batchEvents?: boolean; batchTimeMs?: number; } /** * Event bus options */ export interface FlowEventBusOptions { enableProfiling?: boolean; maxQueueSize?: number; defaultBatchSize?: number; warningThresholdMs?: number; errorHandler?: (error: Error, event: FlowEvent) => void; } /** * Holds performance metrics for the event bus */ interface EventBusMetrics { eventsPublished: number; eventsDelivered: number; /** * Count events by type for optimized metrics tracking */ eventTypeCounts: Record<string, number>; averageProcessingTimeMs: number; totalProcessingTimeMs: number; maxProcessingTimeMs: number; queueOverflowCount: number; errorCount: number; activeSubscriptions: number; /** * Total events processed through the system * Memory-optimized tracking separate from published events */ eventsProcessed: number; /** * Number of events processed in batch mode for memory efficiency */ batchedEventCount: number; } /** * Manages event handling for flow system with optimized event routing * and efficient subscription management. */ export declare class FlowEventBus { private eventEmitter; private eventQueue; private processing; private batchedEvents; private batchTimers; private options; private handlerSets; private handlerFilters; private handlerPriorities; private metrics; constructor(options?: FlowEventBusOptions); /** * Subscribe to flow events with optional filtering * @param eventType Event type to subscribe to (can include wildcards) * @param handler Event handler function * @param options Subscription options * @returns Subscription ID that can be used to unsubscribe */ subscribe<T extends FlowEvent>(eventType: string, handler: EventHandler<T>, options?: EventSubscriptionOptions): string; /** * Subscribe to multiple event types at once * @param eventTypes Array of event types to subscribe to * @param handler Event handler function * @param options Subscription options * @returns Array of subscription IDs */ subscribeToMany<T extends FlowEvent>(eventTypes: string[], handler: EventHandler<T>, options?: EventSubscriptionOptions): string[]; /** * Unsubscribe from events * @param subscriptionId Subscription ID returned from subscribe() */ unsubscribe(subscriptionId: string): boolean; /** * Publish an event to the event bus * @param event Event to publish * @param immediate Whether to process immediately or queue */ publish<T extends FlowEvent>(event: T, immediate?: boolean): Promise<void>; /** * Publish multiple events at once * @param events Events to publish * @param options Publishing options */ publishMany(events: FlowEvent[], options?: { immediate?: boolean; }): Promise<void>; /** * Publish events in batched mode for better performance * @param eventType Event type for batching * @param event Event to add to batch * @param batchId Batch identifier (default is event type) * @param flushTimeMs Time in milliseconds before automatically flushing batch */ publishBatched<T extends FlowEvent>(eventType: string, event: T, batchId?: string, flushTimeMs?: number): void; /** * Flush a batched event immediately * @param eventType Event type to flush * @param batchId Batch identifier (default is event type) */ flushBatch(eventType: string, batchId?: string): void; /** * Process events in the queue based on priority */ private processEventQueue; /** * Get performance metrics */ getMetrics(): EventBusMetrics; /** * Reset all metrics */ resetMetrics(): void; /** * Remove all event listeners */ removeAllListeners(): void; } export {}; //# sourceMappingURL=FlowEventBus.d.ts.map