openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
45 lines (44 loc) • 1.48 kB
JavaScript
import { t as avoidTrailingHighSurrogateBreak } from "./utf16-slice-D_ngcYKd.mjs";
//#region packages/memory-host-sdk/src/host/embedding-input-limits.ts
function estimateUtf8Bytes(text) {
if (!text) return 0;
return Buffer.byteLength(text, "utf8");
}
function estimateStructuredEmbeddingInputBytes(input) {
if (!input.parts?.length) return estimateUtf8Bytes(input.text);
let total = 0;
for (const part of input.parts) {
if (part.type === "text") {
total += estimateUtf8Bytes(part.text);
continue;
}
total += estimateUtf8Bytes(part.mimeType);
total += estimateUtf8Bytes(part.data);
}
return total;
}
function splitTextToUtf8ByteLimit(text, maxUtf8Bytes) {
if (maxUtf8Bytes <= 0) return [text];
if (estimateUtf8Bytes(text) <= maxUtf8Bytes) return [text];
const parts = [];
let cursor = 0;
while (cursor < text.length) {
let low = cursor + 1;
let high = Math.min(text.length, cursor + maxUtf8Bytes);
let best = cursor;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (estimateUtf8Bytes(text.slice(cursor, mid)) <= maxUtf8Bytes) {
best = mid;
low = mid + 1;
} else high = mid - 1;
}
if (best <= cursor) best = Math.min(text.length, cursor + 1);
best = avoidTrailingHighSurrogateBreak(text, cursor, best);
parts.push(text.slice(cursor, best));
cursor = best;
}
return parts;
}
//#endregion
export { estimateUtf8Bytes as n, splitTextToUtf8ByteLimit as r, estimateStructuredEmbeddingInputBytes as t };