UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

43 lines (42 loc) • 1.18 kB
import { t as avoidTrailingHighSurrogateBreak } from "./utf16-slice-CuDyEXgS.mjs"; //#region packages/memory-host-sdk/src/host/embedding-input-limits.ts function estimateUtf8Bytes(e) { return Buffer.byteLength(e, "utf8"); } function estimateStructuredEmbeddingInputBytes(e) { if (!e.parts?.length) return estimateUtf8Bytes(e.text); let t = 0; for (const n of e.parts) { if (n.type === "text") { t += estimateUtf8Bytes(n.text); continue; } t += estimateUtf8Bytes(n.mimeType); t += estimateUtf8Bytes(n.data); } return t; } function splitTextToUtf8ByteLimit(e, t) { if (t <= 0 || estimateUtf8Bytes(e) <= t) return [e]; const n = []; let r = 0; while (r < e.length) { let i = r + 1; let a = Math.min(e.length, r + t); let o = r; while (i <= a) { const n = Math.floor((i + a) / 2); if (estimateUtf8Bytes(e.slice(r, n)) <= t) { o = n; i = n + 1; } else a = n - 1; } if (o <= r) o = Math.min(e.length, r + 1); o = avoidTrailingHighSurrogateBreak(e, r, o); n.push(e.slice(r, o)); r = o; } return n; } //#endregion export { estimateUtf8Bytes as n, splitTextToUtf8ByteLimit as r, estimateStructuredEmbeddingInputBytes as t };