From cefc53ef2ee84f556d595ea2efa30a3713f67fed Mon Sep 17 00:00:00 2001 From: wuyangji <694410194@qq.com> Date: Thu, 14 May 2026 04:57:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(stats):=20=E4=BF=AE=E6=AD=A3=20usage-only?= =?UTF-8?q?=20=E4=BC=9A=E8=AF=9D=E6=B6=88=E6=81=AF=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cortex-cli/src/stats_cmd.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/cortex-cli/src/stats_cmd.rs b/src/cortex-cli/src/stats_cmd.rs index 1e407503e..55fa4a1e8 100644 --- a/src/cortex-cli/src/stats_cmd.rs +++ b/src/cortex-cli/src/stats_cmd.rs @@ -549,6 +549,9 @@ fn parse_session_file(path: &PathBuf) -> Result { if session_input > 0 || session_output > 0 { data.input_tokens = session_input; data.output_tokens = session_output; + if data.message_count == 0 { + data.message_count = 1; + } } } @@ -700,6 +703,7 @@ fn format_cost(cost: f64) -> String { #[cfg(test)] mod tests { use super::*; + use std::fs; #[test] fn test_format_number() { @@ -755,4 +759,32 @@ mod tests { let err = validate_days_range("abc").unwrap_err(); assert!(err.contains("not a valid number")); } + + #[test] + fn test_parse_session_file_counts_usage_only_session_as_one_message() { + let path = std::env::temp_dir().join(format!( + "cortex-stats-usage-only-{}.json", + std::process::id() + )); + + fs::write( + &path, + r#"{ + "created_at": "2026-04-09T00:00:00Z", + "model": "gpt-4o", + "usage": { + "input_tokens": 1000000, + "output_tokens": 1000000 + } + }"#, + ) + .unwrap(); + + let data = parse_session_file(&path).unwrap(); + let _ = fs::remove_file(&path); + + assert_eq!(data.message_count, 1); + assert_eq!(data.input_tokens, 1000000); + assert_eq!(data.output_tokens, 1000000); + } }