Skip to main content

aither_agent/
error.rs

1//! Agent error types.
2
3use core::fmt;
4
5/// Errors that can occur during agent execution.
6#[derive(Debug, Clone)]
7pub enum AgentError {
8    /// LLM returned an error.
9    Llm(String),
10
11    /// Tool execution failed.
12    ToolExecution {
13        /// Name of the tool that failed.
14        name: String,
15        /// The underlying error message.
16        error: String,
17    },
18
19    /// Maximum iterations exceeded without completing the task.
20    MaxIterations {
21        /// The iteration limit that was exceeded.
22        limit: usize,
23    },
24
25    /// A hook rejected the operation.
26    HookRejected {
27        /// Name of the hook that rejected.
28        hook: &'static str,
29        /// Reason for rejection.
30        reason: String,
31    },
32
33    /// Tool not found.
34    ToolNotFound {
35        /// Name of the missing tool.
36        name: String,
37    },
38
39    /// Configuration error.
40    Config(String),
41}
42
43impl fmt::Display for AgentError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::Llm(e) => write!(f, "LLM error: {e}"),
47            Self::ToolExecution { name, error } => {
48                write!(f, "tool '{name}' failed: {error}")
49            }
50            Self::MaxIterations { limit } => {
51                write!(f, "exceeded maximum iterations ({limit})")
52            }
53            Self::HookRejected { hook, reason } => {
54                write!(f, "hook '{hook}' rejected: {reason}")
55            }
56            Self::ToolNotFound { name } => {
57                write!(f, "tool '{name}' not found")
58            }
59            Self::Config(msg) => write!(f, "configuration error: {msg}"),
60        }
61    }
62}
63
64impl std::error::Error for AgentError {}
65
66impl AgentError {
67    /// Returns `true` if this is a provider-level failure worth retrying
68    /// (e.g. with a different provider in a multi-provider setup).
69    ///
70    /// Only `Llm` errors are considered retryable — they indicate the
71    /// LLM provider itself failed (rate limit, timeout, auth, server error, etc.).
72    /// Other variants (`MaxIterations`, `HookRejected`, `ToolNotFound`, etc.)
73    /// are not provider-related and retrying would not help.
74    #[must_use]
75    pub const fn is_retryable(&self) -> bool {
76        matches!(self, Self::Llm(_))
77    }
78}
79
80impl From<anyhow::Error> for AgentError {
81    fn from(error: anyhow::Error) -> Self {
82        Self::Llm(error.to_string())
83    }
84}
85
86impl From<String> for AgentError {
87    fn from(error: String) -> Self {
88        Self::Llm(error)
89    }
90}