claude_agent/agent/
events.rs1use super::state::{AgentMetrics, AgentState};
4use crate::types::{Message, StopReason, Usage};
5
6#[derive(Debug, Clone)]
8pub enum AgentEvent {
9 Text(String),
10 ToolStart {
11 id: String,
12 name: String,
13 input: serde_json::Value,
14 },
15 ToolEnd {
16 id: String,
17 output: String,
18 is_error: bool,
19 },
20 Thinking(String),
21 ContextUpdate {
22 used_tokens: u64,
23 max_tokens: u64,
24 },
25 RulesActivated {
26 file_path: String,
27 rule_names: Vec<String>,
28 },
29 CompactStarted,
30 CompactCompleted {
31 previous_tokens: u64,
32 current_tokens: u64,
33 },
34 Complete(Box<AgentResult>),
35}
36
37#[derive(Debug, Clone)]
39pub struct AgentResult {
40 pub text: String,
41 pub usage: Usage,
42 pub tool_calls: usize,
43 pub iterations: usize,
44 pub stop_reason: StopReason,
45 pub state: AgentState,
46 pub metrics: AgentMetrics,
47 pub session_id: String,
48 pub structured_output: Option<serde_json::Value>,
49 pub messages: Vec<Message>,
50 pub uuid: String,
52}
53
54impl AgentResult {
55 #[must_use]
56 pub fn text(&self) -> &str {
57 &self.text
58 }
59
60 #[must_use]
61 pub fn total_tokens(&self) -> u32 {
62 self.usage.total()
63 }
64
65 #[must_use]
66 pub fn metrics(&self) -> &AgentMetrics {
67 &self.metrics
68 }
69
70 #[must_use]
71 pub fn session_id(&self) -> &str {
72 &self.session_id
73 }
74
75 pub fn extract<T: serde::de::DeserializeOwned>(&self) -> crate::Result<T> {
76 let value = self
77 .structured_output
78 .as_ref()
79 .ok_or_else(|| crate::Error::Parse("No structured output available".to_string()))?;
80 serde_json::from_value(value.clone()).map_err(|e| crate::Error::Parse(e.to_string()))
81 }
82}