openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
94 lines (93 loc) • 4.63 kB
JavaScript
import { t as settlesWithin } from "./settle-within-Bj8A0Tzk.mjs";
import { o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs";
import { o as getGatewayContextResolver } from "./gateway-context-binding-BdXZoMU9.mjs";
import { st as SESSION_CONTROLLER_DRAIN_TIMEOUT_MS } from "./session-controller.state-BApgK0mV.mjs";
import { C as trackSessionControllerSourceWork } from "./session-controller.lifecycle-C_Z3sybN.mjs";
import { g as isRpcSourceRegistered, r as getRpcSourceForGateway } from "./session-controller.rpc-sources-CTFbRW2t.mjs";
import "./session-controller.mailbox-BcK8UF_b.mjs";
//#region src/gateway/chat-abort-lifecycle-internal.ts
/** Terminal outcome and physical persistence settle independently. */
function markChatAbortTerminalOutcome(e) {
if (e) e.adapter.terminalOutcomeObserved = true;
}
/** Retired producers may publish until another exact source owns the protocol ID. */
function captureChatAbortRegistrationGuard(e) {
const t = e.map(({ runId: e, entry: t }) => ({
runId: e,
entry: t,
resolveGatewayContext: t ? getGatewayContextResolver(t.input) : getPluginRuntimeGatewayRequestScope()?.resolveGatewayContext
}));
return () => t.every(({ runId: e, entry: t, resolveGatewayContext: n }) => t && isRpcSourceRegistered(t) || !getRpcSourceForGateway(e, n));
}
const terminalPersistenceErrorByEntry = /* @__PURE__ */ new WeakMap();
const terminalDispatchByEntry = /* @__PURE__ */ new WeakMap();
const removalWaitersByEntry = /* @__PURE__ */ new WeakMap();
/** Retain the subscription owner's receipt on the exact captured registration. */
function bindChatAbortTerminalDispatch(e, t, n) {
if (!e || !n) return;
const r = Object.assign(n, { settled: t });
for (const n of e) {
terminalDispatchByEntry.set(n, r);
trackSessionControllerSourceWork(n.input, t);
}
}
function isCurrentChatAbortTerminalDispatch(e, t) {
return terminalDispatchByEntry.get(e) === t;
}
function markChatAbortTerminalPersistenceError(e, t) {
if (t === void 0) {
terminalPersistenceErrorByEntry.delete(e);
return;
}
terminalPersistenceErrorByEntry.set(e, t);
}
function notifyChatAbortControllerRemoved(e) {
const t = removalWaitersByEntry.get(e);
removalWaitersByEntry.delete(e);
for (const e of t ?? []) e();
}
/** A requester deadline never releases source, producer, or persistence custody. */
async function waitForChatAbortAcknowledgment(e) {
if (!await settlesWithin(e, 15e3)) throw new Error("Cancellation was requested, but cleanup is still pending. Check the turn status before retrying Stop.");
return await e;
}
/** Cancellation joins accepted dispatch before reporting the captured terminal write outcome. */
async function waitForChatAbortTerminalPersistence(e) {
const t = terminalDispatchByEntry.get(e);
const n = e.adapter.projectSessionTerminalPersistence;
if (t) await t.settled;
const r = n ?? e.adapter.projectSessionTerminalPersistence;
if (r) await r;
if (!r && terminalPersistenceErrorByEntry.has(e)) throw terminalPersistenceErrorByEntry.get(e);
if (t?.failure) throw t.failure.error;
if (!r && e.adapter.projectSessionTerminalPending === true) throw new Error("Session cancellation has no terminal persistence owner");
}
/** Waits for captured run registrations and their terminal persistence owner to leave. */
async function waitForChatAbortControllerRemoval(e) {
const terminalOwnersSettled = () => e.targets.every(({ entry: e }) => e.adapter.projectSessionTerminalPending !== true && e.adapter.projectSessionTerminalPersistence === void 0 && !terminalPersistenceErrorByEntry.has(e));
const t = [];
const n = e.targets.flatMap(({ entry: e }) => {
if (!isRpcSourceRegistered(e)) return [];
return [new Promise((n) => {
const r = removalWaitersByEntry.get(e) ?? /* @__PURE__ */ new Set();
r.add(n);
removalWaitersByEntry.set(e, r);
t.push({
entry: e,
resolve: n
});
})];
});
if (n.length === 0) return terminalOwnersSettled();
try {
return await settlesWithin(Promise.all(n), Math.max(0, e.timeoutMs)) && terminalOwnersSettled();
} finally {
for (const { entry: e, resolve: n } of t) {
const t = removalWaitersByEntry.get(e);
t?.delete(n);
if (t?.size === 0) removalWaitersByEntry.delete(e);
}
}
}
//#endregion
export { markChatAbortTerminalPersistenceError as a, waitForChatAbortControllerRemoval as c, markChatAbortTerminalOutcome as i, waitForChatAbortTerminalPersistence as l, captureChatAbortRegistrationGuard as n, notifyChatAbortControllerRemoved as o, isCurrentChatAbortTerminalDispatch as r, waitForChatAbortAcknowledgment as s, bindChatAbortTerminalDispatch as t };