feat(llm): improve UTF-8 handling in response processing to prevent infinite loops

feat(shell): enhance output capturing by using threads for stdout and stderr
feat(tui): update usage widget to display token counts and provider/model information
refactor(tui): simplify status bar rendering by removing unnecessary token calculations
This commit is contained in:
asepharyana
2026-07-20 16:16:26 +07:00
parent 6bef4a3f82
commit 7ea226509c
4 changed files with 68 additions and 45 deletions
+13 -4
View File
@@ -386,13 +386,22 @@ impl LlmClient {
break;
}
byte_buf.extend_from_slice(&chunk_buf[..n]);
// Drain any bytes that are not valid UTF-8 to prevent
// infinite loop when a non-UTF-8 sequence is received.
let valid_len = match std::str::from_utf8(&byte_buf) {
Ok(s) => s.len(),
Err(e) => e.valid_up_to(),
Err(e) => {
let n = e.valid_up_to();
if n == 0 {
// No valid UTF-8 prefix; skip the first byte (likely
// a partial multi-byte sequence or stray byte).
byte_buf.drain(..1);
continue;
}
n
}
};
if valid_len == 0 {
continue;
}
let text =
String::from_utf8_lossy(&byte_buf[..valid_len]).into_owned();
byte_buf.drain(..valid_len);