openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
58 lines (57 loc) • 2.74 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
//#region src/agents/accepted-session-spawn.ts
/** Normalizes accepted child-session spawn results from loose tool payloads. */
const acceptedSpawnsByRun = resolveGlobalSingleton(Symbol.for("openclaw.acceptedSessionSpawnsByRun"), () => /* @__PURE__ */ new WeakMap());
function mergeAcceptedSessionSpawnsForRun(e, t = []) {
let n = acceptedSpawnsByRun.get(e);
if (!n && t.length > 0) {
n = /* @__PURE__ */ new Map();
acceptedSpawnsByRun.set(e, n);
}
for (const e of t) n?.set(e.runId, n.get(e.runId) ?? e);
return n ? [...n.values()] : [];
}
/** A confirmed cancellation preserves acceptance but ends its owed completion. */
function retireAcceptedSessionSpawnCompletionForRun(e, t) {
const n = acceptedSpawnsByRun.get(e);
const r = n?.get(t.runId);
if (n && r?.childSessionKey === t.childSessionKey && r.expectsCompletionMessage === true) n.set(t.runId, {
...r,
expectsCompletionMessage: false
});
}
/** Normalize a tool result that accepted a child session spawn. */
function normalizeAcceptedSessionSpawnResult(e) {
const t = asOptionalRecord(asOptionalRecord(e)?.details);
if (!t || t.status !== "accepted") return null;
const n = normalizeOptionalString(t.runId);
const r = normalizeOptionalString(t.childSessionKey);
if (!n || !r) return null;
const i = normalizeOptionalString(t.sessionUrl);
const a = i ? URL.parse(i) : null;
const o = normalizeOptionalString(t.label);
return {
runId: n,
childSessionKey: r,
...a?.protocol === "http:" || a?.protocol === "https:" ? { sessionUrl: i } : {},
...o ? { label: o } : {},
...t.publicRead === true ? { publicRead: true } : {},
expectsCompletionMessage: t.expectsCompletionMessage === true
};
}
/** Return true when a collection contains at least one accepted child spawn. */
function hasAcceptedSessionSpawn(e) {
return Boolean(e?.length);
}
/** Return true when an accepted child owns the requester's terminal completion. */
function hasCompletionMessageSessionSpawn(e) {
return e?.some((e) => e.expectsCompletionMessage === true) === true;
}
/** Return true when an accepted child owes the requester a completion or awaits its collection. */
function hasOwedSessionSpawn(e) {
return e?.some((e) => e.expectsCompletionMessage === true || e.collector === true) === true;
}
//#endregion
export { normalizeAcceptedSessionSpawnResult as a, mergeAcceptedSessionSpawnsForRun as i, hasCompletionMessageSessionSpawn as n, retireAcceptedSessionSpawnCompletionForRun as o, hasOwedSessionSpawn as r, hasAcceptedSessionSpawn as t };