openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
97 lines (96 loc) • 3.44 kB
JavaScript
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import "./gateway-request-scope-BvvDhs5D.mjs";
import { o as getGatewayContextResolver } from "./gateway-context-binding-BdXZoMU9.mjs";
import { a as createAgentRunDirectAbortError } from "./run-termination-0Ig0-bk6.mjs";
import { vt as getSessionControllerSourceIdentity } from "./session-controller.state-BApgK0mV.mjs";
import { a as getRpcSourceIdentity, r as getRpcSourceForGateway } from "./session-controller.rpc-sources-CTFbRW2t.mjs";
import { i as captureSessionControllerStop, r as stopSession } from "./session-controller.stop-BqKrTY7V.mjs";
import { t as abortChatRunById } from "./chat-abort-DHc0iNFP.mjs";
import { t as abortControlledSubagents } from "./chat-abort-descendants-2IwJM3Ms.mjs";
//#region src/gateway/server-methods/chat-abort-exact-run.ts
/** Retain the physical source across asynchronous Stop metadata preparation. */
function captureSourceAssertion(e, t) {
const n = getSessionControllerSourceIdentity(e);
return () => {
t?.();
const r = getSessionControllerSourceIdentity(e);
if (r.sessionKey !== n.sessionKey || r.sessionId !== n.sessionId || r.agentId !== n.agentId) throw new Error("Run changed before cancellation; retry Stop.");
};
}
/**
* Stops one exact Gateway RPC run through Stop source `client-run`: its source
* input, its chat presentation, and that turn's controlled subagents.
*/
async function stopExactClientRun(e) {
const { ops: t, runId: n, active: r } = e;
const i = captureSessionControllerStop({ inputs: [r.input] });
const { sessionKey: a, sessionId: o, agentId: s } = getRpcSourceIdentity(r);
const c = { aborted: false };
const l = captureSourceAssertion(r.input, e.assertCurrent);
const markAborted = () => {
if (!c.aborted) {
c.aborted = true;
e.onAborted?.();
}
};
const assertCurrent = () => {
l();
const e = getRpcSourceForGateway(n, getGatewayContextResolver(r.input));
if (e && e !== r) throw new Error("Run changed before cancellation; retry Stop.");
};
try {
await stopSession({
source: "client-run",
capture: i,
assertCurrent,
reason: createAgentRunDirectAbortError(),
hookContext: {
...e.hookContext,
sessionKey: a,
sessionId: o
},
afterParent: e.afterParent,
continueChildStop: () => c.aborted,
onCancelled: (e) => {
if (e === r.input) markAborted();
},
cancelInput: (i, o) => abortChatRunById(t, {
runId: n,
sessionKey: a,
expectedEntry: r,
cancel: o,
assertCurrent,
stopReason: "rpc",
onAbortPrepared: e.onAbortPrepared,
onAbortCommitted: markAborted
}).aborted,
stopChildren: async (t) => {
c.descendants = await abortControlledSubagents({
cfg: e.cfg,
sessionKey: a,
agentId: s,
requesterTurnRunId: n,
sessionId: o,
lifecycleRevision: e.lifecycleRevision,
assertCurrent,
beforeKill: t,
parentStopAccepted: () => c.aborted
});
return {
stopped: c.descendants?.killed ?? 0,
failed: c.descendants?.status === "error" ? c.descendants.failed : 0
};
}
}).completed;
} catch (e) {
if (c.aborted && c.descendants?.status === "error") c.descendants = {
...c.descendants,
failed: c.descendants.failed + 1,
error: `${c.descendants.error}; ${formatErrorMessage(e)}`
};
else c.failure = { error: e };
}
return c;
}
//#endregion
export { stopExactClientRun as n, captureSourceAssertion as t };