openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
246 lines (245 loc) • 11.5 kB
JavaScript
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs";
import { t as pruneMapToMaxSize } from "./map-size-BBr20tbc.mjs";
import "./utils-C8YECn2Q.mjs";
import { r as resolveInstalledPluginIndexInstallOwner, t as isInstalledPluginIndexInstallOwnerAmbiguous } from "./installed-plugin-index-install-owner-DoEpptXw.mjs";
import { n as registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle-C9EqhiS5.mjs";
import { f as resolveEffectiveEnableState, u as normalizePluginsConfig } from "./config-state-B1JlHgba.mjs";
import { r as loadInstalledPluginIndexInstallRecords } from "./installed-plugin-index-record-reader-B-7uGQZ9.mjs";
import { i as isTrustedOfficialPluginInstallRecord } from "./official-external-install-records-DHqBi8wk.mjs";
import { p as resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot-By9ySblt.mjs";
import { n as resolvePluginControlPlaneWorkspace } from "./control-plane-workspace-OscAdfLv.mjs";
import { t as createInstalledPluginOwnershipResolver } from "./installed-plugin-package-ownership-BAh5IZ9-.mjs";
import { t as ManagedPluginLifecycleError } from "./management-lifecycle-error-COJYzrtT.mjs";
import { r as assertConfigWriteAllowedInCurrentMode } from "./config-write-guard-CxNxfJ_I.mjs";
import { s as withPluginLifecycleLeaseAsync } from "./plugin-lifecycle-lease-CXjHGDEI.mjs";
import { a as writePersistedInstalledPluginIndexInstallRecordsWithLease } from "./installed-plugin-index-records-MNCNSlg8.mjs";
import { c as resolvePluginInstallRecordIntegrity, i as diffDeclaredSurfaceWidening, r as computeDeclaredSurfaceHash, s as resolveAcceptedSurfaceCurrent, t as buildPluginCapabilityConsentReview, u as resolvePluginPackageDeclaredSurface } from "./capability-summary-Dfhfz0nD.mjs";
import { n as resolvePluginArtifactDeclaredSurface, t as inspectPluginCapabilityArtifact } from "./capability-artifact-t95a8xNx.mjs";
import path from "node:path";
//#region src/plugins/capability-consent.ts
/** Preserve caller control-flow failures across installers that normalize exceptions. */
function capturePluginCapabilityConsentHandlerErrors(e) {
let t;
return {
onCapabilityConsent: e ? async (n) => {
try {
return await e(n);
} catch (e) {
t = { error: e };
throw e;
}
} : void 0,
rethrowCallbackError: () => {
if (t) throw t.error;
}
};
}
const pendingPluginCapabilityReviews = /* @__PURE__ */ new Map();
registerPluginMetadataProcessMemoLifecycleClear(() => {
pendingPluginCapabilityReviews.clear();
});
function resolvePendingPluginCapabilityReview(e) {
return pendingPluginCapabilityReviews.get(e);
}
function acceptManagedPluginDeclaredSurface(e, t) {
const n = resolvePluginInstallRecordIntegrity(e)?.integrity;
const r = {
...e,
acceptedSurface: t,
acceptedSurfaceHash: computeDeclaredSurfaceHash(t),
acceptedSurfaceAt: (/* @__PURE__ */ new Date()).toISOString()
};
delete r.acceptedSurfaceIntegrity;
if (n) r.acceptedSurfaceIntegrity = n;
return r;
}
function throwManagedPluginCapabilityConsentRequired(e, t = "Rerun the openclaw plugins install, enable, update, or reload command with --accept-capabilities after reviewing the plugin.") {
pendingPluginCapabilityReviews.delete(e.pluginId);
pendingPluginCapabilityReviews.set(e.pluginId, e);
pruneMapToMaxSize(pendingPluginCapabilityReviews, 32);
throw new ManagedPluginLifecycleError(`Plugin "${e.pluginId}" requires capability consent. ${t}`, { capabilityConsent: {
pluginId: e.pluginId,
reviewToken: e.reviewToken,
...e.widened ? { widened: e.widened } : {},
...e.acceptedAt ? { acceptedAt: e.acceptedAt } : {}
} });
}
/** Enforce and durably acknowledge consent before an installed plugin is enabled. */
async function resolvePluginCapabilityConsent(e) {
const t = e.env ?? process.env;
return await withPluginLifecycleLeaseAsync({ env: t }, async (n) => {
const r = resolvePluginControlPlaneWorkspace({
config: e.config,
env: t
});
const i = e.metadata ?? resolvePluginMetadataSnapshot({
allowCurrent: false,
config: e.config,
env: t,
...r.workspaceDir !== void 0 ? { workspaceDir: r.workspaceDir } : {}
});
const a = i.normalizePluginId(e.pluginId);
const o = i.index.plugins.find((e) => e.pluginId === a);
if (!o || o.origin === "bundled") return;
if (!resolveInstalledPluginIndexInstallOwner(o) && !isInstalledPluginIndexInstallOwnerAmbiguous(o) && !Object.hasOwn(i.index.installRecords, a)) return;
const s = createInstalledPluginOwnershipResolver(i.index, t).resolvePackage(a);
if (!s.ok) throw new ManagedPluginLifecycleError(s.error);
const { installOwner: c, installRecord: l } = s.value;
const u = i.byPluginId.get(a);
if (!u) throw new ManagedPluginLifecycleError(`Plugin "${a}" has no installed manifest.`);
if (u.trustedOfficialInstall && !e.recordOfficialCapabilities) {
pendingPluginCapabilityReviews.delete(a);
return;
}
const d = resolvePluginPackageDeclaredSurface(s.value, i.byPluginId);
if (!d) throw new ManagedPluginLifecycleError(`Plugin package "${c}" has incomplete manifest metadata.`);
const f = buildPluginCapabilityConsentReview({
pluginId: a,
manifest: u,
record: l,
config: e.config,
declared: d
});
if (resolveAcceptedSurfaceCurrent(l, d)) {
pendingPluginCapabilityReviews.delete(a);
return;
}
assertConfigWriteAllowedInCurrentMode({ env: t });
const p = e.acknowledge ?? await e.onCapabilityConsent?.(f);
if (!p) throwManagedPluginCapabilityConsentRequired(f);
await e.beforePersistentEffect?.();
const m = await loadInstalledPluginIndexInstallRecords({ env: t });
const h = m[c];
if (!h?.installPath) throw new ManagedPluginLifecycleError(`Plugin "${a}" no longer has a verifiable installed package record.`);
const g = resolvePluginArtifactDeclaredSurface(h.installPath, t, { config: e.config });
const _ = buildPluginCapabilityConsentReview({
pluginId: a,
manifest: u,
record: h,
config: e.config,
declared: g
});
if (p.reviewToken !== _.reviewToken) throwManagedPluginCapabilityConsentRequired(_);
e.beforePersistentApply?.();
await writePersistedInstalledPluginIndexInstallRecordsWithLease({
...m,
[c]: acceptManagedPluginDeclaredSurface(h, g)
}, {
env: t,
config: e.config,
lease: n,
assertCurrent: e.beforePersistentApply
});
pendingPluginCapabilityReviews.delete(a);
});
}
/** Bind artifact consent to verified staged bytes and carry acceptance into the record commit. */
function createManagedPluginArtifactConsentHandler(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of Object.entries(e.previousRecords ?? {})) if (r.installPath) try {
t.set(n, resolvePluginArtifactDeclaredSurface(r.installPath, e.env, { config: e.config }));
} catch {}
const n = /* @__PURE__ */ new Map();
return {
onBeforePluginArtifactCommit: async (r) => {
n.clear();
const i = Object.entries(e.previousRecords ?? {}).filter(([t, n]) => t === r.pluginId || t === e.previousPluginOwners?.get(r.pluginId) || Boolean(r.currentArtifactDir && n.installPath && path.resolve(resolveUserPath(r.currentArtifactDir, e.env)) === path.resolve(resolveUserPath(n.installPath, e.env))));
if (i.length > 1) throw new ManagedPluginLifecycleError(`Plugin "${r.pluginId}" matches multiple installed package owners.`);
const [a, o] = i[0] ?? [];
const s = a ? t.get(a) : void 0;
const { config: c, env: l } = e;
const u = r.pluginId;
const d = r.stagedArtifactDir;
const f = o?.installPath ?? r.currentArtifactDir;
const p = {
source: e.source,
installPath: r.stagedArtifactDir,
...e.spec ? { spec: e.spec } : {},
...e.expectedIntegrity ? { integrity: e.expectedIntegrity } : {}
};
const m = r.sourceRecord;
const { acknowledgeCapabilities: h, onCapabilityConsent: g, requireCapabilityConsent: _, recordOfficialCapabilities: v } = e;
const y = e.beforePersistentEffect ? () => e.beforePersistentEffect?.(r) : void 0;
const b = r.mode;
const x = !e.updatingPluginIds?.length || e.updatingPluginIds.some((t) => resolveEffectiveEnableState({
id: t,
origin: "global",
config: normalizePluginsConfig(e.config.plugins),
rootConfig: e.config
}).enabled);
const S = {
config: c,
currentArtifactDir: f
};
const { declared: C, manifest: w } = inspectPluginCapabilityArtifact(d, l, S);
const isOfficialArtifact = (e) => m !== void 0 && isTrustedOfficialPluginInstallRecord({
pluginId: u,
packageName: e?.packageName,
record: m
});
const T = isOfficialArtifact(w);
const reviewArtifact = (e, t) => buildPluginCapabilityConsentReview({
pluginId: u,
manifest: t ?? { name: u },
record: m ?? p,
config: c,
declared: e,
...s ? { previousDeclared: s } : {}
});
const E = reviewArtifact(C, w);
let D = false;
if (b === "update" && s) {
const { hasWidening: e } = diffDeclaredSurfaceWidening(s, C);
const t = o !== void 0 && resolveAcceptedSurfaceCurrent(o, s) && resolvePluginInstallRecordIntegrity(o) !== void 0;
D = !e && t;
}
const O = _ ? await g?.(E) : T && !v || !x || D ? { reviewToken: E.reviewToken } : h ?? await g?.(E);
if (O) await y?.();
const { declared: k, manifest: A, contextEngineIdsByPlugin: j } = inspectPluginCapabilityArtifact(d, l, S);
const M = computeDeclaredSurfaceHash(k);
if (!O || O.reviewToken !== M || T && !isOfficialArtifact(A)) throwManagedPluginCapabilityConsentRequired(M === E.reviewToken ? E : reviewArtifact(k, A), `The plugin was not ${f ? "updated" : "installed"}. Re-run the same "openclaw plugins install" or "openclaw plugins update" command with --accept-capabilities, keeping its source and other options. For Doctor or setup, complete the plugin command first, then retry Doctor or setup.`);
pendingPluginCapabilityReviews.delete(u);
const N = (!T || v) && (x || D) ? k : void 0;
n.set(r.pluginId, {
declared: N,
contextEngineIdsByPlugin: j
});
},
applyAcceptedSurface: (e, t) => {
const r = n.get(e);
if (!r) throw new ManagedPluginLifecycleError(`Plugin "${e}" did not expose its verified artifact for capability review.`);
const i = { ...t };
delete i.contextEngineIdsByPlugin;
if (Object.keys(r.contextEngineIdsByPlugin).length > 0) i.contextEngineIdsByPlugin = structuredClone(r.contextEngineIdsByPlugin);
return r.declared ? acceptManagedPluginDeclaredSurface(i, r.declared) : i;
}
};
}
/** Prepare the same package-owned consent history for every managed installer. */
async function prepareManagedPluginArtifactConsentHandler(e) {
const t = e.env ?? process.env;
const n = e.previousRecords ?? await loadInstalledPluginIndexInstallRecords({ env: t });
const r = resolvePluginControlPlaneWorkspace({
config: e.config,
env: t
});
const i = Object.keys(n).length > 0 ? resolvePluginMetadataSnapshot({
allowCurrent: false,
config: e.config,
env: t,
...r.workspaceDir !== void 0 ? { workspaceDir: r.workspaceDir } : {}
}) : void 0;
const a = /* @__PURE__ */ new Map();
for (const e of i?.index.plugins ?? []) {
const t = resolveInstalledPluginIndexInstallOwner(e);
if (t) a.set(e.pluginId, t);
}
return createManagedPluginArtifactConsentHandler({
...e,
env: t,
previousRecords: n,
previousPluginOwners: a
});
}
//#endregion
export { resolvePluginCapabilityConsent as a, resolvePendingPluginCapabilityReview as i, createManagedPluginArtifactConsentHandler as n, prepareManagedPluginArtifactConsentHandler as r, capturePluginCapabilityConsentHandlerErrors as t };