fix(streaming): fix SSE buffer corruption and add CJK sentence splitting (#1794)

This commit is contained in:
Sathvik Gilakamsetty
2026-05-15 19:36:19 -07:00
committed by GitHub
parent 26cdbdd4c1
commit 5841856288
3 changed files with 22 additions and 2 deletions
@@ -311,6 +311,7 @@ fn split_sentences(text: &str) -> Vec<String> {
current.push(chars[i]);
let ch = chars[i];
// Latin sentence terminators: split on ". " followed by uppercase.
if (ch == '.' || ch == '!' || ch == '?')
&& i + 2 < chars.len()
&& chars[i + 1] == ' '
@@ -325,6 +326,17 @@ fn split_sentences(text: &str) -> Vec<String> {
continue;
}
// CJK sentence terminators: split after fullwidth period/exclamation/question.
if (ch == '\u{3002}' || ch == '\u{FF01}' || ch == '\u{FF1F}') && i + 1 < chars.len() {
let trimmed = current.trim().to_string();
if !trimmed.is_empty() {
parts.push(trimmed);
}
current.clear();
i += 1;
continue;
}
i += 1;
}
@@ -180,6 +180,15 @@ fn split_sentences_single_sentence_without_terminator() {
assert_eq!(out.len(), 1);
}
#[test]
fn split_sentences_splits_on_cjk_terminators() {
let out = split_sentences("你好世界。今天天气很好!你觉得呢?");
assert_eq!(out.len(), 3);
assert_eq!(out[0], "你好世界。");
assert_eq!(out[1], "今天天气很好!");
assert_eq!(out[2], "你觉得呢?");
}
#[test]
fn group_sentences_single_entry_roundtrip() {
let v: Vec<String> = vec!["Hello world".into()];
+1 -2
View File
@@ -52,8 +52,7 @@ pub(crate) fn sse_bytes_to_chunks(
// Process complete lines
while let Some(pos) = buffer.find('\n') {
let line = buffer.drain(..=pos).collect::<String>();
buffer = buffer[pos + 1..].to_string();
let line: String = buffer.drain(..=pos).collect();
match parse_sse_line(&line) {
Ok(Some(content)) => {