UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 9.21 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{r as truncateUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import{t as isBlockedObjectKey}from"./prototype-keys-Dc7Yq32J.mjs";import{t as parseConfigPathArrayIndex}from"./path-array-index-DO9QgC44.mjs";import{n as formatConcreteConfigPath,s as toDotPath}from"./dot-path-B1bYAWM5.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{t as normalizeConfigModelSelectionParent}from"./model-input-normalization-CChsFZq0.mjs";import{n as quotePowerShellArg,t as quoteCliArg}from"./quote-cli-arg-BUHoGELl.mjs";import{t as rejectConfigNonFiniteNumbers}from"./value-tree-BXoqoxTN.mjs";import JSON5 from"json5"; //!src/cli/config-cli-path.ts function formatConfigSetPath(e,t,n){return formatConcreteConfigPath(t??e,n)}function refusalPathLabel(e,t){return e===`patch`?formatConfigSetPath(t):toDotPath(t)}function replacePathArgument(e){const t=quoteCliArg(e);const n=quotePowerShellArg(e);if(t===e||t===n)return`--replace-path ${t}`;return`--replace-path ${t} in bash and zsh, or --replace-path ${n} in PowerShell`}function parseConfigSetValue(e,t){const n=e.trim();let r;try{r=t?JSON.parse(n):JSON5.parse(n)}catch(n){if(t){const t=(n instanceof Error?n.message:String(n)).trim().replace(/[.。]+$/u,``);const r=e.length>48?`${truncateUtf16Safe(e,45).trimEnd()}...`:e;const i=[`Could not parse ${JSON.stringify(r)} as JSON for --strict-json.`,`${t}.`,`Use valid JSON. For structured changes, use a JSON5 config patch object file with \`${formatCliCommand(`openclaw config patch --file <path> --dry-run`)}\`.`,`For plain strings, omit --strict-json.`].join(` `);throw new Error(i,{cause:n})}return e}rejectConfigNonFiniteNumbers(r);return r}function validatePathSegments(e){for(const t of e)if(isBlockedObjectKey(t))throw new Error(`Invalid path segment: ${t}`)}function getAtPath(e,t){let n=e;for(const e of t){if(!n||typeof n!==`object`)return{found:false};if(Array.isArray(n)){const t=parseConfigPathArrayIndex(e);if(t===void 0||t>=n.length)return{found:false};n=n[t];continue}const t=n;if(!Object.hasOwn(t,e))return{found:false};n=t[e]}return{found:true,value:n}}function formatConfigUnsetMissingPathMessage(e){if(e.runtimeOnly)return`Config path not found in authored config: ${e.path}. It only exists after runtime defaults are applied, so there is nothing for config unset to remove. Use ${formatCliCommand(`openclaw config set <path> <value>`)} to override the inherited value.`;return`Config path not found: ${e.path}. Nothing was changed. Run ${formatCliCommand(`openclaw config get <path>`)} first if you are unsure of the path.`}function schemaHasType(e,t){return e.type===t||Array.isArray(e.type)&&e.type.includes(t)}function schemaAlternatives(e,t=/* @__PURE__ */ new Set){if(t.has(e))return[];t.add(e);const n=[e];for(const r of[`anyOf`,`oneOf`,`allOf`]){const i=e[r];if(!Array.isArray(i))continue;for(const e of i)if(isRecord(e))n.push(...schemaAlternatives(e,t))}return n}function schemaLooksArray(e){return schemaHasType(e,`array`)||isRecord(e.items)||Array.isArray(e.items)}function schemaLooksObject(e){return schemaHasType(e,`object`)||isRecord(e.properties)||e.additionalProperties===true||isRecord(e.additionalProperties)}function schemasAtPath(e,t){if(!e)return[];let n=[e];for(const e of t){n=n.flatMap(t=>{const n=[];for(const r of schemaAlternatives(t)){if(Object.keys(r).length===0){n.push(r);continue}if(schemaLooksArray(r)){const t=parseConfigPathArrayIndex(e);if(t!==void 0){const e=Array.isArray(r.items)?r.items[t]:r.items;if(isRecord(e))n.push(e)}continue}const t=(isRecord(r.properties)?r.properties:void 0)?.[e];if(isRecord(t))n.push(t);else if(r.additionalProperties===true)n.push({});else if(isRecord(r.additionalProperties))n.push(r.additionalProperties)}return n});if(n.length===0)return[]}return n}function isConfigSchemaPath(e,t){if(t.length===1&&t[0]===`$schema`)return true;return schemasAtPath(e,t).length>0}function setAtPath(e,t,n,r){const i=t.at(-1);if(i===void 0)throw new Error(`Config path must contain at least one segment`);let a=e;for(const[e,n]of t.slice(0,-1).entries()){const i=t[e+1];let o=false;if(i&&!r?.numericObjectKeys&&parseConfigPathArrayIndex(i)!==void 0){if(typeof r?.pathTokens?.[e+1]===`number`)o=true;else if(!r?.quotedNumericSegments?.has(e+1)){const n=t.slice(0,e+1);const i=schemasAtPath(r?.schema,n).flatMap(e=>schemaAlternatives(e));o=true;if(i.length>0){const e=i.some(e=>schemaLooksArray(e));o=e===i.some(e=>schemaLooksObject(e))||e}}}if(Array.isArray(a)){const e=parseConfigPathArrayIndex(n);if(e===void 0)throw new Error(`Expected numeric index for array segment "${n}"`);const t=a[e];if(!t||typeof t!==`object`)a[e]=o?[]:{};a=a[e];continue}if(!a||typeof a!==`object`)throw new Error(`Cannot traverse into "${n}" (not an object)`);const s=a;const c=Object.hasOwn(s,n)?s[n]:void 0;if(!c||typeof c!==`object`)s[n]=normalizeConfigModelSelectionParent(c,t,e)??(o?[]:{});a=s[n]}if(Array.isArray(a)){const e=parseConfigPathArrayIndex(i);if(e===void 0)throw new Error(`Expected numeric index for array segment "${i}"`);a[e]=n;return}if(!a||typeof a!==`object`)throw new Error(`Cannot set "${i}" (parent is not an object)`);a[i]=n}function modelArrayIds(e){if(!Array.isArray(e))return null;const t=/* @__PURE__ */ new Set;for(const n of e){if(!isRecord(n)||typeof n.id!==`string`||!n.id.trim())return null;t.add(n.id.trim())}return t}function mergeModelArrays(e,t,n){const r=[...e];const i=[];const a=/* @__PURE__ */ new Map;for(const[e,t]of r.entries())if(isRecord(t)&&typeof t.id===`string`&&t.id.trim())a.set(t.id.trim(),e);for(const e of t){if(isRecord(e)&&typeof e.id===`string`&&e.id.trim()){const t=e.id.trim();const o=a.get(t);if(o!==void 0){const t=r[o];r[o]=isRecord(t)?{...t,...e}:e;for(const t of Object.keys(e))i.push([...n,String(o),t]);continue}a.set(t,r.length)}i.push([...n,String(r.length)]);r.push(e)}return{value:r,suppliedPaths:i}}function isProviderModelListPath(e){return e.length===4&&e[0]===`models`&&e[1]===`providers`&&e[3]===`models`}function mergePathSegments(e){const t=[];for(let n=e;n;n=n.parent)t.push(n.segment);return t.toReversed()}function isProviderModelListMergePath(e){const t=e.parent?.parent;const n=t?.parent;return e.segment===`models`&&t?.segment===`providers`&&n?.segment===`models`&&n.parent===void 0}function mergeConfigValue(e,t,n,r){if(isProviderModelListPath(n)&&Array.isArray(e)&&Array.isArray(t))return mergeModelArrays(e,t,n);if(isRecord(e)&&isRecord(t)){const r={...e};const i=[];let a;for(const e of n)a={parent:a,segment:e};const o=[{target:r,patch:t,path:a}];while(o.length>0){const e=o.pop();for(const[t,n]of Object.entries(e.patch)){const r=e.target[t];const a={parent:e.path,segment:t};if(Object.hasOwn(e.target,t)&&isProviderModelListMergePath(a)&&Array.isArray(r)&&Array.isArray(n)){const o=mergeModelArrays(r,n,mergePathSegments(a));e.target[t]=o.value;i.push(...o.suppliedPaths)}else if(Object.hasOwn(e.target,t)&&isRecord(r)&&isRecord(n)){const i={...r};e.target[t]=i;o.push({target:i,patch:n,path:a})}else{e.target[t]=n;i.push(mergePathSegments(a))}}}return{value:r,suppliedPaths:i}}const i=refusalPathLabel(r,n);throw new Error(`Cannot merge ${i}; use ${r===`patch`?replacePathArgument(i):`--replace`} to replace intentionally.`)}function mergeAtPath(e,t,n,r){const i=getAtPath(e,t);const a=i.found?mergeConfigValue(i.value,n,t,r?.command??`set`):{value:n,suppliedPaths:[t]};setAtPath(e,t,a.value,r);return a.suppliedPaths}function assertNonDestructiveReplacement(e){if(e.allowReplace)return;const t=getAtPath(e.root,e.path);if(!t.found)return;const n=refusalPathLabel(e.command,e.path);let r;let i;const a=e.path;const o=a.join(`.`);if((o===`agents.defaults.models`||o===`models.providers`||a.length===3&&a[0]===`models`&&a[1]===`providers`||o===`agents.entries`||o===`plugins.entries`||o===`auth.profiles`)&&isRecord(t.value)){if(!isRecord(e.value))return;const n=new Set(Object.keys(e.value));r=Object.keys(t.value).filter(e=>!n.has(e));i=`object values`}else if(isProviderModelListPath(e.path)){const n=modelArrayIds(t.value);const a=modelArrayIds(e.value);if(!n||!a)return;r=[...n].filter(e=>!a.has(e));i=`by id`}else return;if(r.length>0){const t=r.slice(0,6);const a=r.length>t.length?`, ... ${r.length-t.length} more`:``;const o=`${t.join(`, `)}${a}`;const s=e.command===`patch`?`Use ${replacePathArgument(n)} to replace intentionally.`:`Use --merge to merge ${i} or --replace to replace intentionally.`;throw new Error(`Refusing to replace ${n}; it would remove existing entries: ${o}. ${s}`)}}function unsetAtPath(e,t){const n=t.at(-1);if(n===void 0)return{removed:false};const r=getAtPath(e,t.slice(0,-1)).value;if(Array.isArray(r)){const e=parseConfigPathArrayIndex(n);if(e===void 0||e>=r.length)return{removed:false};r.splice(e,1);return{removed:true,leafContainer:`array`}}if(!r||typeof r!==`object`)return{removed:false};const i=r;if(!Object.hasOwn(i,n))return{removed:false};delete i[n];return{removed:true,leafContainer:`object`}}export{isConfigSchemaPath as a,setAtPath as c,getAtPath as i,unsetAtPath as l,formatConfigSetPath as n,mergeAtPath as o,formatConfigUnsetMissingPathMessage as r,parseConfigSetValue as s,assertNonDestructiveReplacement as t,validatePathSegments as u};