chore: improve code quality, omit head 2 newlines in handler but in not client

This commit is contained in:
sigoden 2023-03-10 07:36:46 +08:00
parent 978d4c54c0
commit 865fb49af9
2 changed files with 4 additions and 8 deletions

View File

@ -104,7 +104,6 @@ impl ChatGptClient {
bail!("Request failed");
}
let mut stream = res.bytes_stream().eventsource();
let mut virgin = true;
while let Some(part) = stream.next().await {
let chunk = part?.data;
if chunk == "[DONE]" {
@ -117,12 +116,6 @@ impl ChatGptClient {
if text.is_empty() {
continue;
}
if virgin {
virgin = false;
if text == "\n\n" {
continue;
}
}
handler.text(text)?;
}
}

View File

@ -121,6 +121,10 @@ impl ReplyStreamHandler {
}
pub fn text(&mut self, text: &str) -> Result<()> {
if self.buffer.is_empty() && text == "\n\n" {
return Ok(());
}
self.buffer.push_str(text);
match self.sender.as_ref() {
Some(tx) => {
let ret = tx
@ -132,7 +136,6 @@ impl ReplyStreamHandler {
print_now!("{}", text);
}
}
self.buffer.push_str(text);
Ok(())
}