1use std::error::Error as StdError;
4
5pub type BoxError = Box<dyn StdError + Send + Sync>;
7
8pub type Result<T> = std::result::Result<T, AgentError>;
10
11#[derive(Debug, thiserror::Error)]
13pub enum ProviderError {
14 #[error("Missing API key - set {env_var}")]
16 MissingApiKey {
17 env_var: &'static str,
19 },
20 #[error("HTTP request failed: {0}")]
22 Http(String),
23 #[error("API error {status}: {message}")]
25 Api {
26 status: u16,
28 message: String,
30 },
31 #[error("Response parsing failed: {0}")]
33 Parse(#[from] serde_json::Error),
34 #[error("Rate limited - retry after {retry_after_secs}s")]
36 RateLimited {
37 retry_after_secs: u64,
39 },
40 #[error("Context window exceeded")]
42 ContextWindowExceeded,
43 #[error("Invalid provider response: {0}")]
45 InvalidResponse(String),
46 #[error("Unsupported provider feature: {0}")]
48 Unsupported(&'static str),
49}
50
51#[derive(Debug, thiserror::Error)]
53pub enum ToolError {
54 #[error("Invalid tool input: {0}")]
56 InvalidInput(String),
57 #[error("Tool execution failed: {0}")]
59 Execution(String),
60 #[error("Tool execution timed out")]
62 Timeout,
63 #[error("Permission denied: {0}")]
65 PermissionDenied(String),
66}
67
68#[derive(Debug, thiserror::Error)]
70pub enum MemoryError {
71 #[error("Memory backend failed: {0}")]
73 Backend(String),
74 #[error("Memory serialization failed: {0}")]
76 Serialization(#[from] serde_json::Error),
77}
78
79#[derive(Debug, thiserror::Error)]
81pub enum McpError {
82 #[error("Invalid MCP command")]
84 InvalidCommand,
85 #[error("Failed to spawn MCP server: {0}")]
87 SpawnFailed(String),
88 #[error("MCP protocol error: {0}")]
90 Protocol(String),
91 #[error("MCP response error: {0}")]
93 Response(String),
94 #[error("MCP request timed out")]
96 Timeout,
97}
98
99#[derive(Debug, thiserror::Error)]
101pub enum AgentError {
102 #[error("LLM provider error: {0}")]
104 Provider(#[from] ProviderError),
105 #[error("Tool error: {0}")]
107 ToolError(#[from] ToolError),
108 #[error("Tool execution failed '{name}': {source}")]
110 Tool {
111 name: String,
113 #[source]
114 source: BoxError,
116 },
117 #[error("MCP error: {0}")]
119 Mcp(#[from] McpError),
120 #[error("Memory error: {0}")]
122 Memory(#[from] MemoryError),
123 #[error("Max steps reached: {steps}")]
125 MaxStepsReached {
126 steps: usize,
128 },
129 #[error("Serialization error: {0}")]
131 Serde(#[from] serde_json::Error),
132 #[error("I/O error: {0}")]
134 Io(#[from] std::io::Error),
135 #[error("Missing required field: {0}")]
137 MissingField(&'static str),
138 #[error("No LLM provider configured")]
140 NoLlmProvider,
141 #[error("Tool not found: {0}")]
143 ToolNotFound(String),
144 #[error("Agent not found: {0}")]
146 AgentNotFound(String),
147 #[error("Invalid configuration: {0}")]
149 InvalidConfiguration(String),
150 #[error("Invalid stream payload")]
152 InvalidStream,
153 #[error("Context window exceeded")]
155 ContextWindowExceeded,
156 #[error("{0}")]
158 Message(String),
159}
160
161impl AgentError {
162 pub fn tool_failure(name: impl Into<String>, source: impl Into<BoxError>) -> Self {
164 Self::Tool {
165 name: name.into(),
166 source: source.into(),
167 }
168 }
169}