Skip to main content

zeph_core/agent/
error.rs

1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4#[derive(Debug, thiserror::Error)]
5pub enum AgentError {
6    #[error(transparent)]
7    Llm(#[from] zeph_llm::LlmError),
8
9    #[error(transparent)]
10    Channel(#[from] crate::channel::ChannelError),
11
12    #[error(transparent)]
13    Memory(#[from] zeph_memory::MemoryError),
14
15    #[error(transparent)]
16    Skill(#[from] zeph_skills::SkillError),
17
18    #[error(transparent)]
19    Tool(#[from] zeph_tools::executor::ToolError),
20
21    #[error("I/O error: {0}")]
22    Io(#[from] std::io::Error),
23
24    #[error("{0}")]
25    Other(String),
26}
27
28impl AgentError {
29    /// Returns true if this error originates from a context length exceeded condition.
30    #[must_use]
31    pub fn is_context_length_error(&self) -> bool {
32        if let Self::Llm(e) = self {
33            return e.is_context_length_error();
34        }
35        false
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn agent_error_detects_context_length_from_llm() {
45        let e = AgentError::Llm(zeph_llm::LlmError::ContextLengthExceeded);
46        assert!(e.is_context_length_error());
47    }
48
49    #[test]
50    fn agent_error_detects_context_length_from_other_message() {
51        let e = AgentError::Llm(zeph_llm::LlmError::Other("context length exceeded".into()));
52        assert!(e.is_context_length_error());
53    }
54
55    #[test]
56    fn agent_error_non_llm_variant_not_detected() {
57        let e = AgentError::Other("something went wrong".into());
58        assert!(!e.is_context_length_error());
59    }
60}