UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.43 kB
import{l as normalizeOptionalString,m as readNonBlankString}from"./string-coerce-CpTFcSwY.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{c as readFileDescriptorBoundedSync}from"./boundary-file-read-bB9An35K.mjs";import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import"./errors-D37BJTfI.mjs";import{r as decodeUtf8Input}from"./utf8-input-CiFSnxL0.mjs";import{t as rejectConfigNonFiniteNumbers}from"./value-tree-BXoqoxTN.mjs";import fs from"node:fs";import JSON5 from"json5"; //!src/cli/config-set-input.ts const CONFIG_MUTATION_FILE_MAX_BYTES=8388608;function readConfigMutationFileSync(e,t){const n=process.platform===`win32`?`r`:fs.constants.O_RDONLY|fs.constants.O_NONBLOCK;let r;try{r=fs.openSync(e,n)}catch(n){if(hasErrnoCode(n,`ENOENT`))throw new Error(`${t} not found: ${e}. Check the path and try again.`,{cause:n});throw n}try{if(!fs.fstatSync(r).isFile())throw new Error(`${t} must be a regular file: ${e}. Choose a JSON5 input file and try again.`);try{return decodeUtf8Input(readFileDescriptorBoundedSync(r,CONFIG_MUTATION_FILE_MAX_BYTES),t)}catch(n){if(n instanceof RangeError)throw new RangeError(`${t} exceeds the 8 MiB supported maximum (${CONFIG_MUTATION_FILE_MAX_BYTES} bytes): ${e}`,{cause:n});throw n}}finally{fs.closeSync(r)}}function resolveConfigSetMode(e){const t=e.refProvider!==void 0||e.refSource!==void 0||e.refId!==void 0;const n=Boolean(e.providerSource!==void 0||e.providerAllowlist?.length||e.providerPath!==void 0||e.providerMode!==void 0||e.providerTimeoutMs!==void 0||e.providerMaxBytes!==void 0||e.providerCommand!==void 0||e.providerArg?.length||e.providerNoOutputTimeoutMs!==void 0||e.providerMaxOutputBytes!==void 0||e.providerJsonOnly||e.providerEnv?.length||e.providerPassEnv?.length||e.providerTrustedDir?.length);if(e.batchJson!==void 0||e.batchFile!==void 0){if(t||n)throw new Error(`config set mode error: batch mode (--batch-json/--batch-file) cannot be combined with ref builder (--ref-*) or provider builder (--provider-*) flags.`);return`batch`}if(t&&n)throw new Error(`config set mode error: choose exactly one mode: ref builder (--ref-provider/--ref-source/--ref-id) or provider builder (--provider-*), not both.`);return t?`ref_builder`:n?`provider_builder`:e.strictJson||e.json?`json`:`value`}function parseConfigMutationJson5(e,t){let n;try{n=JSON5.parse(e)}catch(e){throw new Error(`Failed to parse ${t}: ${String(e)}`,{cause:e})}rejectConfigNonFiniteNumbers(n);return n}function parseBatchEntries(e,t){const n=parseConfigMutationJson5(e,t);if(!Array.isArray(n))throw new Error(`${t} must be a JSON array.`);if(n.length===0)throw new Error(`${t} must contain at least one config update.`);return n.map((e,n)=>{if(!isRecord(e))throw new Error(`${t}[${n}] must be an object.`);const r=normalizeOptionalString(e.path);if(!r)throw new Error(`${t}[${n}].path is required.`);const i=[`value`,`ref`,`provider`].filter(t=>Object.hasOwn(e,t));const a=i.length===1?i[0]:void 0;if(a===void 0)throw new Error(`${t}[${n}] must include exactly one of: value, ref, provider.`);return{path:r,[a]:e[a]}})}function parseConfigSetCurrentExpectation(e){const t=e.expectCurrentAbsent===true;const n=e.expectCurrentJson;const r=n!==void 0;if(!t&&!r)return;if(t&&r)throw new Error(`config set mode error: choose either --expect-current-absent or --expect-current-json, not both.`);if(e.dryRun)throw new Error(`config set mode error: conditional expectations cannot be combined with --dry-run.`);if(e.batchJson!==void 0||e.batchFile!==void 0)throw new Error(`config set mode error: conditional expectations require one path operation and cannot be combined with batch mode.`);if(n===void 0)return{kind:`absent`};let i;try{i=JSON.parse(n);rejectConfigNonFiniteNumbers(i)}catch(e){throw new Error(`config set mode error: --expect-current-json must be valid JSON.`,{cause:e})}return{kind:`json`,value:i}}function parseBatchSource(e){const t=e.batchJson;if(t!==void 0){if(e.batchFile!==void 0)throw new Error(`Use either --batch-json or --batch-file, not both.`);return parseBatchEntries(t,`--batch-json`)}if(e.batchFile===void 0)return null;const n=readNonBlankString(e.batchFile);if(!n)throw new Error(`--batch-file must not be empty.`);return parseBatchEntries(readConfigMutationFileSync(n,`--batch-file`),`--batch-file`)}export{resolveConfigSetMode as a,readConfigMutationFileSync as i,parseConfigMutationJson5 as n,parseConfigSetCurrentExpectation as r,parseBatchSource as t};