openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
123 lines (122 loc) • 3.82 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { t as captureEffectAuthority } from "./effect-authority-nglmRKHz.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/shared/channel-read-authority.ts
const completionKey = Symbol.for("openclaw.channelReadAuthority.completion");
const authorityScope = resolveGlobalSingleton(Symbol.for("openclaw.channelReadAuthority"), () => new AsyncLocalStorage());
/** Capture at request submission; retain this exact check through queues and retries. */
function captureChannelReadAuthority() {
return authorityScope.getStore();
}
/** Internal media ownership; the SDK continues to expose only the callable assertion. */
function captureChannelReadScope() {
return authorityScope.getStore()?.[completionKey];
}
/** Preserve the read failure while reporting failures to release its owned output. */
async function settleChannelReadResource(e, t) {
try {
await e.settle(t);
} catch (e) {
try {
const [{ createSubsystemLogger: t }, { redactToolPayloadText: n }] = await Promise.all([import("./subsystem-DbE05_DO.mjs"), import("./redact-DdU3em1_.mjs")]);
const r = t("channels");
const i = e instanceof AggregateError ? e.errors : [e];
for (const e of i) r.error(`Channel read output cleanup failed: ${n(String(e))}`);
} catch {
console.error("Channel read output cleanup failed; diagnostic logger unavailable.");
}
}
}
async function settleReadResources(e, t) {
await Promise.all(Array.from(e, (e) => settleChannelReadResource(e, t)));
e.clear();
}
/** Host-only entry: neither model params nor plugin declarations mint read authority. */
async function withChannelReadAuthority(e, t, n, r) {
if (!e) {
const e = await t();
r?.(e);
return e;
}
const i = captureEffectAuthority();
const a = authorityScope.getStore();
const o = a?.[completionKey];
const s = [o?.signal, n].filter((e) => Boolean(e));
const c = s.length > 1 ? AbortSignal.any(s) : s[0];
const l = /* @__PURE__ */ new Set();
let u = true;
const assertAuthority = () => {
a?.();
if (!u) throw new Error("Channel read authority is no longer active.");
c?.throwIfAborted();
e();
for (const e of l) e.assertCurrent?.();
};
const d = Object.assign(assertAuthority, { [completionKey]: {
assertCurrent: assertAuthority,
signal: c,
registerResource: (t) => {
assertAuthority();
l.add({
key: t.key,
settle: t.settle,
onDelegated: t.onDelegated,
assertCurrent: () => {
c?.throwIfAborted();
e();
t.assertCurrent?.();
}
});
},
delegateResource: (e, t, n) => {
assertAuthority();
const r = Array.from(l).find((t) => t.key === e);
if (!r) return o?.delegateResource(e, t, n) ?? false;
const i = r.settle;
const a = r.assertCurrent;
r.onDelegated?.(n);
r.settle = (e) => t(e, i);
r.assertCurrent = () => {
a?.();
n?.();
};
return true;
},
discardResource: async (e) => {
const t = Array.from(l).find((t) => t.key === e);
if (!t) return await o?.discardResource(e) ?? false;
await settleChannelReadResource(t, false);
l.delete(t);
return true;
}
} });
assertAuthority();
try {
let e;
try {
e = await authorityScope.run(d, t);
} catch (e) {
await i.initiate(assertAuthority);
throw e;
}
await i.initiate(() => {
assertAuthority();
if (o) {
for (const e of l) o.registerResource(e);
l.clear();
}
r?.(e);
u = false;
});
if (l.size > 0) await settleReadResources(l, true);
return e;
} catch (e) {
u = false;
if (l.size > 0) await settleReadResources(l, false);
throw e;
} finally {
u = false;
}
}
//#endregion
export { withChannelReadAuthority as i, captureChannelReadScope as n, settleChannelReadResource as r, captureChannelReadAuthority as t };