ag_agent/model/session.rs
1//! Session usage statistics produced by agent transports.
2
3/// Token and diff usage statistics associated with one agent session or
4/// isolated prompt.
5#[derive(Clone, PartialEq, Eq, Debug, Default)]
6pub struct SessionStats {
7 /// Added diff lines currently attributed to the session worktree.
8 pub added_lines: u64,
9 /// Deleted diff lines currently attributed to the session worktree.
10 pub deleted_lines: u64,
11 /// Input/prompt tokens consumed by this session.
12 pub input_tokens: u64,
13 /// Output/response tokens produced by this session.
14 pub output_tokens: u64,
15}
16
17impl SessionStats {
18 /// Counts added and deleted lines in one git patch while ignoring file
19 /// header markers such as `+++` and `---`.
20 pub fn line_change_counts(diff: &str) -> (u64, u64) {
21 diff.lines()
22 .fold((0_u64, 0_u64), |(added_lines, deleted_lines), line| {
23 if line.starts_with('+') && !line.starts_with("+++") {
24 return (added_lines.saturating_add(1), deleted_lines);
25 }
26
27 if line.starts_with('-') && !line.starts_with("---") {
28 return (added_lines, deleted_lines.saturating_add(1));
29 }
30
31 (added_lines, deleted_lines)
32 })
33 }
34}