UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

194 lines (193 loc) • 4.88 kB
import { n as estimateStringChars, t as CHARS_PER_TOKEN_ESTIMATE } from "./cjk-chars-CgnWl5qA.mjs"; import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs"; import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs"; //#region packages/memory-host-sdk/src/host/markdown-chunks.ts const MEMORY_CHUNKING_VERSION = 6; function curatedMarkdownEntryKind(e) { return e.startsWith("- ") ? "entry" : /^#{1,6}(?:\s|$)/u.test(e) ? "section" : void 0; } function* curatedMarkdownEntrySpans(e) { let t = 1; let n = e[0]?.startsWith("- ") ? "entry" : "section"; for (let r = 1; r < e.length; r += 1) { const i = curatedMarkdownEntryKind(e[r] ?? ""); if (i) { yield { startLine: t, endLine: r, kind: n }; t = r + 1; n = i; } } yield { startLine: t, endLine: e.length, kind: n }; } function splitCuratedMarkdownEntries(e) { const t = e.split("\n"); return Array.from(curatedMarkdownEntrySpans(t), ({ startLine: e, endLine: n, kind: r }) => ({ startLine: e, endLine: n, text: t.slice(e - 1, n).join("\n"), kind: r })); } /** Takes the trailing slice of text within the weighted char budget, without splitting surrogate pairs. */ function takeTailByEstimatedChars(e, t) { let n = 0; let r = e.length; while (r > 0) { const i = r - ((e.codePointAt(r - 2) ?? 0) > 65535 ? 2 : 1); n += estimateStringChars(e.slice(i, r)); if (!(n <= t)) break; r = i; } return e.slice(r); } /** Incremental text chunking: callers retain only the current chunk and overlap. */ function createMarkdownChunker(e, t) { const n = Math.max(32, e.tokens * 4); const r = Math.max(0, e.overlap * 4); let i = []; let a = 0; let o = ""; const flush = () => { const e = i[0]; const n = i[i.length - 1]; if (!e || !n) return; const r = i.map((e) => e.line).join("\n"); const a = e.lineNo; const o = n.lineNo; t({ startLine: a, endLine: o, text: r, hash: sha256Hex(r), embeddingInput: { text: r } }); }; const carryOverlap = (e) => { if (e <= 0 || i.length === 0) { i = []; a = 0; return; } let t = 0; const n = []; for (let r = i.length - 1; r >= 0; r -= 1) { const a = i[r]; if (!a) continue; const o = estimateStringChars(a.line) + 1; const s = e - t; if (o > s) { const e = n.length === 0 ? takeTailByEstimatedChars(a.line, s - 1) : ""; if (e.length > 0) { n.unshift({ line: e, lineNo: a.lineNo }); t += estimateStringChars(e) + 1; } break; } t += o; n.unshift(a); if (t >= e) break; } i = n; a = t; }; const appendSegment = (e, t, o) => { const s = o + 1; if (a + s > n && i.length > 0) { flush(); carryOverlap(Math.min(r, Math.max(0, n - s))); } i.push({ line: e, lineNo: t }); a += s; }; return { appendLine(e, t, r = true) { const i = o + e; o = ""; if (i.length === 0 && r) appendSegment("", t, 0); else for (let e = 0; e < i.length;) { if (!r && i.length - e <= n) { o = i.slice(e); break; } const a = truncateUtf16Safe(i.slice(e), n); const s = estimateStringChars(a); if (s > n) { let e = 0; let r = 0; let i = 0; for (const o of a) { const s = estimateStringChars(o); if (i + s > n) { appendSegment(a.slice(e, r), t, i); e = r; i = 0; } r += o.length; i += s; } appendSegment(a.slice(e), t, i); } else appendSegment(a, t, s); e += a.length; } }, flush() { flush(); i = []; a = 0; } }; } function chunkMarkdown(e, t) { const n = e.split("\n"); const r = []; const i = t.perEntry ? curatedMarkdownEntrySpans(n) : [{ startLine: 1, endLine: n.length, kind: "section" }]; for (const e of i) { const i = createMarkdownChunker(t, (t) => { if (e.kind === "entry") { t.entryStartLine = e.startLine; t.entryEndLine = e.endLine; } r.push(t); }); for (let t = e.startLine - 1; t < e.endLine; t += 1) i.appendLine(n[t] ?? "", t + 1); i.flush(); } return r; } /** * Remap chunk startLine/endLine from content-relative positions to original * source file positions using a lineMap. Each entry in lineMap gives the * 1-indexed source line for the corresponding 0-indexed content line. * * This is used for session JSONL files where buildSessionEntry() flattens * messages into a plain-text string before chunking. Without remapping the * stored line numbers would reference positions in the flattened text rather * than the original JSONL file. */ function remapChunkLines(e, t) { if (!t || t.length === 0) return; for (const n of e) { n.startLine = t[n.startLine - 1] ?? n.startLine; n.endLine = t[n.endLine - 1] ?? n.endLine; } } //#endregion export { splitCuratedMarkdownEntries as a, remapChunkLines as i, chunkMarkdown as n, createMarkdownChunker as r, MEMORY_CHUNKING_VERSION as t };