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