import { classifyFailoverReason, isAuthErrorMessage } from "./fallback/reason.js";
//#region src/agent/client-error-format.ts
const API_KEY_MISSING_RE = /^No API key found for (\S+)/i;
function reasonToKind(reason) {
	switch (reason) {
		case "auth": return "provider_auth_invalid";
		case "rate_limit": return "rate_limit";
		case "timeout": return "timeout";
		case "billing": return "billing";
		default: return "unknown";
	}
}
function reasonToCode(reason) {
	switch (reason) {
		case "auth": return "provider_auth_invalid";
		case "rate_limit": return "rate_limit";
		case "timeout": return "timeout";
		case "billing": return "billing";
		case "format": return "format";
		default: return "unknown";
	}
}
function providerDeepLink(kind) {
	if (kind === "provider_setup_required" || kind === "provider_auth_invalid") return "/settings/credentials";
}
function tryParseStructuredPayload(text) {
	if (!text.startsWith("{")) return null;
	try {
		const parsed = JSON.parse(text);
		if (typeof parsed.kind !== "string" || typeof parsed.message !== "string") return null;
		return {
			kind: parsed.kind,
			code: typeof parsed.code === "string" ? parsed.code : parsed.kind,
			provider: typeof parsed.provider === "string" ? parsed.provider : void 0,
			modelRef: typeof parsed.modelRef === "string" ? parsed.modelRef : void 0,
			deepLink: typeof parsed.deepLink === "string" ? parsed.deepLink : void 0,
			message: parsed.message
		};
	} catch {
		return null;
	}
}
/**
* Map a raw agent/LLM error into a structured JSON payload the web UI can render
* as an actionable, i18n-friendly card. Falls back to plain `Error: <raw>` when
* the input is already a legacy gateway string.
*/
function formatAgentRunErrorForClient(rawError, context) {
	const trimmed = rawError.trim();
	if (!trimmed) return JSON.stringify({
		kind: "unknown",
		code: "unknown",
		message: "Assistant turn failed"
	});
	const existing = tryParseStructuredPayload(trimmed);
	if (existing) return JSON.stringify(existing);
	const missingMatch = API_KEY_MISSING_RE.exec(trimmed);
	if (missingMatch) {
		const provider = missingMatch[1].replace(/\.$/, "");
		return JSON.stringify({
			kind: "provider_setup_required",
			code: "provider_setup_required",
			provider,
			deepLink: "/settings/credentials",
			message: trimmed
		});
	}
	const reason = classifyFailoverReason(trimmed);
	const kind = reasonToKind(reason);
	const payload = {
		kind,
		code: reasonToCode(reason),
		message: trimmed,
		...context?.provider ? { provider: context.provider } : {},
		...context?.modelRef ? { modelRef: context.modelRef } : {}
	};
	const deepLink = providerDeepLink(kind);
	if (deepLink) payload.deepLink = deepLink;
	if (kind === "unknown" && isAuthErrorMessage(trimmed)) {
		payload.kind = "provider_auth_invalid";
		payload.code = "provider_auth_invalid";
		payload.deepLink = "/settings/credentials";
	}
	return JSON.stringify(payload);
}
/** Human-readable text for TUI/CLI when SSE carries structured JSON. */
function formatAgentRunErrorForDisplay(content) {
	const trimmed = content.trim();
	if (trimmed.startsWith("Error: ")) return trimmed.slice(7);
	const parsed = tryParseStructuredPayload(trimmed);
	if (parsed?.message) return parsed.message;
	return trimmed;
}
//#endregion
export { formatAgentRunErrorForClient, formatAgentRunErrorForDisplay };

//# sourceMappingURL=client-error-format.js.map