openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
147 lines (146 loc) • 5.2 kB
JavaScript
import { o as throwSettledAggregate } from "./error-graph-internal-DdjdM90N.mjs";
import { t as AsyncWorkScope } from "./async-work-scope-BXagjUap.mjs";
import { i as getOrCreatePromise } from "./lazy-promise-DiCvjz9w.mjs";
import { l as getPluginValueInstance } from "./plugin-instance-scope-oezVdCr2.mjs";
import { a as capturePluginLifecycleAuthority, f as getPluginRegistryLifetime } from "./registry-lifecycle-CJjWnl-y.mjs";
import { i as collectRegistryInvocationInstances, n as getPluginRegistryInspectionResources, r as PluginInvocationScope } from "./registry-inspection-resources-CDO3vUtM.mjs";
import { E as resolvePluginRegistryLoadCacheKey, T as isPluginRegistryLoadInFlight, t as acquirePluginRegistryForInspection } from "./loader-runtime-load-Ba16vW6K.mjs";
import "./loader--j41eir2.mjs";
import { n as preparePluginCapabilityProviderLookup, o as acquireBundledCapabilityRuntimeRegistry, r as preparePluginCapabilityProviderResolution } from "./capability-provider-runtime-DX1ZFq8m.mjs";
//#region src/plugins/capability-provider-acquisition.ts
/** Retains owned registrations while external hosts keep their own custody. */
async function acquirePluginCapabilityProviders(e) {
const t = new AsyncWorkScope();
const n = [];
const r = /* @__PURE__ */ new Map();
const i = /* @__PURE__ */ new Map();
const a = /* @__PURE__ */ new Map();
const captureAuthority = (e) => {
if (!a.has(e)) a.set(e, capturePluginLifecycleAuthority(e, void 0, { scopedRuntime: true }));
};
const dispose = () => {
for (const e of i.values()) e?.forEach((e) => e.release());
return Promise.allSettled(n.map(async (e) => await e())).then((e) => {
throwSettledAggregate(e, "Capability registration cleanup failed");
});
};
const retain = (e, t) => {
if (!e) return;
const r = getPluginRegistryInspectionResources(e);
if (!i.has(e)) {
captureAuthority(e);
const t = r?.retain().release ?? getPluginRegistryLifetime(e)?.retain();
if (t) n.push(t);
i.set(e, t ? /* @__PURE__ */ new Map() : void 0);
}
const a = i.get(e);
if (!a) return;
return (n) => {
const i = t === void 0 ? void 0 : getPluginValueInstance(n);
if (t !== void 0 && !i) return n;
const o = i ?? e;
let s = a.get(o);
if (!s) {
s = r ? r.createInvocationScope(e, i && [i]) : new PluginInvocationScope(e, i ? [i] : collectRegistryInvocationInstances(e), { retained: true });
a.set(o, s);
}
return s.wrap(n);
};
};
let o;
const release = () => o ??= Promise.resolve().then(async () => {
t.beginClose();
try {
await t.runWhenIdle(dispose);
} finally {
await t.drain();
}
});
const run = (e) => o ? Promise.reject(/* @__PURE__ */ new Error("Capability provider acquisition has been released")) : t.track(e);
const execute = async (e) => {
let t = [];
if (e.load) {
const i = e.prepareLoad();
let a = i.loadedRegistry;
if (!a) {
const e = i.resolveLoadOptions();
if (!isPluginRegistryLoadInFlight(e)) {
const t = resolvePluginRegistryLoadCacheKey(e);
a = await getOrCreatePromise(r, t, () => acquirePluginRegistryForInspection(e).then((e) => {
n.push(e.release);
captureAuthority(e.registry);
return e.registry;
}));
}
}
const o = i.fallback(a);
t = o.entries;
if (o.pluginIds.length > 0) {
const r = await acquireBundledCapabilityRuntimeRegistry({
...e.load.loadOptions,
pluginIds: o.pluginIds
});
n.push(r.release);
captureAuthority(r.registry);
t = i.merge(t, r.registry);
}
}
return e.resolve(t);
};
const resolveProviders = (t) => run(() => execute(preparePluginCapabilityProviderResolution({
...t,
key: e.key
}, retain)));
const resolveProvider = (t) => run(() => execute(preparePluginCapabilityProviderLookup({
...t,
key: e.key
}, (e) => retain(e, t.providerId))));
try {
return {
providers: e.providerId === void 0 ? await resolveProviders(e) : [await resolveProvider({
providerId: e.providerId,
cfg: e.cfg
})].filter((e) => e !== void 0),
run,
resolveProviders,
resolveProvider,
assertOpen: () => {
if (o || [...a.values()].some((e) => !e?.())) throw new Error("The provider setup changed while preparing this request. Retry with the current provider setup.");
},
release
};
} catch (e) {
return await finishCapabilityOperation({
ok: false,
error: e
}, release);
}
}
async function finishCapabilityOperation(e, t) {
try {
await t();
} catch (t) {
throw e.ok ? t : new AggregateError([e.error, t], "Capability operation and registration cleanup failed", { cause: e.error });
}
if (!e.ok) throw e.error;
return e.value;
}
/** Keeps callback-shaped operations on the same acquisition and actual-work owner. */
async function withAcquiredPluginCapabilityProviders(e, t) {
const n = await acquirePluginCapabilityProviders(e);
let r;
try {
r = {
ok: true,
value: await n.run(() => t(n.providers, n))
};
} catch (e) {
r = {
ok: false,
error: e
};
}
return await finishCapabilityOperation(r, n.release);
}
//#endregion
export { finishCapabilityOperation as n, withAcquiredPluginCapabilityProviders as r, acquirePluginCapabilityProviders as t };