import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/agent/tools/error-pattern-matcher.ts
init_logger();
const log = createLogger("ErrorPatternMatcher");
const DEFAULT_CONFIG = {
	enabled: true,
	patterns: [
		{
			name: "file-not-found",
			description: "File or directory does not exist",
			regex: /no such file or directory|file not found|cannot find/i,
			suggestion: "Check if the file path is correct. Use list_dir to verify directory contents before reading.",
			autoRetry: false,
			maxRetries: 0,
			severity: "medium"
		},
		{
			name: "permission-denied",
			description: "Insufficient permissions to access file",
			regex: /permission denied|access denied|unauthorized/i,
			suggestion: "Check file permissions. Consider using sudo if safe, or request user intervention.",
			autoRetry: false,
			maxRetries: 0,
			severity: "high"
		},
		{
			name: "file-too-large",
			description: "File exceeds size limits",
			regex: /file too large|exceeds.*size|too big to read/i,
			suggestion: "Use truncate parameter or read specific sections. Consider using grep/search instead.",
			autoRetry: false,
			maxRetries: 0,
			severity: "low"
		},
		{
			name: "command-not-found",
			description: "Shell command does not exist",
			regex: /command not found|not recognized|no such command/i,
			suggestion: "Verify the command is installed. Check PATH or install the required package.",
			autoRetry: false,
			maxRetries: 0,
			severity: "medium"
		},
		{
			name: "timeout",
			description: "Command execution timed out",
			regex: /timeout|timed out|execution time exceeded/i,
			suggestion: "Command took too long. Consider optimizing the command, breaking into smaller tasks, or increasing timeout.",
			autoRetry: true,
			maxRetries: 1,
			severity: "medium"
		},
		{
			name: "exit-code-nonzero",
			description: "Command exited with error code",
			regex: /exit code \d+|returned non-zero|failed with error/i,
			suggestion: "Check command syntax and arguments. Review stderr output for details.",
			autoRetry: false,
			maxRetries: 0,
			severity: "medium"
		},
		{
			name: "network-error",
			description: "Network connectivity issue",
			regex: /network error|connection refused|cannot connect|dns/i,
			suggestion: "Check network connectivity. Verify the service is running and accessible.",
			autoRetry: true,
			maxRetries: 2,
			severity: "high"
		},
		{
			name: "http-error",
			description: "HTTP request failed",
			regex: /http \d{3}|request failed|bad response/i,
			suggestion: "Check the URL and request parameters. Review API documentation.",
			autoRetry: true,
			maxRetries: 1,
			severity: "medium"
		},
		{
			name: "git-conflict",
			description: "Git merge conflict",
			regex: /merge conflict|conflict marker|cannot merge/i,
			suggestion: "Resolve merge conflicts manually. Check conflicted files and edit to resolve.",
			autoRetry: false,
			maxRetries: 0,
			severity: "high"
		},
		{
			name: "git-not-repository",
			description: "Not a git repository",
			regex: /not a git repository|fatal: not a git repo/i,
			suggestion: "Initialize git repository first with `git init` or navigate to correct directory.",
			autoRetry: false,
			maxRetries: 0,
			severity: "low"
		},
		{
			name: "out-of-memory",
			description: "Insufficient memory",
			regex: /out of memory|memory allocation failed|cannot allocate/i,
			suggestion: "Reduce data size or process in smaller chunks. Consider using streaming.",
			autoRetry: false,
			maxRetries: 0,
			severity: "critical"
		},
		{
			name: "disk-full",
			description: "No disk space available",
			regex: /no space left|disk full|insufficient space/i,
			suggestion: "Free up disk space. Delete unnecessary files or expand storage.",
			autoRetry: false,
			maxRetries: 0,
			severity: "critical"
		},
		{
			name: "syntax-error",
			description: "Code syntax error",
			regex: /syntax error|parse error|invalid syntax|unexpected token/i,
			suggestion: "Review code syntax. Check for missing brackets, quotes, or semicolons.",
			autoRetry: false,
			maxRetries: 0,
			severity: "medium"
		},
		{
			name: "type-error",
			description: "Type mismatch error",
			regex: /type error|type mismatch|cannot convert/i,
			suggestion: "Check variable types. Ensure correct type is used for the operation.",
			autoRetry: false,
			maxRetries: 0,
			severity: "medium"
		}
	],
	defaultMaxRetries: 1,
	logMatches: true
};
var ErrorPatternMatcher = class {
	config;
	matchHistory = /* @__PURE__ */ new Map();
	constructor(config = {}) {
		this.config = {
			...DEFAULT_CONFIG,
			...config
		};
	}
	/**
	* Match error against known patterns
	*/
	matchError(error) {
		if (!this.config.enabled) return {
			matched: false,
			originalError: error,
			suggestion: error,
			shouldRetry: false,
			maxRetries: 0
		};
		const errorLower = error.toLowerCase();
		for (const pattern of this.config.patterns) if (pattern.regex.test(errorLower)) {
			const count = this.matchHistory.get(pattern.name) || 0;
			this.matchHistory.set(pattern.name, count + 1);
			if (this.config.logMatches) log.warn({
				pattern: pattern.name,
				severity: pattern.severity
			}, `Matched error pattern: ${pattern.description}`);
			return {
				matched: true,
				pattern,
				originalError: error,
				suggestion: `[Error: ${pattern.name}]\n${pattern.suggestion}`,
				shouldRetry: pattern.autoRetry,
				maxRetries: pattern.maxRetries
			};
		}
		return {
			matched: false,
			originalError: error,
			suggestion: error,
			shouldRetry: false,
			maxRetries: this.config.defaultMaxRetries
		};
	}
	/**
	* Get recovery suggestion for an error
	*/
	getRecoverySuggestion(error) {
		return this.matchError(error).suggestion;
	}
	/**
	* Check if error should be retried
	*/
	shouldRetry(error) {
		return this.matchError(error).shouldRetry;
	}
	/**
	* Get max retries for an error
	*/
	getMaxRetries(error) {
		return this.matchError(error).maxRetries;
	}
	/**
	* Get error statistics
	*/
	getStats() {
		const totalMatches = Array.from(this.matchHistory.values()).reduce((sum, c) => sum + c, 0);
		const topErrors = Array.from(this.matchHistory.entries()).map(([name, count]) => ({
			name,
			count
		})).sort((a, b) => b.count - a.count).slice(0, 10);
		return {
			totalMatches,
			byPattern: new Map(this.matchHistory),
			topErrors
		};
	}
	/**
	* Get error report as text
	*/
	getReportText() {
		const stats = this.getStats();
		const lines = [
			"Error Pattern Matching Report",
			"=============================",
			`Total Matches: ${stats.totalMatches}`,
			"",
			"Top Error Patterns:"
		];
		for (const { name, count } of stats.topErrors) {
			const severity = this.config.patterns.find((p) => p.name === name)?.severity || "unknown";
			lines.push(`  - ${name}: ${count} occurrences (severity: ${severity})`);
		}
		return lines.join("\n");
	}
	/**
	* Reset match history
	*/
	reset() {
		this.matchHistory.clear();
		log.info("Error pattern matcher reset");
	}
	/**
	* Add custom error pattern
	*/
	addPattern(pattern) {
		this.config.patterns.push(pattern);
		log.info({ name: pattern.name }, "Added custom error pattern");
	}
	/**
	* Remove error pattern by name
	*/
	removePattern(name) {
		const index = this.config.patterns.findIndex((p) => p.name === name);
		if (index !== -1) {
			this.config.patterns.splice(index, 1);
			log.info({ name }, "Removed error pattern");
			return true;
		}
		return false;
	}
	/**
	* Get all patterns
	*/
	getPatterns() {
		return [...this.config.patterns];
	}
	/**
	* Get config
	*/
	getConfig() {
		return { ...this.config };
	}
	/**
	* Update config
	*/
	setConfig(config) {
		this.config = {
			...this.config,
			...config
		};
	}
};
//#endregion
export { ErrorPatternMatcher };

//# sourceMappingURL=error-pattern-matcher.js.map