UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

121 lines (120 loc) • 4.97 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { g as extractErrorHttpStatus } from "./redact-Mj7pppxC.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { i as describeFailoverError, s as hasModelFallbackStop } from "./failover-error-CbIiC30n.mjs"; import { n as extractFailoverHttpStatus } from "./retry-evidence-f1T47dhA.mjs"; import { o as isSummaryProviderError } from "./types-CD5C4AET.mjs"; import { n as classifyAssistantFailoverReason, t as buildAssistantFailoverSignal } from "./assistant-message-failures-Cv3Qnsh5.mjs"; //#region src/agents/embedded-agent-runner/compact-reasons.ts const COMPACTION_PROVIDER_4XX = /* @__PURE__ */ new Set([ 400, 401, 403, 429 ]); const COMPACTION_PROVIDER_5XX = /* @__PURE__ */ new Set([ 500, 502, 503, 504 ]); const COMPACTION_TEXT_REASONS = [ ["unshrinkable_request", ["estimated foreground request budget still exceeds"]], ["no_compactable_entries", ["nothing to compact", "no real conversation messages"]], ["below_threshold", ["below threshold", "already under target"]], ["already_compacted", ["already compacted", "already_compacted"]], ["deferred_background", ["deferred to background"]], ["live_context_still_exceeds_target", ["still exceeds target"]], [ "transcript_persistence_failed", ["session transcript", "not persisted"], "all" ], ["guard_blocked", ["guard"]], ["summary_failed", ["summary"]], ["timeout", ["timed out", "timeout"]] ]; const DEFERRED_CONTEXT_ENGINE_COMPACTION_REASON = "deferred to background context-engine maintenance"; function buildCompactionFailureResult(e, t) { return { ok: false, compacted: false, reason: e, ...t ? { failure: t } : {} }; } function isGenericCompactionCancelledReason(e) { const t = normalizeLowercaseStringOrEmpty(e); return t === "compaction cancelled" || t === "error: compaction cancelled"; } /** Preserve terminal failures; otherwise project display text and failure provenance together. */ function resolveCompactionFailure(e) { if (hasModelFallbackStop(e.error)) throw e.error; const t = formatErrorMessage(e.error); const n = !e.abortSignal?.aborted && e.error instanceof Error && e.error.name === "Error" && isGenericCompactionCancelledReason(t) ? e.safeguardCancellation : void 0; return { reason: n?.reason ?? t, error: n?.error ?? e.error }; } function findSummaryProviderError(e) { let t = e; while (t instanceof Error) { if (isSummaryProviderError(t)) return t; t = t.cause; } } /** Classify the retained provider response, not the compaction wrappers' display text. */ function describeCompactionFailure(e) { const t = findSummaryProviderError(e); const n = classifyAssistantFailoverReason(t?.response); if (t && n) { const e = buildAssistantFailoverSignal(t.response); return { message: e.message ?? t.message, rawError: e.message, reason: n, status: e.status, code: e.code, provider: e.provider, model: t.response.model }; } return describeFailoverError(e); } /** * Only an actual summary timeout qualifies: the summary watchdog fired (the caller is still * live, so its composed signal aborted on the deadline), or the provider answered 408/504. * Failover's broader "timeout" class also covers fast 5xx, 410, and DNS failures, which keep * their owners' outcomes. */ function isSummaryTimeoutFailure(e) { const t = resolveCompactionFailure(e).error; if (e.summarySignal?.aborted) return true; const n = findSummaryProviderError(t); const r = n ? extractErrorHttpStatus(n.response.errorMessage?.trim() ?? "")?.code : void 0; return r === 408 || r === 504; } function classifyCompactionReason(e) { const t = normalizeLowercaseStringOrEmpty(e); if (!t) return "unknown"; if (t.startsWith("no api key found") || t.startsWith("authentication failed for ") && t.includes("credentials may have expired")) return "auth_failed"; for (const [e, n, r] of COMPACTION_TEXT_REASONS) { const includes = (e) => t.includes(e); if (r === "all" ? n.every(includes) : n.some(includes)) return e; } const n = extractFailoverHttpStatus(e, { includeLabeledStatus: true }); if (n !== void 0 && COMPACTION_PROVIDER_4XX.has(n)) return "provider_error_4xx"; if (n !== void 0 && COMPACTION_PROVIDER_5XX.has(n)) return "provider_error_5xx"; return "unknown"; } function isBenignCompactionSkipReason(e) { const t = classifyCompactionReason(e); return t === "below_threshold" || t === "already_compacted"; } function isBenignCompactionSkipResult(e) { if (e.compacted) return false; return isBenignCompactionSkipReason(e.reason) || e.ok && classifyCompactionReason(e.reason) === "no_compactable_entries"; } //#endregion export { isBenignCompactionSkipReason as a, resolveCompactionFailure as c, describeCompactionFailure as i, buildCompactionFailureResult as n, isBenignCompactionSkipResult as o, classifyCompactionReason as r, isSummaryTimeoutFailure as s, DEFERRED_CONTEXT_ENGINE_COMPACTION_REASON as t };