openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
96 lines (95 loc) • 3.52 kB
JavaScript
import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs";
import { i as createAbortError } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.mjs";
import { o as isCodeModeControlTool } from "./code-mode-control-tools-2pBIuXDg.mjs";
import { s as attachInternalToolExecutionPreparer, v as getInternalToolExecutionPreparer } from "./internal-hooks-Ch3rqxs-.mjs";
import { r as markToolExecutionNotStarted } from "./tool-effect-receipt-BbbEwqBS.mjs";
import { a as copyAgentToolMetadata } from "./agent-tool-metadata-nrQgRaqv.mjs";
import "./tool-result-error-B2GHe83l.mjs";
//#region src/agents/agent-tools.abort.ts
/**
* Combines per-call cancellation with run-level aborts while preserving
* identity-backed metadata on wrapped tools.
*/
function throwAbortError() {
throw markToolExecutionNotStarted(createAbortError("Aborted"));
}
/**
* Races a tool execute promise against the combined abort signal so an abort
* settles the wrapped call immediately instead of awaiting the tool forever.
* JavaScript cannot cancel a running promise: a tool that never observes the
* signal keeps executing in the background and may settle later, but its late
* settlement is detached here so the result never lands in an aborted run.
* Tool settlements pass through untouched to preserve tool error semantics,
* including non-Error rejections.
*/
function raceWithAbortSignal(e, t, n) {
return new Promise((r, i) => {
const onAbort = () => {
t.removeEventListener("abort", onAbort);
const e = n?.reason;
if (n?.aborted && t.reason === e && e?.code === "sessions_yield" && e.turnHandoff === true) return;
i(createAbortError("Aborted"));
};
t.addEventListener("abort", onAbort, { once: true });
e.then((e) => {
t.removeEventListener("abort", onAbort);
r(e);
}, (e) => {
t.removeEventListener("abort", onAbort);
i(e);
});
if (t.aborted) onAbort();
});
}
function isTurnYieldCall(e, t) {
const n = asOptionalObjectRecord(t);
return e.name === "sessions_yield" || e.name === "plugins" && n?.action === "reloadAndResume" || e.name === "github_publish" && (n?.action === "prepare_pr" || (n?.action ?? "publish") === "publish" && typeof n?.workspacePath === "string");
}
function wrapToolWithAbortSignal(e, t) {
if (!t) return e;
const n = e.execute;
if (!n) return e;
const r = isCodeModeControlTool(e);
const i = {
...e,
execute: async (i, a, o, s) => {
const c = o ? AbortSignal.any([o, t]) : t;
if (c.aborted) throwAbortError();
const l = n(i, a, c, s);
return r ? await l : await raceWithAbortSignal(l, c, isTurnYieldCall(e, a) ? t : void 0);
}
};
copyAgentToolMetadata(e, i, (e) => wrapToolWithAbortSignal(e, t));
const a = getInternalToolExecutionPreparer(e);
if (a) attachInternalToolExecutionPreparer(i, async (n) => {
const i = n.signal ? AbortSignal.any([n.signal, t]) : t;
if (i.aborted) throwAbortError();
const o = e.name === "sessions_yield" ? t : void 0;
const s = a({
...n,
signal: i
});
let c;
try {
c = await raceWithAbortSignal(s, i, o);
} catch (e) {
s.then((e) => e.dispose(), () => void 0);
throw e;
}
if (c.kind === "immediate") return c;
return {
kind: "ready",
args: c.args,
execute: (n) => {
if (i.aborted) throwAbortError();
const a = c.execute(n);
return r ? a : raceWithAbortSignal(a, i, isTurnYieldCall(e, c.args) ? t : void 0);
},
dispose: c.dispose
};
});
return i;
}
//#endregion
export { wrapToolWithAbortSignal as n, raceWithAbortSignal as t };