openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
190 lines (189 loc) • 8.45 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { i as getPluginExecutionFrame, o as runWithPluginExecutionFrame } from "./plugin-instance-invocation-Bq_toGDw.mjs";
import { r as withAgentDatabasePreparationContext } from "./agent-database-preparation-context-CkVoUMTY.mjs";
import { n as getPluginRegistryState } from "./runtime-state-DVrhXeqI.mjs";
import { n as getPluginRuntimeExecutionFrame, t as PluginRuntimeExecutionFrame } from "./execution-frame-D6-zKKdm.mjs";
import { n as getPluginRuntimeLoadContextState } from "./load-context-state-Cj2HcQlf.mjs";
import "./gateway-context-binding-BdXZoMU9.mjs";
import { types } from "node:util";
//#region src/plugins/plugin-return-value.ts
const promiseThen = Object.getOwnPropertyDescriptor(Promise.prototype, "then");
const promiseSpecies = Object.getOwnPropertyDescriptor(Promise, Symbol.species);
/** Preserve foreign then/species results while identifying native host continuations. */
function mapPluginReturnPromise(e, t, n) {
const r = e.then;
const i = types.isPromise(e) && Object.getPrototypeOf(e) === Promise.prototype ? Object.getOwnPropertyDescriptor(e, "constructor") ?? Object.getOwnPropertyDescriptor(Promise.prototype, "constructor") : void 0;
const a = r === promiseThen?.value && i?.value === Promise && Object.getOwnPropertyDescriptor(Promise, Symbol.species)?.get === promiseSpecies?.get;
return {
value: Reflect.apply(r, e, n ? [t, n] : [t]),
host: a
};
}
/** Capture a returned then method once without turning synchronous values into async work. */
function resolvePluginReturnPromise(e) {
if (types.isPromise(e)) return Promise.resolve(e);
if (e === null || typeof e !== "object" && typeof e !== "function") return;
let t;
try {
t = Reflect.get(e, "then");
} catch (e) {
return Promise.reject(e);
}
if (typeof t !== "function") return;
return Promise.resolve({ then(n, r) {
Reflect.apply(t, e, [n, r]);
} });
}
//#endregion
//#region src/plugins/runtime/gateway-request-scope.ts
function runWithPluginGatewayScope(e, t, n) {
const r = getPluginExecutionFrame();
const i = n ?? r?.invocation;
const a = getPluginRuntimeExecutionFrame(r);
if (a?.gatewayScope === e && a.invocation === i) return t();
return runWithPluginExecutionFrame(new PluginRuntimeExecutionFrame(r ?? {}, e, a?.generationRegistry, i), t);
}
const isNotWebchatConnect = () => false;
function settleRegistryScope(e, t) {
return mapPluginReturnPromise(e, (e) => {
t.length = 0;
return e;
}, (e) => {
t.length = 0;
throw e;
}).value;
}
const ExpiredPluginRegistryScopeError = resolveGlobalSingleton(Symbol.for("openclaw.expiredPluginRegistryScopeError"), () => class extends Error {
constructor() {
super("Plugin registry scope is no longer available");
}
});
const borrowedRegistryProperty = {
enumerable: true,
configurable: true
};
function bindBorrowedRegistry(e, t, n = false) {
let r = n;
let i = t && new WeakRef(t);
borrowedRegistryProperty.get = () => {
const e = i?.deref();
if (r || i && !e) throw new ExpiredPluginRegistryScopeError();
return e;
};
borrowedRegistryProperty.set = (e) => {
i = e && new WeakRef(e);
r = false;
};
Object.defineProperty(e, "pluginRegistry", borrowedRegistryProperty);
}
function copyGatewayScope(e, t = true) {
const n = { isWebchatConnect: isNotWebchatConnect };
if (!e) return n;
for (const t of Object.keys(e)) if (t !== "pluginRegistry") Reflect.set(n, t, Reflect.get(e, t));
for (const t of Object.getOwnPropertySymbols(e)) if (Object.prototype.propertyIsEnumerable.call(e, t)) Reflect.set(n, t, Reflect.get(e, t));
if (t && Object.prototype.propertyIsEnumerable.call(e, "pluginRegistry")) try {
bindBorrowedRegistry(n, e.pluginRegistry);
} catch (e) {
if (!(e instanceof ExpiredPluginRegistryScopeError)) throw e;
bindBorrowedRegistry(n, void 0, true);
}
return n;
}
/** Carry only closure-bound node authorities into a nested request scope. */
function getPluginRuntimeGatewayNodeAuthorities() {
const e = getPluginRuntimeGatewayRequestScope();
return {
invokeWithSessionNodeAuthority: e?.invokeWithSessionNodeAuthority,
nodePlacementGrantAuthority: e?.nodePlacementGrantAuthority
};
}
/**
* Runs plugin gateway handlers with request-scoped context that runtime helpers can read.
*/
function withPluginRuntimeGatewayRequestScope(e, t) {
return runWithPluginGatewayScope(e, t);
}
/** Runs detached work with its captured Gateway binding, including an explicitly unbound owner. */
function withPluginRuntimeGatewayContextResolver(e, t, n) {
const r = n?.inheritRequestScope === false ? void 0 : getPluginRuntimeGatewayRequestScope();
const i = copyGatewayScope(r);
i.isWebchatConnect = r?.isWebchatConnect ?? isNotWebchatConnect;
i.resolveGatewayContext = e;
delete i.context;
return withAgentDatabasePreparationContext(resolveAgentDatabasePreparation(e), () => runWithPluginGatewayScope(i, t));
}
const resolveUnboundAgentDatabasePreparation = () => void 0;
const agentDatabasePreparationResolvers = /* @__PURE__ */ new WeakMap();
function resolveAgentDatabasePreparation(e) {
if (!e) return resolveUnboundAgentDatabasePreparation;
let resolve = agentDatabasePreparationResolvers.get(e);
if (!resolve) {
resolve = () => e()?.agentDatabaseStartup;
agentDatabasePreparationResolvers.set(e, resolve);
}
return resolve;
}
/** Runs work against an owned registry handle while preserving any gateway request facts. */
function withPluginRuntimeRegistryScope(e, t, n) {
if (!e) return t();
return runWithPluginGatewayScope(createRegistryScope(e, getPluginRuntimeGatewayRequestScope(), n), () => {
const n = t();
const r = resolvePluginReturnPromise(n);
return r ? settleRegistryScope(r, [e]) : n;
});
}
function createRegistryScope(e, t, n) {
const r = copyGatewayScope(t, false);
let i;
if (n === void 0 && t) try {
if (t.pluginRegistry === e) i = t.declaredProviderOwners;
} catch (e) {
if (!(e instanceof ExpiredPluginRegistryScopeError)) throw e;
}
bindBorrowedRegistry(r, e);
r.declaredProviderOwners = n ?? i ?? getPluginRuntimeLoadContextState(e)?.declaredProviderOwners;
return r;
}
function applyPluginScope(e, t) {
e.pluginId = t.pluginId;
if (t.pluginSource !== void 0) e.pluginSource = t.pluginSource;
else delete e.pluginSource;
if (t.pluginOrigin !== void 0) e.pluginOrigin = t.pluginOrigin;
else delete e.pluginOrigin;
if (t.pluginTrustedOfficialInstall !== void 0) e.pluginTrustedOfficialInstall = t.pluginTrustedOfficialInstall;
else delete e.pluginTrustedOfficialInstall;
}
/**
* Runs work under the current gateway request scope while attaching plugin identity.
*/
function withPluginRuntimePluginScope(e, t, n, r) {
const i = getPluginRuntimeGatewayRequestScope();
const a = n ? createRegistryScope(n, i) : copyGatewayScope(i);
applyPluginScope(a, e);
return runWithPluginGatewayScope(a, t, r);
}
/** Drops only generation selection; authenticated Gateway caller and authority stay attached. */
function runOutsidePluginRuntimeRegistryScope(e) {
const t = getPluginRuntimeGatewayRequestScope();
if (!t) return e();
const n = copyGatewayScope(t, false);
n.pluginRegistry = void 0;
n.declaredProviderOwners = void 0;
return runWithPluginGatewayScope(n, e);
}
function getPluginRuntimeGatewayRequestScope() {
return getPluginRuntimeExecutionFrame()?.gatewayScope;
}
/** Reads registration/request/active registry precedence without initializing a cold runtime. */
function getPluginRegistryForContext() {
const e = getPluginRegistryState();
return e?.registrationContext?.registry ?? getPluginRuntimeGatewayRequestScope()?.pluginRegistry ?? e?.activeRegistry ?? null;
}
/** Live request context for trusted built-in tools that need direct runtime state. */
function getInProcessGatewayRequestContext(e) {
if (e) return e();
const t = getPluginRuntimeGatewayRequestScope();
return t?.resolveGatewayContext ? t.resolveGatewayContext() : t?.context;
}
//#endregion
export { getPluginRuntimeGatewayNodeAuthorities as a, withPluginRuntimeGatewayContextResolver as c, withPluginRuntimeRegistryScope as d, mapPluginReturnPromise as f, getPluginRegistryForContext as i, withPluginRuntimeGatewayRequestScope as l, createRegistryScope as n, getPluginRuntimeGatewayRequestScope as o, resolvePluginReturnPromise as p, getInProcessGatewayRequestContext as r, runOutsidePluginRuntimeRegistryScope as s, ExpiredPluginRegistryScopeError as t, withPluginRuntimePluginScope as u };