UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

69 lines (68 loc) • 2.61 kB
import { p as toErrorObject } from "./error-coercion-hDrHOSGy.mjs"; import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs"; import "./abort-signal-D4Q6SEw7.mjs"; import "./errors-BKzCeTYb.mjs"; //#region src/agents/embedded-agent-runner/run/abortable.ts /** Marks AbortErrors produced by abortable() so provider aborts stay retryable. */ const OPENCLAW_ABORTABLE_WRAPPER = Symbol.for("openclaw.abortable.wrapper"); function isOpenClawAbortableWrapper(e) { return e !== null && typeof e === "object" && OPENCLAW_ABORTABLE_WRAPPER in e; } function createAbortableError(e) { const t = e.reason; const n = new Error(t instanceof Error ? t.message : "aborted", t ? { cause: t } : void 0); return Object.assign(n, { name: "AbortError", [OPENCLAW_ABORTABLE_WRAPPER]: true }); } const RUN_LIVENESS_JOIN_TIMEOUT_MS = 12e4; function finishRunLivenessJoin(e) { e.finish?.(); } /** * Awaits post-turn work that must never dead-end the run: races the joined * promise against the run-abort signal and a liveness deadline. Timeout and * abort RESOLVE (timeout after `onTimeout`) instead of rejecting so settlement * still produces a visible terminal outcome; rejections also resolve because * the joined chains own their error logging. */ function joinWithRunLivenessDeadline(e) { return new Promise((t) => { const finish = (r) => { if (!n.finish) return; n.finish = void 0; clearTimeout(i); e.runAbortSignal?.removeEventListener("abort", onAbort); if (r === "timeout") e.onTimeout(); t(); }; const n = { finish: () => finish("settled") }; const r = finishRunLivenessJoin.bind(void 0, n); const onAbort = () => finish("abort"); const i = setTimeout(() => finish("timeout"), e.timeoutMs ?? 12e4); i.unref(); if (e.runAbortSignal?.aborted) { finish("abort"); return; } e.runAbortSignal?.addEventListener("abort", onAbort, { once: true }); Promise.resolve().then(() => e.joinWork()).then(r, r); }); } /** * Races a promise against an AbortSignal while preserving normal promise * settlement. Abort wins immediately and rejected non-Error payloads are * normalized so callers can safely log/inspect them as Error objects. */ function abortable(e, t) { if (e.aborted) { t.catch(() => {}); return Promise.reject(createAbortableError(e)); } return racePromiseWithAbortSignal(() => t.catch((e) => { throw toErrorObject(e, "Non-Error rejection"); }), e, createAbortableError); } //#endregion export { joinWithRunLivenessDeadline as a, isOpenClawAbortableWrapper as i, abortable as n, createAbortableError as r, RUN_LIVENESS_JOIN_TIMEOUT_MS as t };