UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 7.57 kB
import{t as ExitError}from"./runtime-DIcLIHcp.mjs";import{t as adoptProcessPluginCache,u as getPluginMetadataSnapshotCache}from"./plugin-cache-B6QVpzXr.mjs";import{S as resolveIsConfigReadOnly}from"./paths-D_FOvGon.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{s as resolveOpenClawStateSqlitePath}from"./openclaw-state-db.paths-CbgXfEg1.mjs";import{a as isExistingOpenClawStateSchema,i as getExistingOpenClawStateSchemaPath}from"./openclaw-state-db-schema-policy-S6cXjtH_.mjs";import{a as isConfigReadFailure,n as createConfigReadError,r as createInvalidConfigError,t as configFailureHeading}from"./io.invalid-config-B2POcxOa.mjs";import{O as setRuntimeConfigSnapshot}from"./runtime-snapshot-Bg5K4JNu.mjs";import{s as readConfigFileSnapshot}from"./io.runtime-BvSeVFIY.mjs";import"./config-DkZ8y8Gv.mjs";import{a as withSuppressedNotes}from"./note-e4DtVgg2.mjs"; //!src/cli/program/config-guard.ts const ALLOWED_INVALID_COMMANDS=/* @__PURE__ */ new Set([`audit`,`doctor`,`logs`,`health`,`help`,`status`]);const ALLOWED_INVALID_GATEWAY_SUBCOMMANDS=/* @__PURE__ */ new Set([`run`,`status`,`probe`,`health`,`discover`,`install`,`uninstall`,`start`,`stop`,`restart`]);const TRAILING_DOCTOR_HINT=/(;\s*|(?<=[.!?])\s+)(?:re)?run\s+["`]openclaw doctor(?:\s+--fix)?["`](?:\s+to\s+((?:(?![.!?]\s).)+?))?[.!?]?$/iu;function withoutTrailingDoctorCommand(e){const t=TRAILING_DOCTOR_HINT.exec(e);if(!t)return e;const[,n=``,r]=t;const i=e.slice(0,t.index);if(!r)return/[.!?]$/u.test(i)?i:`${i}.`;return`${i}${n}${n.startsWith(`;`)?r:r.charAt(0).toUpperCase()+r.slice(1)}.`}let didRunStartupConfigPreflight=false;let configSnapshotPromise=null;function resetConfigGuardStateForTests(){didRunStartupConfigPreflight=false;configSnapshotPromise=null}async function getConfigSnapshot(e,t){if(e?.observe===false)return readConfigFileSnapshot({...e,...t?{measure:t}:{}});if(!configSnapshotPromise){const e=readConfigFileSnapshot(t?{measure:t}:void 0);configSnapshotPromise=e;e.catch(()=>{if(configSnapshotPromise===e)configSnapshotPromise=null})}return configSnapshotPromise}async function ensureConfigReady(e){try{var t=_usingCtx();const n=t.a(new AsyncDisposableStack);let r;n.defer(async()=>{await r?.[Symbol.asyncDispose]()});const i=e.commandPath??[];const a=i[0];const o=i[1];const s=a===`gateway`&&(o===void 0||o===`run`||o.trim()===``);const c=getExistingOpenClawStateSchemaPath();const l=c!==void 0&&(a===`node`&&o===`run`||a===`connect`);if(c!==void 0){if(!l)throw new Error(`The managed node runtime cannot run shared-state maintenance commands.`);if(!isExistingOpenClawStateSchema(resolveOpenClawStateSqlitePath()))throw new Error(`The managed node runtime state directory changed after launcher admission.`)}const u=(a===`gateway`||a===`daemon`)&&o===`restart`;let d=null;const f=!e.validateConfigOnly&&a!==`doctor`&&!l&&a!==`config`&&a!==`health`&&a!==`logs`&&a!==`sessions`&&!u&&!(a===`update`&&o===`status`);if(!didRunStartupConfigPreflight&&f){didRunStartupConfigPreflight=true;const runStartupConfigPreflight=async()=>(await import(`./startup-config-preflight-C391wpm6.mjs`)).runStartupConfigPreflight({gateway:s,...e.measure?{measure:e.measure}:{},...a===`status`?{observe:false}:{},...s?{validateStartupConfig:async t=>{const{getGatewayStartGuardErrors:n}=await import(`./pre-bootstrap-DlQB7q0s.mjs`);const r=n({allowUnconfigured:e.allowInvalid,configExists:t.exists,mode:t.config.gateway?.mode});if(r.length>0){e.runtime.error(r.join(` `));throw new ExitError(78)}}}:{},...e.beforeStatePreparation?{beforeStatePreparation:e.beforeStatePreparation}:{}});try{d=!e.suppressDoctorStdout?await runStartupConfigPreflight():await withSuppressedNotes(runStartupConfigPreflight);r=d.pluginCache}catch(t){if(s)await(await import(`./startup-maintenance-Dq8m-yia.mjs`)).handleGatewayStartupMaintenance(t);if(t instanceof ExitError)e.runtime.exit(t.code);throw t}}const p=e.validateConfigOnly||a===`logs`?{observe:false,pluginValidation:`core-only`}:l||a===`status`||u?{observe:false}:void 0;const m=d?.snapshot??await getConfigSnapshot(p,e.measure);const h=a===`gateway`&&(o===void 0||o.trim()===``);const g=a?e.allowInvalid===true||ALLOWED_INVALID_COMMANDS.has(a)||h||a===`gateway`&&o&&ALLOWED_INVALID_GATEWAY_SUBCOMMANDS.has(o):false;if(!(m.exists&&!m.valid)){setRuntimeConfigSnapshot(m.runtimeConfig??m.config,m.sourceConfig);if(s&&d?.pluginMetadataSnapshot){adoptProcessPluginCache(getPluginMetadataSnapshotCache(d.pluginMetadataSnapshot));r=void 0}return}const[{formatConfigIssueLines:_},{renderConfigValidationIssueLines:v},{colorize:y,isRich:b,theme:x},{shortenHomePath:S},{formatCliCommand:C},{isPluginPackagingRuntimeOutputInvalidConfigSnapshot:w},{formatPluginPackagingRuntimeOutputRecoveryHint:T}]=await Promise.all([import(`./issue-format-BityoCuQ.mjs`),import(`./issue-location-C4AXigVA.mjs`),import(`./terminal-core/theme.js`),import(`./utils-B7PqG5qw.mjs`),import(`./command-format-CrgMMWZl.mjs`),import(`./recovery-policy-DHvS0Z6i.mjs`),import(`./config-recovery-hints-BI2iwpZK.mjs`)]);const E=v(m);const D=resolveIsConfigReadOnly();const O=_(D?m.legacyIssues.map(e=>({...e,message:withoutTrailingDoctorCommand(e.message)})):m.legacyIssues,`-`);const k=b();const muted=e=>y(k,x.muted,e);const error=e=>y(k,x.error,e);const heading=e=>y(k,x.heading,e);const commandText=e=>y(k,x.command,e);const A=isConfigReadFailure(m);e.runtime.error(heading(configFailureHeading(m)));e.runtime.error(`${muted(`File:`)} ${muted(S(m.path))}`);if(E.length>0){e.runtime.error(muted(`Problem:`));e.runtime.error(E.map(e=>` ${error(e)}`).join(` `))}if(O.length>0){e.runtime.error(muted(`Legacy config keys detected:`));e.runtime.error(O.map(e=>` ${error(e)}`).join(` `))}e.runtime.error(``);const j=w(m);const M=a===`gateway`&&(o===void 0||o===`run`||o===`start`||o===`restart`);const N=!g||M&&e.allowInvalid!==true;const P=N&&!A&&!e.suppressDoctorStdout&&!D&&!l;if(A)e.runtime.error(muted(`Resolve the read error shown above, then retry.`));else if(j||D||!P){const t=j?T():D?(await import(`./config-write-guard-Ofxe5ue0.mjs`)).createConfigMutationError({configPath:m.path}).message:commandText(C(`openclaw doctor --fix`));e.runtime.error(`${muted(`Fix:`)} ${t}`)}if(!D){e.runtime.error(`${muted(`Inspect:`)} ${commandText(C(`openclaw config validate`))}`);e.runtime.error(muted(A?`Audit, status, health, logs, and doctor commands still run when config cannot be read.`:`Audit, status, health, logs, and doctor commands still run with invalid config.`))}if(N&&(await import(`./json-output-mode-D0MKcXgn.mjs`)).isJsonOutputModeActive(process.argv)){const{writeInvalidConfigCliJson:t}=await import(`./config-validation-output-BRQG7Cdz.mjs`);t(e.runtime,m)}if(j&&M){e.runtime.exit(78);return}if(P&&!j){const{offerInvalidConfigRecovery:t}=await import(`./invalid-config-recovery-CRKNfEH9.mjs`);if(await t({runtime:e.runtime,retry:async()=>{configSnapshotPromise=null;const t=f?await(await import(`./startup-config-preflight-C391wpm6.mjs`)).runStartupConfigPreflight({gateway:false,...e.measure?{measure:e.measure}:{},...p?.observe===false?{observe:false}:{}}):void 0;n.use(t?.pluginCache);const r=t?.snapshot??await getConfigSnapshot(p,e.measure);if(r.exists&&!r.valid){const e=v(r).join(` `)||`Unknown validation issue.`;throw isConfigReadFailure(r)?createConfigReadError(r,e):createInvalidConfigError(r.path,e)}setRuntimeConfigSnapshot(r.runtimeConfig??r.config,r.sourceConfig)}}))return;e.runtime.exit(M?78:1);return}if(N)e.runtime.exit(M&&!A?78:1)}catch(e){t.e=e}finally{await t.d()}}const testApi={resetConfigGuardStateForTests};export{ensureConfigReady,testApi};