openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
170 lines (169 loc) • 6.33 kB
JavaScript
import { l as isRecord } from "../../record-coerce-CVPuDqcc.mjs";
import { t as FsSafeError, w as root } from "../../fs-safe-BbWvmdVn.mjs";
import "../../string-coerce-runtime-BwAZZDf5.mjs";
import "../../security-runtime-C-zE_Zy_.mjs";
import { C as walkMemoryWikiDirectory } from "../../markdown-CWBRLPHM.mjs";
import { a as resolveMemoryWikiConfig, i as resolveMemoryWikiAgentConfig, o as resolveMemoryWikiConfiguredAgentIds } from "../../config-BbCc3-tw.mjs";
import { a as resolveMemoryWikiImportRunsDir, c as createMemoryWikiSourceSyncStateStore, r as createMemoryWikiImportRunStateStore } from "../../import-runs-state-CdrnhnHo.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region extensions/memory-wiki/doctor-contract-api.ts
const LEGACY_MEMORY_WIKI_COMPILED_CACHE_PATHS = [".openclaw-wiki/cache/agent-digest.json", ".openclaw-wiki/cache/claims.jsonl"];
function resolveHomeDir(e) {
return e.HOME?.trim() || e.USERPROFILE?.trim() || void 0;
}
function isMissingPathError(e) {
return e instanceof FsSafeError && e.code === "not-found" || isRecord(e) && e.code === "ENOENT";
}
async function safeLegacyCacheFileExists(e, t) {
try {
return (await e.stat(t)).isFile;
} catch (e) {
if (isMissingPathError(e) || e instanceof FsSafeError) return false;
throw e;
}
}
async function openExistingVaultRoot(e) {
try {
return await root(e);
} catch (e) {
if (isMissingPathError(e)) return null;
throw e;
}
}
function readConfiguredPluginConfig(e) {
const t = e.plugins?.entries;
const n = isRecord(t) ? t["memory-wiki"] : void 0;
if (!isRecord(n) || !isRecord(n.config)) return;
return n.config;
}
function resolveConfiguredVaultRoots(e) {
const t = resolveHomeDir(e.env);
const n = resolveMemoryWikiConfig(readConfiguredPluginConfig(e.config), {
homedir: t,
env: e.env
});
if (n.vault.scope === "global") return [n.vault.path];
return resolveMemoryWikiConfiguredAgentIds(e.config).map((t) => resolveMemoryWikiAgentConfig({
config: n,
appConfig: e.config,
agentId: t
}).vault.path);
}
function unsupportedJsonWarning(e, t = false) {
return `Memory Wiki: upgrades from pre-July-2026 JSON state are no longer migrated (${e}). ${t ? "No current SQLite source-sync state was found; an empty store cannot be distinguished from unmigrated state. " : "No current SQLite import-run record was found. "}Restore a backup produced by a July 2026 or newer release, or back up and move this retired file aside after verifying the SQLite state, then rerun openclaw doctor --fix. The file was left unchanged.`;
}
async function unsupportedSourceSyncFiles(e) {
const t = [];
const n = createMemoryWikiSourceSyncStateStore(e.context.openPluginStateKeyedStore);
for (const r of resolveConfiguredVaultRoots(e)) {
const e = path.join(r, ".openclaw-wiki", "source-sync.json");
try {
await fs.lstat(e);
} catch (e) {
if (isMissingPathError(e)) continue;
throw e;
}
const i = await n.read(r);
if (Object.keys(i.entries).length === 0) t.push(unsupportedJsonWarning(e, true));
}
return t;
}
async function unsupportedImportRunFiles(e) {
const t = [];
const n = createMemoryWikiImportRunStateStore(e.context.openPluginStateKeyedStore);
for (const r of resolveConfiguredVaultRoots(e)) {
const e = resolveMemoryWikiImportRunsDir(r);
const i = await walkMemoryWikiDirectory(e, "", {
maxDepth: 1,
entryFilter: (e) => e.kind === "directory" ? "skip-subtree" : e.kind === "file" && e.relativePath.endsWith(".json") ? "include" : "skip"
}).catch((e) => {
if (isMissingPathError(e)) return [];
throw e;
});
if (i.length === 0) continue;
const a = new Set((await n.list(r)).map((e) => e.runId));
for (const n of i) if (!a.has(n.relativePath.slice(0, -5))) t.push(unsupportedJsonWarning(path.join(e, n.relativePath)));
}
return t;
}
const stateMigrations = [
{
id: "memory-wiki-compiled-cache-file-cleanup",
label: "Memory Wiki compiled cache files",
collectBackupResources(e) {
return resolveConfiguredVaultRoots(e).flatMap((e) => LEGACY_MEMORY_WIKI_COMPILED_CACHE_PATHS.map((t) => ({
path: path.join(e, t),
kind: "file"
})));
},
async detectLegacyState(e) {
const t = [];
for (const n of resolveConfiguredVaultRoots(e)) {
const e = await openExistingVaultRoot(n);
if (!e) continue;
const r = (await Promise.all(LEGACY_MEMORY_WIKI_COMPILED_CACHE_PATHS.map(async (t) => {
const r = path.join(n, t);
return await safeLegacyCacheFileExists(e, t) ? r : null;
}))).filter((e) => Boolean(e));
for (const e of r) t.push(`- Remove rebuildable Memory Wiki compiled cache: ${e}`);
}
return t.length > 0 ? { preview: t } : null;
},
async migrateLegacyState(e) {
const t = [];
const n = [];
for (const r of resolveConfiguredVaultRoots(e)) {
const e = await openExistingVaultRoot(r);
if (!e) continue;
for (const i of LEGACY_MEMORY_WIKI_COMPILED_CACHE_PATHS) {
const a = path.join(r, i);
if (!await safeLegacyCacheFileExists(e, i)) continue;
try {
await e.remove(i);
t.push(`Removed rebuildable Memory Wiki compiled cache: ${a}`);
} catch (e) {
if (!isMissingPathError(e)) n.push(`Skipped rebuildable Memory Wiki compiled cache cleanup. Run openclaw doctor --fix to retry. ${a}: ${String(e)}`);
}
}
}
return {
changes: t,
warnings: n,
...n.length > 0 ? { warningDisposition: "recoverable" } : {}
};
}
},
{
id: "memory-wiki-source-sync-json-to-plugin-state",
label: "Memory Wiki unsupported source-sync JSON",
collectBackupResources: () => [],
async detectLegacyState(e) {
const t = await unsupportedSourceSyncFiles(e);
return t.length > 0 ? { preview: t.map((e) => `- ${e}`) } : null;
},
async migrateLegacyState(e) {
return {
changes: [],
warnings: await unsupportedSourceSyncFiles(e)
};
}
},
{
id: "memory-wiki-import-runs-json-to-plugin-state",
label: "Memory Wiki unsupported import-run JSON",
collectBackupResources: () => [],
async detectLegacyState(e) {
const t = await unsupportedImportRunFiles(e);
return t.length > 0 ? { preview: t.map((e) => `- ${e}`) } : null;
},
async migrateLegacyState(e) {
return {
changes: [],
warnings: await unsupportedImportRunFiles(e)
};
}
}
];
//#endregion
export { stateMigrations };