import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/agent/models/request-limiter.ts
init_logger();
const log = createLogger("RequestLimiter");
const DEFAULT_CONFIG = {
	maxRequestsPerTurn: 50,
	warnThreshold: .8,
	softLimit: false
};
var RequestLimiter = class {
	requestCount = 0;
	config;
	turnStartTime = Date.now();
	/** Avoid spamming logs: `recordRequest` runs every LLM round while above the warn threshold. */
	approachWarningLogged = false;
	limitReachedLogged = false;
	constructor(config = {}) {
		this.config = {
			...DEFAULT_CONFIG,
			...config
		};
	}
	/**
	* Record a request and check if it's allowed
	*/
	recordRequest() {
		this.requestCount++;
		const remaining = Math.max(0, this.config.maxRequestsPerTurn - this.requestCount);
		const isWarning = this.requestCount / this.config.maxRequestsPerTurn >= this.config.warnThreshold;
		const shouldStop = !this.config.softLimit && this.requestCount >= this.config.maxRequestsPerTurn;
		const result = {
			allowed: !shouldStop,
			count: this.requestCount,
			remaining,
			limit: this.config.maxRequestsPerTurn,
			isWarning,
			shouldStop
		};
		if (isWarning && !this.approachWarningLogged) {
			this.approachWarningLogged = true;
			log.warn({
				count: this.requestCount,
				limit: this.config.maxRequestsPerTurn,
				remaining
			}, "Approaching request limit");
		}
		if (shouldStop && !this.limitReachedLogged) {
			this.limitReachedLogged = true;
			log.error({
				count: this.requestCount,
				limit: this.config.maxRequestsPerTurn
			}, "Request limit reached");
		}
		return result;
	}
	/**
	* Check current request status without recording
	*/
	getStatus() {
		const remaining = Math.max(0, this.config.maxRequestsPerTurn - this.requestCount);
		const isWarning = this.requestCount / this.config.maxRequestsPerTurn >= this.config.warnThreshold;
		const shouldStop = !this.config.softLimit && this.requestCount >= this.config.maxRequestsPerTurn;
		return {
			allowed: !shouldStop,
			count: this.requestCount,
			remaining,
			limit: this.config.maxRequestsPerTurn,
			isWarning,
			shouldStop
		};
	}
	/**
	* Get current request count
	*/
	getCount() {
		return this.requestCount;
	}
	/**
	* Get remaining requests
	*/
	getRemaining() {
		return Math.max(0, this.config.maxRequestsPerTurn - this.requestCount);
	}
	/**
	* Check if limit is reached
	*/
	isLimitReached() {
		return this.requestCount >= this.config.maxRequestsPerTurn;
	}
	/**
	* Check if approaching limit (warning threshold)
	*/
	isApproachingLimit() {
		return this.requestCount >= this.config.maxRequestsPerTurn * this.config.warnThreshold;
	}
	/**
	* Get usage percentage
	*/
	getUsagePercent() {
		if (this.config.maxRequestsPerTurn <= 0) return 1;
		return this.requestCount / this.config.maxRequestsPerTurn;
	}
	/**
	* Get warning message for agent
	*/
	getWarningMessage() {
		const status = this.getStatus();
		if (status.shouldStop) return `⚠️ Request limit reached (${this.requestCount}/${this.config.maxRequestsPerTurn}). Stopping execution.`;
		if (status.isWarning) return `⚠️ Approaching request limit (${this.requestCount}/${this.config.maxRequestsPerTurn}). Remaining: ${status.remaining}`;
		return null;
	}
	/**
	* Reset counter (called at end of turn)
	*/
	reset() {
		if (this.requestCount > 0) log.info({
			count: this.requestCount,
			duration: Date.now() - this.turnStartTime
		}, "Request limiter reset");
		this.requestCount = 0;
		this.turnStartTime = Date.now();
		this.approachWarningLogged = false;
		this.limitReachedLogged = false;
	}
	/**
	* Manually adjust count (for testing or special cases)
	*/
	setCount(count) {
		this.requestCount = Math.max(0, count);
	}
	/**
	* Get turn duration in milliseconds
	*/
	getTurnDuration() {
		return Date.now() - this.turnStartTime;
	}
	/**
	* Get stats for reporting
	*/
	getStats() {
		return {
			count: this.requestCount,
			limit: this.config.maxRequestsPerTurn,
			remaining: this.getRemaining(),
			usagePercent: this.getUsagePercent(),
			turnDurationMs: this.getTurnDuration(),
			isWarning: this.isApproachingLimit(),
			isLimitReached: this.isLimitReached()
		};
	}
};
//#endregion
export { RequestLimiter };

//# sourceMappingURL=request-limiter.js.map