UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

198 lines (197 loc) • 13.1 kB
import { c as normalizeOptionalLowercaseString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { n as safeParseJsonRecord } from "./json-coercion-CaJLaav-.mjs"; import { C as isGenericProviderInternalError, T as parseApiErrorInfo, _ as extractLeadingHttpStatus } from "./redact-Mj7pppxC.mjs"; import { t as inspectTlsCertificateError } from "./tls-certificate-errors-ddRmxWST.mjs"; import { _ as matchesFormatErrorPattern, a as isAuthPermanentErrorMessage, c as isPeriodicUsageLimitErrorMessage, f as isRateLimitErrorMessage, h as isTimeoutErrorMessage, i as isAuthErrorMessage, m as isSessionTranscriptValidationErrorMessage, o as isBillingErrorMessage, p as isServerErrorMessage, s as isOverloadedErrorMessage, u as isProviderCompletedErrorFinishReasonMessage, v as resolveExecutionApprovalFailureMessage } from "./message-patterns-xrpp7Aip.mjs"; import { a as matchesContextOverflowMessage, i as looksLikeProviderContextOverflowCandidate, n as isContextOverflowErrorFromTables } from "./context-overflow-tables-CeoOZL8Z.mjs"; import { s as classifyOAuthRefreshFailure } from "./oauth-refresh-failure-DxrQiYzV.mjs"; import { a as failoverReasonFromClassification, c as isExactUnknownNoDetailsError, f as isUnsupportedImageInputErrorMessage, i as classifyFailoverReasonFromCode, l as isGenericUnknownStreamErrorMessage, m as toReasonClassification, n as classifyFailoverClassificationFromHttpStatus, o as inferSignalStatus, p as toPluginClassification, r as classifyFailoverReasonFrom402Text, s as isClaudeCliAuthError, t as classifyCoreFailoverReasonFromErrorType, u as isReplayInvalidErrorMessage } from "./classification-rules-DfXxEGkP.mjs"; //#region src/agents/embedded-agent-helpers/image-errors.ts const IMAGE_DIMENSION_ERROR_RE = /image dimensions exceed max allowed size for many-image requests:\s*(\d+)\s*pixels/i; const IMAGE_DIMENSION_PATH_RE = /messages\.(\d+)\.content\.(\d+)\.image/i; const IMAGE_SIZE_ERROR_RE = /image exceeds\s*(\d+(?:\.\d+)?)\s*mb/i; function parseImageDimensionError(e) { if (!/image dimensions exceed max allowed size/i.test(e)) return null; const t = e.match(IMAGE_DIMENSION_ERROR_RE); const n = e.match(IMAGE_DIMENSION_PATH_RE); return { maxDimensionPx: t?.[1] ? Number.parseInt(t[1], 10) : void 0, messageIndex: n?.[1] ? Number.parseInt(n[1], 10) : void 0, contentIndex: n?.[2] ? Number.parseInt(n[2], 10) : void 0 }; } function isImageDimensionErrorMessage(e) { return Boolean(parseImageDimensionError(e)); } function parseImageSizeError(e) { if (!/image exceeds[\s\S]*mb/i.test(e)) return null; const t = e.match(IMAGE_SIZE_ERROR_RE); return { maxMb: t?.[1] ? Number.parseFloat(t[1]) : void 0 }; } function isImageSizeError(e) { return Boolean(e && parseImageSizeError(e)); } //#endregion //#region src/agents/live-model-errors.ts const LISTED_MODEL_CHAT_REJECTION_RE = /\bmodel\b[^\r\n,;]{0,120}\bdoes not exist\b|not supported with the Responses API|not supported for generateContent|response modalities|bidiGenerateContent|not allowed to sample/i; /** * A deterministic 400, 403 or 404 that says the model cannot serve this chat request. Only * meaningful for a model in the credential's current listing; otherwise the model is missing. */ function isListedModelChatRejection(e, t) { return (e === 400 || e === 403 || e === 404) && LISTED_MODEL_CHAT_REJECTION_RE.test(t); } /** * Probe and fallback code uses these string checks to distinguish missing or * deprecated model ids from generic provider/runtime failures. */ function isModelNotFoundErrorMessage(e) { const t = e.trim(); return /no endpoints found for/i.test(t) || /\brouter not found\b/i.test(t) || /unknown model/i.test(t) || /\bmodel\b[^\r\n]{0,120}?\b(?:was|is|has been) retired\b/i.test(t) || /\b[a-z0-9][a-z0-9._:/-]* was retired at \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [+-]\d{4} [a-z]+ \(ref:/i.test(t) || /model(?:[_\-\s])?not(?:[_\-\s])?found|\bmodel\b.{0,60}?\bnot found\b/i.test(t) || /\b404\b/.test(t) && /not(?:[_\-\s])?found/i.test(t) || /not_found_error/i.test(t) || /\bnot supported model\b/i.test(t) || /\bmodel\b[^.]{0,120}?\bis not supported when using\b[^.]{0,80}?\bwith a ChatGPT account\b/i.test(t) || /model:\s*[a-z0-9._/-]+/i.test(t) && /not(?:[_\-\s])?found/i.test(t) || /models\/[^\s]+ is not found/i.test(t) || /model/i.test(t) && /does not exist/i.test(t) || /selected model/i.test(t) && /not(?:[_\-\s])?found/i.test(t) || /model/i.test(t) && /deprecated/i.test(t) && /(upgrade|transition) to/i.test(t) || /\bmodel\b(?:\s+(?!(?:parameters?|params?|fields?|options?|arguments?|settings?|names?|propert(?:y|ies)|endpoints?|versions?)\b)[`'"]?[\w./:@-]+[`'"]?)?\s+(?:is|was|has been) deprecated\b(?![^.\r\n]{0,80}\b(?:will|scheduled|soon)\b)/i.test(t) || /stealth model/i.test(t) && /find it here/i.test(t) || /is not a valid model id/i.test(t) || /invalid model/i.test(t) && !/invalid model reference/i.test(t); } //#endregion //#region src/agents/failover/classify-core.ts /** Classifies prepared error facts without resolving provider runtime. */ const HTML_BODY_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i; const HTML_CLOSE_RE = /<\/html>/i; function isHtmlErrorResponse(e) { const t = e.trim().replace(/^error:\s*/i, "").trim(); const n = extractLeadingHttpStatus(t)?.rest ?? t; return HTML_BODY_RE.test(n) && HTML_CLOSE_RE.test(n); } const PRIORITY_MESSAGE_CLASSIFIERS = [ [isUnsupportedImageInputErrorMessage, "format"], [isClaudeCliAuthError, "auth"], [isSessionTranscriptValidationErrorMessage, "format"], [isCliSessionExpiredErrorMessage, "session_expired"], [isModelNotFoundErrorMessage, "model_not_found"], [(e) => /\bworkers_ai\b.*\bquota limit exceeded\b/i.test(e), "rate_limit"], [(e) => /\bmodelnotreadyexception\b/i.test(e), "overloaded"], [(e) => /model(?:_is)?_deactivated|model has been deactivated/i.test(e), "model_not_found"] ]; const GENERAL_MESSAGE_CLASSIFIERS = [ [isRateLimitErrorMessage, "rate_limit"], [isOverloadedErrorMessage, "overloaded"], [isProviderCompletedErrorFinishReasonMessage, "server_error"], [(e) => isStructuredServerErrorMessage(e) && !isBillingErrorMessage(e) && !isAuthPermanentErrorMessage(e) && !isAuthErrorMessage(e), "server_error"], [isGenericProviderInternalError, "timeout"], [(e) => Boolean(classifyOAuthRefreshFailure(e)?.reason), "auth_permanent"], [isAuthPermanentErrorMessage, "auth_permanent"], [isAuthErrorMessage, "auth"], [(e) => isGenericUnknownStreamErrorMessage(e) || isServerErrorMessage(e) || isJsonApiInternalServerError(e), "timeout"], [isCloudCodeAssistFormatError, "format"], [isExactUnknownNoDetailsError, "no_error_details"], [isTimeoutErrorMessage, "timeout"], [(e) => matchesContextOverflowMessage(e, "assistant-error"), "context_overflow"] ]; function isTransportHtmlErrorStatus(e) { return e === 408 || e === 499 || e !== void 0 && e >= 500 && e < 600; } function classifyFailoverClassificationFromMessage(e, t, n) { if (isImageDimensionErrorMessage(e) || isImageSizeError(e)) return null; const r = PRIORITY_MESSAGE_CLASSIFIERS.find(([n]) => n(e, t))?.[1]; if (r) return toReasonClassification(r); if (isContextOverflowErrorFromTables(e)) return { kind: "context_overflow" }; if (isReplayInvalidErrorMessage(e)) return toReasonClassification("format"); const i = classifyFailoverReasonFrom402Text(e); if (i) return toReasonClassification(i); if (extractLeadingHttpStatus(e.trim())?.code !== 429 && isBillingErrorMessage(e)) return toReasonClassification("billing"); if (isPeriodicUsageLimitErrorMessage(e)) return toReasonClassification(isBillingErrorMessage(e) ? "billing" : "rate_limit"); const a = GENERAL_MESSAGE_CLASSIFIERS.find(([t]) => t(e))?.[1]; if (a) return toPluginClassification(a); const o = classifyCoreFailoverReasonFromErrorType(parseApiErrorInfo(e)?.type ?? n); if (o) return toReasonClassification(o); return classifyFailoverClassificationFromHttpStatus(inferSignalStatus({ message: e }), e, null, void 0, t); } function classifyFailoverDetailCandidates(e, t) { for (const n of e ?? []) { const e = classifyFailoverClassificationFromMessage(n, t); if (e) return e; } return null; } function mergeMessageAndDetailClassification(e, t) { if (!e || !t) return e ?? t; if (e.kind === "context_overflow") return e; return t.kind === "context_overflow" || t.reason === "billing" && e.reason === "rate_limit" || e.reason === "format" ? t : e; } function hasIndependentTransientMessage(e, t, n) { const r = e && (extractLeadingHttpStatus(e)?.rest ?? e); const i = parseApiErrorInfo(r); const a = !i && r ? safeParseJsonRecord(r) : null; const o = i ? i.message : a ? typeof a.message === "string" ? a.message : void 0 : r; return Boolean(o && classifyFailoverReasonFromCode(o) !== t && failoverReasonFromClassification(classifyFailoverClassificationFromMessage(o, n)) === t); } function classifyFailoverSignalCore(e, t) { if (resolveExecutionApprovalFailureMessage(e.message)) return null; const n = inferSignalStatus(e); const r = Number.isFinite(e.status) ? e.status : void 0; const i = e.message ? classifyFailoverClassificationFromMessage(e.message, e.provider, e.errorType) : null; const a = mergeMessageAndDetailClassification(i, classifyFailoverDetailCandidates(e.details, e.provider)); const o = classifyCoreFailoverReasonFromErrorType(e.errorType); const s = o ? toReasonClassification(o) : null; const c = r ?? (e.provider && (n === 401 || n === 403 || n === 429) ? n : void 0); const l = Boolean(e.message || e.code || e.errorType || n !== void 0); const u = c !== void 0 || e.code !== void 0 || e.errorType !== void 0; const d = Boolean(e.message && looksLikeProviderContextOverflowCandidate(e.message)); const f = l && (u || d || !i); let p = null; if (f) { const n = { errorMessage: e.message ?? "", provider: e.provider, status: c, code: e.code, errorType: e.errorType }; p = t?.(n) ?? null; if (!p) for (const r of e.details ?? []) { p = t?.({ ...n, errorMessage: r }) ?? null; if (p) break; } } const m = inspectTlsCertificateError(e); if (!p && m && n === void 0) return toReasonClassification("tls_certificate"); if (!p && e.message && isTransportHtmlErrorStatus(n) && isHtmlErrorResponse(e.message)) return classifyFailoverClassificationFromHttpStatus(n, void 0, null, e.status, e.provider); const h = classifyFailoverReasonFromCode(e.code) ?? classifyFailoverReasonFromCode(parseApiErrorInfo(e.message)?.code); const g = p ? toPluginClassification(p) : h === "format" ? toReasonClassification("format") : mergeMessageAndDetailClassification(a ?? s, h ? toReasonClassification(h) : null); if (h === "auth_permanent") return toReasonClassification(h); const _ = classifyFailoverClassificationFromHttpStatus(n, e.message, g, e.status, e.provider, { preserveProviderSignalClassification: p !== null }) ?? (h ? toReasonClassification(h) : g); if (!p && n !== void 0 && n >= 400 && n < 500 && n !== 408 && n !== 409 && n !== 410 && n !== 429 && n !== 499 && _?.kind === "reason" && _.reason === h && (h === "rate_limit" || h === "overloaded" || h === "timeout")) { const t = parseApiErrorInfo(e.message); if (!(hasIndependentTransientMessage(e.message, h, e.provider) || e.details?.some((t) => hasIndependentTransientMessage(t, h, e.provider))) && !/^throttling_?exception$/i.test(e.code ?? t?.code ?? "")) return { ..._, sameModelRetry: false }; } return _; } function isCloudCodeAssistFormatError(e) { return !isImageDimensionErrorMessage(e) && matchesFormatErrorPattern(e); } const API_ERROR_TRANSIENT_SIGNALS_RE = /internal server error|overload|temporarily unavailable|service unavailable|unknown error|server error|bad gateway|gateway timeout|upstream error|backend error|try again later|temporarily.+unable|unexpected error/i; function isJsonApiInternalServerError(e) { if (!normalizeLowercaseStringOrEmpty(e).includes("\"type\":\"api_error\"")) return false; if (isBillingErrorMessage(e)) return false; return API_ERROR_TRANSIENT_SIGNALS_RE.test(e); } function isStructuredServerErrorMessage(e) { const t = normalizeOptionalLowercaseString(parseApiErrorInfo(e)?.type); if (t === "server_error" || t === "upstream_error") return true; const n = normalizeLowercaseStringOrEmpty(e); return n.includes("\"type\":\"server_error\"") || n.includes("\"code\":\"server_error\"") || n.includes("\"type\":\"upstream_error\"") || n.includes("\"code\":\"upstream_error\""); } function isCliSessionExpiredErrorMessage(e) { return /\b(?:session (?:not found|does not exist|expired|invalid)|conversation (?:not found|does not exist|expired|invalid)|no conversation found|no such session|invalid session|(?:session|conversation) id not found)\b/.test(normalizeLowercaseStringOrEmpty(e)); } function classifyFailoverReasonCore(e, t, n) { return failoverReasonFromClassification(classifyFailoverSignalCore({ message: e, provider: t?.provider }, n)); } //#endregion export { parseImageDimensionError as a, isListedModelChatRejection as i, classifyFailoverSignalCore as n, parseImageSizeError as o, isCloudCodeAssistFormatError as r, classifyFailoverReasonCore as t };