mirror of
https://github.com/tinyhumansai/openhuman.git
synced 2026-07-30 23:14:37 +00:00
fix(streaming): fix SSE buffer corruption and add CJK sentence splitting (#1794)
This commit is contained in:
@@ -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()];
|
||||
|
||||
@@ -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)) => {
|
||||
|
||||
Reference in New Issue
Block a user