openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
200 lines (199 loc) • 6.87 kB
JavaScript
import { g as readStringValue } from "../../string-coerce-CCIiB4H7.mjs";
import { c as resolveUserPath } from "../../home-dir-EKmgmdPG.mjs";
import { t as asBoolean } from "../../boolean-2vRuVafy.mjs";
import { a as asOptionalRecord } from "../../record-coerce-CVPuDqcc.mjs";
import { a as extractErrorCode } from "../../error-coercion-hDrHOSGy.mjs";
import { r as resolveStateDir } from "../../state-dir-DiG0gXzD.mjs";
import "../../state-paths-qdCZ9HDY.mjs";
import "../../string-coerce-runtime-BwAZZDf5.mjs";
import "../../text-utility-runtime-Ds8jsG0J.mjs";
import "../../error-runtime-BM740gJj.mjs";
import { i as resolvePluginConfigObject } from "../../plugin-config-runtime-CFUaG6iN.mjs";
import fs from "node:fs";
import path from "node:path";
import fs$1 from "node:fs/promises";
//#region extensions/canvas/src/config-migration.ts
const RETIRED_HOST_KEYS = [
"root",
"port",
"liveReload"
];
function readLegacyCanvasRoot(e) {
const t = asOptionalRecord(asOptionalRecord(e)?.canvasHost);
const n = asOptionalRecord(resolvePluginConfigObject(e, "canvas")?.host);
return {
...t,
...n
}.root;
}
function resolveLegacyCanvasDocumentsDir(e) {
const t = readStringValue(readLegacyCanvasRoot(e.config))?.trim();
if (!t) return null;
const n = path.join(path.resolve(resolveUserPath(t, e.env)), "documents");
const r = path.resolve(e.stateDir, "canvas", "documents");
if (n === r) return null;
try {
if (fs.realpathSync(n) === fs.realpathSync(r)) return null;
} catch {}
return n;
}
function listLegacyCanvasDocumentIds(e) {
try {
return fs.readdirSync(e, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name).toSorted();
} catch (t) {
if (extractErrorCode(t) === "ENOENT") return [];
throw new Error(`Cannot read Canvas documents at ${e}: ${String(t)}. Keep plugins.entries.canvas.config.host.root, fix access, then rerun "openclaw doctor --fix".`, { cause: t });
}
}
/** Removes retired file-host settings while preserving the route enablement choice. */
function migrateCanvasHostConfig(e) {
const t = asOptionalRecord(e.canvasHost);
const n = resolvePluginConfigObject(e, "canvas");
const r = asOptionalRecord(n?.host);
const i = readLegacyCanvasRoot(e);
let a = readStringValue(i)?.includes("${") === true;
if (!a) try {
const t = resolveLegacyCanvasDocumentsDir({
config: e,
env: process.env,
stateDir: resolveStateDir()
});
a = t !== null && listLegacyCanvasDocumentIds(t).length > 0;
} catch {
a = true;
}
const o = RETIRED_HOST_KEYS.filter((e) => Object.hasOwn(r ?? {}, e) && !(e === "root" && a));
if (!t && o.length === 0) return null;
const s = structuredClone(e);
delete s.canvasHost;
const c = asBoolean(r?.enabled) ?? asBoolean(t?.enabled);
const l = asOptionalRecord(s.plugins) ?? {};
const u = asOptionalRecord(l.entries) ?? {};
const d = asOptionalRecord(u.canvas) ?? {};
const f = asOptionalRecord(d.config) ?? {};
if (r || c !== void 0 || a) {
if (c === void 0 && !a) delete f.host;
else f.host = {
...c !== void 0 ? { enabled: c } : {},
...a ? { root: i } : {}
};
d.config = f;
u.canvas = d;
l.entries = u;
s.plugins = l;
}
const p = [];
if (t) p.push(a ? "Migrated canvasHost to plugins.entries.canvas.config.host; retained root for document migration retry." : c === void 0 ? "Removed retired canvasHost configuration." : "Migrated canvasHost.enabled to plugins.entries.canvas.config.host.enabled.");
if (o.length > 0) p.push(`Removed retired Canvas host config: ${o.map((e) => `plugins.entries.canvas.config.host.${e}`).join(", ")}.`);
return {
config: s,
changes: p
};
}
//#endregion
//#region extensions/canvas/doctor-contract-api.ts
const RETIRED_CANVAS_HOST_CONFIG_PATH = [
"plugins",
"entries",
"canvas",
"config",
"host"
];
/** Retired Canvas file-host settings detected before strict plugin validation. */
const legacyConfigRules = [{
path: ["canvasHost"],
message: "canvasHost is retired; only plugins.entries.canvas.config.host.enabled remains. Run \"openclaw doctor --fix\"."
}, ...[
"root",
"port",
"liveReload"
].map((e) => ({
path: [...RETIRED_CANVAS_HOST_CONFIG_PATH, e],
message: `${[...RETIRED_CANVAS_HOST_CONFIG_PATH, e].join(".")} is retired. Run "openclaw doctor --fix".`
}))];
/** Removes retired file-host config while preserving the surviving enablement switch. */
function normalizeCompatibilityConfig({ cfg: e }) {
return migrateCanvasHostConfig(e) ?? {
config: e,
changes: []
};
}
const stateMigrations = [{
id: "canvas-custom-root-documents-to-core",
label: "Canvas documents in a custom host root",
collectBackupResources(e) {
const t = resolveLegacyCanvasDocumentsDir(e);
if (!t || listLegacyCanvasDocumentIds(t).length === 0) return [];
return [{
path: t,
kind: "directory"
}, {
path: path.resolve(e.stateDir, "canvas", "documents"),
kind: "directory"
}];
},
async detectLegacyState(e) {
const t = resolveLegacyCanvasDocumentsDir(e);
if (!t) return null;
const n = listLegacyCanvasDocumentIds(t);
if (n.length === 0) return null;
return { preview: [`- Canvas documents: ${t} -> ${path.resolve(e.stateDir, "canvas", "documents")} (${n.length} document(s))`] };
},
async migrateLegacyState(e) {
const t = [];
const n = [];
const r = resolveLegacyCanvasDocumentsDir(e);
if (!r) return {
changes: t,
warnings: n
};
const i = listLegacyCanvasDocumentIds(r);
if (i.length === 0) return {
changes: t,
warnings: n
};
const { pathExists: a } = await import("../../plugin-sdk/text-utility-runtime.js");
const o = path.resolve(e.stateDir, "canvas", "documents");
await fs$1.mkdir(o, { recursive: true });
let s = 0;
for (const e of i) {
const t = path.join(r, e);
const i = path.join(o, e);
let c;
try {
if (await a(i)) throw new Error("core target already exists");
c = await fs$1.mkdtemp(path.join(o, ".canvas-migrate-"));
const n = path.join(c, e);
await fs$1.cp(t, n, {
recursive: true,
errorOnExist: true,
force: false
});
if (await a(i)) throw new Error("core target was created during migration");
await fs$1.rename(n, i);
await fs$1.rm(t, {
recursive: true,
force: true
});
s += 1;
} catch (t) {
n.push(`Skipped Canvas document ${e}; core target may already exist: ${String(t)}. Keep plugins.entries.canvas.config.host.root, resolve the copy or target conflict, then rerun "openclaw doctor --fix".`);
} finally {
if (c) await fs$1.rm(c, {
recursive: true,
force: true
}).catch(() => void 0);
}
}
if (s > 0) t.push(`Migrated ${s} Canvas document(s) into core storage`);
try {
if ((await fs$1.readdir(r)).length === 0) await fs$1.rmdir(r);
} catch {}
return {
changes: t,
warnings: n
};
}
}];
//#endregion
export { legacyConfigRules, normalizeCompatibilityConfig, stateMigrations };