Skip to main content

walrus_core/agent/
event.rs

1//! Agent event types for step-based execution and streaming.
2//!
3//! Two-level design:
4//! - [`AgentStep`]: data record of one LLM round (response + tool dispatch).
5//! - [`AgentEvent`]: fine-grained streaming enum for real-time UI updates.
6//! - [`AgentResponse`]: final result after a full agent run.
7//! - [`AgentStopReason`]: why the agent stopped.
8
9use crate::model::{Message, Response, ToolCall};
10
11/// A fine-grained event emitted during agent execution.
12///
13/// Yielded by `Agent::run_stream()` or emitted via `Hook::on_event()`
14/// for real-time status reporting to clients.
15#[derive(Debug, Clone)]
16pub enum AgentEvent {
17    /// Text content delta from the model.
18    TextDelta(String),
19    /// Thinking/reasoning content delta from the model.
20    ThinkingDelta(String),
21    /// Model is calling tools (with the tool calls).
22    ToolCallsStart(Vec<ToolCall>),
23    /// A single tool completed execution.
24    ToolResult {
25        /// The tool call ID this result belongs to.
26        call_id: String,
27        /// The output from the tool.
28        output: String,
29    },
30    /// All tools completed, continuing to next iteration.
31    ToolCallsComplete,
32    /// Context was compacted — carries the compaction summary.
33    Compact { summary: String },
34    /// Agent finished with final response.
35    Done(AgentResponse),
36}
37
38/// Data record of one LLM round (one model call + tool dispatch).
39#[derive(Debug, Clone)]
40pub struct AgentStep {
41    /// The model's response for this step.
42    pub response: Response,
43    /// Tool calls made in this step (if any).
44    pub tool_calls: Vec<ToolCall>,
45    /// Results from tool executions as messages.
46    pub tool_results: Vec<Message>,
47}
48
49/// Final response from a complete agent run.
50#[derive(Debug, Clone)]
51pub struct AgentResponse {
52    /// All steps taken during execution.
53    pub steps: Vec<AgentStep>,
54    /// Final text response (if any).
55    pub final_response: Option<String>,
56    /// Total number of iterations performed.
57    pub iterations: usize,
58    /// Why the agent stopped.
59    pub stop_reason: AgentStopReason,
60}
61
62/// Why the agent stopped executing.
63#[derive(Debug, Clone, PartialEq)]
64pub enum AgentStopReason {
65    /// Model produced a text response with no tool calls.
66    TextResponse,
67    /// Maximum iterations reached.
68    MaxIterations,
69    /// No tool calls and no text response.
70    NoAction,
71    /// Error during execution.
72    Error(String),
73}