rpi_agent/error.rs
1//! Mirrors `packages/agent/src` error surface. Pi's agent layer reports failures
2//! via thrown errors that the loop catches and encodes into tool results / event
3//! sequences; Rust encodes the same cases as a per-crate `thiserror` enum.
4
5/// Failures raised inside the agent loop. Cannot cross the provider boundary
6/// (provider failures arrive as `AssistantMessageEvent::Error`); these surface
7/// from tool execution, argument validation, queue/state misuse, or abort.
8#[derive(Debug, Clone, thiserror::Error)]
9pub enum AgentError {
10 #[error("tool error: {0}")]
11 Tool(String),
12
13 /// A failed tool that still owns native content and accounting metadata.
14 #[error("structured tool error")]
15 StructuredTool(Box<crate::types::AgentToolResult>),
16
17 #[error("validation error: {0}")]
18 Validation(String),
19
20 #[error("operation aborted")]
21 Abort,
22
23 #[error("provider error: {0}")]
24 Provider(String),
25
26 #[error("queue error: {0}")]
27 Queue(String),
28
29 #[error("invalid state: {0}")]
30 State(String),
31}
32
33impl AgentError {
34 pub fn tool(message: impl Into<String>) -> Self {
35 AgentError::Tool(message.into())
36 }
37
38 /// True when the error originated from an abort. Used by the loop to decide
39 /// the terminal event shape (`Aborted` vs `Error`).
40 pub fn is_abort(&self) -> bool {
41 matches!(self, AgentError::Abort)
42 }
43}