openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 17.1 kB
JavaScript
import{d as captureEnvironmentKey,f as resolveEnvironmentKey}from"./env-fLMEolk5.mjs";import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import{u as normalizeSortedUniqueStringEntries}from"./string-normalization-D_oBUpO3.mjs";import{r as getRuntimeConfigCapture}from"./runtime-config-capture-state-B3_P0bBA.mjs";import{a as getPluginCache}from"./plugin-cache-B6QVpzXr.mjs";import"./utils-Dlk0IYAa.mjs";import{f as resolvePluginRuntimeArtifactPreference}from"./bundled-dir-CMJ7m2eP.mjs";import{C as normalizePluginsConfigWithResolverCore,b as withContextEngineOwner,h as resolveEligibleContextEngineOwnerIds,m as resolveEligibleContextEngineDeclaredOwners,r as createPluginActivationSource,t as applyTestPluginDefaults,u as normalizePluginsConfig}from"./config-state-C1eV6UWf.mjs";import{i as resolveConfigEnvVars}from"./env-substitution-CbEfgBpH.mjs";import{i as getPluginRegistryForContext}from"./gateway-request-scope-7deHmsb_.mjs";import{n as getPluginRuntimeLoadContextState}from"./load-context-state-30rh4Jdb.mjs";import{r as normalizeProviderId}from"./provider-id-A-8WTtLA.mjs";import{a as createConfigRuntimeEnv}from"./config-env-vars-DCDGIBtT.mjs";import{p as extractPluginInstallRecordsFromInstalledPluginIndex}from"./installed-plugin-index-B04a42mO.mjs";import{i as loadInstalledPluginIndexInstallRecordsSync}from"./installed-plugin-index-record-reader-rJXlMUQ8.mjs";import{n as resolvePluginDiscoveryContext}from"./plugin-control-plane-context-CfwJGrnQ.mjs";import{i as getCurrentPluginMetadataSnapshot,p as normalizePluginIdScope}from"./current-plugin-metadata-snapshot-CYi8VAby.mjs";import{c as getPluginLoaderCacheState}from"./registry-lifecycle-DGwblXFd.mjs";import{f as getActivePluginRegistryWorkspaceDir,l as getActivePluginRegistry,u as getActivePluginRegistryKey}from"./runtime-Cl6PvJHC.mjs";import{n as getPluginRegistryRuntime}from"./registry-runtime-binding-BmYsFHdu.mjs";import{s as matchesPluginRuntimeArtifactSelection}from"./plugin-runtime-artifact-binding-_oNTmROe.mjs";import{t as captureRuntimeConfig}from"./runtime-source-projection-DvPkX_nq.mjs";import{t as resolvePluginActivationSourceConfig}from"./activation-source-config-CrlaPKB7.mjs";import"./env-vars-T_m-ovbQ.mjs";import{s as resolvePluginRegistrationConfigKey,t as activationConfigFingerprint}from"./load-context-BgV8IMWg.mjs";import"./installed-plugin-index-records-DMOoWiOF.mjs";import{createHash}from"node:crypto";
//!src/plugins/provider-synthetic-auth.ts
const scopes=/* @__PURE__ */ new WeakMap;const restoredConfigs=/* @__PURE__ */ new WeakSet;function scopeFor(e,t,n=false){if(!e)return;const r=restoredConfigs.has(e)?e:t.preparationOwner??e;let i=scopes.get(r);if(!i&&n){i=/* @__PURE__ */ new WeakMap;scopes.set(r,i)}const a=n?captureEnvironmentKey(t.env):resolveEnvironmentKey(t.env);let o=i?.get(a);if(!o&&n){o=/* @__PURE__ */ new Map;i?.set(a,o)}let s=o?.get(t.workspaceDir);if(!s&&n){s={resolvers:/* @__PURE__ */ new WeakMap};o?.set(t.workspaceDir,s)}return s}function readPreparedSyntheticAuthFact(e,t={}){const n=scopeFor(e.config,t);if(e.config&&restoredConfigs.has(e.config)&&!n?.restored)throw new Error(`Prepared synthetic auth environment or workspace does not match`);return n?.restored?.get(normalizeProviderId(e.provider))}function restorePreparedSyntheticAuthFacts(e,t){const n=new Map(e.map(e=>[normalizeProviderId(e.providerRef),e]));for(const{config:e,...r}of t){const t=scopeFor(e,r,true);restoredConfigs.add(e);t.restored=n}}function inheritPreparedSyntheticAuthScope(e,t){if(!e||!t||!restoredConfigs.has(e))return;scopes.set(t,scopes.get(e));restoredConfigs.add(t)}function resolveSyntheticAuthWithProvider(e,t,n={}){const r=readPreparedSyntheticAuthFact(t,n);if(r)return r.result??void 0;if(!e.prepareSyntheticAuth)return e.resolveSyntheticAuth?.(t)??void 0;const i=scopeFor(t.config,n);if(i?.restored)throw new Error(`Prepared synthetic auth is missing for ${t.provider}`);return i?.resolvers.get(e.prepareSyntheticAuth)?.get(normalizeProviderId(t.provider))?.result??void 0}async function prepareSyntheticAuthWithProvider(e,t,n={}){n.signal?.throwIfAborted();const r=readPreparedSyntheticAuthFact(t,n);if(r)return r.result??void 0;if(!e.prepareSyntheticAuth)return e.resolveSyntheticAuth?.(t)??void 0;const i=scopeFor(t.config,n,true);if(i?.restored)throw new Error(`Prepared synthetic auth is missing for ${t.provider}`);const a=normalizeProviderId(t.provider);const o=e.prepareSyntheticAuth;let s=i?.resolvers.get(o);if(!s&&i){s=/* @__PURE__ */ new Map;i.resolvers.set(o,s)}const c=s?.get(a)??{pending:/* @__PURE__ */ new Map};s?.set(a,c);let l=c.result;if(l===void 0){let r=c.pending.get(n.signal);if(!r){r=Promise.resolve().then(()=>{n.signal?.throwIfAborted();return o({...t,env:n.env,signal:n.signal,pluginRoot:e.pluginRoot})}).then(e=>{n.signal?.throwIfAborted();return c.result=e??null}).finally(()=>{c.pending.delete(n.signal)});c.pending.set(n.signal,r)}l=await r}n.signal?.throwIfAborted();return l??void 0}
//!src/plugins/loader-load-context.ts
const runtimeBindingCacheIds=/* @__PURE__ */ new WeakMap;let nextRuntimeBindingCacheId=1;function resolveRuntimeBindingCacheId(e){if(!e)return;const t=runtimeBindingCacheIds.get(e);if(t!==void 0)return t;const n=nextRuntimeBindingCacheId++;runtimeBindingCacheIds.set(e,n);return n}function buildActivationMetadataHash(e){const t=Object.entries(e.activationSource.rootConfig?.channels??{}).flatMap(([e,t])=>{if(!t||typeof t!==`object`||Array.isArray(t))return[];const n=t.enabled;return typeof n===`boolean`?[[e,n]]:[]}).toSorted(([e],[t])=>e.localeCompare(t));const n=Object.entries(e.activationSource.plugins.entries).map(([e,{enabled:t}])=>[e,t]).toSorted(([e],[t])=>e.localeCompare(t));const r=Object.entries(e.autoEnabledReasons).map(([e,t])=>[e,[...t]]).toSorted(([e],[t])=>e.localeCompare(t));return createHash(`sha256`).update(JSON.stringify({enabled:e.activationSource.plugins.enabled,allow:e.activationSource.plugins.allow,deny:e.activationSource.plugins.deny,memorySlot:e.activationSource.plugins.slots.memory,contextEngineSlot:e.activationSource.plugins.slots.contextEngine,entries:n,channelEnablement:t,autoEnabledReasons:r})).digest(`hex`)}function resolveRuntimeSubagentMode(e){if(e?.allowGatewaySubagentBinding===true)return`gateway-bindable`;return e?.subagent?`explicit`:`default`}function resolveCoreGatewayMethodNames(e){return[.../* @__PURE__ */ new Set([...e.coreGatewayMethodNames??[],...Object.keys(e.coreGatewayHandlers??{})])].toSorted()}function mergePluginTrustList(e,t){if(e===t||t.length===0)return e;const n=[...e];const r=new Set(n);for(const e of t)if(!r.has(e)){n.push(e);r.add(e)}return n.length===e.length?e:n}function mergeTrustPluginConfigFromActivationSource(e){const t=e.activationSource.plugins;const n=mergePluginTrustList(e.normalized.allow,t.allow);const r=mergePluginTrustList(e.normalized.deny,t.deny);const i=mergePluginTrustList(e.normalized.loadPaths,t.loadPaths);if(n===e.normalized.allow&&r===e.normalized.deny&&i===e.normalized.loadPaths)return e.normalized;return{...e.normalized,allow:n,deny:r,loadPaths:i}}function resolvePluginLoadCacheContext(e={}){const t=getPluginLoaderCacheState();const n=e.resolveRawConfigEnvVars===true;const r=e.env??process.env;const i=e.config??{};const a=resolvePluginActivationSourceConfig({config:e.config,activationSourceConfig:e.activationSourceConfig});const o=n?createConfigRuntimeEnv(i,r):r;const s=applyTestPluginDefaults(n?resolveConfigEnvVars(i,o,{onMissing:()=>void 0}):i,o);const c=n?resolveConfigEnvVars(a,o,{onMissing:()=>void 0}):a;const l=e.normalizePluginId?t=>normalizePluginsConfigWithResolverCore(t,e.normalizePluginId):normalizePluginsConfig;const u=l(s.plugins);const d=createPluginActivationSource({config:c,plugins:s.plugins===c.plugins?u:l(c.plugins)});const f=mergeTrustPluginConfigFromActivationSource({normalized:u,activationSource:d});const p=normalizePluginIdScope(e.onlyPluginIds);const m=e.includeSetupOnlyChannelPlugins===true;const h=e.forceSetupOnlyChannelPlugins===true;const g=e.channelPluginLoadIntent??`full`;const _=resolvePluginRuntimeArtifactPreference(e.preferBuiltPluginArtifacts);const v=resolveRuntimeSubagentMode(e.runtimeOptions);const y=v===`gateway-bindable`&&e.mode!==`cli-metadata`&&(!e.runtimeOptions?.nodes||!e.runtimeOptions?.subagent)?getActivePluginRegistry():void 0;const b=y?getPluginRegistryRuntime(y):void 0;const x=resolveCoreGatewayMethodNames(e);const S=e.metadataSnapshot??(e.installRecords===void 0&&f.loadPaths===u.loadPaths&&!n&&(e.env===void 0||e.env===process.env)?getCurrentPluginMetadataSnapshot({config:i,env:o,workspaceDir:e.workspaceDir})??(p!==void 0?getCurrentPluginMetadataSnapshot({config:i,env:o,workspaceDir:e.workspaceDir,pluginIds:p}):void 0):void 0);const C={...e.installRecords??(S?extractPluginInstallRecordsFromInstalledPluginIndex(S.index):loadInstalledPluginIndexInstallRecordsSync({env:o})),...s.plugins?.installs};const w=resolvePluginDiscoveryContext({workspaceDir:e.workspaceDir,loadPaths:f.loadPaths,env:o});const T=resolvePluginRegistrationConfigKey({runtimeEntries:u.entries,sourceEntries:d.plugins.entries});const E=e.mode!==`cli-metadata`&&e.activate!==false;const D=e.runtimeSideEffects??E;const O=e.registrationConfigOrigin;const k=!E&&O&&getRuntimeConfigCapture(O)?resolveRuntimeBindingCacheId(O):void 0;const A=e.manifestRegistry??(e.discovery===void 0?S?.manifestRegistry:void 0);const{roots:j,loadPaths:M,devSourceRoot:N}=w;const P=Object.fromEntries(Object.entries(C).map(([e,t])=>[e,{...t,installPath:typeof t.installPath===`string`?resolveUserPath(t.installPath,o):t.installPath,sourcePath:typeof t.sourcePath===`string`?resolveUserPath(t.sourcePath,o):t.sourcePath}]));const F={metadataSnapshot:resolveRuntimeBindingCacheId(S),roots:j,devSourceRoot:N,plugins:{...f,loadPaths:M,entries:Object.entries(f.entries).map(([e,t])=>[e,t.enabled])},registrationConfigKey:T,installs:P,discoverySources:(e.manifestRegistry?void 0:e.discovery)?.candidates.map(e=>[e.effectivePluginId??e.idHint,e.origin,e.rootDir,e.source,e.setupSource,e.sourcePreferred,e.configSelected,e.packageManifest?.build?.bundledDist]),activationMetadataKey:buildActivationMetadataHash({activationSource:d,autoEnabledReasons:e.autoEnabledReasons??{}}),capabilityCatalogIdentity:e.capabilityCatalog?JSON.stringify([e.capabilityCatalog.family,resolveRuntimeBindingCacheId(e.capabilityCatalog.context),resolveRuntimeBindingCacheId(getPluginCache()),resolveRuntimeBindingCacheId(getPluginRegistryForContext()??void 0)]):void 0,allowProcessHomeSessionCatalogs:e.allowProcessHomeSessionCatalogs!==false,onlyPluginIds:p,includeSetupOnlyChannelPlugins:m,forceSetupOnlyChannelPlugins:h,channelPluginLoadIntent:g,artifactPreference:_,resolveRawConfigEnvVars:e.resolveRawConfigEnvVars===true,loadModules:e.loadModules!==false,toolDiscovery:e.toolDiscovery===true,runtimeSubagentMode:v,runtimeBindingIdentity:JSON.stringify({capabilityCatalogContext:resolveRuntimeBindingCacheId(e.capabilityCatalogContext),modelAuth:resolveRuntimeBindingCacheId(e.runtimeOptions?.modelAuth),modelConfig:resolveRuntimeBindingCacheId(e.runtimeOptions?.modelConfig),nodes:resolveRuntimeBindingCacheId(e.runtimeOptions?.nodes),subagent:resolveRuntimeBindingCacheId(e.runtimeOptions?.subagent),borrowedGatewayRuntime:E?void 0:resolveRuntimeBindingCacheId(b)}),pluginSdkResolution:e.pluginSdkResolution??`auto`,coreGatewayMethodNames:x,activate:E,runtimeSideEffects:D,mode:e.mode??`full`,expectedSourceDigests:e.expectedSourceDigests?Object.entries(e.expectedSourceDigests).toSorted(([e],[t])=>e.localeCompare(t)):void 0};const I=k?[activationConfigFingerprint(s),activationConfigFingerprint(c)]:void 0;const L=JSON.stringify(F);const R=I?JSON.stringify({...F,roots:{...j,workspace:void 0},registrationConfigOrigin:k,registrationSnapshots:I}):L;const resolveManifestCacheKey=e=>createHash(`sha256`).update(e?R:L).update(JSON.stringify(e?.plugins.map(e=>[e.id,e.contextEngineIds,e.origin,e.rootDir,e.source,e.setupSource,e.providerDiscoverySource,e.capabilityCatalogSource,e.sourcePreferred,e.packageManifest?.build?.bundledDist]))??``).digest(`hex`);const z=resolveManifestCacheKey(A);let B;const capture=()=>{if(!B){const e=captureRuntimeConfig(s);if(!n)inheritPreparedSyntheticAuthScope(i,e);if(k!==void 0&&e!==s)runtimeBindingCacheIds.set(e,k);const t=c===s?e:captureRuntimeConfig(c);const r=l(e.plugins);const a=createPluginActivationSource({config:t,plugins:e.plugins===t.plugins?r:l(t.plugins)});B={cfg:e,activationSourceConfig:t,activationSource:a,normalized:mergeTrustPluginConfigFromActivationSource({normalized:r,activationSource:a})}}return B};return{cacheState:t,env:o,get cfg(){return capture().cfg},registrationConfigKey:T,metadataSnapshot:S,get normalized(){return capture().normalized},get activationSourceConfig(){return capture().activationSourceConfig},get activationSource(){return capture().activationSource},selectContextEngineOwner(e){const t=S?.registryIndex.plugins.map(e=>({id:e.pluginId,contextEngineIds:e.contextEngineIds}))??e;const n=capture();const r=n.normalized.slots.contextEngine;const i=resolveEligibleContextEngineOwnerIds(n.normalized,r,t);if(i.length>1){const e=resolveEligibleContextEngineDeclaredOwners(n.normalized,r,t).pluginIds.length===i.length?`declared`:`approved`;throw new Error(`Context engine "${r}" has ambiguous ${e} owners: ${i.toSorted().join(`, `)}. Select an engine with a unique plugin owner.`)}n.normalized=withContextEngineOwner(n.normalized,t);n.activationSource={...n.activationSource,plugins:withContextEngineOwner(n.activationSource.plugins,t)};const a=i[0];return r&&(n.normalized.contextEngineOwnerId||t.some(e=>e.contextEngineIds?.includes(r)))?Object.freeze({engineId:r,owner:a?`plugin:${a}`:null}):void 0},autoEnabledReasons:e.autoEnabledReasons??{},onlyPluginIds:p,includeSetupOnlyChannelPlugins:m,forceSetupOnlyChannelPlugins:h,channelPluginLoadIntent:g,artifactPreference:_,shouldActivate:E,runtimeSideEffects:D,shouldLoadModules:e.loadModules!==false,runtimeSubagentMode:v,borrowedGatewayRuntime:b,installRecords:C,devSourceRoot:w.devSourceRoot,cacheKey:z,resolveManifestCacheKey}}
//!src/plugins/active-runtime-registry.ts
function resolveCompatibleRuntimePluginRegistry(e){const t=getActivePluginRegistry()??void 0;if(!t||e===void 0)return t;const n=getActivePluginRegistryKey();if(!n)return;const r=resolvePluginLoadCacheContext(e).cacheKey;if(r===n)return t;const i=getPluginRuntimeLoadContextState(t)?.loaderCacheIdentity;return i&&(i.requestKey===n&&i.resolvedKey===r||i.resolvedKey===n&&i.requestKey===r)?t:void 0}function isRuntimePluginRecordLoaded(e){return e.status===`loaded`&&(e.format===`bundle`||e.imported!==false)}function listRuntimePluginIdsFromRegistry(e){return normalizeSortedUniqueStringEntries(e.plugins.filter(isRuntimePluginRecordLoaded).map(e=>e.id))}function listLoadedRuntimePluginIds(){const e=getActivePluginRegistry();return e?listRuntimePluginIdsFromRegistry(e):[]}function registryContainsRuntimePluginIds(e,t){if(t===void 0)return true;if(t.length===0&&e.plugins.length>0)return false;const n=new Set(t);for(const t of e.plugins)if(t.status===void 0||isRuntimePluginRecordLoaded(t))n.delete(t.id);if(t.length>0&&n.size===0)return true;if(e.plugins.some(e=>n.has(e.id)))return false;for(const[r,i]of Object.entries(e)){if(r===`diagnostics`||r===`channelSetups`||!Array.isArray(i))continue;for(const e of i)if(e&&typeof e===`object`&&`pluginId`in e){const r=e.pluginId;if(typeof r===`string`&&r.length>0){if(t.length===0)return false;n.delete(r);if(n.size===0)return true}}}return t.length===0}function createRuntimePluginManifestLookup(e,t,n){const r=new Map(e.plugins.filter(isRuntimePluginRecordLoaded).map(e=>[e.id,e]));const i=new Map(t.toReversed().map(e=>[e.id,e]));return e=>{const t=r.get(e);const a=i.get(e);return t&&a&&t.origin===a.origin&&matchesPluginRuntimeArtifactSelection(t,a,n)?t:void 0}}function getLoadedRuntimePluginRegistry(e={}){const t=e.requiredPluginIds??e.loadOptions?.onlyPluginIds;const n=t===void 0?void 0:normalizeSortedUniqueStringEntries(t);if(e.loadOptions&&n===void 0)return resolveCompatibleRuntimePluginRegistry(e.loadOptions);const r=getActivePluginRegistryWorkspaceDir();const i=e.workspaceDir??e.loadOptions?.workspaceDir;if((Object.hasOwn(e,`workspaceDir`)||e.loadOptions||i!==void 0)&&r!==i)return;const a=getActivePluginRegistry();if(!a)return;if(!registryContainsRuntimePluginIds(a,n)||e.loadOptions?.manifestRegistry&&n!==void 0&&!n.every(createRuntimePluginManifestLookup(a,e.loadOptions.manifestRegistry.plugins,e.loadOptions.preferBuiltPluginArtifacts)))return;if(e.loadOptions?.discovery&&!e.loadOptions.manifestRegistry)return resolveCompatibleRuntimePluginRegistry(e.loadOptions);return a}export{registryContainsRuntimePluginIds as a,resolveRuntimeSubagentMode as c,readPreparedSyntheticAuthFact as d,resolveSyntheticAuthWithProvider as f,listRuntimePluginIdsFromRegistry as i,inheritPreparedSyntheticAuthScope as l,getLoadedRuntimePluginRegistry as n,resolveCompatibleRuntimePluginRegistry as o,restorePreparedSyntheticAuthFacts as p,listLoadedRuntimePluginIds as r,resolvePluginLoadCacheContext as s,createRuntimePluginManifestLookup as t,prepareSyntheticAuthWithProvider as u};