//#region src/agent/fallback/reason.ts
function isRateLimitErrorMessage(raw) {
	return raw.toLowerCase().includes("rate limit") || /429/i.test(raw);
}
function isTimeoutErrorMessage(raw) {
	return raw.toLowerCase().includes("timeout") || raw.toLowerCase().includes("timed out");
}
function isBillingErrorMessage(raw) {
	const lower = raw.toLowerCase();
	return lower.includes("402") || lower.includes("billing") || lower.includes("insufficient credit");
}
function isAuthErrorMessage(raw) {
	const lower = raw.toLowerCase();
	return lower.includes("401") || lower.includes("403") || lower.includes("unauthorized") || lower.includes("invalid api key");
}
function isFormatErrorMessage(raw) {
	return raw.toLowerCase().includes("invalid request format") || raw.toLowerCase().includes("tool_use.id");
}
function getStatusCode(err) {
	if (!err || typeof err !== "object") return void 0;
	const status = err.status ?? err.statusCode;
	if (typeof status === "number") return status;
	if (typeof status === "string" && /^\d+$/.test(status)) return Number(status);
}
function getErrorCode(err) {
	if (!err || typeof err !== "object") return void 0;
	const code = err.code;
	if (typeof code === "string") return code.trim() || void 0;
}
function getErrorMessage(err) {
	if (err instanceof Error) return err.message;
	if (typeof err === "string") return err;
	if (err && typeof err === "object") {
		const msg = err.message;
		if (typeof msg === "string") return msg;
	}
	return String(err);
}
function classifyFailoverReason(err) {
	const status = getStatusCode(err);
	if (status === 402) return "billing";
	if (status === 429) return "rate_limit";
	if (status === 401 || status === 403) return "auth";
	if (status === 408) return "timeout";
	if (status === 400) return "format";
	const code = (getErrorCode(err) ?? "").toUpperCase();
	if (["ETIMEDOUT", "ECONNRESET"].includes(code)) return "timeout";
	const message = getErrorMessage(err);
	if (!message) return "unknown";
	if (isRateLimitErrorMessage(message)) return "rate_limit";
	if (isBillingErrorMessage(message)) return "billing";
	if (isTimeoutErrorMessage(message)) return "timeout";
	if (isAuthErrorMessage(message)) return "auth";
	if (isFormatErrorMessage(message)) return "format";
	const lower = message.toLowerCase();
	if (lower.includes("fetch failed") || lower.includes("econnreset") || lower.includes("enotfound") || lower.includes("socket hang up") || lower.includes("getaddrinfo")) return "timeout";
	return "unknown";
}
//#endregion
export { classifyFailoverReason, isAuthErrorMessage, isBillingErrorMessage, isFormatErrorMessage, isRateLimitErrorMessage, isTimeoutErrorMessage };

//# sourceMappingURL=reason.js.map