mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-29 16:54:30 +00:00
refactor(agents): dedupe sentence break scanning
This commit is contained in:
@@ -24,6 +24,26 @@ type ParagraphBreak = {
|
||||
length: number;
|
||||
};
|
||||
|
||||
function findSafeSentenceBreakIndex(
|
||||
text: string,
|
||||
fenceSpans: FenceSpan[],
|
||||
minChars: number,
|
||||
): number {
|
||||
const matches = text.matchAll(/[.!?](?=\s|$)/g);
|
||||
let sentenceIdx = -1;
|
||||
for (const match of matches) {
|
||||
const at = match.index ?? -1;
|
||||
if (at < minChars) {
|
||||
continue;
|
||||
}
|
||||
const candidate = at + 1;
|
||||
if (isSafeFenceBreak(fenceSpans, candidate)) {
|
||||
sentenceIdx = candidate;
|
||||
}
|
||||
}
|
||||
return sentenceIdx >= minChars ? sentenceIdx : -1;
|
||||
}
|
||||
|
||||
export class EmbeddedBlockChunker {
|
||||
#buffer = "";
|
||||
readonly #chunking: BlockReplyChunking;
|
||||
@@ -211,19 +231,8 @@ export class EmbeddedBlockChunker {
|
||||
}
|
||||
|
||||
if (preference !== "newline") {
|
||||
const matches = buffer.matchAll(/[.!?](?=\s|$)/g);
|
||||
let sentenceIdx = -1;
|
||||
for (const match of matches) {
|
||||
const at = match.index ?? -1;
|
||||
if (at < minChars) {
|
||||
continue;
|
||||
}
|
||||
const candidate = at + 1;
|
||||
if (isSafeFenceBreak(fenceSpans, candidate)) {
|
||||
sentenceIdx = candidate;
|
||||
}
|
||||
}
|
||||
if (sentenceIdx >= minChars) {
|
||||
const sentenceIdx = findSafeSentenceBreakIndex(buffer, fenceSpans, minChars);
|
||||
if (sentenceIdx !== -1) {
|
||||
return { index: sentenceIdx };
|
||||
}
|
||||
}
|
||||
@@ -271,19 +280,8 @@ export class EmbeddedBlockChunker {
|
||||
}
|
||||
|
||||
if (preference !== "newline") {
|
||||
const matches = window.matchAll(/[.!?](?=\s|$)/g);
|
||||
let sentenceIdx = -1;
|
||||
for (const match of matches) {
|
||||
const at = match.index ?? -1;
|
||||
if (at < minChars) {
|
||||
continue;
|
||||
}
|
||||
const candidate = at + 1;
|
||||
if (isSafeFenceBreak(fenceSpans, candidate)) {
|
||||
sentenceIdx = candidate;
|
||||
}
|
||||
}
|
||||
if (sentenceIdx >= minChars) {
|
||||
const sentenceIdx = findSafeSentenceBreakIndex(window, fenceSpans, minChars);
|
||||
if (sentenceIdx !== -1) {
|
||||
return { index: sentenceIdx };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user