Fix bugs from useGeminiStream refactor (#284)

This commit is contained in:
Tae Hyung Kim
2025-05-07 21:15:41 -07:00
committed by GitHub
parent d524309e3c
commit 13eadcea45
2 changed files with 51 additions and 20 deletions

View File

@@ -184,3 +184,35 @@ export const findSafeSplitPoint = (
// to keep the entire content as one piece.
return content.length;
};
export const findLastSafeSplitPoint = (content: string) => {
const enclosingBlockStart = findEnclosingCodeBlockStart(
content,
content.length,
);
if (enclosingBlockStart !== -1) {
// The end of the content is contained in a code block. Split right before.
return enclosingBlockStart;
}
// Search for the last double newline (\n\n) not in a code block.
let searchStartIndex = content.length;
while (searchStartIndex >= 0) {
const dnlIndex = content.lastIndexOf('\n\n', searchStartIndex);
if (dnlIndex === -1) {
// No more double newlines found after idealMaxLength
break;
}
const potentialSplitPoint = dnlIndex + 2;
if (!isIndexInsideCodeBlock(content, potentialSplitPoint)) {
return potentialSplitPoint;
}
searchStartIndex = potentialSplitPoint; // Continue search after the found \n\n
}
// If no safe double newline found after idealMaxLength, return content.length
// to keep the entire content as one piece.
return content.length;
};