UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

114 lines (113 loc) • 5.86 kB
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs"; import { S as resolveIsConfigReadOnly, p as resolveCanonicalConfigPath } from "./paths-DAcAg5aV.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { y as resolveFutureConfigActionBlock } from "./config-env-vars-ByeGN-9N.mjs"; import { a as createConfigIoContext } from "./io.recovery-BpeT7cx2.mjs"; import { r as createInvalidConfigError } from "./io.invalid-config-DK-BYZ4Y.mjs"; import { s as parseConfigJson5 } from "./io.read-helpers-qAcqsRC0.mjs"; import { a as preserveDeferredPluginMigrationConfig } from "./deferred-plugin-migration-config-B3TJC-SY.mjs"; import { c as recoverConfigFromLastKnownGoodCore, n as prepareConfigRecoveryFromContext, t as createConfigIO } from "./io.factory-DEa_bi2j.mjs"; import { _ as recoverConfigFromJsonRootSuffix } from "./io.runtime-DsJ2dLCQ.mjs"; import "./io-4QsrsWTJ.mjs"; import { a as resolveCronJobsStorePathFromConfig } from "./paths-CY4cvrR6.mjs"; import { n as assertNoRetiredStateFiles } from "./state-migrations.retired-files-CuvyprJk.mjs"; import { t as findRetiredConfigUpgradeRequirement } from "./retired-config-formats-wq9eeal6.mjs"; import { t as note } from "./note-CnLgHvug.mjs"; import { t as classifyOtelGrpcMigrationOwnership } from "./include-migration-ownership-CWJZ0wk-.mjs"; import { t as applyLegacyDoctorMigrations } from "./legacy-config-compat-B1mpdXN2.mjs"; import { t as listRetiredCronStateFiles } from "./state-migrations.retired-cron-files-RrTWK6Mt.mjs"; import path from "node:path"; //#region src/commands/doctor-config-preflight-legacy-config.ts const transformDoctorRecoveryCandidate = (e) => { const t = classifyOtelGrpcMigrationOwnership({ snapshot: { path: e.configPath, includeProvenance: e.includeProvenance }, authoredConfig: e.candidate.parsed, resolvedConfig: e.resolvedConfig }); if (t && t.kind !== "direct") throw createInvalidConfigError(e.configPath, t.kind === "resolved-only" ? "candidate migration cannot persist an env-resolved diagnostics.otel.protocol repair" : "candidate migration requires an include-owned diagnostics.otel.protocol repair"); const n = applyLegacyDoctorMigrations(e.candidate.parsed, { sourceConfigBeforeMigrations: e.resolvedConfig, context: { authoredRaw: e.candidate.parsed, resolvedRaw: e.resolvedConfig } }); return n.next ? preserveDeferredPluginMigrationConfig({ sourceConfig: e.candidate.parsed, nextConfig: n.next, pending: e.deferredPluginMigrations }) : e.candidate.parsed; }; async function migrateLegacyDoctorConfig(e) { if (!e.enabled || resolveIsConfigReadOnly(process.env)) return; const t = await e.measure("legacy-config-migration", maybeMigrateLegacyConfig); if (t.length > 0) note(t.map((e) => `- ${e}`).join("\n"), "Doctor changes"); } /** Repair active legacy bytes before considering an older backup. */ async function prepareDoctorConfigRecovery(e) { let t = e.snapshotRead; let n = t.snapshot; const assertSupportedConfig = (e) => { const t = findRetiredConfigUpgradeRequirement(e); if (t) throw new Error(`${t.message} ${t.nextAction}`); }; assertSupportedConfig(n.sourceConfigBeforeMigrations ?? n.sourceConfig); const r = n.sourceConfigBeforeMigrations ?? n.sourceConfig; if (e.enabled && !e.stateMigrationsRequested && typeof asOptionalRecord(r?.cron)?.store === "string") assertNoRetiredStateFiles("Cron state (state migration is disabled)", await listRetiredCronStateFiles(resolveCronJobsStorePathFromConfig(r ?? {}))); let i = null; const a = e.enabled && !resolveFutureConfigActionBlock({ action: "recover config", snapshot: n }); if (a && n.valid && e.databaseAccess !== "none") { const r = await prepareConfigRecoveryFromContext(createConfigIoContext({ configPath: n.path }, transformDoctorRecoveryCandidate), n, "doctor-repair"); if (r) { await r.apply(); t = await e.readSnapshot(); n = t.snapshot; } } if (a && n.exists && !n.valid) { i = typeof n.raw === "string" && parseConfigJson5(n.raw).ok ? await e.planRepair(n) : null; let r = false; const a = e.databaseAccess === "none" ? createConfigIO({ configPath: n.path, observe: false, pluginValidation: "core-only" }).recoverConfigFromJsonRootSuffix : recoverConfigFromJsonRootSuffix; if (!i && await a(n, assertSupportedConfig)) { note("Removed non-JSON prefix from openclaw.json.", "Config"); r = true; } else if (!i && e.databaseAccess !== "none") { const e = createConfigIoContext({ configPath: n.path }, transformDoctorRecoveryCandidate); if (await recoverConfigFromLastKnownGoodCore({ snapshot: n, reason: "doctor-invalid-config", deps: e.deps, prepareCandidate: e.prepareRecoveryBackupCandidate })) { note("Restored openclaw.json from last-known-good; original saved as .clobbered.*.", "Config"); r = true; } } if (r) { t = await e.readSnapshot(); n = t.snapshot; } if (e.databaseAccess !== "none" && !n.valid && typeof n.raw === "string" && !parseConfigJson5(n.raw).ok) throw new Error(`Config at ${n.path} is not parseable and cannot be repaired automatically. The file remains unchanged. Inspect the exact parse error with ${formatCliCommand("openclaw config validate")}, then hand-edit the file; or move it aside and run ${formatCliCommand("openclaw onboard")} to generate a fresh config.`); } return { snapshotRead: t, activeConfigRepair: i }; } async function maybeMigrateLegacyConfig() { if (process.env.OPENCLAW_STATE_DIR?.trim() || process.env.OPENCLAW_HOME?.trim() || process.env.OPENCLAW_CONFIG_PATH?.trim()) return []; const { renameLegacyConfigFile: e } = await import("./state-migrations.state-dir-DUGRrWf7.mjs"); return e(path.dirname(resolveCanonicalConfigPath())); } //#endregion export { prepareDoctorConfigRecovery as n, migrateLegacyDoctorConfig as t };