UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

119 lines (118 loc) • 4.57 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { r as collectErrorGraphCandidates, u as readErrorCauses } from "./error-coercion-hDrHOSGy.mjs"; import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs"; import "./abort-signal-D4Q6SEw7.mjs"; import { n as monotonicNow, r as remainingMonotonicMs, t as monotonicDeadlineAfter } from "./monotonic-time-Cc4T2fkU.mjs"; import { randomUUID } from "node:crypto"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/agents/mcp-startup-backoff.ts const authorizationCapture = resolveGlobalSingleton(Symbol.for("openclaw.mcpStartupAuthorizationCapture"), () => new AsyncLocalStorage()); /** Capture denial at its credential owner, before asynchronous cleanup can yield. */ function recordMcpStartupAuthorizationRequired(e, t) { authorizationCapture.getStore()?.(e, t); } const startups = resolveGlobalSingleton(Symbol.for("openclaw.mcpStartupBackoff"), () => /* @__PURE__ */ new Map()); let errorModule; var McpStartupBackoffError = class extends Error { constructor(e, t, n, r) { const i = e.authorization ? Infinity : e.owner === n && e.delayMs === 3e4 ? e.retryAfterMs - e.delayMs + r : e.retryAfterMs; super(e.authorization ? e.message : `${e.message}; server unavailable; retry after ${new Date(Date.now() + remainingMonotonicMs(i)).toISOString()}. Check server reachability or reload MCP after fixing it.`); this.reportFailure = t; this.retryAfterMs = i; this.authorizationRequired = e.authorization !== void 0; } }; function resetMcpStartupBackoff(e) { if (e === void 0) { for (const e of startups.values()) e.authorization?.dispose(); startups.clear(); } else { startups.get(e)?.authorization?.dispose(); startups.delete(e); } } function getMcpStartupBackoff(e, t, n, r = e) { const i = startups.has(r) ? r : e; const a = startups.get(i); if (a?.authorization && !a.authorization.isCurrent()) { resetMcpStartupBackoff(i); return; } if (!a?.message) return; return new McpStartupBackoffError(a, false, t, n); } async function connectWithMcpStartupBackoff(e, t, n, r, i = e) { const { redactMcpDiagnosticError: a } = await (errorModule ??= import("./mcp-error-CDWGRpmN.mjs")); while (startups.get(e)?.pending) await racePromiseWithAbortSignal(startups.get(e).pending.catch(() => void 0), t); t.throwIfAborted(); const o = getMcpStartupBackoff(e, t, r, i); if (o && monotonicNow() < o.retryAfterMs) throw o; const s = startups.get(e) ?? { owner: t, delayMs: 15e3, retryAfterMs: 0, message: "" }; startups.set(e, s); let c = e; let l = true; try { s.pending = authorizationCapture.run((t, n) => { if (l && startups.get(e) === s) { s.authorization?.dispose(); s.authorization = { error: t, ...n() }; } }, n); await s.pending; if (startups.get(e) === s) resetMcpStartupBackoff(e); } catch (n) { if (t.aborted || s.authorization && !collectErrorGraphCandidates(n, readErrorCauses).includes(s.authorization.error)) { s.authorization?.dispose(); s.authorization = void 0; } if (t.aborted) { if (!s.message && startups.get(e) === s) startups.delete(e); throw n; } s.delayMs = Math.min(s.delayMs * 2, 6e5); s.retryAfterMs = monotonicDeadlineAfter(s.delayMs); s.message = a(n); const o = !s.authorization || !startups.has(i) || startups.get(i) === s; if (s.authorization && startups.get(e) === s && i !== e) { resetMcpStartupBackoff(i); startups.delete(e); c = i; startups.set(c, s); } throw new McpStartupBackoffError(s, o, t, r); } finally { l = false; s.pending = void 0; if (startups.get(c) !== s) s.authorization?.dispose(); for (const [e, t] of startups) { if (startups.size <= 1024) break; if (!t.pending) resetMcpStartupBackoff(e); } } } /** A runtime owns transient failures; credential denials outlive native capability contexts. */ function createMcpStartupBackoff(e) { const t = JSON.stringify([...e.identity, ...e.runScoped ? [randomUUID()] : []]); const n = JSON.stringify([...e.identity.slice(2), "oauth-authorization"]); return { get: () => getMcpStartupBackoff(t, e.signal, e.recoveryRetryMs, n), connect: (r) => connectWithMcpStartupBackoff(t, e.signal, r, e.recoveryRetryMs, n), reset() { resetMcpStartupBackoff(t); resetMcpStartupBackoff(n); }, dispose() { if (e.runScoped) resetMcpStartupBackoff(t); } }; } //#endregion export { resetMcpStartupBackoff as i, createMcpStartupBackoff as n, recordMcpStartupAuthorizationRequired as r, McpStartupBackoffError as t };