UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

135 lines (134 loc) 5.85 kB
import { t as asBoolean } from "./boolean-DmBL0YJK.js"; import { a as isFailoverError, n as findErrorProperty, o as isSignalTimeoutReason } from "./error-Bb_OF8ag.js"; //#region src/agents/run-timeout-attribution.ts const AGENT_RUN_TIMEOUT_PHASE_SET = /* @__PURE__ */ new Set([ "queue", "preflight", "provider", "post_turn", "gateway_draining" ]); /** Normalizes raw timeout phase metadata into a known agent run phase. */ function normalizeAgentRunTimeoutPhase(value) { if (typeof value !== "string") return; const normalized = value.trim(); return AGENT_RUN_TIMEOUT_PHASE_SET.has(normalized) ? normalized : void 0; } //#endregion //#region src/agents/run-termination.ts /** * Shared agent run termination constants. * * Runtime and stream consumers use these stable literals to recognize user or * controller aborts without matching free-form error text. */ /** Stop reason emitted when an agent run is aborted. */ const AGENT_RUN_ABORTED_STOP_REASON = "aborted"; /** Error text used for aborted agent runs. */ const AGENT_RUN_ABORTED_ERROR = "agent run aborted"; const AGENT_RUN_RESTART_ABORT_STOP_REASON = "restart"; const AGENT_RUN_SUPERSEDED_STOP_REASON = "superseded"; /** Error text used for agent runs aborted by a gateway restart. */ const AGENT_RUN_RESTART_ABORT_ERROR = "agent run aborted for restart"; const AGENT_RUN_SUPERSEDED_ERROR = "agent run superseded by a newer session writer"; /** * Transports copy this code onto the persisted assistant message via * `errorCode`, so restart recovery can recognize its own abort without matching * free-form provider error text. */ const AGENT_RUN_RESTART_ABORT_ERROR_CODE = "OPENCLAW_RESTART_ABORT"; const AGENT_RUN_SUPERSEDED_ABORT_ERROR_CODE = "AGENT_RUN_SUPERSEDED_ABORT"; const AGENT_RUN_DIRECT_ABORT_ERROR_CODE = "OPENCLAW_DIRECT_ABORT"; function createAgentRunDirectAbortError() { const error = /* @__PURE__ */ new Error(AGENT_RUN_ABORTED_ERROR); error.name = "AbortError"; error.code = AGENT_RUN_DIRECT_ABORT_ERROR_CODE; return error; } function hasAgentRunAbortCode(value, code) { try { return value instanceof Error && "code" in value && value.code === code; } catch { return false; } } function isAgentRunDirectAbortReason(value) { return hasAgentRunAbortCode(value, AGENT_RUN_DIRECT_ABORT_ERROR_CODE); } function createAgentRunRestartAbortError() { const error = /* @__PURE__ */ new Error(AGENT_RUN_RESTART_ABORT_ERROR); error.name = "AbortError"; error.code = AGENT_RUN_RESTART_ABORT_ERROR_CODE; return error; } function createAgentRunSupersededAbortError() { const error = /* @__PURE__ */ new Error(AGENT_RUN_SUPERSEDED_ERROR); error.name = "AbortError"; error.code = AGENT_RUN_SUPERSEDED_ABORT_ERROR_CODE; return error; } function isAgentRunRestartAbortReason(value) { return hasAgentRunAbortCode(value, AGENT_RUN_RESTART_ABORT_ERROR_CODE); } function isAgentRunSupersededAbortReason(value) { return hasAgentRunAbortCode(value, AGENT_RUN_SUPERSEDED_ABORT_ERROR_CODE); } function throwAgentRunRestartAbortReason(value) { if (isAgentRunRestartAbortReason(value)) throw value; } function resolveAgentRunAbortLifecycleFields(signal) { if (!signal?.aborted) return {}; return { aborted: true, stopReason: isAgentRunRestartAbortReason(signal.reason) ? AGENT_RUN_RESTART_ABORT_STOP_REASON : isSignalTimeoutReason(signal.reason) ? "timeout" : AGENT_RUN_ABORTED_STOP_REASON }; } function resolveRunErrorTimeout(error) { try { const timeout = findErrorProperty(error, (candidate) => isFailoverError(candidate) ? candidate.timeout : isSignalTimeoutReason(candidate) ? { timeoutPhase: "provider" } : void 0); if (!timeout) return; const timeoutPhase = normalizeAgentRunTimeoutPhase(timeout.timeoutPhase); const providerStarted = asBoolean(timeout.providerStarted); return { ...timeoutPhase ? { timeoutPhase } : {}, ...providerStarted !== void 0 ? { providerStarted } : {} }; } catch { return; } } /** Preserve recorded run timeouts when no caller abort signal was raised. */ function resolveAgentRunErrorLifecycleFields(error, signal) { const abortFields = resolveAgentRunAbortLifecycleFields(signal); if (abortFields.aborted) return abortFields; if (isAgentRunDirectAbortReason(error)) return { aborted: true, stopReason: "aborted" }; const timeout = resolveRunErrorTimeout(error); return timeout ? { stopReason: "timeout", ...timeout } : {}; } /** Returns whether a stop reason is the stable aborted-run reason. */ function isAbortedAgentStopReason(value) { return value === AGENT_RUN_ABORTED_STOP_REASON || value === "restart"; } /** * CLI tool terminal reason for one-shot and live runners. * Abort-signal lifecycle is authoritative so a timeout abort stays timed_out * even when the delivered error is a generic AbortError. */ function resolveCliToolTerminalReason(params) { const terminal = resolveAgentRunErrorLifecycleFields(params.error, params.abortSignal); if (terminal.stopReason === "timeout") return "timed_out"; if (terminal.aborted) return "cancelled"; const { error } = params; try { if (error instanceof Error && error.name === "AbortError") return "cancelled"; } catch {} return "failed"; } //#endregion export { throwAgentRunRestartAbortReason as _, AGENT_RUN_SUPERSEDED_ERROR as a, createAgentRunRestartAbortError as c, isAgentRunDirectAbortReason as d, isAgentRunRestartAbortReason as f, resolveCliToolTerminalReason as g, resolveAgentRunErrorLifecycleFields as h, AGENT_RUN_RESTART_ABORT_STOP_REASON as i, createAgentRunSupersededAbortError as l, resolveAgentRunAbortLifecycleFields as m, AGENT_RUN_RESTART_ABORT_ERROR as n, AGENT_RUN_SUPERSEDED_STOP_REASON as o, isAgentRunSupersededAbortReason as p, AGENT_RUN_RESTART_ABORT_ERROR_CODE as r, createAgentRunDirectAbortError as s, AGENT_RUN_ABORTED_ERROR as t, isAbortedAgentStopReason as u, normalizeAgentRunTimeoutPhase as v };