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("Plan error: {0}")]
43    Plan(String),
44
45    #[error("Plan step '{step_id}' failed: {message}")]
46    PlanStepFailed { step_id: String, message: String },
47
48    #[error("Plan generation failed: {0}")]
49    PlanGeneration(String),
50
51    #[error("Plan storage error: {0}")]
52    PlanStorage(String),
53
54    #[error("Internal error: {0}")]
55    Internal(String),
56}
57
58impl AgentError {
59    pub fn llm(message: impl Into<String>) -> Self {
60        Self::Llm(message.into())
61    }
62
63    pub fn json(message: impl Into<String>) -> Self {
64        Self::Json(message.into())
65    }
66
67    pub fn internal(message: impl Into<String>) -> Self {
68        Self::Internal(message.into())
69    }
70
71    pub fn tool_not_found(name: impl Into<String>) -> Self {
72        Self::ToolNotFound { name: name.into() }
73    }
74
75    pub fn session_not_found(id: u64) -> Self {
76        Self::SessionNotFound(id)
77    }
78
79    pub fn plan(message: impl Into<String>) -> Self {
80        Self::Plan(message.into())
81    }
82
83    pub fn plan_step_failed(step_id: impl Into<String>, message: impl Into<String>) -> Self {
84        Self::PlanStepFailed {
85            step_id: step_id.into(),
86            message: message.into(),
87        }
88    }
89
90    pub fn plan_generation(message: impl Into<String>) -> Self {
91        Self::PlanGeneration(message.into())
92    }
93
94    pub fn plan_storage(message: impl Into<String>) -> Self {
95        Self::PlanStorage(message.into())
96    }
97
98    pub fn is_cancelled(&self) -> bool {
99        matches!(self, Self::Cancelled)
100    }
101
102    pub fn is_retryable(&self) -> bool {
103        matches!(self, Self::Llm(_) | Self::LlmApi { .. } | Self::LlmStream(_))
104    }
105}
106
107pub type AgentResult<T> = Result<T, AgentError>;