// server/modules/orchestration/a2a/bus.ts
// In-process pub/sub on top of Node's EventEmitter.
// Subscribers receive every event for a given taskId; an
// "all" subscriber receives every event regardless of task.
// Note: the literal taskId "__all__" is reserved for the broadcast
// channel; callers must not use it as a real taskId.
import { EventEmitter } from 'node:events';
const ALL = '__all__';
class A2ABus {
    emitter = new EventEmitter();
    constructor() {
        this.emitter.setMaxListeners(0); // SSE clients can be numerous
    }
    /** Synchronous: listeners run before publish() returns. */
    publish(event) {
        this.emitter.emit(event.taskId, event);
        this.emitter.emit(ALL, event);
    }
    /** Subscribe to events for a specific taskId. Returns an unsubscribe
     *  function — caller MUST invoke it to release the listener; the bus
     *  retains a strong reference until then. */
    subscribe(taskId, listener) {
        this.emitter.on(taskId, listener);
        return () => this.emitter.off(taskId, listener);
    }
    /** Subscribe to ALL events. Returns an unsubscribe function with the
     *  same release semantics as subscribe(). */
    subscribeAll(listener) {
        this.emitter.on(ALL, listener);
        return () => this.emitter.off(ALL, listener);
    }
}
export const a2aBus = new A2ABus();
//# sourceMappingURL=bus.js.map