agtrace_engine/session/
summary.rs

1use super::types::AgentSession;
2use serde::{Deserialize, Serialize};
3
4/// Statistical summary of session event composition.
5///
6/// Provides aggregated counts of different event types within a session
7/// for analysis and reporting purposes.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct SessionSummary {
10    /// Breakdown of event counts by type.
11    pub event_counts: EventCounts,
12}
13
14/// Count of events by type within a session.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct EventCounts {
17    /// Total number of events in the session.
18    pub total: usize,
19    /// Number of user input messages.
20    pub user_messages: usize,
21    /// Number of assistant response messages.
22    pub assistant_messages: usize,
23    /// Number of tool call invocations.
24    pub tool_calls: usize,
25    /// Number of reasoning/thinking blocks.
26    pub reasoning_blocks: usize,
27}
28
29/// Generate statistical summary from a session.
30///
31/// Counts all events by type to produce an aggregated view of session composition.
32pub fn summarize(session: &AgentSession) -> SessionSummary {
33    let user_count = session.turns.len();
34    let mut assistant_count = 0;
35    let mut tool_call_count = 0;
36    let mut reasoning_count = 0;
37    let mut total_event_count = 0;
38
39    for turn in &session.turns {
40        total_event_count += 1;
41
42        for step in &turn.steps {
43            if step.message.is_some() {
44                assistant_count += 1;
45                total_event_count += 1;
46            }
47            if step.reasoning.is_some() {
48                reasoning_count += 1;
49                total_event_count += 1;
50            }
51
52            tool_call_count += step.tools.len();
53            total_event_count += step.tools.len() * 2;
54
55            if step.usage.is_some() {
56                total_event_count += 1;
57            }
58        }
59    }
60
61    SessionSummary {
62        event_counts: EventCounts {
63            total: total_event_count,
64            user_messages: user_count,
65            assistant_messages: assistant_count,
66            tool_calls: tool_call_count,
67            reasoning_blocks: reasoning_count,
68        },
69    }
70}