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