UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 8.05 kB
import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import{l as isRecord,o as asRecord}from"./record-coerce-BTbqdJ3-.mjs";import"./utils-Dlk0IYAa.mjs";import{r as loadPluginManifest}from"./manifest-D8YdMedM.mjs";import{s as tracePluginLifecyclePhaseAsync}from"./discovery-Bo4UPPd9.mjs";import{d as tryReadJsonSync}from"./json-files-BBQ_0Np0.mjs";import{l as listOfficialExternalPluginCatalogEntries,p as resolveOfficialExternalPluginInstall}from"./official-external-plugin-catalog-BkSwu5lo.mjs";import{s as parseRegistryNpmSpec}from"./npm-registry-spec-D06XlvdA.mjs";import{r as loadInstalledPluginIndexInstallRecords}from"./installed-plugin-index-record-reader-rJXlMUQ8.mjs";import{g as resolveOfficialExternalPluginId}from"./official-external-plugin-catalog-source-WW0GK1Kk.mjs";import{l as readConfigFileSnapshotForWrite}from"./io.runtime-BvSeVFIY.mjs";import"./config-DkZ8y8Gv.mjs";import"./installed-plugin-index-records-DMOoWiOF.mjs";import{t as findBundledPluginSource}from"./bundled-sources-imFFFyl2.mjs";import{a as resolveFileNpmSpecToLocalPath,i as resolveBundledInstallPlanBeforeNpm,r as parseNpmPrefixSpec,t as isBareNpmPackageName}from"./install-source-spec-bt-2-4Mo.mjs";import{n as listPersistedBundledPluginRecoveryLocations}from"./location-bridges-DuxWteFT.mjs";import{a as supportsInstallConfigSingleTopLevelIncludeShape,i as selectInstallMutationWriteOptions,r as resolveInstallConfigMutationPreflights}from"./install-config-mutation-CrrUoXCz.mjs";import fs from"node:fs";import path from"node:path"; //!src/plugins/install-config.ts function resolveFullyBlockedConfigMutationReason(e){if(e.pluginMutation.mode!==`blocked`||e.hookMutation.mode!==`blocked`)return null;if(e.pluginMutation.reason===e.hookMutation.reason)return e.pluginMutation.reason;return`Config plugin and hook mutations are both blocked. ${e.pluginMutation.reason} ${e.hookMutation.reason}`}var PluginInstallConfigError=class extends Error{constructor(e,t){super(e);this.blockedSnapshot=t;this.code=`INVALID_CONFIG`;this.name=`PluginInstallConfigError`}};function extractMissingPluginLoadPath(e){if(e.path!==`plugins.load.paths`)return null;const t=e.message.indexOf(`plugin path not found:`);if(t<0)return null;return e.message.slice(t+22).trim()||null}function isOwnedMissingPluginLoadPathIssue(e,t,n=process.env){const r=extractMissingPluginLoadPath(e);return r!==null&&t.has(resolveUserPath(r,n))}function isAllowedPluginRecoveryIssue(e,t,n){const r=t.bundledPluginId?.trim();if(!r)return false;return e.path===`channels.${r}`&&e.message===`unknown channel id: ${r}`||e.path.startsWith(`channels.`)&&e.message.startsWith(`invalid config for plugin ${r}:`)||isOwnedMissingPluginLoadPathIssue(e,n)||e.path===`plugins.entries.${r}`&&e.message.includes(`requires compiled runtime output`)||e.path===`tools.web.search.provider`&&e.message.includes(`plugin "${r}"`)}function removeOwnedMissingPluginLoadPaths(e,t,n,r){const i=/* @__PURE__ */ new Set;for(const e of t){const t=extractMissingPluginLoadPath(e);if(!t)continue;const a=resolveUserPath(t,r);if(n.has(a))i.add(a)}const a=e.plugins?.load?.paths;if(i.size===0||!Array.isArray(a))return e;const o=a.filter(e=>typeof e!==`string`||!i.has(resolveUserPath(e,r)));if(o.length===a.length)return e;return{...e,plugins:{...e.plugins,load:{...e.plugins?.load,paths:o}}}}async function resolveRequestedPluginInstallPaths(e,t,n,r){if(!t.some(e=>extractMissingPluginLoadPath(e)!==null))return/* @__PURE__ */ new Set;const i=await loadInstalledPluginIndexInstallRecords();const a=/* @__PURE__ */ new Set;const o=n.bundledPluginId?.trim();if(!o)return a;const s=i[o]??e.plugins?.installs?.[o];for(const e of[s?.sourcePath,s?.installPath])if(typeof e===`string`&&e.trim())a.add(resolveUserPath(e,r));if(t.some(e=>extractMissingPluginLoadPath(e)!==null&&!isOwnedMissingPluginLoadPathIssue(e,a,r))){const e=(await listPersistedBundledPluginRecoveryLocations({env:r})).filter(e=>e.pluginId===o).flatMap(e=>e.loadPaths);for(const t of e)a.add(resolveUserPath(t,r))}return a}async function recoverPluginInstallConfig(e,t){if(resolvePluginInstallInvalidConfigPolicy(e)!==`allow-plugin-recovery`)throw new PluginInstallConfigError("Config invalid; run `openclaw doctor --fix` before installing plugins.");const n=t.parsed??{};if(!t.exists||Object.keys(n).length===0)throw new PluginInstallConfigError("Config file could not be parsed; run `openclaw doctor` to repair it.");const r=await resolveRequestedPluginInstallPaths(t.config,t.issues,e,process.env);if(t.legacyIssues.length>0||t.issues.length===0||t.issues.some(t=>!isAllowedPluginRecoveryIssue(t,e,r))){const t=e.bundledPluginId??`the requested plugin`;throw new PluginInstallConfigError(`Config invalid outside the plugin recovery path for ${t}; run \`openclaw doctor --fix\` before reinstalling it.`)}if(Object.hasOwn(n,`$include`)||!supportsInstallConfigSingleTopLevelIncludeShape(isRecord(n)?n.plugins:void 0))throw new PluginInstallConfigError("Config plugin recovery uses an unsupported $include shape; use a single-file top-level plugins include or run `openclaw doctor --fix` before reinstalling it.");return removeOwnedMissingPluginLoadPaths(t.config,t.issues,r,process.env)}async function loadConfigForInstall(e){const{snapshot:t,writeOptions:n}=await tracePluginLifecyclePhaseAsync(`config read`,()=>readConfigFileSnapshotForWrite(),{command:`install`});const r=selectInstallMutationWriteOptions(n);const i=t.valid?t.sourceConfig:await recoverPluginInstallConfig(e,t);const a=asRecord(t.parsed);const{hookMutation:o,pluginMutation:s}=resolveInstallConfigMutationPreflights({parsed:a,snapshotPath:t.path,writeOptions:r});const c={config:i,baseHash:t.hash,writeOptions:r,hookMutation:o,pluginMutation:s};if((!t.valid||e.installKind===`plugin`)&&s.mode===`blocked`)throw new PluginInstallConfigError(s.reason,c);return c}function readPluginInstallRecoveryMetadata(e){const t=path.join(e,`package.json`);if(!fs.existsSync(t))return{allowInvalidConfigRecovery:false};const n=loadPluginManifest(e,false);const r=n.ok?n.manifest.id:void 0;const i=tryReadJsonSync(t);return{...r?{pluginId:r}:{},allowInvalidConfigRecovery:i?.openclaw?.install?.allowInvalidConfigRecovery===true}}function resolvePluginInstallRecoveryMetadata(e,t,n){if(t!==void 0){const e=readPluginInstallRecoveryMetadata(t);return e.pluginId||e.allowInvalidConfigRecovery?e:{}}const r=parseNpmPrefixSpec(e);const i=n===`npm`?r??e.trim():n===void 0?r:null;const a=(n===void 0||n===`official`)&&isBareNpmPackageName(e)?e.trim():void 0;if(!i&&!a)return{};const o=i?parseRegistryNpmSpec(i)?.name:void 0;for(const e of listOfficialExternalPluginCatalogEntries()){const t=resolveOfficialExternalPluginInstall(e);const n=resolveOfficialExternalPluginId(e);const r=t?.npmSpec?.trim();if(!t||!(i?r&&(r===i||r===o):n===a))continue;return{...n?{pluginId:n}:{},allowInvalidConfigRecovery:t?.allowInvalidConfigRecovery===true}}return{}}function resolvePluginInstallRequestContext(e){if(e.marketplace)return{ok:true,request:{rawSpec:e.rawSpec,installKind:`plugin`,marketplace:e.marketplace}};const t=resolveFileNpmSpecToLocalPath(e.rawSpec);if(t&&!t.ok)return t;const n=t?.path??e.rawSpec;const r=resolveUserPath(e.localPath??n);const i=e.source?e.source===`local`||e.source===`bundled`?r:void 0:t||fs.existsSync(r)?r:resolveBundledInstallPlanBeforeNpm({rawSpec:e.rawSpec,findBundledSource:e=>findBundledPluginSource({lookup:e})})?.bundledSource.localPath;const a=resolvePluginInstallRecoveryMetadata(e.rawSpec,i,e.source);return{ok:true,request:{rawSpec:e.rawSpec,...e.installKind===`plugin`||a.pluginId?{installKind:`plugin`}:{},...a.pluginId?{bundledPluginId:a.pluginId}:{},...a.allowInvalidConfigRecovery!==void 0?{allowInvalidConfigRecovery:a.allowInvalidConfigRecovery}:{}}}}function resolvePluginInstallInvalidConfigPolicy(e){return e?.allowInvalidConfigRecovery===true?`allow-plugin-recovery`:`deny`}export{resolvePluginInstallRequestContext as a,resolvePluginInstallInvalidConfigPolicy as i,loadConfigForInstall as n,resolveFullyBlockedConfigMutationReason as r,PluginInstallConfigError as t};