1use crate::types::{ThreadId, TokenUsage, ToolResult, ToolTier};
16use serde::{Deserialize, Serialize};
17use std::time::Duration;
18
19#[derive(Clone, Debug, Serialize, Deserialize)]
22#[serde(tag = "type", rename_all = "snake_case")]
23pub enum AgentEvent {
24 Start { thread_id: ThreadId, turn: usize },
26
27 Thinking { message_id: String, text: String },
29
30 TextDelta { message_id: String, delta: String },
32
33 Text { message_id: String, text: String },
35
36 ToolCallStart {
38 id: String,
39 name: String,
40 display_name: String,
41 input: serde_json::Value,
42 tier: ToolTier,
43 },
44
45 ToolCallEnd {
47 id: String,
48 name: String,
49 display_name: String,
50 result: ToolResult,
51 },
52
53 ToolProgress {
55 id: String,
57 name: String,
59 display_name: String,
61 stage: String,
63 message: String,
65 data: Option<serde_json::Value>,
67 },
68
69 ToolRequiresConfirmation {
72 id: String,
73 name: String,
74 input: serde_json::Value,
75 description: String,
76 },
77
78 TurnComplete { turn: usize, usage: TokenUsage },
80
81 Done {
83 thread_id: ThreadId,
84 total_turns: usize,
85 total_usage: TokenUsage,
86 duration: Duration,
87 },
88
89 Error { message: String, recoverable: bool },
91
92 ContextCompacted {
94 original_count: usize,
96 new_count: usize,
98 original_tokens: usize,
100 new_tokens: usize,
102 },
103
104 SubagentProgress {
106 subagent_id: String,
108 subagent_name: String,
110 tool_name: String,
112 tool_context: String,
114 completed: bool,
116 success: bool,
118 tool_count: u32,
120 total_tokens: u64,
122 },
123}
124
125impl AgentEvent {
126 #[must_use]
127 pub const fn start(thread_id: ThreadId, turn: usize) -> Self {
128 Self::Start { thread_id, turn }
129 }
130
131 #[must_use]
132 pub fn thinking(message_id: impl Into<String>, text: impl Into<String>) -> Self {
133 Self::Thinking {
134 message_id: message_id.into(),
135 text: text.into(),
136 }
137 }
138
139 #[must_use]
140 pub fn text_delta(message_id: impl Into<String>, delta: impl Into<String>) -> Self {
141 Self::TextDelta {
142 message_id: message_id.into(),
143 delta: delta.into(),
144 }
145 }
146
147 #[must_use]
148 pub fn text(message_id: impl Into<String>, text: impl Into<String>) -> Self {
149 Self::Text {
150 message_id: message_id.into(),
151 text: text.into(),
152 }
153 }
154
155 #[must_use]
156 pub fn tool_call_start(
157 id: impl Into<String>,
158 name: impl Into<String>,
159 display_name: impl Into<String>,
160 input: serde_json::Value,
161 tier: ToolTier,
162 ) -> Self {
163 Self::ToolCallStart {
164 id: id.into(),
165 name: name.into(),
166 display_name: display_name.into(),
167 input,
168 tier,
169 }
170 }
171
172 #[must_use]
173 pub fn tool_call_end(
174 id: impl Into<String>,
175 name: impl Into<String>,
176 display_name: impl Into<String>,
177 result: ToolResult,
178 ) -> Self {
179 Self::ToolCallEnd {
180 id: id.into(),
181 name: name.into(),
182 display_name: display_name.into(),
183 result,
184 }
185 }
186
187 #[must_use]
188 pub fn tool_progress(
189 id: impl Into<String>,
190 name: impl Into<String>,
191 display_name: impl Into<String>,
192 stage: impl Into<String>,
193 message: impl Into<String>,
194 data: Option<serde_json::Value>,
195 ) -> Self {
196 Self::ToolProgress {
197 id: id.into(),
198 name: name.into(),
199 display_name: display_name.into(),
200 stage: stage.into(),
201 message: message.into(),
202 data,
203 }
204 }
205
206 #[must_use]
207 pub const fn done(
208 thread_id: ThreadId,
209 total_turns: usize,
210 total_usage: TokenUsage,
211 duration: Duration,
212 ) -> Self {
213 Self::Done {
214 thread_id,
215 total_turns,
216 total_usage,
217 duration,
218 }
219 }
220
221 #[must_use]
222 pub fn error(message: impl Into<String>, recoverable: bool) -> Self {
223 Self::Error {
224 message: message.into(),
225 recoverable,
226 }
227 }
228
229 #[must_use]
230 pub const fn context_compacted(
231 original_count: usize,
232 new_count: usize,
233 original_tokens: usize,
234 new_tokens: usize,
235 ) -> Self {
236 Self::ContextCompacted {
237 original_count,
238 new_count,
239 original_tokens,
240 new_tokens,
241 }
242 }
243}