openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
148 lines (147 loc) • 6.57 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { a as getPluginCache, d as getProcessPluginCache, h as preparePluginCacheFact, s as getPluginCacheRetirementSignal } from "./plugin-cache-B_eRuqTM.mjs";
import { n as isArtifactPreservingStateRead, r as withArtifactPreservingStateReads } from "./artifact-preserving-state-reads-BfhmBr-V.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import { a as getActiveOpenClawStateDatabaseReadSnapshotAsync, i as getActiveOpenClawStateDatabaseReadSnapshot, n as executeExistingOpenClawStateRead } from "./openclaw-state-db-readonly-B6YHb1oF.mjs";
import { t as readConfigMachineState } from "./config-machine-state-DzLVE1Wy.mjs";
import { r as resolveActivePluginInstallRoots, t as hasActivePluginInstallRoots } from "./install-root-context-CeM6pu6m.mjs";
import { n as registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle-C9EqhiS5.mjs";
//#region src/plugins/plugin-metadata-state-worker.ts
async function readPluginMetadataState(e, t, n) {
const r = "stateKeys" in e ? [...e.stateKeys] : [e.selector === "installed-index" ? "plugins.installedIndex" : "plugins.bundledDiscovery"];
const read = () => executeExistingOpenClawStateRead(t, {
type: "plugins.metadata.read",
input: { stateKeys: r }
}, { snapshotPreparation: "awaited" });
const i = await (n ? withArtifactPreservingStateReads(read) : read());
if (i === void 0) return [];
return i.rows;
}
/** Read raw metadata through the shared reader, including retained snapshot scopes. */
async function readPluginMetadataStateRow(e, t, n = false) {
const [r] = await readPluginMetadataState({ selector: e }, t, n);
return r ? { value_json: r.value_json } : void 0;
}
/** Policy and inventory preparation share one worker request and SQLite snapshot. */
function readPluginMetadataStateRows(e, t, n = false) {
return readPluginMetadataState({ stateKeys: e }, t, n);
}
//#endregion
//#region src/plugins/bundled-discovery-state.ts
function parseBundledDiscoveryMode(e) {
return e === "compat" || e === "allowlist" ? e : void 0;
}
function resolveBundledDiscoveryOptions(e) {
return e.path || e.database || !hasActivePluginInstallRoots() ? e : {
...e,
env: {
...e.env ?? process.env,
OPENCLAW_STATE_DIR: resolveActivePluginInstallRoots(e.env).stateDir
}
};
}
function readBundledDiscoveryMode(e = {}, t = {}) {
const n = resolveBundledDiscoveryOptions(e);
return parseBundledDiscoveryMode(readConfigMachineState("plugins.bundledDiscovery", n, t));
}
const discoveryState = resolveGlobalSingleton(Symbol.for("openclaw.bundledDiscoveryMode"), () => ({ snapshotModes: /* @__PURE__ */ new WeakMap() }));
registerPluginMetadataProcessMemoLifecycleClear(() => {
clearBundledDiscoveryModeMemo();
});
function resolveBundledDiscoveryMemoKey(e) {
const t = hasActivePluginInstallRoots() ? {
...e,
OPENCLAW_STATE_DIR: resolveActivePluginInstallRoots(e).stateDir
} : e;
return resolveOpenClawStateSqlitePath(t);
}
/**
* Callers loading a registry with an explicit env pass it so the mode comes
* from that env's state root; omitting it reads the process root. Pinned
* install roots win over both, matching readBundledDiscoveryMode.
*/
function readBundledDiscoveryModeMemoized(e = process.env, t = {}, n) {
const r = e === process.env ? {} : { env: e };
const i = t.artifactPreservingReadOnly || isArtifactPreservingStateRead() ? getActiveOpenClawStateDatabaseReadSnapshot(resolveBundledDiscoveryOptions(r)) : void 0;
if (i || t.artifactPreservingReadOnly) {
const e = i && discoveryState.snapshotModes.get(i);
if (e) return e.value;
const n = readBundledDiscoveryMode(r, t);
if (i) discoveryState.snapshotModes.set(i, { value: n });
return n;
}
const a = resolveBundledDiscoveryMemoKey(e);
if (discoveryState.memoized?.key !== a) {
const t = getPluginCache();
const r = t.preparedBundledDiscoveryModes.get(a);
if (r && "value" in r) {
getPluginCacheRetirementSignal(t).throwIfAborted();
discoveryState.memoized = {
key: a,
value: r.value.value
};
} else discoveryState.memoized = {
key: a,
value: n ? parseBundledDiscoveryMode(n(a)) : readBundledDiscoveryMode(e === process.env ? {} : { env: e })
};
}
return discoveryState.memoized.value;
}
/** Prepare the same machine-owned fact for synchronous metadata derivation. */
async function prepareBundledDiscoveryMode(e = process.env, t) {
const n = getPluginCache();
const r = resolveBundledDiscoveryMemoKey(e);
const i = resolveBundledDiscoveryOptions({ env: e });
const a = isArtifactPreservingStateRead() ? await getActiveOpenClawStateDatabaseReadSnapshotAsync(i) : void 0;
if (a) {
const e = discoveryState.snapshotModes.get(a);
let o = e?.value;
if (!e) {
const e = t ? await t(r) : await readPluginMetadataStateRow("bundled-discovery", i);
o = parseBundledDiscoveryMode(e ? JSON.parse(e.value_json) : void 0);
}
const activate = () => {
discoveryState.snapshotModes.set(a, { value: o });
if (n.kind === "operation") n.preparedBundledDiscoveryModes.set(r, { value: {
value: o,
generation: n
} });
};
activate();
return activate;
}
const o = n.preparedBundledDiscoveryModes;
const s = await preparePluginCacheFact(n, o, r, async () => {
let i;
if (discoveryState.memoized?.key === r) i = discoveryState.memoized.value;
else {
const n = t ? await t(r) : await readPluginMetadataStateRow("bundled-discovery", resolveBundledDiscoveryOptions({ env: e }));
i = parseBundledDiscoveryMode(n ? JSON.parse(n.value_json) : void 0);
}
return {
value: i,
generation: n
};
});
const activate = () => {
s.assertCurrent();
discoveryState.memoized = {
key: r,
value: s.value.value
};
};
activate();
return activate;
}
/**
* Clears the memo after a machine-state write so same-process readers observe
* the new mode. Without this, doctor's migration could cache the pre-migration
* absent mode and rebuild plugin indexes against stale strict-gate decisions.
*/
function clearBundledDiscoveryModeMemo() {
discoveryState.memoized = void 0;
discoveryState.snapshotModes = /* @__PURE__ */ new WeakMap();
for (const e of /* @__PURE__ */ new Set([getPluginCache(), getProcessPluginCache()])) e.preparedBundledDiscoveryModes.clear();
}
//#endregion
export { readPluginMetadataStateRow as a, readBundledDiscoveryModeMemoized as i, prepareBundledDiscoveryMode as n, readPluginMetadataStateRows as o, readBundledDiscoveryMode as r, clearBundledDiscoveryModeMemo as t };