const DEFAULT_WARNING_THRESHOLD = 0.75;
const DEFAULT_CRITICAL_THRESHOLD = 0.9;
/**
 * Per-session token budget tracker.
 * Advisory only — never blocks operations, but recommends compression.
 */
export class BudgetManager {
    limit;
    consumed = 0;
    consumptionByTool = new Map();
    warningThreshold;
    criticalThreshold;
    constructor(options) {
        this.limit = options?.limit ?? null;
        this.warningThreshold = options?.warningThreshold ?? DEFAULT_WARNING_THRESHOLD;
        this.criticalThreshold = options?.criticalThreshold ?? DEFAULT_CRITICAL_THRESHOLD;
    }
    /**
     * Record token consumption for a tool.
     */
    consume(tokens, toolName) {
        this.consumed += tokens;
        if (toolName) {
            const current = this.consumptionByTool.get(toolName) ?? 0;
            this.consumptionByTool.set(toolName, current + tokens);
        }
    }
    /**
     * Get remaining tokens in budget. Returns Infinity if no limit.
     */
    remaining() {
        if (this.limit === null) {
            return Infinity;
        }
        return Math.max(0, this.limit - this.consumed);
    }
    /**
     * Check if budget has been exceeded.
     */
    isOverBudget() {
        if (this.limit === null) {
            return false;
        }
        return this.consumed > this.limit;
    }
    /**
     * Check if at warning level (75% by default).
     */
    isWarning() {
        if (this.limit === null) {
            return false;
        }
        return this.consumed / this.limit >= this.warningThreshold;
    }
    /**
     * Check if at critical level (90% by default).
     */
    isCritical() {
        if (this.limit === null) {
            return false;
        }
        return this.consumed / this.limit >= this.criticalThreshold;
    }
    /**
     * Get full budget report.
     */
    report() {
        const byTool = {};
        for (const [tool, tokens] of this.consumptionByTool) {
            byTool[tool] = tokens;
        }
        return {
            limit: this.limit ?? 0,
            consumed: this.consumed,
            remaining: this.limit !== null ? Math.max(0, this.limit - this.consumed) : 0,
            warningThreshold: this.warningThreshold,
            criticalThreshold: this.criticalThreshold,
            isOverBudget: this.isOverBudget(),
            consumptionByTool: byTool,
        };
    }
    /**
     * Set a new budget limit.
     */
    setLimit(limit) {
        this.limit = limit;
    }
    /**
     * Reset consumption tracking.
     */
    reset() {
        this.consumed = 0;
        this.consumptionByTool.clear();
    }
    /**
     * Get total consumed tokens.
     */
    getConsumed() {
        return this.consumed;
    }
}
//# sourceMappingURL=budget.js.map