Skip to main content

bamboo_agent_core/agent/
events.rs

1//! Agent event system for real-time streaming.
2//!
3//! This module defines the event types emitted during agent execution,
4//! which are streamed to clients via Server-Sent Events (SSE).
5//!
6//! # Event Types
7//!
8//! - [`AgentEvent`] - All possible agent execution events
9//! - [`TokenUsage`] - Token consumption statistics
10//! - [`TokenBudgetUsage`] - Detailed token budget information
11//!
12//! # Event Flow
13//!
14//! 1. **Token** events stream generated text
15//! 2. **ToolStart/ToolComplete** track tool execution
16//! 3. **TaskListUpdated** tracks progress
17//! 4. **TokenBudgetUpdated** reports context management
18//! 5. **Complete** or **Error** ends the stream
19//!
20//! # Example
21//!
22//! ```javascript
23//! const eventSource = new EventSource('/api/v1/events/session-id');
24//! eventSource.onmessage = (event) => {
25//!   const data = JSON.parse(event.data);
26//!   switch (data.type) {
27//!     case 'token':
28//!       console.log('Token:', data.content);
29//!       break;
30//!     case 'complete':
31//!       console.log('Done!');
32//!       eventSource.close();
33//!       break;
34//!   }
35//! };
36//! ```
37
38use crate::tools::ToolResult;
39use bamboo_domain::{TaskItemStatus, TaskList};
40use chrono::{DateTime, Utc};
41use serde::{Deserialize, Serialize};
42
43/// Represents events emitted during agent execution.
44///
45/// These events are streamed to clients via SSE to provide real-time
46/// feedback on agent progress, tool execution, and completion.
47///
48/// # Variants
49///
50/// ## Text Generation
51/// - `Token` - Streaming text token
52/// - `ReasoningToken` - Streaming reasoning/thinking token (separate channel)
53///
54/// ## Tool Execution
55/// - `ToolStart` - Tool execution started
56/// - `ToolComplete` - Tool finished successfully
57/// - `ToolError` - Tool execution failed
58///
59/// ## User Interaction
60/// - `NeedClarification` - Agent needs user input
61///
62/// ## Progress Tracking
63/// - `TaskListUpdated` - Task list created or modified
64/// - `TaskListItemProgress` - Individual item progress
65/// - `TaskListCompleted` - All items completed
66/// - `TaskEvaluationStarted` - Task evaluation began
67/// - `TaskEvaluationCompleted` - Task evaluation finished
68///
69/// ## Context Management
70/// - `TokenBudgetUpdated` - Context budget changed
71/// - `ContextCompressionStatus` - Context compression lifecycle progress
72/// - `ContextSummarized` - Old messages summarized
73///
74/// ## Sub-sessions (Async Spawn)
75/// - `SubSessionStarted` - A child session is created and scheduled to run
76/// - `SubSessionEvent` - Forwarded raw child event (full fidelity)
77/// - `SubSessionHeartbeat` - Periodic heartbeat while the child is running
78/// - `SubSessionCompleted` - Child session finished (completed/cancelled/error)
79///
80/// ## Terminal Events
81/// - `Complete` - Execution finished successfully
82/// - `Error` - Execution failed
83///
84/// # Serialization
85///
86/// Events are serialized as JSON with a `type` field for discrimination:
87/// ```json
88/// {"type": "token", "content": "Hello"}
89/// {"type": "complete", "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}}
90/// ```
91#[derive(Debug, Clone, Serialize, Deserialize)]
92#[serde(tag = "type", rename_all = "snake_case")]
93pub enum AgentEvent {
94    /// Text token generated by the LLM.
95    Token {
96        /// Generated text content
97        content: String,
98    },
99
100    /// Reasoning/thinking token generated by the LLM.
101    ///
102    /// This is streamed separately from assistant answer tokens so the UI can
103    /// choose whether and how to display model reasoning traces.
104    ReasoningToken {
105        /// Generated reasoning content
106        content: String,
107    },
108
109    /// Streaming output emitted while a specific tool call is running.
110    ///
111    /// This is used to render "live output" inside a tool-call card in the UI
112    /// without mixing tool output into the assistant's main token stream.
113    ToolToken {
114        /// Tool call identifier that this output belongs to.
115        tool_call_id: String,
116        /// Output chunk.
117        content: String,
118    },
119
120    /// Tool execution started.
121    ToolStart {
122        /// Unique tool call identifier
123        tool_call_id: String,
124        /// Name of the tool being executed
125        tool_name: String,
126        /// Tool arguments (JSON)
127        arguments: serde_json::Value,
128    },
129
130    /// Tool execution completed successfully.
131    ToolComplete {
132        /// Tool call identifier
133        tool_call_id: String,
134        /// Tool execution result
135        result: ToolResult,
136    },
137
138    /// Tool execution failed.
139    ToolError {
140        /// Tool call identifier
141        tool_call_id: String,
142        /// Error message
143        error: String,
144    },
145
146    /// Structured lifecycle event for tool execution tracking.
147    ///
148    /// These events complement `ToolStart`/`ToolComplete`/`ToolError` with
149    /// richer metadata (mutability, auto-approval, wall-clock timing) and
150    /// are emitted by `ToolEmitter` (in `bamboo-agent-tools`).
151    ToolLifecycle {
152        /// Tool call identifier
153        tool_call_id: String,
154        /// Canonical tool name
155        tool_name: String,
156        /// Lifecycle phase: "begin", "finished", "error", "cancelled"
157        phase: String,
158        /// Wall-clock milliseconds since the call began (None for begin)
159        #[serde(skip_serializing_if = "Option::is_none")]
160        elapsed_ms: Option<u64>,
161        /// Whether the tool mutates state (writes files, runs commands)
162        is_mutating: bool,
163        /// Whether execution was auto-approved (no user prompt needed)
164        auto_approved: bool,
165        /// Human-readable summary
166        #[serde(skip_serializing_if = "Option::is_none")]
167        summary: Option<String>,
168        /// Error message (if phase == "error")
169        #[serde(skip_serializing_if = "Option::is_none")]
170        error: Option<String>,
171    },
172
173    /// Agent needs clarification from the user.
174    NeedClarification {
175        /// Question to ask the user
176        question: String,
177        /// Optional predefined options
178        options: Option<Vec<String>>,
179        /// Tool call identifier that triggered this clarification
180        #[serde(default, skip_serializing_if = "Option::is_none")]
181        tool_call_id: Option<String>,
182        /// Whether the user can provide a free-text response
183        #[serde(default = "default_allow_custom")]
184        allow_custom: bool,
185    },
186
187    /// Emitted when task list is created or updated.
188    TaskListUpdated {
189        /// Current task list state.
190        task_list: TaskList,
191    },
192
193    /// Emitted when a task item makes progress (delta update).
194    TaskListItemProgress {
195        /// Session identifier
196        session_id: String,
197        /// Item identifier
198        item_id: String,
199        /// New item status
200        status: TaskItemStatus,
201        /// Number of tool calls made
202        tool_calls_count: usize,
203        /// Item version (for optimistic concurrency)
204        version: u64,
205    },
206
207    /// Emitted when all task items are completed.
208    TaskListCompleted {
209        /// Session identifier
210        session_id: String,
211        /// Completion timestamp
212        completed_at: DateTime<Utc>,
213        /// Total agent rounds executed
214        total_rounds: u32,
215        /// Total tool calls made
216        total_tool_calls: usize,
217    },
218
219    /// Emitted when task evaluation starts.
220    TaskEvaluationStarted {
221        /// Session identifier
222        session_id: String,
223        /// Number of items to evaluate
224        items_count: usize,
225    },
226
227    /// Emitted when task evaluation completes.
228    TaskEvaluationCompleted {
229        /// Session identifier
230        session_id: String,
231        /// Number of items updated
232        updates_count: usize,
233        /// Evaluation reasoning
234        reasoning: String,
235    },
236
237    /// Emitted when token budget is prepared (after context truncation)
238    TokenBudgetUpdated {
239        /// Token budget details
240        usage: TokenBudgetUsage,
241    },
242
243    /// Emitted when host-side context compression lifecycle changes.
244    ContextCompressionStatus {
245        /// Compression phase label (for example: pre-turn, mid-turn).
246        phase: String,
247        /// Compression status: started | completed | failed | skipped
248        status: String,
249    },
250
251    /// Emitted when conversation context is summarized
252    ContextSummarized {
253        /// Generated summary text
254        summary: String,
255        /// Number of old messages summarized
256        messages_summarized: usize,
257        /// Tokens saved by summarization
258        tokens_saved: u32,
259        /// Context usage percentage before compression
260        #[serde(default)]
261        usage_before_percent: f64,
262        /// Context usage percentage after compression
263        #[serde(default)]
264        usage_after_percent: f64,
265        /// What triggered the compression: "auto" | "manual" | "critical"
266        #[serde(default)]
267        trigger_type: String,
268    },
269
270    /// Emitted when context pressure reaches warning or critical levels.
271    /// Frontend should display this to the user as a proactive notification.
272    ContextPressureNotification {
273        /// Context usage as a percentage of the context window.
274        percent: f64,
275        /// Severity level: "warning" (70%) or "critical" (90%).
276        level: String,
277        /// Human-readable message describing the pressure state.
278        message: String,
279    },
280
281    /// A child session was spawned from a parent session (async background job).
282    SubSessionStarted {
283        parent_session_id: String,
284        child_session_id: String,
285        /// Optional title (useful for UI lists).
286        #[serde(default, skip_serializing_if = "Option::is_none")]
287        title: Option<String>,
288    },
289
290    /// Forwarded raw child event to the parent session stream.
291    ///
292    /// Child sessions are not allowed to spawn further sessions, so this should not nest.
293    SubSessionEvent {
294        parent_session_id: String,
295        child_session_id: String,
296        event: Box<AgentEvent>,
297    },
298
299    /// Heartbeat emitted while a child session is running.
300    SubSessionHeartbeat {
301        parent_session_id: String,
302        child_session_id: String,
303        timestamp: DateTime<Utc>,
304    },
305
306    /// Child session finished (completed/cancelled/error).
307    SubSessionCompleted {
308        parent_session_id: String,
309        child_session_id: String,
310        /// One of: "completed" | "cancelled" | "error" | "skipped"
311        status: String,
312        #[serde(default, skip_serializing_if = "Option::is_none")]
313        error: Option<String>,
314    },
315
316    /// Plan mode was entered.
317    PlanModeEntered {
318        /// Session identifier
319        session_id: String,
320        /// Optional reason for entering plan mode
321        #[serde(default, skip_serializing_if = "Option::is_none")]
322        reason: Option<String>,
323        /// Previous permission mode before entering plan mode
324        pre_permission_mode: String,
325    },
326
327    /// Plan mode was exited.
328    PlanModeExited {
329        /// Session identifier
330        session_id: String,
331        /// Whether the exit was approved by the user
332        approved: bool,
333        /// The permission mode restored after exiting
334        restored_mode: String,
335        /// Plan content that was reviewed, if any
336        #[serde(default, skip_serializing_if = "Option::is_none")]
337        plan: Option<String>,
338    },
339
340    /// Plan file was updated.
341    PlanFileUpdated {
342        /// Session identifier
343        session_id: String,
344        /// Path to the plan file
345        file_path: String,
346        /// Summary of the plan content (truncated)
347        content_summary: String,
348    },
349
350    /// Runner progress update emitted at the start of each agent turn.
351    ///
352    /// Used to track live execution progress (round count, current activity)
353    /// for diagnostic visibility, especially for child sessions.
354    RunnerProgress {
355        /// Session identifier
356        session_id: String,
357        /// Current turn/round count
358        round_count: u32,
359    },
360
361    /// Agent execution completed successfully.
362    Complete {
363        /// Final token usage statistics
364        usage: TokenUsage,
365    },
366
367    /// Agent execution failed.
368    Error {
369        /// Error message
370        message: String,
371    },
372}
373
374fn default_allow_custom() -> bool {
375    true
376}
377
378/// Re-exported shared token usage type.
379///
380/// See [`bamboo_domain::TokenUsage`] for the canonical definition.
381pub use bamboo_domain::TokenUsage;
382
383pub use bamboo_domain::budget_types::TokenBudgetUsage;
384
385#[cfg(test)]
386mod tests {
387    use super::*;
388    use bamboo_domain::{TaskItem, TaskItemStatus, TaskList};
389
390    fn sample_task_list() -> TaskList {
391        TaskList {
392            session_id: "session-1".to_string(),
393            title: "Task List".to_string(),
394            items: vec![TaskItem {
395                id: "task_1".to_string(),
396                description: "Implement event rename".to_string(),
397                status: TaskItemStatus::InProgress,
398                depends_on: Vec::new(),
399                notes: "Implementing".to_string(),
400                ..TaskItem::default()
401            }],
402            created_at: Utc::now(),
403            updated_at: Utc::now(),
404        }
405    }
406
407    #[test]
408    fn task_list_updated_serializes_with_task_names() {
409        let event = AgentEvent::TaskListUpdated {
410            task_list: sample_task_list(),
411        };
412
413        let value = serde_json::to_value(event).expect("event should serialize");
414        assert_eq!(value["type"], "task_list_updated");
415        assert!(value.get("task_list").is_some());
416        assert!(value.get("todo_list").is_none());
417    }
418
419    #[test]
420    fn task_evaluation_completed_serializes_with_task_type() {
421        let event = AgentEvent::TaskEvaluationCompleted {
422            session_id: "session-1".to_string(),
423            updates_count: 2,
424            reasoning: "Updated statuses".to_string(),
425        };
426
427        let value = serde_json::to_value(event).expect("event should serialize");
428        assert_eq!(value["type"], "task_evaluation_completed");
429    }
430
431    #[test]
432    fn context_compression_status_serializes_with_phase_and_status() {
433        let event = AgentEvent::ContextCompressionStatus {
434            phase: "mid-turn".to_string(),
435            status: "started".to_string(),
436        };
437
438        let value = serde_json::to_value(event).expect("event should serialize");
439        assert_eq!(value["type"], "context_compression_status");
440        assert_eq!(value["phase"], "mid-turn");
441        assert_eq!(value["status"], "started");
442    }
443
444    #[test]
445    fn need_clarification_serializes_with_new_fields() {
446        let event = AgentEvent::NeedClarification {
447            question: "Continue?".to_string(),
448            options: Some(vec!["Yes".to_string(), "No".to_string()]),
449            tool_call_id: Some("tool-1".to_string()),
450            allow_custom: false,
451        };
452
453        let value = serde_json::to_value(event).expect("event should serialize");
454        assert_eq!(value["type"], "need_clarification");
455        assert_eq!(value["question"], "Continue?");
456        assert_eq!(value["options"], serde_json::json!(["Yes", "No"]));
457        assert_eq!(value["tool_call_id"], "tool-1");
458        assert_eq!(value["allow_custom"], false);
459    }
460
461    #[test]
462    fn need_clarification_deserializes_from_old_format_without_new_fields() {
463        let json = serde_json::json!({
464            "type": "need_clarification",
465            "question": "Continue?",
466            "options": ["Yes", "No"]
467        });
468
469        let event: AgentEvent =
470            serde_json::from_value(json).expect("should deserialize old format");
471        match event {
472            AgentEvent::NeedClarification {
473                question,
474                options,
475                tool_call_id,
476                allow_custom,
477            } => {
478                assert_eq!(question, "Continue?");
479                assert_eq!(options, Some(vec!["Yes".to_string(), "No".to_string()]));
480                assert_eq!(tool_call_id, None);
481                assert!(allow_custom); // default_allow_custom returns true
482            }
483            other => panic!("unexpected event: {other:?}"),
484        }
485    }
486
487    #[test]
488    fn need_clarification_deserializes_with_allow_custom_false() {
489        let json = serde_json::json!({
490            "type": "need_clarification",
491            "question": "Pick one",
492            "allow_custom": false
493        });
494
495        let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
496        match event {
497            AgentEvent::NeedClarification {
498                question,
499                options,
500                tool_call_id,
501                allow_custom,
502            } => {
503                assert_eq!(question, "Pick one");
504                assert_eq!(options, None);
505                assert_eq!(tool_call_id, None);
506                assert!(!allow_custom);
507            }
508            other => panic!("unexpected event: {other:?}"),
509        }
510    }
511
512    #[test]
513    fn plan_mode_entered_serializes_correctly() {
514        let event = AgentEvent::PlanModeEntered {
515            session_id: "sess-1".to_string(),
516            reason: Some("Complex refactor".to_string()),
517            pre_permission_mode: "default".to_string(),
518        };
519
520        let value = serde_json::to_value(event).expect("event should serialize");
521        assert_eq!(value["type"], "plan_mode_entered");
522        assert_eq!(value["session_id"], "sess-1");
523        assert_eq!(value["reason"], "Complex refactor");
524        assert_eq!(value["pre_permission_mode"], "default");
525    }
526
527    #[test]
528    fn plan_mode_exited_serializes_correctly() {
529        let event = AgentEvent::PlanModeExited {
530            session_id: "sess-1".to_string(),
531            approved: true,
532            restored_mode: "accept_edits".to_string(),
533            plan: Some("# Plan\n1. Step one".to_string()),
534        };
535
536        let value = serde_json::to_value(event).expect("event should serialize");
537        assert_eq!(value["type"], "plan_mode_exited");
538        assert_eq!(value["session_id"], "sess-1");
539        assert_eq!(value["approved"], true);
540        assert_eq!(value["restored_mode"], "accept_edits");
541        assert_eq!(value["plan"], "# Plan\n1. Step one");
542    }
543
544    #[test]
545    fn plan_file_updated_serializes_correctly() {
546        let event = AgentEvent::PlanFileUpdated {
547            session_id: "sess-1".to_string(),
548            file_path: "/tmp/plans/sess-1.md".to_string(),
549            content_summary: "Implementation plan for feature X".to_string(),
550        };
551
552        let value = serde_json::to_value(event).expect("event should serialize");
553        assert_eq!(value["type"], "plan_file_updated");
554        assert_eq!(value["session_id"], "sess-1");
555        assert_eq!(value["file_path"], "/tmp/plans/sess-1.md");
556        assert_eq!(
557            value["content_summary"],
558            "Implementation plan for feature X"
559        );
560    }
561
562    #[test]
563    fn plan_mode_events_deserialize_without_optional_fields() {
564        let json = serde_json::json!({
565            "type": "plan_mode_entered",
566            "session_id": "sess-1",
567            "pre_permission_mode": "default"
568        });
569
570        let event: AgentEvent = serde_json::from_value(json).expect("should deserialize");
571        match event {
572            AgentEvent::PlanModeEntered {
573                session_id,
574                reason,
575                pre_permission_mode,
576            } => {
577                assert_eq!(session_id, "sess-1");
578                assert_eq!(reason, None);
579                assert_eq!(pre_permission_mode, "default");
580            }
581            other => panic!("unexpected event: {other:?}"),
582        }
583    }
584}