fix: 解析 llama-cli 进度动画和回复在同一行的情况 (清理 \b 转义)

This commit is contained in:
2026-07-04 17:28:06 +08:00
parent 1c037db0c2
commit 9069145de6

View File

@@ -325,12 +325,6 @@ fn parse_llama_output(raw: &str) -> String {
continue; continue;
} }
// 跳过进度动画行(只含 |/-\ 和空格)
let cleaned: String = trimmed.chars().filter(|c| !"|\\-/ ".contains(*c)).collect();
if cleaned.is_empty() {
continue;
}
// 遇到性能统计行 [ Prompt: ... ] 停止 // 遇到性能统计行 [ Prompt: ... ] 停止
if trimmed.starts_with('[') && trimmed.contains("Generation") { if trimmed.starts_with('[') && trimmed.contains("Generation") {
break; break;
@@ -340,7 +334,19 @@ fn parse_llama_output(raw: &str) -> String {
break; break;
} }
reply_lines.push(trimmed); // 清理进度动画字符 |/-\ 和转义序列 \b
// llama-cli 的进度动画用 \b退格刷新stdout 捕获后变成字面 \b
let cleaned: String = trimmed
.replace("\\b", "") // 退格转义
.chars()
.filter(|c| !"|\\-/".contains(*c))
.collect();
let cleaned = cleaned.trim();
if cleaned.is_empty() {
continue;
}
reply_lines.push(cleaned);
} }
reply_lines.join("\n").trim().to_string() reply_lines.join("\n").trim().to_string()
@@ -352,12 +358,13 @@ mod tests {
#[test] #[test]
fn test_parse_llama_output() { fn test_parse_llama_output() {
// 进度动画和回复在同一行,用 \b 分隔
let raw = " █ █ build : b1-fdb1db8\n\ let raw = " █ █ build : b1-fdb1db8\n\
model : model_store/llm/qwen2.5-0.5b-q4_k_m.gguf\n\ model : model_store/llm/qwen2.5-0.5b-q4_k_m.gguf\n\
\n\ \n\
> hello\n\ > hello\n\
\n\ \n\
|/-\\|/-\\ 汪!\n\ \\b-\\b\\\\\\b|\\b/\\b-\\b\\\\\\b|\\b/\\b \b汪!\n\
\n\ \n\
[ Prompt: 31.1 t/s | Generation: 26.3 t/s ]\n\ [ Prompt: 31.1 t/s | Generation: 26.3 t/s ]\n\
\n\ \n\