/**
 * Session Stats - Real-time context consumption tracking.
 *
 * Records per-tool-call byte usage (raw vs compressed) so users
 * can see where their context window budget goes.
 */
const MAX_CALL_RECORDS = 500; // Rolling window to prevent unbounded memory growth
export class SessionStats {
    startTime = Date.now();
    calls = [];
    _modelTokenBudget = 0;
    _modelTokensConsumed = 0;
    _compressionEvents = 0;
    setModelTokenBudget(budget) {
        this._modelTokenBudget = budget;
    }
    addModelTokensConsumed(tokens) {
        this._modelTokensConsumed += tokens;
    }
    recordCompressionEvent() {
        this._compressionEvents++;
    }
    record(tool, rawBytes, contextBytes) {
        this.calls.push({
            tool,
            timestamp: Date.now(),
            rawBytes,
            contextBytes,
        });
        // Cap the array to prevent unbounded memory growth over long sessions
        if (this.calls.length > MAX_CALL_RECORDS) {
            this.calls = this.calls.slice(-MAX_CALL_RECORDS);
        }
    }
    getSummary() {
        const totalRawBytes = this.calls.reduce((s, c) => s + c.rawBytes, 0);
        const totalContextBytes = this.calls.reduce((s, c) => s + c.contextBytes, 0);
        const savingsRatio = totalContextBytes > 0 ? totalRawBytes / totalContextBytes : 1;
        const savingsPercent = totalRawBytes > 0 ? `${Math.round((1 - totalContextBytes / totalRawBytes) * 100)}%` : '0%';
        // Aggregate by tool
        const toolMap = new Map();
        for (const call of this.calls) {
            const existing = toolMap.get(call.tool) || { calls: 0, contextBytes: 0, rawBytes: 0 };
            existing.calls++;
            existing.contextBytes += call.contextBytes;
            existing.rawBytes += call.rawBytes;
            toolMap.set(call.tool, existing);
        }
        const byTool = Array.from(toolMap.entries())
            .map(([tool, data]) => ({ tool, ...data }))
            .sort((a, b) => b.contextBytes - a.contextBytes);
        return {
            uptimeMs: Date.now() - this.startTime,
            totalCalls: this.calls.length,
            totalContextBytes,
            totalRawBytes,
            savingsRatio: Math.round(savingsRatio * 10) / 10,
            savingsPercent,
            modelTokenBudget: this._modelTokenBudget,
            modelTokensConsumed: this._modelTokensConsumed,
            compressionEvents: this._compressionEvents,
            byTool,
        };
    }
    reset() {
        this.startTime = Date.now();
        this.calls = [];
        this._modelTokenBudget = 0;
        this._modelTokensConsumed = 0;
        this._compressionEvents = 0;
    }
    getCalls() {
        return this.calls;
    }
}
export const globalSessionStats = new SessionStats();
//# sourceMappingURL=session-stats.js.map