mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 12:44:59 +00:00
refactor(shared): reuse outbound text chunking core
This commit is contained in:
34
src/shared/text-chunking.ts
Normal file
34
src/shared/text-chunking.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
export function chunkTextByBreakResolver(
|
||||
text: string,
|
||||
limit: number,
|
||||
resolveBreakIndex: (window: string) => number,
|
||||
): string[] {
|
||||
if (!text) {
|
||||
return [];
|
||||
}
|
||||
if (limit <= 0 || text.length <= limit) {
|
||||
return [text];
|
||||
}
|
||||
const chunks: string[] = [];
|
||||
let remaining = text;
|
||||
while (remaining.length > limit) {
|
||||
const window = remaining.slice(0, limit);
|
||||
const candidateBreak = resolveBreakIndex(window);
|
||||
const breakIdx =
|
||||
Number.isFinite(candidateBreak) && candidateBreak > 0 && candidateBreak <= limit
|
||||
? candidateBreak
|
||||
: limit;
|
||||
const rawChunk = remaining.slice(0, breakIdx);
|
||||
const chunk = rawChunk.trimEnd();
|
||||
if (chunk.length > 0) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
const brokeOnSeparator = breakIdx < remaining.length && /\s/.test(remaining[breakIdx]);
|
||||
const nextStart = Math.min(remaining.length, breakIdx + (brokeOnSeparator ? 1 : 0));
|
||||
remaining = remaining.slice(nextStart).trimStart();
|
||||
}
|
||||
if (remaining.length) {
|
||||
chunks.push(remaining);
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
Reference in New Issue
Block a user