fix: 清理真正的退格符 \u{8} (llama-cli 进度动画)

This commit is contained in:
2026-07-04 17:39:42 +08:00
parent 15ab383603
commit b0e201a16f

View File

@@ -334,10 +334,11 @@ fn parse_llama_output(raw: &str) -> String {
break; break;
} }
// 清理进度动画字符 |/-\ 和转义序列 \b // 清理进度动画字符 |/-\ 和退格符
// llama-cli 的进度动画用 \b退格刷新stdout 捕获后变成字面 \b // llama-cli 的进度动画用退格符 \u{8} 刷新stdout 捕获后保留为字面退格或 \b 转义
let cleaned: String = trimmed let cleaned: String = trimmed
.replace("\\b", "") .replace("\\b", "") // 字面 \b 转义序列
.replace("\u{8}", "") // 真正的退格符
.chars() .chars()
.filter(|c| !"|\\-/".contains(*c)) .filter(|c| !"|\\-/".contains(*c))
.collect::<String>(); .collect::<String>();
@@ -358,19 +359,22 @@ mod tests {
#[test] #[test]
fn test_parse_llama_output() { fn test_parse_llama_output() {
// 进度动画和回复在同一行,用 \b 分隔 // 进度动画和回复在同一行,用真正的退格符 \u{8} 分隔
let raw = " build : b1-fdb1db8\n\ let bs = "\u{8}"; // backspace
let raw = format!(
" 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\
\\b-\\b\\\\\\b|\\b/\\b-\\b\\\\\\b|\\b/\\b 汪!\n\ {bs}-{bs}|{bs}/{bs}-{bs}|{bs}/{bs} 汪!\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\
Exiting...\n"; Exiting...\n"
);
let reply = parse_llama_output(raw); let reply = parse_llama_output(&raw);
assert_eq!(reply, "汪!"); assert_eq!(reply, "汪!");
} }
} }