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    /// Agent finished with final response.
34    Done(AgentResponse),
35}
36
37/// Data record of one LLM round (one model call + tool dispatch).
38#[derive(Debug, Clone)]
39pub struct AgentStep {
40    /// The model's response for this step.
41    pub response: Response,
42    /// Tool calls made in this step (if any).
43    pub tool_calls: Vec<ToolCall>,
44    /// Results from tool executions as messages.
45    pub tool_results: Vec<Message>,
46}
47
48/// Final response from a complete agent run.
49#[derive(Debug, Clone)]
50pub struct AgentResponse {
51    /// All steps taken during execution.
52    pub steps: Vec<AgentStep>,
53    /// Final text response (if any).
54    pub final_response: Option<String>,
55    /// Total number of iterations performed.
56    pub iterations: usize,
57    /// Why the agent stopped.
58    pub stop_reason: AgentStopReason,
59}
60
61/// Why the agent stopped executing.
62#[derive(Debug, Clone, PartialEq)]
63pub enum AgentStopReason {
64    /// Model produced a text response with no tool calls.
65    TextResponse,
66    /// Maximum iterations reached.
67    MaxIterations,
68    /// No tool calls and no text response.
69    NoAction,
70    /// Error during execution.
71    Error(String),
72}