UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.01 kB
import{f as shortenHomePath}from"./utils-Dlk0IYAa.mjs";import{t as isPlainObject}from"./plain-object-CHrMoU5Q.mjs";import{m as resolveConfigPath}from"./paths-D_FOvGon.mjs";import{f as resolveIdentityPathViaExistingAncestorSync}from"./path-existence-BR9MXlgL.mjs";import{n as getDatabaseEngineSelection}from"./context-DZjni16e.mjs";import{n as formatConfigPatchPath,t as collectBaseArrayPaths}from"./patch-replace-paths-BCUuHDoH.mjs";import{n as createMergePatch,t as applyMergePatch}from"./merge-patch-CKGPHlQC.mjs";import{t as prepareConfigWriteTopology}from"./io.write-topology-CdliMKhL.mjs";import{isDeepStrictEqual}from"node:util"; //!src/cli/config-cli-owner-patch.ts const MAX_REPLACE_PATHS=256;function collectReplacePaths(e,t,n){if(!isPlainObject(e))return[];return Object.entries(e).flatMap(([e,r])=>{const i=formatConfigPatchPath(n,e);const a=t.map(t=>isPlainObject(t)?t[e]:void 0);if(isPlainObject(r))return collectReplacePaths(r,a,i);const o=a.flatMap(e=>collectBaseArrayPaths(e,i));return Array.from(new Set(Array.isArray(r)?[i].concat(o):o))})}function createOwnerMergePatch(e,t,n){const r=createMergePatch(e,t);const i=collectReplacePaths(r,[e,n]);const apply=e=>{return applyMergePatch(e,r,{mergeObjectArraysById:true,replaceArrayPaths:new Set(i)})};return{patch:r,replacePaths:i,apply}}async function prepareConfigOwnerChange(e){const{previous:t,explicitSetPaths:n,unsetPaths:r,...i}=e;const a=await prepareConfigWriteTopology({...i,nextConfig:e.next,options:{explicitSetPaths:n},unsetPaths:r});const refusal=e=>new Error(`The running Gateway would save a different result for this change: it cannot see which paths this command set, so it would recompute environment references or agent, auth or session-store ownership differently. Nothing was sent and no settings were saved. Split the change into separate commands (for example set an owner path after the roster or store change), or stop the Gateway through its service owner, wait for ownership to release, then rerun this exact command.`,e===void 0?void 0:{cause:e});const o=await prepareConfigWriteTopology({...i,nextConfig:createOwnerMergePatch(t,a.authoredConfig).apply(e.snapshot.sourceConfig),options:{},unsetPaths:[]}).catch(e=>{throw refusal(e)});if(!isDeepStrictEqual(o.authoredConfig,a.authoredConfig))throw refusal();return{previous:t,next:a.authoredConfig,clearedSessionStoreOwner:a.clearedSessionStoreOwner}}function buildConfigOwnerPatch(e,t){const{patch:n,replacePaths:r,apply:i}=createOwnerMergePatch(e.previous,e.next,t.config);if(r.length>MAX_REPLACE_PATHS||!isDeepStrictEqual(i(e.previous),e.next))throw new Error(`This change cannot be expressed as a Gateway config patch, so it was not sent to the running Gateway. No settings were saved. To apply it, stop the Gateway through its service owner, wait for ownership to release, then rerun this exact command.`);return{raw:JSON.stringify(n),...typeof t.hash===`string`?{baseHash:t.hash}:{},...r.length>0?{replacePaths:r}:{}}}async function bindConfigOwner(e,t){const n=await e(`config.get`,{});const r=resolveConfigPath(t);if(typeof n.path!==`string`||resolveIdentityPathViaExistingAncestorSync(n.path)!==resolveIdentityPathViaExistingAncestorSync(r))throw new Error(`The running Gateway owns this state directory but uses config file ${typeof n.path===`string`?shortenHomePath(n.path):`<unknown>`}, not ${shortenHomePath(r)}. Nothing was sent and no settings were saved. Rerun with the Gateway's config path, or stop the Gateway through its service owner and rerun this exact command.`);if(!getDatabaseEngineSelection()){const{readConfigSnapshotForEngineSelection:e}=await import(`./config-preflight-snapshot-CYPXIJMO.mjs`);await e()}return async t=>{const r=await e(`config.patch`,buildConfigOwnerPatch(t,n));return{hint:r.restart||r.restartPending?`The Gateway is restarting to apply it.`:void 0,warnings:[r.billingRouteWarnings,r.warnings].flatMap(e=>Array.isArray(e)?e.filter(e=>typeof e===`string`):[])}}}export{prepareConfigOwnerChange as n,bindConfigOwner as t};