openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 9.76 kB
JavaScript
import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import"./utils-Dlk0IYAa.mjs";import{f as resolveEffectiveEnableState,u as normalizePluginsConfig}from"./config-state-C1eV6UWf.mjs";import{t as pruneMapToMaxSize}from"./map-size-DLQqorvK.mjs";import{n as registerPluginMetadataProcessMemoLifecycleClear}from"./plugin-metadata-lifecycle-Bdt91XXL.mjs";import{r as resolveInstalledPluginIndexInstallOwner,t as isInstalledPluginIndexInstallOwnerAmbiguous}from"./installed-plugin-index-install-owner-iRSOvGid.mjs";import{r as loadInstalledPluginIndexInstallRecords}from"./installed-plugin-index-record-reader-rJXlMUQ8.mjs";import{i as isTrustedOfficialPluginInstallRecord}from"./official-external-install-records-CVYKmVDj.mjs";import{p as resolvePluginMetadataSnapshot}from"./plugin-metadata-snapshot-CDKhemiT.mjs";import{n as resolvePluginControlPlaneWorkspace}from"./control-plane-workspace-CNfE92_q.mjs";import{t as createInstalledPluginOwnershipResolver}from"./installed-plugin-package-ownership-Dw9mtnM_.mjs";import{t as ManagedPluginLifecycleError}from"./management-lifecycle-error-oQD-HkgF.mjs";import{r as assertConfigWriteAllowedInCurrentMode}from"./config-write-guard-DrPwqEel.mjs";import{s as withPluginLifecycleLeaseAsync}from"./plugin-lifecycle-lease-D-y0xYkr.mjs";import{a as writePersistedInstalledPluginIndexInstallRecordsWithLease}from"./installed-plugin-index-records-DMOoWiOF.mjs";import{c as resolvePluginInstallRecordIntegrity,i as diffDeclaredSurfaceWidening,r as computeDeclaredSurfaceHash,s as resolveAcceptedSurfaceCurrent,t as buildPluginCapabilityConsentReview,u as resolvePluginPackageDeclaredSurface}from"./capability-summary-D1Y-zikG.mjs";import{n as resolvePluginArtifactDeclaredSurface,t as inspectPluginCapabilityArtifact}from"./capability-artifact-Cti5hEse.mjs";import path from"node:path";
//!src/plugins/capability-consent.ts
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}:{}}})}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)})}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,inspectingPreviousArtifact:true}))}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}}}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})}export{resolvePluginCapabilityConsent as a,resolvePendingPluginCapabilityReview as i,createManagedPluginArtifactConsentHandler as n,prepareManagedPluginArtifactConsentHandler as r,capturePluginCapabilityConsentHandlerErrors as t};