import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/agent/tools/error-tracker.ts
init_logger();
const log = createLogger("ToolErrorTracker");
const DEFAULT_CONFIG = {
	maxFailuresPerTool: 3,
	maxTotalFailures: 5,
	resetOnTurnEnd: true,
	failureWindowMs: 300 * 1e3
};
var ToolErrorTracker = class {
	failures = /* @__PURE__ */ new Map();
	totalFailures = 0;
	config;
	constructor(config = {}) {
		this.config = {
			...DEFAULT_CONFIG,
			...config
		};
	}
	/**
	* Record a tool failure
	*/
	recordFailure(toolName, error) {
		const now = Date.now();
		const existing = this.failures.get(toolName);
		if (existing) {
			existing.failureCount++;
			existing.lastError = error;
			existing.lastErrorTime = now;
		} else this.failures.set(toolName, {
			toolName,
			failureCount: 1,
			lastError: error,
			lastErrorTime: now
		});
		this.totalFailures++;
		log.warn({
			toolName,
			failures: this.getFailureCount(toolName),
			total: this.totalFailures
		}, "Tool failure recorded");
	}
	/**
	* Get failure count for a specific tool
	*/
	getFailureCount(toolName) {
		return this.failures.get(toolName)?.failureCount || 0;
	}
	/**
	* Get remaining attempts for a tool
	*/
	remainingAttempts(toolName) {
		const current = this.getFailureCount(toolName);
		return Math.max(0, this.config.maxFailuresPerTool - current);
	}
	/**
	* Check if a specific tool has reached its failure limit
	*/
	isToolLimitReached(toolName) {
		return this.getFailureCount(toolName) >= this.config.maxFailuresPerTool;
	}
	/**
	* Check if total failure limit is reached
	*/
	isTotalLimitReached() {
		return this.totalFailures >= this.config.maxTotalFailures;
	}
	/**
	* Check if any limit is reached (tool-specific or total)
	*/
	isAnyLimitReached() {
		return this.isTotalLimitReached() || Array.from(this.failures.keys()).some((name) => this.isToolLimitReached(name));
	}
	/**
	* Get the tool that has reached its limit (if any)
	*/
	getLimitReachedTool() {
		for (const [name, record] of this.failures.entries()) if (record.failureCount >= this.config.maxFailuresPerTool) return name;
		return null;
	}
	/**
	* Get failure hint message for agent
	*/
	getFailureHint(toolName) {
		const remaining = this.remainingAttempts(toolName);
		const totalRemaining = this.config.maxTotalFailures - this.totalFailures;
		if (remaining === 0) return `⚠️ Tool '${toolName}' has failed ${this.config.maxFailuresPerTool} times. Do not retry this tool.`;
		if (totalRemaining === 0) return `⚠️ Total failure limit (${this.config.maxTotalFailures}) reached. Stop executing tools.`;
		return `⚠️ '${toolName}' has failed ${this.getFailureCount(toolName)} times. Remaining attempts: ${remaining}/${this.config.maxFailuresPerTool}`;
	}
	/**
	* Get all failure records
	*/
	getFailures() {
		return new Map(this.failures);
	}
	/**
	* Get summary of failures
	*/
	getSummary() {
		const byTool = {};
		for (const [name, record] of this.failures.entries()) byTool[name] = record.failureCount;
		return {
			total: this.totalFailures,
			byTool
		};
	}
	/**
	* Reset all counters (called at end of turn)
	*/
	reset() {
		if (!this.config.resetOnTurnEnd) return;
		this.cleanupOldFailures();
		const summary = this.getSummary();
		if (summary.total > 0) log.info({ summary }, "Tool error tracker reset");
		this.failures.clear();
		this.totalFailures = 0;
	}
	/**
	* Reset failures for a specific tool
	*/
	resetTool(toolName) {
		if (this.failures.has(toolName)) {
			const record = this.failures.get(toolName);
			this.totalFailures = Math.max(0, this.totalFailures - record.failureCount);
			this.failures.delete(toolName);
			log.debug({ toolName }, "Tool failure counter reset");
		}
	}
	/**
	* Get tracker configuration
	*/
	getConfig() {
		return { ...this.config };
	}
	/**
	* Clean up old failures (outside the failure window)
	*/
	cleanupOldFailures() {
		const now = Date.now();
		let cleaned = 0;
		for (const [name, record] of this.failures.entries()) if (now - record.lastErrorTime > this.config.failureWindowMs) {
			this.totalFailures -= record.failureCount;
			this.failures.delete(name);
			cleaned++;
		}
		if (cleaned > 0) log.debug({ cleaned }, "Cleaned up old tool failures");
	}
};
//#endregion
export { ToolErrorTracker };

//# sourceMappingURL=error-tracker.js.map