UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

199 lines (198 loc) • 9.01 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import "./agent-run-registry-D7vkrqFT.mjs"; import { v as registerAgentEventLifecycleRotationHandler } from "./agent-events-BdfWYmot.mjs"; import { r as getAgentRunLifecycleGeneration } from "./agent-run-registry-state-BFfw9a4W.mjs"; import { P as runAfterReplyOperationClear } from "./session-controller.state-BApgK0mV.mjs"; import { o as getSubagentRunRuntimeKey } from "./subagent-run-generation-BCjO9e0_.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/auto-reply/reply/queue/types.ts var FollowupRunDeferredError = class extends Error { constructor(e = "Follow-up run deferred") { super(e); this.name = "FollowupRunDeferredError"; } }; function bindReplyOperationQueueDisposition(e, t) { const n = e.onQueueDisposition; e.onQueueDisposition = (e) => { if (t) t.followupQueueDisposition = e; n?.(e); if (t && (e !== "queue-cap-old" || t.admission?.status !== "accepted")) t.admission = { status: "skipped", reason: "queue-cap" }; }; } function isFollowupOperatorAuthorityCurrent(e) { try { e.operatorAuthority?.assertCurrent(); return true; } catch { return false; } } function isFollowupRunAborted(e) { return !e.controllerInput?.custody.cancellationRetired && (e.abortSignal?.aborted === true || e.controllerInput?.abortSignal.aborted === true) || e.queueAbortSignal?.aborted === true || e.operatorAuthority?.signal?.aborted === true; } function resolveFollowupAbortSignal(e) { const t = [ e.controllerInput?.custody.cancellationRetired ? void 0 : e.abortSignal, e.controllerInput?.custody.cancellationRetired ? void 0 : e.controllerInput?.abortSignal, e.queueAbortSignal, e.operatorAuthority?.signal ].filter((e) => e !== void 0); return t.length > 1 ? AbortSignal.any(t) : t[0]; } //#endregion //#region src/auto-reply/reply/goal-continuation-return.ts const { goalReturns, goalSettlements, cohortReturns, retainedReturns } = resolveGlobalSingleton(Symbol.for("openclaw.goalContinuationReturns"), () => ({ goalReturns: new AsyncLocalStorage(), goalSettlements: new AsyncLocalStorage(), cohortReturns: /* @__PURE__ */ new WeakMap(), retainedReturns: /* @__PURE__ */ new Set() })); function closeGoalReturn(e) { if (e.closed) return; e.closed = true; retainedReturns.delete(e); for (const t of e.cohort?.keys ?? []) if (cohortReturns.get(t) === e) cohortReturns.delete(t); e.signal.removeEventListener("abort", e.close); e.release?.(); } /** Preserve the admitted chat route independently of the expiring settle capability. */ function createGoalContinuationReturnScope(e) { const t = resolveFollowupAbortSignal(e.base); const n = { params: e, continuation: e.initialGoalId ? { ...e.expectedSession, goalId: e.initialGoalId } : void 0, signal: t ? AbortSignal.any([e.operation.abortSignal, t]) : e.operation.abortSignal, generation: getAgentRunLifecycleGeneration(), closed: false, close: () => closeGoalReturn(n) }; runAfterReplyOperationClear(e.operation, () => { if (e.operation.result?.kind !== "completed") n.close(); }); return (e) => goalReturns.run(n, e); } /** The reply execution owner supplies its physical run ID before any tools can yield. */ function bindGoalContinuationExecution(e, t) { const n = goalReturns.getStore(); if (n && !n.closed && n.params.sessionKey === t) n.sourceRunId = e; } /** A Goal created by this execution can use its admitted chat return, never another command's. */ function bindCreatedGoalContinuation(e) { const t = goalReturns.getStore(); if (!t || t.closed || t.signal.aborted || t.continuation || t.params.base.run.scheduledAutomation || t.params.operation.result !== null || t.generation !== getAgentRunLifecycleGeneration() || !e.runId || t.sourceRunId !== e.runId || t.params.sessionKey !== e.sessionKey || t.params.base.run.agentId !== e.agentId || t.params.storePath !== e.storePath) return; t.continuation = { ...t.params.expectedSession, goalId: e.goalId }; } /** The committed yield publisher binds the return before the completion wake becomes eligible. */ function promoteGoalContinuationYield(e) { const t = goalSettlements.getStore(); const n = goalReturns.getStore(); const r = t?.admitted && t.runId === e.requesterTurnRunId ? t.owner : n?.sourceRunId === e.requesterTurnRunId ? n : void 0; if (!r || r.closed || r.signal.aborted || e.rearmGeneration === void 0 || !e.entries.length) return; const i = r.continuation; if (!i) return; const { sessionKey: a, base: o } = r.params; if (e.entries.some((t) => t.requesterSessionKey !== a || t.requesterAgentId !== o.run.agentId || t.completionRequesterSessionId !== void 0 && (t.completionRequesterSessionId !== i.sessionId || t.completionRequesterLifecycleRevision !== i.lifecycleRevision) || t.requesterSettleWake?.rearmGeneration !== e.rearmGeneration || t.requesterSettleWake?.requesterYieldBatch !== true)) return; o.operatorAuthority?.assertCurrent(); if (!r.cohort) { const e = o.queuedFollowupReplyDisposition; if (e?.kind === "drop") { r.close(); return; } r.params.base = { ...o, queuedFollowupReplyDisposition: e?.kind === "deliver" ? { kind: "deliver", deliver: e.deliver.createSourceRetry?.() ?? e.deliver } : e }; r.release = o.operatorAuthority?.retain?.(); retainedReturns.add(r); r.signal.addEventListener("abort", r.close, { once: true }); } for (const e of r.cohort?.keys ?? []) cohortReturns.delete(e); r.cohort = { keys: e.entries.map(getSubagentRunRuntimeKey), rearmGeneration: e.rearmGeneration }; for (const e of r.cohort.keys) { const t = cohortReturns.get(e); if (t && t !== r) t.close(); } for (const e of r.cohort.keys) cohortReturns.set(e, r); } function findGoalReturn(e) { const t = e[0]; const n = t && cohortReturns.get(getSubagentRunRuntimeKey(t)); return n && !n.closed && n.generation === getAgentRunLifecycleGeneration() && n.cohort?.keys.length === e.length && e.every((e) => cohortReturns.get(getSubagentRunRuntimeKey(e)) === n && e.requesterSettleWake?.rearmGeneration === n.cohort?.rearmGeneration) ? n : void 0; } /** Transient exact-run scope: ordinary commands never acquire a Goal return. */ function withGoalContinuationSettlement(e, t, n, r) { const i = n() ? findGoalReturn(e) : void 0; if (i && i.terminal?.runId !== t) i.terminal = void 0; return i ? goalSettlements.run({ owner: i, runId: t, isCurrent: n }, r) : r(); } /** Command preparation captures current Goal facts without injecting them into a native runtime. */ function prepareGoalContinuationContext(e) { const t = goalSettlements.getStore(); if (t?.runId === e.runId && t.owner.params.sessionKey === e.sessionKey && !t.owner.closed && t.isCurrent()) t.entry = e.sessionEntry; } /** Only the actual built-in harness consumes this transient context. */ function readGoalContinuationContext(e) { const t = goalSettlements.getStore(); if (!t || t.runId !== e.runId || t.owner.params.sessionKey !== e.sessionKey || t.owner.closed || !t.isCurrent()) return; const n = t.owner.continuation; const r = n && t.owner.params.context(n, t.entry, e.currentInboundContext); if (!r) return; t.owner.params.base.operatorAuthority?.assertCurrent(); t.admitted = true; return r; } /** Publish the actual command terminal result, never a synthetic success for a failure. */ function recordGoalContinuationSettlement(e) { const t = goalSettlements.getStore(); if (t && t.runId === e.runId && t.owner.params.sessionKey === e.sessionKey && t.isCurrent() && !t.owner.closed) t.owner.terminal = e.succeeded ? { runId: e.runId, result: e.result } : void 0; } /** Run only after the persisted batch commit; waiting inside the command would deadlock. */ async function finishGoalContinuationSettlement(e, t) { const n = findGoalReturn(e); if (!n) return; try { if (t && n.terminal && n.continuation) await n.params.resume({ continuation: n.continuation, base: n.params.base, ...n.terminal, isCurrent: () => !n.closed && n.generation === getAgentRunLifecycleGeneration() }); } catch (e) { if (n.closed || !isFollowupOperatorAuthorityCurrent(n.params.base)) return; throw e; } finally { n.close(); } } function revokeGoalContinuationReturn(e) { cohortReturns.get(getSubagentRunRuntimeKey(e))?.close(); } registerAgentEventLifecycleRotationHandler("goal-continuation-returns", () => { for (const e of retainedReturns) e.close(); }); //#endregion export { prepareGoalContinuationContext as a, recordGoalContinuationSettlement as c, FollowupRunDeferredError as d, bindReplyOperationQueueDisposition as f, resolveFollowupAbortSignal as h, finishGoalContinuationSettlement as i, revokeGoalContinuationReturn as l, isFollowupRunAborted as m, bindGoalContinuationExecution as n, promoteGoalContinuationYield as o, isFollowupOperatorAuthorityCurrent as p, createGoalContinuationReturnScope as r, readGoalContinuationContext as s, bindCreatedGoalContinuation as t, withGoalContinuationSettlement as u };