openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
152 lines (151 loc) • 6.49 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { t as GATEWAY_WORK_ADMISSION_STATE } from "./gateway-work-admission-state-X3ZM0tLe.mjs";
import { t as createDeferredCore } from "./deferred-D0La5CRk.mjs";
import { o as getAsyncWorkSignal, t as AsyncWorkScope } from "./async-work-scope-BXagjUap.mjs";
import { a as pluginInstanceInvocation } from "./plugin-instance-invocation-Bq_toGDw.mjs";
import { o as getPluginRuntimeGatewayRequestScope, p as resolvePluginReturnPromise } from "./gateway-request-scope-BvvDhs5D.mjs";
import { t as bindPluginSharedStateAdmission } from "./plugin-shared-state-admission-BFOkrCb7.mjs";
import { i as hasRetainedPluginRuntimeCloseError } from "./runtime-close-error-Ak-r7PLO.mjs";
import { i as getCanonicalGatewayContextResolver } from "./gateway-context-binding-BdXZoMU9.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/plugins/legacy-sdk-resource-host.ts
/** Owns resources borrowed by shipped SDK results that have no release method. */
var LegacyPluginSdkResourceHost = class {
constructor() {
this.work = new AsyncWorkScope();
this.claims = /* @__PURE__ */ new Map();
this.providerProjections = /* @__PURE__ */ new WeakMap();
this.pending = /* @__PURE__ */ new Set();
this.failures = [];
}
get scheduler() {
this.assertOpen();
if (!this.gatewayScheduler) throw new Error("Plugin SDK resource host has no Gateway scheduler");
this.gatewayScheduler.signal.throwIfAborted();
return this.gatewayScheduler;
}
bindScheduler(e) {
this.assertOpen();
e.signal.throwIfAborted();
if (this.gatewayScheduler && this.gatewayScheduler !== e) throw new Error("Plugin SDK resource host already belongs to another Gateway scheduler");
this.gatewayScheduler = e;
this.gatewayShutdownSignal = GATEWAY_WORK_ADMISSION_STATE.restartDrainController.signal;
}
assertOpen() {
if (this.closing || this.work.isClosing) throw new Error("Plugin SDK resource host is closed");
}
run(e) {
return hostContext.run(this, e);
}
track(e) {
this.assertOpen();
return this.work.track(() => this.run(e));
}
assertSharedStateAdmission() {
const e = sharedStateWorkContext.getStore();
if (e?.host === this && e.active) return;
if (this.gatewayShutdownSignal?.aborted || this.gatewayScheduler?.signal.aborted || this.closing || this.work.isClosing) throw Object.assign(/* @__PURE__ */ new Error("Gateway is closing; plugin shared-state admission is closed"), { code: "STATE_DATABASE_READ_ADMISSION_INVALIDATED" });
}
/** Join accepted state work before SDK resources and the shared worker retire. */
trackSharedStateWork(e) {
this.assertSharedStateAdmission();
const t = {
host: this,
active: true
};
return this.work.track(async () => {
try {
return await sharedStateWorkContext.run(t, e);
} finally {
t.active = false;
}
});
}
/** Preserve synchronous SDK hooks while joining their asynchronous results and descendants. */
invoke(e) {
if (getAsyncWorkSignal() !== this.work.signal) this.assertOpen();
return this.work.run(() => this.run(() => {
const t = e();
const n = resolvePluginReturnPromise(t);
return n ? this.work.track(() => n) : t;
}));
}
adopt(e, t) {
this.assertOpen();
if (this.claims.has(e)) this.releaseClaim(t);
else this.claims.set(e, t);
}
/** View lookup shares identity; only adopted or temporary claims own disposal. */
getProviderProjection(e, t) {
this.assertOpen();
let n = this.providerProjections.get(e);
if (!n) {
n = t();
this.providerProjections.set(e, n);
}
return n;
}
forgetProviderProjection(e, t) {
if (this.providerProjections.get(e) === t) this.providerProjections.delete(e);
}
/** Projection failures still own their asynchronous release until it settles. */
releaseClaim(e) {
const t = createDeferredCore();
const n = t.promise.catch((e) => {
this.failures.push(e);
}).finally(() => {
this.pending.delete(n);
});
this.pending.add(n);
t.resolve(AsyncWorkScope.runWhenAllIdle(() => [this.work], () => e.release()));
}
/** Fence new SDK work and join its tails before prepared resources can retire. */
async drainWork() {
await this.work.drain();
await this.drainPendingReleases();
if (this.failures.some(hasRetainedPluginRuntimeCloseError)) throw new AggregateError(this.failures, "Plugin SDK resources could not all be disposed");
}
async drainPendingReleases() {
while (this.pending.size > 0) await Promise.all(this.pending);
}
close() {
if (!this.closing) this.closing = Promise.resolve().then(async () => {
await this.drainWork();
const e = [...this.claims.values()];
this.claims.clear();
for (const t of e) this.releaseClaim(t);
await this.drainPendingReleases();
if (this.failures.length > 0) throw new AggregateError(this.failures, "Plugin SDK resources could not all be disposed");
});
return this.closing;
}
};
const { hostContext, gatewayHosts, sharedStateWorkContext } = resolveGlobalSingleton(Symbol.for("openclaw.legacyPluginSdkResourceHosts"), () => ({
hostContext: new AsyncLocalStorage(),
sharedStateWorkContext: new AsyncLocalStorage(),
gatewayHosts: /* @__PURE__ */ new WeakMap()
}));
const STANDALONE_HOST_KEY = Symbol.for("openclaw.legacyPluginSdkStandaloneResourceHost");
/** Associate exact host resolvers without calling them after their authority closes. */
function bindLegacyPluginSdkResourceHost(e, t) {
gatewayHosts.set(e, t);
}
function getBoundLegacyPluginSdkResourceHost() {
const e = getPluginRuntimeGatewayRequestScope();
const t = e?.resolveGatewayContext ?? e?.context?.resolveGatewayContext;
if (t) {
const e = getCanonicalGatewayContextResolver(t);
const n = e ? gatewayHosts.get(e) : void 0;
if (!n) throw new Error("Gateway SDK resource host is not bound");
return n;
}
const n = hostContext.getStore();
return n === globalThis[STANDALONE_HOST_KEY] ? void 0 : n;
}
/** Standalone callers of the shipped bare-result SDK retain their process lifetime. */
function getLegacyPluginSdkResourceHost() {
return getBoundLegacyPluginSdkResourceHost() ?? resolveGlobalSingleton(STANDALONE_HOST_KEY, () => new LegacyPluginSdkResourceHost());
}
bindPluginSharedStateAdmission(() => pluginInstanceInvocation.getStore() ? getBoundLegacyPluginSdkResourceHost() : void 0);
//#endregion
export { getLegacyPluginSdkResourceHost as i, bindLegacyPluginSdkResourceHost as n, getBoundLegacyPluginSdkResourceHost as r, LegacyPluginSdkResourceHost as t };