UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

199 lines (198 loc) • 8.45 kB
import { t as ExitError } from "./runtime-B-SLYP-m.mjs"; import { S as resolveIsConfigReadOnly } from "./paths-DAcAg5aV.mjs"; import { t as adoptProcessPluginCache, u as getPluginMetadataSnapshotCache } from "./plugin-cache-B_eRuqTM.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { a as isExistingOpenClawStateSchema, i as getExistingOpenClawStateSchemaPath } from "./openclaw-state-db-schema-policy-DvHzXD76.mjs"; import { a as isConfigReadFailure, n as createConfigReadError, r as createInvalidConfigError, t as configFailureHeading } from "./io.invalid-config-DK-BYZ4Y.mjs"; import { O as setRuntimeConfigSnapshot } from "./runtime-snapshot-WbwbY5b7.mjs"; import { s as readConfigFileSnapshot } from "./io.runtime-DsJ2dLCQ.mjs"; import "./config-CMA-J084.mjs"; import { a as withSuppressedNotes } from "./note-CnLgHvug.mjs"; //#region 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" ]); 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-BabpMUdN.mjs")).runStartupConfigPreflight({ gateway: s, ...e.measure ? { measure: e.measure } : {}, ...a === "status" ? { observe: false } : {}, ...s ? { validateStartupConfig: async (t) => { const { getGatewayStartGuardErrors: n } = await import("./pre-bootstrap-BvgO1zPk.mjs"); const r = n({ allowUnconfigured: e.allowInvalid, configExists: t.exists, mode: t.config.gateway?.mode }); if (r.length > 0) { e.runtime.error(r.join("\n")); 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-BWhzokOu.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-U_pi1HLf.mjs"), import("./issue-location-CkMq9pCi.mjs"), import("./terminal-core/theme.js"), import("./utils-C6VO5Omq.mjs"), import("./command-format-BehjL3Ya.mjs"), import("./recovery-policy-CTp11RHF.mjs"), import("./config-recovery-hints-CkKLc1qY.mjs") ]); const E = v(m); const D = m.legacyIssues.length > 0 ? _(m.legacyIssues, "-") : []; const O = b(); const muted = (e) => y(O, x.muted, e); const error = (e) => y(O, x.error, e); const heading = (e) => y(O, x.heading, e); const commandText = (e) => y(O, x.command, e); const k = 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("\n")); } if (D.length > 0) { e.runtime.error(muted("Legacy config keys detected:")); e.runtime.error(D.map((e) => ` ${error(e)}`).join("\n")); } e.runtime.error(""); const A = w(m); const j = resolveIsConfigReadOnly(); const M = a === "gateway" && (o === void 0 || o === "run" || o === "start" || o === "restart"); const N = !g || M && e.allowInvalid !== true; const P = N && !k && !e.suppressDoctorStdout && !j && !l; if (k) e.runtime.error(muted("Resolve the read error shown above, then retry.")); else if (A || j || !P) { const t = A ? T() : j ? (await import("./config-write-guard-C8xOz4HA.mjs")).createConfigMutationError({ configPath: m.path }).message : commandText(C("openclaw doctor --fix")); e.runtime.error(`${muted("Fix:")} ${t}`); } e.runtime.error(`${muted("Inspect:")} ${commandText(C("openclaw config validate"))}`); e.runtime.error(muted(k ? "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-hTr0tmcB.mjs")).isJsonOutputModeActive(process.argv)) { const { writeInvalidConfigCliJson: t } = await import("./config-validation-output-BWmyUQ4V.mjs"); t(e.runtime, m); } if (A && M) { e.runtime.exit(78); return; } if (P && !A) { const { offerInvalidConfigRecovery: t } = await import("./invalid-config-recovery-ExdYI41x.mjs"); if (await t({ runtime: e.runtime, retry: async () => { configSnapshotPromise = null; const t = f ? await (await import("./startup-config-preflight-BabpMUdN.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("\n") || "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 && !k ? 78 : 1); } catch (e) { t.e = e; } finally { await t.d(); } } const testApi = { resetConfigGuardStateForTests }; //#endregion export { ensureConfigReady, testApi };