mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:51:26 +00:00
refactor: share paren-aware chunk break scan
This commit is contained in:
@@ -91,23 +91,7 @@ export function chunkText(text: string, limit: number): string[] {
|
||||
const window = remaining.slice(0, limit);
|
||||
|
||||
// 1) Prefer a newline break inside the window (outside parentheses).
|
||||
let lastNewline = -1;
|
||||
let lastWhitespace = -1;
|
||||
let depth = 0;
|
||||
for (let i = 0; i < window.length; i++) {
|
||||
const char = window[i];
|
||||
if (char === "(") {
|
||||
depth += 1;
|
||||
continue;
|
||||
}
|
||||
if (char === ")" && depth > 0) {
|
||||
depth -= 1;
|
||||
continue;
|
||||
}
|
||||
if (depth !== 0) continue;
|
||||
if (char === "\n") lastNewline = i;
|
||||
else if (/\s/.test(char)) lastWhitespace = i;
|
||||
}
|
||||
const { lastNewline, lastWhitespace } = scanParenAwareBreakpoints(window);
|
||||
|
||||
// 2) Otherwise prefer the last whitespace (word boundary) inside the window.
|
||||
let breakIdx = lastNewline > 0 ? lastNewline : lastWhitespace;
|
||||
@@ -243,12 +227,26 @@ function pickSafeBreakIndex(
|
||||
window: string,
|
||||
spans: ReturnType<typeof parseFenceSpans>,
|
||||
): number {
|
||||
const { lastNewline, lastWhitespace } = scanParenAwareBreakpoints(
|
||||
window,
|
||||
(index) => isSafeFenceBreak(spans, index),
|
||||
);
|
||||
|
||||
if (lastNewline > 0) return lastNewline;
|
||||
if (lastWhitespace > 0) return lastWhitespace;
|
||||
return -1;
|
||||
}
|
||||
|
||||
function scanParenAwareBreakpoints(
|
||||
window: string,
|
||||
isAllowed: (index: number) => boolean = () => true,
|
||||
): { lastNewline: number; lastWhitespace: number } {
|
||||
let lastNewline = -1;
|
||||
let lastWhitespace = -1;
|
||||
let depth = 0;
|
||||
|
||||
for (let i = 0; i < window.length; i++) {
|
||||
if (!isSafeFenceBreak(spans, i)) continue;
|
||||
if (!isAllowed(i)) continue;
|
||||
const char = window[i];
|
||||
if (char === "(") {
|
||||
depth += 1;
|
||||
@@ -263,7 +261,5 @@ function pickSafeBreakIndex(
|
||||
else if (/\s/.test(char)) lastWhitespace = i;
|
||||
}
|
||||
|
||||
if (lastNewline > 0) return lastNewline;
|
||||
if (lastWhitespace > 0) return lastWhitespace;
|
||||
return -1;
|
||||
return { lastNewline, lastWhitespace };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user