UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.67 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{t as isBlockedObjectKey}from"./prototype-keys-Dc7Yq32J.mjs";import{t as parseConfigPathArrayIndex}from"./path-array-index-DO9QgC44.mjs";import{r as parseConcreteConfigPath,s as toDotPath}from"./dot-path-B1bYAWM5.mjs";import{t as normalizePluginPolicyId}from"./plugin-policy-id-Nz6ZTOOH.mjs";import{o as readConfigWritePendingMigrations,r as formatDeferredPluginMigration}from"./deferred-plugin-migrations-D_BIDN9_.mjs";import{t as collectPluginConfigContractMatches}from"./config-contract-matches-CX4BPR_F.mjs";import{t as applyUnsetPathsForWrite}from"./config-path-mutation-CQ3JedXX.mjs";import{isDeepStrictEqual}from"node:util"; //!src/config/deferred-plugin-migration-config.ts const snapshotMigrationFacts=/* @__PURE__ */ new WeakMap;function setDeferredPluginMigrationConfigFacts(e,t){if(t!==void 0)snapshotMigrationFacts.set(e,structuredClone(t))}function getDeferredPluginMigrationConfigFacts(e){return isRecord(e)?snapshotMigrationFacts.get(e):void 0}async function readConfigWriteMigrationFacts(e,t,n){return await readConfigWritePendingMigrations(e.path,t,n?.deferredPluginMigrations??getDeferredPluginMigrationConfigFacts(e.sourceConfig))}function readPathValue(e,t){let n=e;for(const e of t){if(isBlockedObjectKey(e))return;if(Array.isArray(n)){const t=parseConfigPathArrayIndex(e);n=t===void 0?void 0:n[t]}else n=isRecord(n)&&Object.hasOwn(n,e)?n[e]:void 0}return n}function uniquePaths(e){return[...new Map(e.map(e=>[JSON.stringify(e),e])).values()]}function pluginConfigPaths(e,t){const n=isRecord(e)&&isRecord(e.plugins)?e.plugins.entries:void 0;return isRecord(n)?Object.keys(n).filter(e=>normalizePluginPolicyId(e)===normalizePluginPolicyId(t)).map(e=>[`plugins`,`entries`,e,`config`]):[]}function hasDeferredPluginMigrationConfig(e,t){if((t.validationExcludedPaths??[]).some(t=>readPathValue(e,t)!==void 0))return true;const n=pluginConfigPaths(e,t.pluginId);return[...t.configPaths??[],...n].some(t=>{const r=readPathValue(e,t);return r!==void 0&&!(n.some(e=>e.length===t.length&&e.every((e,n)=>e===t[n]))&&isRecord(r)&&Object.keys(r).length===0)})}function resolveDeferredPluginMigrationConfigPaths(e){const t=(e.compatibilityMigrationPaths??[]).flatMap(t=>collectPluginConfigContractMatches({root:e.config,pathPattern:t}).map(({path:e})=>parseConcreteConfigPath(e)));const n=uniquePaths([...pluginConfigPaths(e.config,e.pluginId),[`session`,`store`],...t]).filter(t=>readPathValue(e.config,t)!==void 0);return{...n.length>0?{configPaths:n}:{},...t.length>0?{validationExcludedPaths:uniquePaths(t)}:{}}}function restorePath(e,t,n){const[r,...i]=n;if(r===void 0)return structuredClone(e);if(isBlockedObjectKey(r))return t;if(Array.isArray(e)){const n=parseConfigPathArrayIndex(r);if(n===void 0||n>=e.length)return t;const a=Array.isArray(t)?[...t]:[];a[n]=restorePath(e[n],a[n],i);return a}if(!isRecord(e)||!Object.hasOwn(e,r))return t;const a=isRecord(t)?{...t}:{};a[r]=restorePath(e[r],a[r],i);return a}function retainedPluginPaths(e,t,n){return(e.configPaths??[]).filter(r=>{if(r[0]!==`plugins`||r[1]!==`entries`||normalizePluginPolicyId(r[2]??``)!==normalizePluginPolicyId(e.pluginId))return true;const i=r.slice(0,3);return!n?.some(e=>isDeepStrictEqual(e,i))||readPathValue(t,i)!==void 0})}function assertDeferredPluginMigrationConfigEditAllowed(e){for(const t of e.pending)for(const n of retainedPluginPaths(t,e.nextConfig,e.unsetPaths))if(e.editedPaths.some(e=>n.every((t,n)=>e[n]===t)||e.every((e,t)=>n[t]===e))&&!isDeepStrictEqual(readPathValue(e.sourceConfig,n),readPathValue(e.nextConfig,n)))throw new Error(`Cannot edit retained config at "${toDotPath(n)}". ${formatDeferredPluginMigration(t)}`)}function preserveDeferredPluginMigrationConfig(e){const{explicitSetPaths:t,unsetPaths:n,auditOrigin:r}=e.writeOptions??{};const i=applyUnsetPathsForWrite(e.nextConfig,n);if(e.pending.length>0)assertDeferredPluginMigrationConfigEditAllowed({...e,nextConfig:i,unsetPaths:n,editedPaths:r===`config-rpc`?[[]]:[...t??[],...n??[]]});let a=e.nextConfig;for(const t of e.pending)for(const r of retainedPluginPaths(t,i,n))if(r.length>0)a=restorePath(e.sourceConfig,a,r);return isRecord(a)?a:e.nextConfig}function omitDeferredPluginMigrationConfig(e,t){return isRecord(e)?applyUnsetPathsForWrite(e,t?.flatMap(e=>e.validationExcludedPaths??[])):e}export{preserveDeferredPluginMigrationConfig as a,setDeferredPluginMigrationConfigFacts as c,omitDeferredPluginMigrationConfig as i,getDeferredPluginMigrationConfigFacts as n,readConfigWriteMigrationFacts as o,hasDeferredPluginMigrationConfig as r,resolveDeferredPluginMigrationConfigPaths as s,assertDeferredPluginMigrationConfigEditAllowed as t};