Skip to main content

agent_base/types/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum AgentError {
5    #[error("LLM call failed: {0}")]
6    Llm(String),
7
8    #[error("LLM API error: {message}")]
9    LlmApi { message: String },
10
11    #[error("SSE stream error: {0}")]
12    LlmStream(String),
13
14    #[error("JSON parse error: {0}")]
15    Json(String),
16
17    #[error("Tool '{name}' not registered")]
18    ToolNotFound { name: String },
19
20    #[error("Tool '{name}' argument parsing failed: {raw}")]
21    ToolArgsInvalid { name: String, raw: String },
22
23    #[error("Tool '{name}' execution failed: {source}")]
24    ToolExecution {
25        name: String,
26        #[source]
27        source: Box<AgentError>,
28    },
29
30    #[error("Tool call rejected by approval: {tool_name}")]
31    ApprovalDenied { tool_name: String },
32
33    #[error("Session {0} not found")]
34    SessionNotFound(u64),
35
36    #[error("Max turns ({limit}) reached, stopping forcibly")]
37    MaxTurnsExceeded { limit: u32 },
38
39    #[error("Operation cancelled")]
40    Cancelled,
41
42    #[error("Internal error: {0}")]
43    Internal(String),
44}
45
46impl AgentError {
47    pub fn llm(message: impl Into<String>) -> Self {
48        Self::Llm(message.into())
49    }
50
51    pub fn json(message: impl Into<String>) -> Self {
52        Self::Json(message.into())
53    }
54
55    pub fn internal(message: impl Into<String>) -> Self {
56        Self::Internal(message.into())
57    }
58
59    pub fn tool_not_found(name: impl Into<String>) -> Self {
60        Self::ToolNotFound { name: name.into() }
61    }
62
63    pub fn session_not_found(id: u64) -> Self {
64        Self::SessionNotFound(id)
65    }
66
67    pub fn is_cancelled(&self) -> bool {
68        matches!(self, Self::Cancelled)
69    }
70
71    pub fn is_retryable(&self) -> bool {
72        matches!(self, Self::Llm(_) | Self::LlmApi { .. } | Self::LlmStream(_))
73    }
74}
75
76pub type AgentResult<T> = Result<T, AgentError>;