"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBuffer = void 0;
class EventBuffer {
    constructor() {
        this.runStates = new Map();
    }
    processEvent(event) {
        const runId = event.runId;
        let runState = this.runStates.get(runId);
        if (!runState) {
            runState = {
                runId,
                createdAt: Date.now(),
                tokenCount: 0,
                status: 'created',
                events: []
            };
            this.runStates.set(runId, runState);
        }
        runState.events.push(event);
        if (runState.events.length > EventBuffer.MAX_EVENTS_PER_RUN) {
            runState.events.shift();
        }
        switch (event.type) {
            case 'created':
                runState.createdAt = event.at;
                break;
            case 'started':
                runState.startedAt = event.at;
                runState.status = 'running';
                break;
            case 'token':
                runState.lastTokenAt = event.at;
                runState.tokenCount += event.tokens;
                if (event.cost !== undefined) {
                    runState.cost = (runState.cost || 0) + event.cost;
                }
                break;
            case 'finish':
                runState.status = 'finished';
                break;
            case 'error':
                runState.status = 'finished';
                break;
        }
    }
    getRunState(runId) {
        return this.runStates.get(runId);
    }
    getActiveRuns() {
        return Array.from(this.runStates.values())
            .filter(run => run.status === 'created' || run.status === 'running');
    }
    markKilled(runId, reason) {
        const runState = this.runStates.get(runId);
        if (runState) {
            runState.status = 'killed';
            runState.killReason = reason;
        }
    }
    cleanup() {
        const now = Date.now();
        const maxAge = 60 * 60 * 1000;
        for (const [runId, runState] of this.runStates.entries()) {
            if (runState.status === 'finished' || runState.status === 'killed') {
                const age = now - Math.max(runState.createdAt, runState.startedAt || 0);
                if (age > maxAge) {
                    this.runStates.delete(runId);
                }
            }
        }
    }
    getRecentEvents(runId, count = 10) {
        const runState = this.runStates.get(runId);
        if (!runState)
            return [];
        return runState.events.slice(-count);
    }
    getTokenEventsInWindow(runId, windowMs) {
        const runState = this.runStates.get(runId);
        if (!runState)
            return [];
        const now = Date.now();
        const cutoff = now - windowMs;
        return runState.events
            .filter(event => event.type === 'token' && event.at >= cutoff)
            .slice(-10);
    }
}
exports.EventBuffer = EventBuffer;
EventBuffer.MAX_EVENTS_PER_RUN = 100;
//# sourceMappingURL=event-buffer.js.map