UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 10 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{m as normalizeTrimmedStringList}from"./string-normalization-D_oBUpO3.mjs";import{c as getPluginCacheRoot,l as getPluginCacheSource}from"./plugin-cache-B6QVpzXr.mjs";import{c as readPluginCacheFile,n as parsePluginCacheJson}from"./plugin-cache-files-BGuripl0.mjs";import{s as resolveBundledPluginsDir,t as areBundledPluginsDisabled}from"./bundled-dir-CMJ7m2eP.mjs";import{i as getPluginRegistryForContext}from"./gateway-request-scope-7deHmsb_.mjs";import{t as normalizeManifestPlatforms}from"./manifest-platforms-C_xO3tQf.mjs";import{t as shouldRejectHardlinkedPluginFiles}from"./hardlink-policy-S8MtjHnf.mjs";import{r as loadPluginPublicSurfaceModuleSync,t as getCachedPluginModuleLoader}from"./plugin-module-loader-cache-Bj5H0vwC.mjs";import{i as getPluginInstance,u as getPluginValueInstance}from"./plugin-instance-scope-Bx-frH07.mjs";import{s as resolveLoaderPackageRoot}from"./sdk-alias-6-h-uxTt.mjs";import{n as resolveBundledPluginSourcePublicSurfacePath,r as resolvePluginRootPublicSurfaceLocation,t as resolveBundledPluginPublicSurfacePath}from"./public-surface-runtime-Bi95EhPa.mjs";import{fileURLToPath}from"node:url";import path from"node:path"; //!src/plugin-sdk/facade-resolution-shared.ts const RUNTIME_FACADE_MATCH_TIERS=[`id`,`folder`,`channel`];function resolveRuntimeFacadeModuleLocation(e){if(e.env!==void 0&&e.env!==process.env)return;const t=getPluginRegistryForContext()?.plugins;if(!t)return;let n;for(const r of RUNTIME_FACADE_MATCH_TIERS){for(const i of t){if(i.status!==`loaded`||!i.rootDir)continue;if(!(r===`id`?i.id===e.dirName:r===`folder`?path.basename(i.rootDir)===e.dirName:i.channelIds.includes(e.dirName)))continue;if(n)throw new Error(`Plugin public surface ${e.dirName} has ambiguous runtime ownership; use its plugin id.`);n=i}if(n)break}if(!n)return;const r=resolvePluginRootPublicSurfaceLocation({pluginRoot:n.rootDir,pluginId:n.id,artifactBasename:e.artifactBasename});return r?{...r,pluginId:n.id,origin:n.origin}:null}function readBundledPluginManifestRecordFromDir(e){const t=readPluginCacheFile({rootDir:e,relativePath:`openclaw.plugin.json`,rejectHardlinks:false});if(!t.ok)return null;try{const n=parsePluginCacheJson(t,{json5:true});if(!n.ok||!isRecord(n.value))return null;const r=n.value;const i=normalizeOptionalString(r.id);if(!i)return null;return{id:i,origin:`bundled`,enabledByDefault:r.enabledByDefault===true,enabledByDefaultOnPlatforms:normalizeManifestPlatforms(r.enabledByDefaultOnPlatforms),rootDir:e,channels:normalizeTrimmedStringList(r.channels)}}catch{return null}}function resolveBundledMetadataManifestRecord(e){if(!e.location)return null;let t=e.sourceExtensionsRoot;if(!e.location.modulePath.startsWith(`${t}${path.sep}`)){const n=resolveBundledPluginsDir(e.env??process.env);if(!n)return null;t=path.resolve(n);if(!e.location.modulePath.startsWith(`${t}${path.sep}`))return null}const n=path.relative(t,e.location.modulePath).split(path.sep)[0];return n?readBundledPluginManifestRecordFromDir(path.join(t,n)):null}function createFacadeResolutionKey(e){const t=areBundledPluginsDisabled(e.env??process.env)?`disabled`:`enabled`;return`${e.dirName}::${e.artifactBasename}::${e.bundledPluginsDir?path.resolve(e.bundledPluginsDir):`<default>`}::${t}`}function resolveFacadeBoundaryRoot(e){if(!e.bundledPluginsDir)return e.packageRoot;const t=path.resolve(e.bundledPluginsDir);return e.modulePath.startsWith(`${t}${path.sep}`)?t:e.packageRoot}function resolveBundledFacadeModuleLocation(e){const t=e.env??process.env;if(areBundledPluginsDisabled(t))return null;const n=e.preferSource??!e.currentModulePath.includes(`${path.sep}dist${path.sep}`);const r=path.resolve(e.packageRoot,`extensions`);const i={rootDir:e.packageRoot,env:e.env,...e.bundledPluginsDir?{bundledPluginsDir:e.bundledPluginsDir,bundledPluginsDirMode:`explicit`}:{},dirName:e.dirName,artifactBasename:e.artifactBasename};const a=n?resolveBundledPluginSourcePublicSurfacePath({dirName:e.dirName,artifactBasename:e.artifactBasename,sourceRoot:e.bundledPluginsDir??r})??(e.bundledPluginsDir&&!areBundledPluginsDisabled(t)?resolveBundledPluginSourcePublicSurfacePath({dirName:e.dirName,artifactBasename:e.artifactBasename,sourceRoot:r}):null)??resolveBundledPluginPublicSurfacePath(i):resolveBundledPluginPublicSurfacePath(i);return a?{origin:`bundled`,modulePath:a,boundaryRoot:resolveFacadeBoundaryRoot({modulePath:a,bundledPluginsDir:e.bundledPluginsDir,packageRoot:e.packageRoot})}:null}function resolveRegistryPluginModuleLocationFromRecords(e){const t=[t=>t.id===e.dirName,t=>path.basename(t.rootDir)===e.dirName,t=>t.channels.includes(e.dirName)];for(const n of t)for(const t of e.registry.filter(n)){const n=path.resolve(t.rootDir);const r=resolvePluginRootPublicSurfaceLocation({pluginRoot:n,pluginId:t.id,artifactBasename:e.artifactBasename});if(r)return{...r,...t.origin?{origin:t.origin}:{}}}return null} //!src/plugin-sdk/facade-loader.ts var MissingPublicSurfaceError=class extends Error{constructor(e,t){super(e,t);this.name=`MissingPublicSurfaceError`}};const CURRENT_MODULE_PATH=fileURLToPath(import.meta.url);const loadedFacadePluginIds=/* @__PURE__ */ new Set;function getOpenClawPackageRoot(){return resolveLoaderPackageRoot({modulePath:fileURLToPath(import.meta.url),moduleUrl:import.meta.url})??fileURLToPath(new URL(`../..`,import.meta.url))}function resolveBundledPublicSurfaceLocation(e){const t=resolveRuntimeFacadeModuleLocation(e);if(t!==void 0)return t;const n=resolveBundledPluginsDir(e.env??process.env);const r=`facade:${e.preferSource??`auto`}:${createFacadeResolutionKey({...e,bundledPluginsDir:n})}`;const i=getPluginCacheRoot(getOpenClawPackageRoot()).artifacts;const a=i.get(r);if(a!==void 0)return a;const o=resolveBundledFacadeModuleLocation({...e,currentModulePath:CURRENT_MODULE_PATH,packageRoot:getOpenClawPackageRoot(),bundledPluginsDir:n});i.set(r,o);return o}function createLazyFacadeObjectValue(e){let t;let n;const resolve=()=>{if(n){const e=getPluginRegistryForContext()?.plugins.find(e=>e.id===n);if(e?.status!==`loaded`||!getPluginInstance(e))throw new Error(`Plugin ${n} was disabled or uninstalled; its facade is unavailable.`)}const r=t??e();n??=getPluginValueInstance(r)?.pluginId;if(!n)t=r;return r};const r={};const syncProperty=e=>{const t=Reflect.getOwnPropertyDescriptor(resolve(),e);if(t){if(n)t.configurable=true;Object.defineProperty(r,e,t)}else Reflect.deleteProperty(r,e);return t};const syncTarget=()=>{const e=resolve();const t=Object.getOwnPropertyDescriptors(e);if(n)for(const e of Reflect.ownKeys(t))Reflect.set(Reflect.get(t,e),`configurable`,true);for(const e of Reflect.ownKeys(r))if(!Object.hasOwn(t,e))Reflect.deleteProperty(r,e);Object.defineProperties(r,t);Reflect.setPrototypeOf(r,Reflect.getPrototypeOf(e));if(!Reflect.isExtensible(e))Reflect.preventExtensions(r)};return new Proxy(r,{defineProperty(e,t,r){const i=resolve();if(n&&(r.configurable===false||r.configurable!==true&&!Object.hasOwn(i,t)))return false;const a=Reflect.defineProperty(i,t,r);if(a)syncProperty(t);return a},deleteProperty(e,t){return Reflect.deleteProperty(resolve(),t)&&Reflect.deleteProperty(r,t)},get(e,t,n){return Reflect.get(resolve(),t,n)},getOwnPropertyDescriptor(e,t){return syncProperty(t)},getPrototypeOf(){return Reflect.getPrototypeOf(resolve())},has(e,t){const n=Reflect.has(resolve(),t);if(!n)Reflect.deleteProperty(r,t);return n},isExtensible(){const e=Reflect.isExtensible(resolve());if(!e)syncTarget();return e},ownKeys(){syncTarget();return Reflect.ownKeys(resolve())},preventExtensions(){const e=resolve();if(n||!Reflect.preventExtensions(e))return false;syncTarget();return true},set(e,t,n,r){return Reflect.set(resolve(),t,n,r)},setPrototypeOf(e,t){return Reflect.setPrototypeOf(resolve(),t)&&Reflect.setPrototypeOf(r,t)}})}function trackFacadeModule(e,t){const n=getPluginCacheSource(e);if(n.facadeTracked)return;n.facadeTracked=true;try{loadedFacadePluginIds.add(typeof t===`function`?t():t)}catch(e){delete n.facadeTracked;throw e}}function isPathAtOrInside(e,t){const n=path.resolve(t);const r=path.resolve(e);return r===n||r.startsWith(n+path.sep)}function resolveFacadeBoundaryOpenParams(e){const t=getPluginCacheRoot(e).publicSurfaceBoundary;if(t)return t;if(isPathAtOrInside(e,getOpenClawPackageRoot()))return{boundaryLabel:`OpenClaw package root`,rejectHardlinks:false};const n=resolveBundledPluginsDir();if(n&&isPathAtOrInside(e,n))return{boundaryLabel:`bundled plugin directory`,rejectHardlinks:false};return{boundaryLabel:`plugin root`,rejectHardlinks:shouldRejectHardlinkedPluginFiles({origin:`global`,rootDir:e})}}function loadFacadeModuleAtLocationSync(e){const t=e.location;const n=loadPluginPublicSurfaceModuleSync({...t,...e.boundary??resolveFacadeBoundaryOpenParams(t.boundaryRoot),surfaceLabel:e.surfaceLabel??`bundled plugin public surface ${t.modulePath}`,pluginId:e.pluginId??t.pluginId,loadModule:e.loadModule??(e=>getCachedPluginModuleLoader({modulePath:e,importerUrl:import.meta.url,preferBuiltDist:true,loaderFilename:import.meta.url})(e))});if(e.trackedPluginId!==void 0)trackFacadeModule(t.modulePath,e.trackedPluginId);return n}function loadBundledPluginPublicSurfaceModuleSyncCore(e){const t=resolveBundledPublicSurfaceLocation(e);if(!t)throw new MissingPublicSurfaceError(`Unable to resolve bundled plugin public surface ${e.dirName}/${e.artifactBasename}`);return loadFacadeModuleAtLocationSync({location:t,trackedPluginId:e.trackedPluginId??e.dirName})}function listImportedBundledPluginFacadeIds(){return[...loadedFacadePluginIds].toSorted((e,t)=>e.localeCompare(t))}export{loadFacadeModuleAtLocationSync as a,resolveBundledMetadataManifestRecord as c,loadBundledPluginPublicSurfaceModuleSyncCore as i,resolveRegistryPluginModuleLocationFromRecords as l,createLazyFacadeObjectValue as n,resolveBundledPublicSurfaceLocation as o,listImportedBundledPluginFacadeIds as r,createFacadeResolutionKey as s,MissingPublicSurfaceError as t,resolveRuntimeFacadeModuleLocation as u};