UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

55 lines (54 loc) • 1.94 kB
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 //#region packages/memory-host-sdk/src/host/embedding-inputs.ts /** Build the common text-only embedding input shape. */ function buildTextEmbeddingInput(text) { return { text }; } /** Return true when a chunk needs structured provider handling, not text splitting. */ function hasNonTextEmbeddingParts(input) { return input?.parts?.some((part) => part.type === "inline-data") ?? false; } //#endregion export { splitTextToUtf8ByteLimit as a, estimateUtf8Bytes as i, hasNonTextEmbeddingParts as n, estimateStructuredEmbeddingInputBytes as r, buildTextEmbeddingInput as t };