//#region src/errors/index.ts
const XOPC_ERROR_MARKER = Symbol.for("xopc.error");
const USER_ERROR_MARKER = Symbol.for("xopc.error.user");
const CONFIG_ERROR_MARKER = Symbol.for("xopc.error.config");
const SYSTEM_ERROR_MARKER = Symbol.for("xopc.error.system");
const NETWORK_ERROR_MARKER = Symbol.for("xopc.error.network");
const PROVIDER_ERROR_MARKER = Symbol.for("xopc.error.provider");
/**
* Base error class for all xopc errors
*/
var XopcError = class XopcError extends Error {
	[XOPC_ERROR_MARKER] = true;
	code;
	category;
	retryable;
	suggestion;
	cause;
	constructor(details, cause) {
		super(details.message);
		this.name = "XopcError";
		this.code = details.code;
		this.category = details.category;
		this.retryable = details.retryable ?? false;
		this.suggestion = details.suggestion;
		this.cause = cause;
		Object.setPrototypeOf(this, XopcError.prototype);
	}
	/**
	* Format error for user display
	*/
	toUserMessage() {
		let msg = `❌ ${this.message}`;
		if (this.suggestion) msg += `\n💡 ${this.suggestion}`;
		return msg;
	}
	/**
	* Format error for logging
	*/
	toLogObject() {
		return {
			code: this.code,
			category: this.category,
			message: this.message,
			retryable: this.retryable,
			cause: this.cause?.message,
			stack: this.stack
		};
	}
};
/**
* User input/operation errors - caused by user actions
*/
var UserError = class UserError extends XopcError {
	[USER_ERROR_MARKER] = true;
	constructor(code, message, suggestion, cause) {
		super({
			code: `USER_${code}`,
			category: "user",
			message,
			suggestion,
			retryable: false
		}, cause);
		this.name = "UserError";
	}
	static invalidCommand(command) {
		return new UserError("INVALID_COMMAND", `Unknown command: "${command}"`, "Use /help to see available commands");
	}
	static invalidInput(message, suggestion) {
		return new UserError("INVALID_INPUT", message, suggestion);
	}
	static unauthorized(message = "You are not authorized to perform this action") {
		return new UserError("UNAUTHORIZED", message, "Contact your administrator");
	}
};
/**
* Configuration errors - caused by invalid/missing config
*/
var ConfigError = class ConfigError extends XopcError {
	[CONFIG_ERROR_MARKER] = true;
	constructor(code, message, suggestion, cause) {
		super({
			code: `CONFIG_${code}`,
			category: "config",
			message,
			suggestion,
			retryable: false
		}, cause);
		this.name = "ConfigError";
	}
	static missingApiKey(provider) {
		return new ConfigError("MISSING_API_KEY", `API key not configured for provider: ${provider}`, `Set ${provider.toUpperCase().replace(/-/g, "_")}_API_KEY environment variable or run 'xopc configure'`);
	}
	static invalidConfig(path, reason) {
		return new ConfigError("INVALID_CONFIG", `Invalid configuration at ${path}: ${reason}`, "Check your config.json or run xopc configure");
	}
	static missingConfig(key) {
		return new ConfigError("MISSING_CONFIG", `Missing required configuration: ${key}`, "Run xopc configure to set up your configuration");
	}
};
/**
* System errors - internal/system-level failures
*/
var SystemError = class SystemError extends XopcError {
	[SYSTEM_ERROR_MARKER] = true;
	constructor(code, message, retryable = false, suggestion, cause) {
		super({
			code: `SYSTEM_${code}`,
			category: "system",
			message,
			suggestion,
			retryable
		}, cause);
		this.name = "SystemError";
	}
	static internal(message, cause) {
		return new SystemError("INTERNAL", message, false, "Please report this issue", cause);
	}
	static filesystem(path, operation, cause) {
		return new SystemError("FILESYSTEM", `Failed to ${operation} at ${path}`, true, "Check file permissions and disk space", cause);
	}
	static outOfMemory() {
		return new SystemError("OUT_OF_MEMORY", "System ran out of memory", true, "Try reducing context size or restarting");
	}
};
/**
* Network errors - connectivity/timeout issues
*/
var NetworkError = class NetworkError extends XopcError {
	[NETWORK_ERROR_MARKER] = true;
	constructor(code, message, retryable = true, suggestion, cause) {
		super({
			code: `NET_${code}`,
			category: "network",
			message,
			suggestion,
			retryable
		}, cause);
		this.name = "NetworkError";
	}
	static timeout(url, timeoutMs, cause) {
		return new NetworkError("TIMEOUT", `Request to ${url} timed out after ${timeoutMs}ms`, true, "Check your internet connection or try again later", cause);
	}
	static connectionFailed(url, cause) {
		return new NetworkError("CONNECTION_FAILED", `Failed to connect to ${url}`, true, "Check your internet connection", cause);
	}
};
/**
* LLM Provider errors - model/API failures
*/
var ProviderError = class ProviderError extends XopcError {
	[PROVIDER_ERROR_MARKER] = true;
	provider;
	model;
	statusCode;
	constructor(provider, code, message, retryable = false, suggestion, cause, statusCode, model) {
		super({
			code: `PROVIDER_${provider.toUpperCase().replace(/-/g, "_")}_${code}`,
			category: "provider",
			message: `[${provider}] ${message}`,
			suggestion,
			retryable
		}, cause);
		this.name = "ProviderError";
		this.provider = provider;
		this.model = model;
		this.statusCode = statusCode;
	}
	static rateLimit(provider, retryAfter, model) {
		return new ProviderError(provider, "RATE_LIMIT", "Rate limit exceeded", true, retryAfter ? `Wait ${retryAfter} seconds before retrying` : "Retry after a short delay", void 0, 429, model);
	}
	static authFailed(provider, cause) {
		return new ProviderError(provider, "AUTH_FAILED", "Authentication failed - check your API key", false, `Verify your ${provider} API key is correct`, cause, 401);
	}
	static modelNotFound(provider, model) {
		return new ProviderError(provider, "MODEL_NOT_FOUND", `Model "${model}" not found`, false, "Check available models with /models command");
	}
	static overloaded(provider, model) {
		return new ProviderError(provider, "OVERLOADED", "Provider is currently overloaded", true, "Retry after a short delay or try a different model", void 0, 503, model);
	}
	static insufficientFunds(provider) {
		return new ProviderError(provider, "INSUFFICIENT_FUNDS", "Insufficient funds or quota exceeded", false, "Check your account balance and billing settings");
	}
};
/**
* Error handling utilities
*/
function isXopcError(error) {
	return error instanceof XopcError || error != null && typeof error === "object" && XOPC_ERROR_MARKER in error;
}
function isUserError(error) {
	return error instanceof UserError || error != null && typeof error === "object" && USER_ERROR_MARKER in error;
}
function isConfigError(error) {
	return error instanceof ConfigError || error != null && typeof error === "object" && CONFIG_ERROR_MARKER in error;
}
function isProviderError(error) {
	return error instanceof ProviderError || error != null && typeof error === "object" && PROVIDER_ERROR_MARKER in error;
}
function isRetryable(error) {
	if (isXopcError(error)) return error.retryable;
	if (error instanceof Error) {
		const msg = error.message.toLowerCase();
		return msg.includes("timeout") || msg.includes("econnreset") || msg.includes("enotfound") || msg.includes("econnrefused");
	}
	return false;
}
/**
* Wrap unknown error into XopcError
*/
function wrapError(error, context) {
	if (isXopcError(error)) return error;
	const message = error instanceof Error ? error.message : String(error);
	const prefix = context ? `${context}: ` : "";
	const lowerMsg = message.toLowerCase();
	if (lowerMsg.includes("api key") || lowerMsg.includes("unauthorized") || lowerMsg.includes("401")) return new ProviderError("unknown", "AUTH_FAILED", prefix + message, false, "Check your API key", error instanceof Error ? error : void 0);
	if (lowerMsg.includes("timeout") || lowerMsg.includes("etimedout")) return NetworkError.timeout("unknown", 3e4, error instanceof Error ? error : void 0);
	if (lowerMsg.includes("rate limit") || lowerMsg.includes("429")) return ProviderError.rateLimit("unknown");
	return SystemError.internal(prefix + message, error instanceof Error ? error : void 0);
}
/**
* Format error for user display
*/
function formatErrorForUser(error) {
	if (isXopcError(error)) return error.toUserMessage();
	if (error instanceof Error) return `❌ ${error.message}`;
	return `❌ An unexpected error occurred: ${String(error)}`;
}
/**
* Format error for logging
*/
function formatErrorForLog(error) {
	if (isXopcError(error)) return error.toLogObject();
	if (error instanceof Error) return {
		name: error.name,
		message: error.message,
		stack: error.stack
	};
	return { error: String(error) };
}
//#endregion
export { ConfigError, NetworkError, ProviderError, SystemError, UserError, XopcError, formatErrorForLog, formatErrorForUser, isConfigError, isProviderError, isRetryable, isUserError, isXopcError, wrapError };

//# sourceMappingURL=index.js.map