UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

196 lines (195 loc) • 6.5 kB
import { i as remapChunkLines, n as chunkMarkdown, r as createMarkdownChunker } from "./markdown-chunks-DoKq_kBI.mjs"; import { a as stripMemoryAnnotationCarriers, n as extractCuratedEntryRecallMetadata } from "./curated-annotations-GumWCHHf.mjs"; import { n as enforceEmbeddingMaxInputTokens, t as hashText } from "./memory-core-host-engine-indexing-EQMS_Jzx.mjs"; //#region extensions/memory-core/src/memory/manager-reset-chunk-boundary.ts function chunkSessionContentAtResetBoundary(e) { const t = e.cutoffLine !== void 0 && e.lineMap ? e.lineMap.findIndex((t) => t >= e.cutoffLine) : -1; if (t <= 0) return chunkMarkdown(e.content, e.chunking); const n = e.content.split("\n"); const chunkPartition = (t, n) => { const r = chunkMarkdown(t, e.chunking); for (const e of r) { e.startLine += n; e.endLine += n; } return r; }; return [...chunkPartition(n.slice(0, t).join("\n"), 0), ...chunkPartition(n.slice(t).join("\n"), t)]; } //#endregion //#region extensions/memory-core/src/memory/manager-index-preparation.ts function prepareMemoryIndexChunks({ entry: e, source: t, content: n, pathClassification: r, chunking: i, cutoffLine: a, provider: o, hardMaxInputTokens: s }) { const c = t === "memory" ? hashText(n) : void 0; const l = e.path.replaceAll("\\", "/"); const u = t === "memory" && (l === "MEMORY.md" || l === "USER.md"); const d = t === "memory" ? stripMemoryAnnotationCarriers(n) : n; const f = t === "memory" ? n.replace(/\r\n/gu, "\n").split("\n") : []; const p = { ...i, perEntry: u }; const m = []; for (const n of t === "sessions" ? chunkSessionContentAtResetBoundary({ content: d, cutoffLine: a, lineMap: e.lineMap, chunking: p }) : chunkMarkdown(d, p)) { if (!n.text.trim()) continue; const i = m.at(-1); if (t === "memory" && i && i.entryStartLine === void 0 && n.endLine > i.endLine && i.text.split("\n").every((e, t) => (!e.trim() || /^ {0,3}#{1,6}(?:\s|$)/u.test(e)) && !e.includes("<!--") && e.trimEnd() === (f[i.startLine + t - 1] ?? "").trimEnd())) { const e = n.text.split("\n").slice(Math.max(0, i.endLine - n.startLine + 1)); n.text = [i.text, ...e].join("\n"); n.startLine = i.startLine; n.hash = hashText(n.text); m.pop(); } n.provenance = resolveChunkProvenance(e, t, n, r.originClass); m.push(n); } const h = /* @__PURE__ */ new Map(); const g = (o !== void 0 ? enforceEmbeddingMaxInputTokens(o, m, s) : m).map((e) => { delete e.embeddingInput; const n = e.entryStartLine ?? e.startLine; const i = e.entryEndLine ?? e.endLine; const a = `${n}:${i}`; let o = h.get(a); if (!o) { o = extractCuratedEntryRecallMetadata({ curatedRoot: r.curatedRoot, projectScopeEligible: t === "memory" && l.toUpperCase() !== "USER.MD", sourceLines: f.slice(n - 1, i) }); h.set(a, o); } return Object.assign(e, o); }); if (t === "sessions" && "lineMap" in e) remapChunkLines(g, e.lineMap); return { contentHash: c, chunks: g }; } function resolveChunkProvenance(e, t, n, r) { const i = e.lineProvenance?.slice(n.startLine - 1, n.endLine) ?? []; if (t === "sessions" && i.length > 0) { const e = [ "owner", "agent", "system", "untrusted" ].findLast((e) => i.some((t) => t.originClass === e)); const t = new Set(i.map((e) => e.sessionKind)); const n = new Set(i.flatMap((e) => e.supersedesKey ? [e.supersedesKey] : [])); return { originClass: e ?? "untrusted", sessionKind: t.size === 1 ? i[0]?.sessionKind ?? "unknown" : "unknown", observedAt: Math.max(...i.map((e) => e.observedAt)), ...n.size === 1 ? { supersedesKey: [...n][0] } : {} }; } return { originClass: r, sessionKind: "unknown", observedAt: Math.max(0, Math.floor(e.mtimeMs)) }; } //#endregion //#region extensions/memory-core/src/memory/manager-session-index-preparation.ts /** Frame one export page for the index worker; framing also bounds a single enormous line. */ function sessionIndexPages(e) { const t = []; let n = []; let r = 0; for (const [i, a] of e.lines.entries()) { let o = 0; do { const s = Math.min(a.length, o + 16384); const c = a.slice(o, s); n.push({ text: c, lineNo: e.startLine + i + 1, endOfLine: s === a.length, sourceLine: e.lineMap[i] ?? e.startLine + i + 1, provenance: e.lineProvenance[i] }); r += c.length * 2 + 64; o = s; if (n.length >= 512 || r >= 65536) { t.push({ lines: n, done: false }); n = []; r = 0; } } while (o < a.length); } if (n.length > 0) t.push({ lines: n, done: false }); return t; } /** Both directions are acknowledged: a bounded worker never retains the full expansion. */ async function prepareSessionIndexStream(e, t, n) { t.consumeInput(); const r = extractCuratedEntryRecallMetadata({ curatedRoot: false, projectScopeEligible: false, sourceLines: [] }); const start = () => { const t = /* @__PURE__ */ new Map(); const n = []; return { facts: t, pending: n, chunker: createMarkdownChunker(e.chunking, (i) => { const a = []; for (const [e, n] of t) if (e < i.startLine) t.delete(e); else if (e <= i.endLine && n.provenance) a.push(n.provenance); if (!i.text.trim()) return; i.provenance = resolveChunkProvenance({ mtimeMs: e.entry.mtimeMs, lineProvenance: a }, "sessions", { ...i, startLine: 1, endLine: a.length }, e.pathClassification.originClass); i.startLine = t.get(i.startLine)?.sourceLine ?? i.startLine; i.endLine = t.get(i.endLine)?.sourceLine ?? i.endLine; for (const t of e.provider ? enforceEmbeddingMaxInputTokens(e.provider, [i], e.hardMaxInputTokens) : [i]) { delete t.embeddingInput; n.push(Object.assign(t, r)); } }), crossedReset: false }; }; let i = start(); for (;;) { n.throwIfCancelled(); const r = await t.request({ chunks: i.pending.splice(0) }); const a = r.input; if (a.reset) i = start(); for (const t of a.lines) { n.throwIfCancelled(); if (!i.crossedReset && e.cutoffLine !== void 0 && t.sourceLine >= e.cutoffLine) { if (t.lineNo > 1) i.chunker.flush(); i.crossedReset = true; } i.facts.set(t.lineNo, t); i.chunker.appendLine(t.text, t.lineNo, t.endOfLine); } r.consumed(); if (a.done) { i.chunker.flush(); (await t.request({ chunks: i.pending.splice(0) })).consumed(); return; } } } //#endregion export { resolveChunkProvenance as i, sessionIndexPages as n, prepareMemoryIndexChunks as r, prepareSessionIndexStream as t };