mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-08 20:02:54 +00:00
Adds broad inline comments and JSDoc for CLI, cron, outbound/channel, plugin SDK, ACP, shared helpers, net policy, and related utility contracts. Proof: git diff --check on latest exact head plus focused cron tests passed; CI had no failing checks observed before merge attempt.
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
/** Splits text into bounded chunks using caller-owned soft-break selection. */
|
|
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);
|
|
// Invalid or zero-width soft breaks would stall the loop, so fall back to the hard limit.
|
|
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);
|
|
}
|
|
// If the break lands before whitespace, consume one separator and trim the rest for the next 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;
|
|
}
|