import { createLogger } from '@4runr/shared';
const logger = createLogger('Gateway:Sentinel:EventStream');
export class GatewayEventStream {
    subscribers = new Set();
    subscribe(callback) {
        this.subscribers.add(callback);
        return {
            unsubscribe: () => {
                this.subscribers.delete(callback);
            }
        };
    }
    emit(event) {
        for (const subscriber of this.subscribers) {
            try {
                subscriber(event);
            }
            catch (error) {
                logger.error('Error in Sentinel event subscriber', {
                    error: error instanceof Error ? error.message : String(error)
                });
            }
        }
    }
    emitCreated(runId) {
        this.emit({
            type: 'created',
            runId,
            at: Date.now()
        });
    }
    emitStarted(runId) {
        this.emit({
            type: 'started',
            runId,
            at: Date.now()
        });
    }
    emitToken(runId, tokens, contentBytes, cost) {
        const event = {
            type: 'token',
            runId,
            at: Date.now(),
            tokens,
            ...(contentBytes !== undefined && { contentBytes }),
            ...(cost !== undefined && { cost })
        };
        this.emit(event);
    }
    emitFinish(runId, status) {
        this.emit({
            type: 'finish',
            runId,
            at: Date.now(),
            status
        });
    }
    emitError(runId, message, code) {
        const event = {
            type: 'error',
            runId,
            at: Date.now(),
            message,
            ...(code !== undefined && { code })
        };
        this.emit(event);
    }
}
//# sourceMappingURL=sentinel-event-stream.js.map