UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

155 lines (154 loc) • 6.81 kB
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { t as loadPluginManifestRegistryCore } from "./manifest-registry-CuWv8ibc.mjs"; import { n as loadBundledPluginPublicArtifactModuleSync } from "./public-surface-loader-DENdCm5a.mjs"; import { S as resolveMemoryDreamingPluginId, b as resolveMemoryDreamingConfig, s as DEFAULT_MEMORY_DREAMING_PLUGIN_ID, x as resolveMemoryDreamingPluginConfig } from "./dreaming-C72ZWZJr.mjs"; import { n as loadPluginRegistryHandle } from "./loader--j41eir2.mjs"; import { t as note } from "./note-CnLgHvug.mjs"; import { i as readMemorySearchManagerStatus } from "./types-Bo5s7TN4.mjs"; import { l as resolveActiveMemoryBackendConfig, o as getActiveMemorySearchManagerCore } from "./memory-runtime-C3z9zu-7.mjs"; import { d as repairShortTermPromotionArtifacts, n as auditShortTermPromotionArtifacts, t as auditDreamingArtifacts, u as repairDreamingArtifacts } from "./memory-core-bundled-runtime-C30jyiwB.mjs"; import { n as resolveMemoryDoctorAgentScopes, t as formatMemoryDoctorAgentMessage } from "./doctor-memory-scope-ClHQl6Ye.mjs"; import { r as maybeRepairWorkspaceMemoryHealth } from "./doctor-workspace-BGlM5rlo.mjs"; //#region src/commands/doctor-memory-recall.ts async function resolveRuntimeMemoryWorkspaceDir(e, t) { const n = (await getActiveMemorySearchManagerCore({ cfg: e, agentId: t, purpose: "status" })).manager; if (!n) return; try { return (await readMemorySearchManagerStatus(n)).workspaceDir?.trim(); } finally { await n.close?.().catch(() => void 0); } } function buildMemoryArtifactIssueNote(e, t, n) { if (e.length === 0) return null; return [ t, ...e.map((e) => `- ${e.message}`), n, e.some((e) => e.fixable) ? `Fix: ${formatCliCommand("openclaw doctor --fix")} or ${formatCliCommand("openclaw memory status --fix")}` : `Verify: ${formatCliCommand("openclaw memory status --deep")}` ].join("\n"); } async function runMemoryRecallHealth(e, t, n = true) { const r = resolveMemoryDoctorAgentScopes(e); const i = r.length > 1; const a = t ? void 0 : resolveMemoryDreamingConfig({ cfg: e, pluginConfig: resolveMemoryDreamingPluginConfig(e) }); let o; const readCronJobs = () => o ??= (async () => { const { resolveCronJobsStorePathFromConfigAsync: t } = await import("./paths-BtIJBbSe.mjs"); const { loadCronJobsStoreWithConfigJobsReadOnly: n } = await import("./read-only-CXJFxfHP.mjs"); return (await n(await t(e))).store.jobs; })(); for (const o of r) { const agentMessage = (e) => formatMemoryDoctorAgentMessage(o.agentId, i, e); const report = (e) => note(agentMessage(e), "Memory search"); try { if (!n) continue; if (resolveActiveMemoryBackendConfig({ cfg: e, agentId: o.agentId })?.backend === "provider-runtime") continue; if (t) await maybeRepairWorkspaceMemoryHealth({ prompter: t, scope: { agentId: o.agentId, workspaceDir: o.workspaceDir, labelAgent: i } }); const r = await resolveRuntimeMemoryWorkspaceDir(e, o.agentId); if (!r) continue; if (!t) { const e = await auditShortTermPromotionArtifacts({ workspaceDir: r }); const t = buildMemoryArtifactIssueNote(e.issues, "Memory recall artifacts need attention:", `Recall store: ${e.storePath}`); if (t) report(t); const n = await auditDreamingArtifacts({ workspaceDir: r }); const i = buildMemoryArtifactIssueNote(n.issues, "Dreaming artifacts need attention:", `Dream corpus: ${n.sessionCorpusDir}`); if (i) report(i); continue; } for (const e of ["recall", "dreaming"]) { if (!(await (e === "recall" ? auditShortTermPromotionArtifacts : auditDreamingArtifacts)({ workspaceDir: r })).issues.some((e) => e.fixable)) continue; if (!await t.confirmRuntimeRepair({ message: agentMessage(e === "recall" ? "Remove dangling memory recalls, normalize recall artifacts, and remove stale promotion locks?" : "Archive contaminated dreaming artifacts and reset derived dream corpus state?"), initialValue: true })) continue; let n; if (e === "recall") { const e = await repairShortTermPromotionArtifacts({ workspaceDir: r }); if (!e.changed) continue; const t = [ [e.removedInvalidEntries, "invalid"], [e.removedDanglingEntries ?? 0, "dangling"], [e.removedOverflowEntries ?? 0, "overflow"] ].filter(([e]) => e > 0).map(([e, t]) => `-${e} ${t} entries`).join(", "); n = [ "Memory recall artifacts repaired:", e.rewroteStore ? `- rewrote recall store${t ? ` (${t})` : ""}` : null, e.removedStaleLock ? "- removed stale promotion lock" : null ]; } else { const e = await repairDreamingArtifacts({ workspaceDir: r }); if (!e.changed) continue; n = [ "Dreaming artifacts repaired:", e.archivedSessionCorpus ? "- archived session corpus" : null, e.archivedSessionIngestion ? "- archived session-ingestion state" : null, e.archivedDreamsDiary ? "- archived dream diary" : null, e.archiveDir ? `- archive dir: ${e.archiveDir}` : null, ...e.warnings.map((e) => `- warning: ${e}`) ]; } n.push(`Verify: ${formatCliCommand("openclaw memory status --deep")}`); note(agentMessage(n.filter(Boolean).join("\n")), "Doctor changes"); } } catch (e) { report(`${t ? "Memory artifact repair" : "Memory recall audit"} could not be completed: ${formatErrorMessage(e)}`); } finally { if (a) { const t = a.enabled && loadPluginRegistryHandle({ config: e, registrationConfigOrigin: e, manifestRegistry: loadPluginManifestRegistryCore({ config: e, workspaceDir: o.workspaceDir }), workspaceDir: o.workspaceDir, loadModules: false, onlyPluginIds: [resolveMemoryDreamingPluginId(e)] }).plugins.some((e) => e.id === "memory-core" && e.status === "loaded"); const n = [`Dreaming: ${t ? "enabled" : "disabled"} (cadence ${a.frequency}).`]; try { const r = loadBundledPluginPublicArtifactModuleSync({ dirName: "memory-core", artifactBasename: "dreaming-status-api.js" }); n.push(...await r.readMemoryCoreDreamingRuns({ workspaceDir: o.workspaceDir, dreamingEnabled: t, cronEnabled: process.env.OPENCLAW_SKIP_CRON !== "1" && (e.cron?.enabled ?? true), readCronJobs })); } catch (e) { n.push(`Dreaming run telemetry unavailable: ${formatErrorMessage(e)}`); } report(n.join("\n")); } } } } async function noteMemoryRecallHealth(e, t = true) { return runMemoryRecallHealth(e, void 0, t); } async function maybeRepairMemoryRecallHealth(e) { return runMemoryRecallHealth(e.cfg, e.prompter); } //#endregion export { maybeRepairMemoryRecallHealth, noteMemoryRecallHealth };