UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

262 lines (261 loc) • 8.44 kB
import "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs"; import { t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs"; import { r as isInsideCode } from "./code-ranges-2XaqsF-S.mjs"; import { a as findFenceSpanAt, s as parseFenceSpans } from "./code-regions-CfNY0afj.mjs"; import { n as findMarkdownCodeRegions } from "./reasoning-tags-DL3sP_9k.mjs"; import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { i as trimEndWhitespaceGraphemes, n as firstGraphemeClusterLength, r as skipWhitespaceGraphemes, t as findGraphemeChunkEnd } from "./grapheme-B63AW15X.mjs"; import { S as resolveChannelStreamingChunkMode } from "./streaming-DsQF8isK.mjs"; import { t as scanParenAwareBreakpoints } from "./text-breakpoints-BMDWjk5t.mjs"; import { n as normalizeChunkLimit, t as chunkTextByBreakResolver } from "./text-chunking-jVP8r8XJ.mjs"; //#region src/auto-reply/chunk.ts const DEFAULT_CHUNK_LIMIT = 4e3; const DEFAULT_CHUNK_MODE = "length"; function resolveChunkConfig(e, t, n) { if (!t || t === "webchat") return {}; const r = (e?.channels)?.[t]; const i = r?.accounts; return { channel: r, account: i ? resolveChannelAccountEntry(i, normalizeAccountId(n), t) : void 0 }; } function resolveTextChunkLimit(e, t, n, r) { const i = r?.fallbackLimit !== void 0 && r.fallbackLimit > 0 ? r.fallbackLimit : DEFAULT_CHUNK_LIMIT; const { channel: a, account: o } = resolveChunkConfig(e, t, n); const s = o?.textChunkLimit ?? a?.textChunkLimit; if (s !== void 0 && s > 0) return s; return i; } function resolveChunkMode(e, t, n) { const { channel: r, account: i } = resolveChunkConfig(e, t, n); return resolveChannelStreamingChunkMode(i) ?? resolveChannelStreamingChunkMode(r) ?? DEFAULT_CHUNK_MODE; } /** * Split text on newlines, trimming line whitespace. * Blank lines are folded into the next non-empty line as leading "\n" prefixes. * Leading and trailing blank lines are capped to the available UTF-16 space. * Long lines can be split by length (default) or kept intact via splitLongLines:false. */ function chunkByNewline(e, t, n) { if (!e) return []; const r = normalizeChunkLimit(t); if (r <= 0) return e.trim() ? [e] : []; const i = n?.splitLongLines !== false; const a = n?.trimLines !== false; const o = splitByNewline(e, n?.isSafeBreak); const s = []; let c = 0; for (const e of o) { const t = trimEndWhitespaceGraphemes(e.slice(skipWhitespaceGraphemes(e))); if (!t) { c += 1; continue; } const n = a ? t : e; const o = c ? Math.max(0, r - firstGraphemeClusterLength(n)) : 0; const l = "\n".repeat(Math.min(c, o)); c = 0; if (!i || n.length + l.length <= r) { s.push(l + n); continue; } const u = Math.max(1, r - l.length); const d = findGraphemeChunkEnd(n, 0, u); const f = n.slice(0, d); s.push(l + f); const p = n.slice(d); if (p) s.push(...chunkText(p, r)); } const l = s.at(-1); if (c > 0 && l !== void 0) { const e = Math.min(c, Math.max(0, r - l.length)); s[s.length - 1] = l + "\n".repeat(e); } return s; } /** * Split text into chunks on paragraph boundaries (blank lines), preserving lists and * single-newline line wraps inside paragraphs. * * - Only breaks at paragraph separators ("\n\n" or more, allowing whitespace on blank lines) * - Packs multiple paragraphs into a single chunk up to `limit` * - Falls back to length-based splitting when a single paragraph exceeds `limit` * (unless `splitLongParagraphs` is disabled) */ function chunkByParagraph(e, t, n) { if (!e) return []; if (t <= 0) return [e]; const r = n?.splitLongParagraphs !== false; let i; const a = e.replace(/\u2029/g, (t, n) => isInsideCode(n, i ??= findMarkdownCodeRegions(e)) ? t : "\n\n"); if (a !== e) i = void 0; const o = a.replace(/\r\n?|\u2028/g, (e, t) => e === "\u2028" && isInsideCode(t, i ??= findMarkdownCodeRegions(a)) ? e : "\n"); if (!/\n[\t ]*\n+/.test(o)) { if (o.length <= t || !r) return [o]; return chunkText(o, t); } const s = o === a && i ? i : findMarkdownCodeRegions(o); const c = []; const l = []; const u = /\n[\t ]*\n+/g; let d = 0; for (const e of o.matchAll(u)) { const t = e.index ?? 0; if (isInsideCode(t, s)) continue; c.push(o.slice(d, t)); l.push(e[0]); d = t + e[0].length; } c.push(o.slice(d)); const f = []; let p = ""; for (const [e, n] of c.entries()) { const i = trimEndWhitespaceGraphemes(n); if (!i) continue; if (p) { const n = `${p}${l[e - 1] ?? "\n\n"}${i}`; if (n.length <= t) { p = n; continue; } f.push(p); p = ""; } if (i.length <= t) p = i; else f.push(...r ? chunkText(i, t) : [i]); } if (p) f.push(p); return f; } function chunkTextWithMode(e, t, n) { if (n === "newline") return chunkByParagraph(e, t); return chunkText(e, t); } function chunkMarkdownTextWithMode(e, t, n) { const r = normalizeChunkLimit(t); if (n === "newline") return chunkByParagraph(e, r, { splitLongParagraphs: false }).flatMap((e) => chunkMarkdownText(e, r)); return chunkMarkdownText(e, r); } function splitByNewline(e, t = () => true) { const n = []; let r = 0; for (let i = 0; i < e.length; i++) if (e[i] === "\n" && t(i)) { n.push(e.slice(r, i)); r = i + 1; } n.push(e.slice(r)); return n; } function chunkText(e, t) { return chunkTextByBreakResolver(e, t, (e) => { const { lastNewline: t, lastWhitespace: n } = scanParenAwareBreakpoints(e, 0, e.length); return t > 0 ? t : n; }); } function chunkMarkdownText(e, t) { const n = normalizeChunkLimit(t); if (!e) return []; if (n <= 0 || e.length <= n) return [e]; const r = []; const i = parseFenceSpans(e); let a = 0; let o; while (a < e.length) { const t = o ? resolveFenceReopenLine(o, n) : ""; const s = t ? `${t}\n` : ""; const c = Math.max(1, n - s.length); if (e.length - a <= c) { const t = `${s}${e.slice(a)}`; if (t.length > 0) r.push(t); break; } o = void 0; const l = Math.min(e.length, a + c); const u = pickSafeBreakIndex(e, a, l, i); let d = findGraphemeChunkEnd(e, a, l, u); const f = findFenceSpanAt(i, d); let p = f; if (f) { const t = `${f.indent}${f.marker}`; if (!resolveFenceReopenLine(f, n)) { d = findGraphemeChunkEnd(e, a, l); p = void 0; } else { const n = a + (c - (t.length + 1)); const r = Math.min(e.length, s ? a + 1 : Math.max(a + 1, f.start + f.openLine.length + 2)); const u = a + (c - t.length); let m = false; let h = e.lastIndexOf("\n", Math.max(a, u - 1)); while (h >= a) { const t = h + 1; if (t < r) break; const n = findFenceSpanAt(i, t); if (n && n.start === f.start) { d = t; m = true; break; } h = e.lastIndexOf("\n", h - 1); } if (!m && r >= u) { d = findGraphemeChunkEnd(e, a, l); p = void 0; o = f; } else { d = findGraphemeChunkEnd(e, a, m ? u : n, m ? d : n); const t = findFenceSpanAt(i, d); p = t && t.start === f.start ? t : void 0; } } } const m = e.slice(a, d); if (!m) break; let h = `${s}${m}`; let g = d; if (p) { const e = `${p.indent}${p.marker}`; h = h.endsWith("\n") ? `${h}${e}` : `${h}\n${e}`; o = p; } else if (!f) { g = skipWhitespaceGraphemes(e, d, 1); g = skipLeadingNewlines(e, g); } r.push(h); a = g; } return r; } function resolveFenceReopenLine(e, t) { const n = `${e.indent}${e.marker}`; if (e.openLine.length + n.length + 3 <= t) return e.openLine; return n.length * 2 + 3 <= t ? n : ""; } function skipLeadingNewlines(e, t = 0) { let n = e[t] === "\n" ? skipWhitespaceGraphemes(e, t, 1) : t; if (n === t) return t; while (n < e.length && e[n] === "\n") n++; return n; } function pickSafeBreakIndex(e, t, n, r) { let i = 0; let a = r.length; while (i < a) { const e = Math.floor((i + a) / 2); const n = r[e]; if (n && n.end <= t) i = e + 1; else a = e; } let o = i < r.length ? r[i] : void 0; const { lastNewline: s, lastWhitespace: c } = scanParenAwareBreakpoints(e, t, n, r.length > 0 ? (e) => { while (o && o.end <= e) { i += 1; o = i < r.length ? r[i] : void 0; } return o && e > o.start ? o.end : void 0; } : void 0); return s > t ? s : c > t ? c : -1; } //#endregion export { chunkText as a, resolveTextChunkLimit as c, chunkMarkdownTextWithMode as i, chunkByParagraph as n, chunkTextWithMode as o, chunkMarkdownText as r, resolveChunkMode as s, chunkByNewline as t };