openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
178 lines (177 loc) • 7.4 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import "./state-paths-qdCZ9HDY.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./error-runtime-BM740gJj.mjs";
import { t as readDoctorAgentEntries } from "./doctor-agent-config-qvoIJni1.mjs";
import { n as LOCAL_MEMORY_EMBEDDING_PROVIDER_ID, r as MISSING_LOCAL_MEMORY_EMBEDDING_PROVIDER_MESSAGE, t as LLAMA_CPP_PROVIDER_INSTALL_COMMAND } from "./local-embedding-provider-C7AaxyXF.mjs";
import fs from "node:fs";
import path from "node:path";
//#region extensions/memory-core/src/doctor-health.ts
const MEMORY_INDEX_META_KEY = "memory_index_meta_v1";
function listConfiguredAgentIds(e) {
const { keyed: t, listed: n } = readDoctorAgentEntries(e);
const r = new Set(Object.keys(t ?? {}));
for (const e of n) {
const t = asOptionalObjectRecord(e);
if (typeof t?.id === "string" && t.id.trim()) r.add(t.id.trim());
}
return r.size > 0 ? [...r] : ["main"];
}
async function readExistingVectorModel(e) {
if (!fs.existsSync(e)) return null;
const { openNodeSqliteDatabase: t, prepareSqliteReadOnlyLocation: n } = await import("./plugin-sdk/sqlite-runtime.js");
let r;
let i;
let a;
let o = null;
try {
r = await n(e);
i = t(r.location, { readOnly: true });
if (i.prepare("SELECT 1 AS ok FROM sqlite_master WHERE type = 'table' AND name = 'memory_index_meta'").get()) {
const e = i.prepare("SELECT value FROM memory_index_meta WHERE key = ?").get(MEMORY_INDEX_META_KEY);
const t = typeof e?.value === "string" ? JSON.parse(e.value) : null;
const n = t && typeof t === "object" && typeof t.model === "string" ? t.model.trim() : "";
o = n && n !== "fts-only" ? n : null;
}
} catch (e) {
a = e;
} finally {
try {
i?.close();
} catch (e) {
a ??= e;
}
if (r && !await r.cleanupAsync()) a ??= /* @__PURE__ */ new Error("Temporary SQLite inspection snapshot cleanup did not complete.");
}
if (a) throw a instanceof Error ? a : new Error("Memory index inspection failed.", { cause: a });
return o;
}
function resolveConfigPrefix(e, t) {
const { keyed: n, listed: r } = readDoctorAgentEntries(e);
const i = asOptionalObjectRecord(n?.[t]);
if (asOptionalObjectRecord(i?.memory)?.search) return `agents.entries.${t}.memory.search`;
const a = r.map(asOptionalObjectRecord).find((e) => e?.id === t);
if (asOptionalObjectRecord(a?.memory)?.search) return `agents.list[].memory.search (agent id ${t})`;
return "memory.search";
}
async function collectVectorProviderFindings(e, t) {
const n = [];
for (const r of listConfiguredAgentIds(e.config).filter(e.shouldInspectAgent)) {
const i = path.join(e.stateDir, "agents", r, "agent", "openclaw-agent.sqlite");
let a;
try {
a = await readExistingVectorModel(i);
} catch (e) {
const { isSqliteSnapshotCapacityError: t } = await import("./plugin-sdk/sqlite-runtime.js");
if (!t(e)) throw e;
n.push({
checkId: MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID,
severity: "warning",
source: "memory-core",
target: r,
requirement: "memory-index-inspection",
errorCode: "memory-index-inspection-unavailable",
message: `Memory Core semantic-index readiness for agent ${r} could not be verified because temporary snapshot storage is unavailable (${formatErrorMessage(e)}).`,
fixHint: "Free temporary snapshot space or quota, then rerun `openclaw doctor --lint --only memory-core/managed-local-embedding-setup`."
});
continue;
}
if (!a) continue;
const o = await t({
config: e.config,
agentId: r,
env: e.env
});
if (o) n.push({
checkId: MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID,
severity: "error",
source: "memory-core",
path: `${resolveConfigPrefix(e.config, r)}.provider`,
target: `${r}/${o.provider}`,
requirement: o.requirement ?? "managed-local-embedding-setup",
message: `Memory index for agent ${r} uses vector model ${a}, but embedding provider "${o.provider}" cannot initialize (${o.reason}).`,
fixHint: o.fixHint
});
}
return n;
}
const MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID = "memory-core/managed-local-embedding-setup";
const pluginStateIsolatedDoctorCheckIds = [MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID];
const registrationsByHost = /* @__PURE__ */ new WeakMap();
function resolveSelectedMemoryProvider(e, t) {
const n = asOptionalObjectRecord(e);
const { keyed: r, listed: i } = readDoctorAgentEntries(e);
const a = asOptionalObjectRecord(r?.[t]) ?? i.map(asOptionalObjectRecord).find((e) => e?.id === t);
const o = asOptionalObjectRecord(asOptionalObjectRecord(n?.memory)?.search);
const s = asOptionalObjectRecord(asOptionalObjectRecord(a?.memory)?.search);
if (!(s?.enabled ?? o?.enabled ?? true)) return null;
const c = normalizeOptionalLowercaseString(s?.provider ?? o?.provider);
return !c || c === "auto" ? "openai" : c;
}
function createManagedLocalEmbeddingSetupCheck(e) {
return {
id: MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID,
kind: "plugin",
source: "memory-core",
defaultEnabled: false,
description: "Checks existing semantic indexes for required managed local embedding setup.",
async detect(t) {
if (!e.memoryCoreActive) return [];
const n = t.env ?? process.env;
try {
return await collectVectorProviderFindings({
config: t.cfg,
env: n,
stateDir: resolveStateDir(n),
shouldInspectAgent: (e) => resolveSelectedMemoryProvider(t.cfg, e) === LOCAL_MEMORY_EMBEDDING_PROVIDER_ID
}, async (t) => {
const n = await e.inspectEmbeddingProviderSetup({
...t,
provider: LOCAL_MEMORY_EMBEDDING_PROVIDER_ID
});
if (n === void 0) return {
provider: LOCAL_MEMORY_EMBEDDING_PROVIDER_ID,
reason: MISSING_LOCAL_MEMORY_EMBEDDING_PROVIDER_MESSAGE,
requirement: "memory-embedding-provider-plugin",
fixHint: `Run \`${LLAMA_CPP_PROVIDER_INSTALL_COMMAND}\`, check that the plugin is enabled, then rerun this check.`
};
return n;
});
} catch (e) {
return [{
checkId: MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID,
severity: "error",
source: "memory-core",
target: "memory-core",
requirement: "memory-index-inspection",
message: `Memory Core semantic-index readiness could not be verified (${formatErrorMessage(e)}).`,
fixHint: "Keep the current Gateway running, resolve the database inspection error, then rerun this check."
}];
}
}
};
}
function registerMemoryCoreDoctorChecks(e) {
let t = registrationsByHost.get(e.registerHealthCheck);
if (t) {
t.state.inspectEmbeddingProviderSetup = e.inspectEmbeddingProviderSetup;
t.state.memoryCoreActive = e.memoryCoreActive;
} else {
const n = {
inspectEmbeddingProviderSetup: e.inspectEmbeddingProviderSetup,
memoryCoreActive: e.memoryCoreActive
};
t = {
check: createManagedLocalEmbeddingSetupCheck(n),
state: n
};
registrationsByHost.set(e.registerHealthCheck, t);
}
if (e.getHealthCheck("memory-core/managed-local-embedding-setup") === t.check) return;
e.registerHealthCheck(t.check);
}
//#endregion
export { pluginStateIsolatedDoctorCheckIds as n, registerMemoryCoreDoctorChecks as r, MEMORY_MANAGED_LOCAL_EMBEDDING_SETUP_CHECK_ID as t };