Skip to content

Commit cefc53e

Browse files
committed
fix(stats): 修正 usage-only 会话消息计数
1 parent 7954d02 commit cefc53e

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/cortex-cli/src/stats_cmd.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ fn parse_session_file(path: &PathBuf) -> Result<SessionData> {
549549
if session_input > 0 || session_output > 0 {
550550
data.input_tokens = session_input;
551551
data.output_tokens = session_output;
552+
if data.message_count == 0 {
553+
data.message_count = 1;
554+
}
552555
}
553556
}
554557

@@ -700,6 +703,7 @@ fn format_cost(cost: f64) -> String {
700703
#[cfg(test)]
701704
mod tests {
702705
use super::*;
706+
use std::fs;
703707

704708
#[test]
705709
fn test_format_number() {
@@ -755,4 +759,32 @@ mod tests {
755759
let err = validate_days_range("abc").unwrap_err();
756760
assert!(err.contains("not a valid number"));
757761
}
762+
763+
#[test]
764+
fn test_parse_session_file_counts_usage_only_session_as_one_message() {
765+
let path = std::env::temp_dir().join(format!(
766+
"cortex-stats-usage-only-{}.json",
767+
std::process::id()
768+
));
769+
770+
fs::write(
771+
&path,
772+
r#"{
773+
"created_at": "2026-04-09T00:00:00Z",
774+
"model": "gpt-4o",
775+
"usage": {
776+
"input_tokens": 1000000,
777+
"output_tokens": 1000000
778+
}
779+
}"#,
780+
)
781+
.unwrap();
782+
783+
let data = parse_session_file(&path).unwrap();
784+
let _ = fs::remove_file(&path);
785+
786+
assert_eq!(data.message_count, 1);
787+
assert_eq!(data.input_tokens, 1000000);
788+
assert_eq!(data.output_tokens, 1000000);
789+
}
758790
}

0 commit comments

Comments
 (0)