1use crate::context_window::ContextWindowPhase;
4use crate::error::AgentError;
5use crate::hook::CheckpointReason;
6use aither_core::llm::ToolResult;
7
8pub use aither_sandbox::BackgroundReason;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum RunPauseReason {
13 PermissionRequest,
15 AskUser,
17 WorkspaceRequest,
19}
20
21#[derive(Debug, Clone)]
23pub enum AgentEvent {
24 RunStart {
26 run_id: String,
28 max_iterations: usize,
30 },
31
32 Text(String),
34
35 Reasoning(String),
37
38 ToolCallDelta {
43 id: String,
45 name: String,
47 arguments_fragment: String,
49 },
50
51 ToolCallStart {
53 id: String,
55 name: String,
57 arguments: String,
59 },
60
61 ToolCallEnd {
63 id: String,
65 name: String,
67 result: ToolResult,
69 },
70
71 TurnComplete {
73 turn: usize,
75 has_tool_calls: bool,
77 },
78
79 Checkpoint {
81 run_id: String,
83 reason: CheckpointReason,
85 turn: usize,
87 phase: ContextWindowPhase,
89 message_count: usize,
91 },
92
93 BackgroundTaskStarted {
95 task_id: String,
97 output_preview: String,
99 output_file: String,
101 reason: BackgroundReason,
103 },
104
105 BackgroundTaskCompleted {
107 task_id: String,
109 result: String,
111 },
112
113 TerminalInputNeeded {
115 task_id: Option<String>,
117 notice: String,
119 },
120
121 RunPaused {
123 reason: RunPauseReason,
125 tool_call_id: String,
127 },
128
129 RunResumed {
131 reason: RunPauseReason,
133 tool_call_id: String,
135 },
136
137 SkillActivated {
139 name: String,
141 allowed_tools: Option<Vec<String>>,
143 resource_paths: Option<Vec<String>>,
145 },
146
147 Complete {
149 final_text: String,
151 turns: usize,
153 },
154
155 Usage(aither_core::llm::Usage),
157
158 Error(AgentError),
160}
161
162impl AgentEvent {
163 #[must_use]
165 pub fn run_start(run_id: impl Into<String>, max_iterations: usize) -> Self {
166 Self::RunStart {
167 run_id: run_id.into(),
168 max_iterations,
169 }
170 }
171
172 #[must_use]
174 pub fn text(content: impl Into<String>) -> Self {
175 Self::Text(content.into())
176 }
177
178 #[must_use]
180 pub fn reasoning(content: impl Into<String>) -> Self {
181 Self::Reasoning(content.into())
182 }
183
184 #[must_use]
186 pub fn tool_delta(
187 id: impl Into<String>,
188 name: impl Into<String>,
189 arguments_fragment: impl Into<String>,
190 ) -> Self {
191 Self::ToolCallDelta {
192 id: id.into(),
193 name: name.into(),
194 arguments_fragment: arguments_fragment.into(),
195 }
196 }
197
198 #[must_use]
200 pub fn tool_start(
201 id: impl Into<String>,
202 name: impl Into<String>,
203 arguments: impl Into<String>,
204 ) -> Self {
205 Self::ToolCallStart {
206 id: id.into(),
207 name: name.into(),
208 arguments: arguments.into(),
209 }
210 }
211
212 #[must_use]
214 pub fn tool_success(
215 id: impl Into<String>,
216 name: impl Into<String>,
217 result: impl Into<String>,
218 ) -> Self {
219 Self::ToolCallEnd {
220 id: id.into(),
221 name: name.into(),
222 result: ToolResult::text(result),
223 }
224 }
225
226 #[must_use]
228 pub fn tool_failure(
229 id: impl Into<String>,
230 name: impl Into<String>,
231 error: impl Into<String>,
232 ) -> Self {
233 Self::ToolCallEnd {
234 id: id.into(),
235 name: name.into(),
236 result: ToolResult::error(error),
237 }
238 }
239
240 #[must_use]
242 pub const fn turn_complete(turn: usize, has_tool_calls: bool) -> Self {
243 Self::TurnComplete {
244 turn,
245 has_tool_calls,
246 }
247 }
248
249 #[must_use]
251 pub fn checkpoint(
252 run_id: impl Into<String>,
253 reason: CheckpointReason,
254 turn: usize,
255 phase: ContextWindowPhase,
256 message_count: usize,
257 ) -> Self {
258 Self::Checkpoint {
259 run_id: run_id.into(),
260 reason,
261 turn,
262 phase,
263 message_count,
264 }
265 }
266
267 #[must_use]
269 pub fn background_task_started(
270 task_id: impl Into<String>,
271 output_preview: impl Into<String>,
272 output_file: impl Into<String>,
273 reason: BackgroundReason,
274 ) -> Self {
275 Self::BackgroundTaskStarted {
276 task_id: task_id.into(),
277 output_preview: output_preview.into(),
278 output_file: output_file.into(),
279 reason,
280 }
281 }
282
283 #[must_use]
285 pub fn background_task_completed(
286 task_id: impl Into<String>,
287 result: impl Into<String>,
288 ) -> Self {
289 Self::BackgroundTaskCompleted {
290 task_id: task_id.into(),
291 result: result.into(),
292 }
293 }
294
295 #[must_use]
297 pub fn terminal_input_needed(task_id: Option<String>, notice: impl Into<String>) -> Self {
298 Self::TerminalInputNeeded {
299 task_id,
300 notice: notice.into(),
301 }
302 }
303
304 #[must_use]
306 pub fn run_paused(reason: RunPauseReason, tool_call_id: impl Into<String>) -> Self {
307 Self::RunPaused {
308 reason,
309 tool_call_id: tool_call_id.into(),
310 }
311 }
312
313 #[must_use]
315 pub fn run_resumed(reason: RunPauseReason, tool_call_id: impl Into<String>) -> Self {
316 Self::RunResumed {
317 reason,
318 tool_call_id: tool_call_id.into(),
319 }
320 }
321
322 #[must_use]
324 pub fn skill_activated(
325 name: impl Into<String>,
326 allowed_tools: Option<Vec<String>>,
327 resource_paths: Option<Vec<String>>,
328 ) -> Self {
329 Self::SkillActivated {
330 name: name.into(),
331 allowed_tools,
332 resource_paths,
333 }
334 }
335
336 #[must_use]
338 pub fn complete(final_text: impl Into<String>, turns: usize) -> Self {
339 Self::Complete {
340 final_text: final_text.into(),
341 turns,
342 }
343 }
344
345 #[must_use]
347 pub const fn error(error: AgentError) -> Self {
348 Self::Error(error)
349 }
350
351 #[must_use]
353 pub const fn is_complete(&self) -> bool {
354 matches!(self, Self::Complete { .. })
355 }
356
357 #[must_use]
359 pub const fn is_error(&self) -> bool {
360 matches!(self, Self::Error(_))
361 }
362
363 #[must_use]
365 pub const fn is_terminal(&self) -> bool {
366 self.is_complete() || self.is_error()
367 }
368}