backpackflow
Version:
A config-driven LLM framework built on top of PocketFlow
144 lines • 4.44 kB
TypeScript
/**
* Core EventStream implementation for BackpackFlow
*
* Provides hierarchical event streaming with namespace support.
* Events follow the pattern: {namespace}:{category}:{action}
*/
import { EventEmitter } from 'events';
import { EventName, EventData, EventListener, EventStreamConfig, EventStreamStats, EventMetrics, SubscriptionOptions } from '../types/events';
/**
* Core EventStream class implementing hierarchical event streaming
*/
export declare class EventStream {
private emitter;
private namespace?;
private globalEvents;
private enableDebugLogs;
private enableMetrics;
private metrics;
private subscriptions;
constructor(config?: EventStreamConfig);
/**
* Emit an event with automatic namespace handling
*/
emit<K extends EventName>(eventType: K, data: EventData<K>): boolean;
/**
* Subscribe to events with optional filtering
*/
on<K extends EventName>(eventType: K | string, listener: EventListener<EventData<K>>, options?: SubscriptionOptions): this;
/**
* Subscribe to namespaced events
*/
onNamespaced<K extends EventName>(eventType: K, listener: EventListener<EventData<K>>, options?: SubscriptionOptions): this;
/**
* Subscribe to events matching a pattern
* Supports wildcards: 'tool:*', '*:error', 'namespace:*:*'
*/
onPattern(pattern: string, listener: (eventName: string, data: any) => void | Promise<void>, options?: SubscriptionOptions): this;
/**
* One-time event listener
*/
once<K extends EventName>(eventType: K | string, listener: EventListener<EventData<K>>): this;
/**
* Remove event listener
*/
off<K extends EventName>(eventType: K | string, listener: EventListener<EventData<K>>): this;
/**
* Get all listeners for an event
*/
listeners<K extends EventName>(eventType: K | string): Function[];
/**
* Remove all listeners for an event or all events
*/
removeAllListeners(eventType?: string): this;
/**
* Create a child stream with a sub-namespace
*/
createChildStream(childNamespace: string, config?: Partial<EventStreamConfig>): EventStream;
/**
* Get event stream statistics
*/
getStats(): EventStreamStats;
/**
* Get current metrics
*/
getMetrics(): EventMetrics;
/**
* Reset metrics
*/
resetMetrics(): void;
/**
* Get or set the namespace
*/
getNamespace(): string | undefined;
setNamespace(namespace: string): void;
/**
* Enable or disable debug logging
*/
setDebugLogging(enabled: boolean): void;
/**
* Check if the stream has any listeners for an event
*/
hasListeners(eventType: string): boolean;
/**
* Get the underlying EventEmitter (for advanced usage)
*/
getEmitter(): EventEmitter;
private setupMetricsCollection;
private updateMetrics;
private wrapListener;
private matchesFilter;
private createPatternRegex;
private formatEventName;
private getCurrentListenerCount;
}
/**
* Factory function to create a new EventStream
*/
export declare function createEventStream(config?: EventStreamConfig): EventStream;
/**
* Factory function to create a namespaced EventStream
*/
export declare function createNamespacedStream(namespace: string, config?: Partial<EventStreamConfig>): EventStream;
/**
* Utility class for managing multiple event streams
*/
export declare class EventStreamManager {
private streams;
private globalStream;
constructor();
/**
* Create or get a namespaced stream
*/
getStream(namespace: string, config?: Partial<EventStreamConfig>): EventStream;
/**
* Get the global stream
*/
getGlobalStream(): EventStream;
/**
* Get all managed streams
*/
getAllStreams(): Map<string, EventStream>;
/**
* Remove a stream
*/
removeStream(namespace: string): boolean;
/**
* Get aggregated statistics from all streams
*/
getAggregatedStats(): {
totalStreams: number;
totalListeners: number;
totalEvents: number;
streamStats: {
namespace: string;
stats: EventStreamStats;
}[];
};
/**
* Cleanup all streams
*/
cleanup(): void;
}
export declare const eventStreamManager: EventStreamManager;
//# sourceMappingURL=event-stream.d.ts.map