openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
75 lines (74 loc) • 2.71 kB
JavaScript
import { W as resolveGlobalSingleton } from "./path-guards-quCEQx1i.mjs";
import { AsyncLocalStorage, AsyncResource } from "node:async_hooks";
//#region src/shared/detached-async-context.ts
const asyncHooks = typeof process === "undefined" ? void 0 : process.getBuiltinModule?.("node:async_hooks");
const detachedAsyncContext = asyncHooks ? resolveGlobalSingleton(Symbol.for("openclaw.detachedAsyncContext"), () => new asyncHooks.AsyncResource("openclaw.detached-async-context")) : void 0;
/** Runs under the context-free async root initialized before managed work can begin. */
function runInDetachedAsyncContext(e) {
return detachedAsyncContext ? detachedAsyncContext.runInAsyncScope(e) : e();
}
//#endregion
//#region src/shared/async-store.ts
function enterAsyncStore(e, t) {
this.enterWith(e);
return t();
}
/** Enter once; the resource restores the caller's frame without copying it again. */
function runWithAsyncStore(e, t, n) {
if (e.getStore() === t) return n();
return new AsyncResource("OpenClawAsyncStore").runInAsyncScope(enterAsyncStore, e, t, n);
}
//#endregion
//#region src/plugins/plugin-instance-invocation.ts
var InvocationFrame = class InvocationFrame {
constructor(e, t = e.invocation) {
this.retentionOwner = e.retentionOwner;
this.invocation = t;
this.metadataScope = e.metadataScope;
this.cacheScope = e.cacheScope;
this.sourceCaptureStorage = e.sourceCaptureStorage;
}
get runtimePluginId() {}
withScopes(e) {
return new InvocationFrame(e);
}
};
/** Copy core scopes through the current owner so runtime-only fields survive. */
function createPluginExecutionFrame(e, t) {
return t ? t.withScopes(e) : new InvocationFrame(e);
}
const pluginExecutionContext = resolveGlobalSingleton(Symbol.for("openclaw.pluginInstanceInvocation"), () => {
const e = new AsyncLocalStorage();
return {
invocation: {
getStore() {
return e.getStore()?.invocation;
},
run(t, n) {
const r = e.getStore();
return r?.invocation === t ? n() : runWithAsyncStore(e, createPluginExecutionFrame({
...r,
invocation: t
}, r), n);
},
exit(t) {
const n = e.getStore();
return n?.invocation ? runWithAsyncStore(e, n.withScopes({
...n,
invocation: void 0
}), t) : t();
}
},
getFrame() {
return e.getStore();
},
runFrame(t, n) {
return runWithAsyncStore(e, t, n);
}
};
});
const pluginInstanceInvocation = pluginExecutionContext.invocation;
const getPluginExecutionFrame = pluginExecutionContext.getFrame;
const runWithPluginExecutionFrame = pluginExecutionContext.runFrame;
//#endregion
export { runInDetachedAsyncContext as i, getPluginExecutionFrame as n, runWithAsyncStore as r, InvocationFrame as t };