openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
115 lines (114 loc) • 4.36 kB
JavaScript
import { a as executeSqliteQueryTakeFirstSync, c as iterateSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs";
import { t as decodeMemoryEmbedding } from "./embedding-vector-B6ralm1Z.mjs";
import "./memory-core-host-engine-storage-DcaZNQQh.mjs";
import "./sqlite-worker-runtime-CKocuU_N.mjs";
import { _ as loadMemoryEmbeddingCache, a as MEMORY_INDEX_META_KEY, g as isValidMemoryEmbedding, i as readMemorySourceHash, o as readMemoryDatabaseFacts, r as readMemorySourceChunks, u as loadMemorySourceFileState } from "./manager-source-index-kernel-BYD04WLI.mjs";
import { i as resolveEmbeddingInputFormatVersion } from "./embedding-inputs-BGMBVJOW.mjs";
import "./memory-core-host-engine-indexing-EQMS_Jzx.mjs";
import "./memory-core-host-engine-schema-CXsNrexG.mjs";
import { Value } from "typebox/value";
import { Type } from "typebox";
//#region extensions/memory-core/src/memory/manager-rebuild-embeddings.ts
const rebuildIdentitySchema = Type.Object({
provider: Type.String(),
model: Type.String(),
providerKey: Type.String(),
embeddingInputFormatVersion: Type.Optional(Type.Integer({ minimum: 0 })),
vectorDims: Type.Integer({
minimum: 1,
maximum: Number.MAX_SAFE_INTEGER
})
});
/** Read only the requested batch, using the published source/path index on either engine. */
function loadMemoryRebuildEmbeddings(e) {
const t = /* @__PURE__ */ new Map();
for (const n of e.query.chunks) {
if (e.cached.has(n.hash)) continue;
const r = `${n.source}:${n.path}`;
let i = t.get(r);
if (!i) {
i = {
source: n.source,
path: n.path,
hashes: /* @__PURE__ */ new Set()
};
t.set(r, i);
}
i.hashes.add(n.hash);
}
if (t.size === 0) return;
const n = getNodeSqliteKysely(e.db);
const r = executeSqliteQueryTakeFirstSync(e.db, n.selectFrom("memory_index_meta").select("value").where("key", "=", MEMORY_INDEX_META_KEY));
if (!r) return;
let i;
try {
i = JSON.parse(r.value);
} catch {
return;
}
const { query: a } = e;
if (!Value.Check(rebuildIdentitySchema, i) || i.model === "fts-only" || !e.identities.some((e) => i.provider === e.provider && i.model === e.model && i.providerKey === e.providerKey) || (i.embeddingInputFormatVersion ?? 0) !== resolveEmbeddingInputFormatVersion(i.model) || a.dimensions !== void 0 && a.dimensions !== i.vectorDims) return;
const o = /* @__PURE__ */ new Map();
const s = /* @__PURE__ */ new Set();
for (const a of t.values()) {
const t = [...a.hashes].filter((t) => !e.cached.has(t));
for (let c = 0; c < t.length; c += 400) {
const l = n.selectFrom("memory_index_chunks").select(["hash", "embedding"]).where("source", "=", a.source).where("path", "=", a.path).where("model", "=", i.model).where("hash", "in", t.slice(c, c + 400)).where((e) => e.exists(e.selectFrom("memory_index_meta").select("key").where("key", "=", MEMORY_INDEX_META_KEY).where("value", "=", r.value)));
for (const t of iterateSqliteQuerySync(e.db, l)) {
const e = o.get(t.hash);
if (e && Buffer.compare(e, t.embedding) !== 0) s.add(t.hash);
else if (!e) o.set(t.hash, t.embedding);
}
}
}
for (const [t, n] of o) {
if (s.has(t)) continue;
const r = decodeMemoryEmbedding(n);
if (isValidMemoryEmbedding(r, i.vectorDims)) e.cached.set(t, r);
}
}
//#endregion
//#region extensions/memory-core/src/memory/manager-index-read.ts
/** Published WAL readers and private shadow writers consume the same read kernels. */
function readMemoryIndex(e, t) {
switch (t.type) {
case "index.facts": return {
type: t.type,
value: readMemoryDatabaseFacts(e)
};
case "source.state": return {
type: t.type,
value: loadMemorySourceFileState({
db: e,
...t.input
})
};
case "source.hash": return {
type: t.type,
value: readMemorySourceHash(e, t.input.source, t.input.path)
};
case "source.chunks": return {
type: t.type,
value: readMemorySourceChunks(e, t.input.source, t.input.path)
};
case "cache.read": {
const n = t.input.cacheEnabled === false ? /* @__PURE__ */ new Map() : loadMemoryEmbeddingCache({
...t.input,
db: e
});
if (t.input.published) loadMemoryRebuildEmbeddings({
db: e,
identities: t.input.providerIdentities,
query: t.input.published,
cached: n
});
return {
type: t.type,
value: n
};
}
}
return t;
}
//#endregion
export { readMemoryIndex as t };