use core::fmt;
use std::borrow::Cow;
use aither_core::llm::{Classify, Failure};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum AgentError {
Llm {
message: String,
failure: Failure,
},
ToolExecution {
name: String,
error: String,
},
MaxIterations {
limit: usize,
},
HookRejected {
hook: Cow<'static, str>,
reason: String,
},
ToolNotFound {
name: String,
},
Config(String),
Approval(std::sync::Arc<aither_core::llm::tool::ApprovalError>),
Cancelled,
Paused(crate::RunPause),
}
impl fmt::Display for AgentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Llm { message, .. } => write!(f, "LLM error: {message}"),
Self::ToolExecution { name, error } => {
write!(f, "tool '{name}' failed: {error}")
}
Self::MaxIterations { limit } => {
write!(f, "exceeded maximum iterations ({limit})")
}
Self::HookRejected { hook, reason } => {
write!(f, "hook '{hook}' rejected: {reason}")
}
Self::ToolNotFound { name } => {
write!(f, "tool '{name}' not found")
}
Self::Config(msg) => write!(f, "configuration error: {msg}"),
Self::Approval(error) => write!(f, "{error}"),
Self::Cancelled => f.write_str("the run was cancelled"),
Self::Paused(pause) => write!(f, "the run paused: {pause:?}"),
}
}
}
impl std::error::Error for AgentError {}
impl AgentError {
pub fn from_model_error<E: std::error::Error + Classify>(error: &E) -> Self {
Self::Llm {
message: error.to_string(),
failure: error.failure(),
}
}
pub fn llm_local(message: impl Into<String>) -> Self {
Self::Llm {
message: message.into(),
failure: Failure::permanent(),
}
}
#[must_use]
pub const fn failure(&self) -> Option<&Failure> {
match self {
Self::Llm { failure, .. } => Some(failure),
_ => None,
}
}
}
impl From<anyhow::Error> for AgentError {
fn from(error: anyhow::Error) -> Self {
Self::llm_local(error.to_string())
}
}
impl From<String> for AgentError {
fn from(error: String) -> Self {
Self::llm_local(error)
}
}
impl From<crate::context::CompactionError> for AgentError {
fn from(error: crate::context::CompactionError) -> Self {
use crate::context::CompactionError;
let failure = match &error {
CompactionError::Provider { failure, .. } => *failure,
CompactionError::Invalid { .. } | CompactionError::Empty => Failure::permanent(),
};
Self::Llm {
message: error.to_string(),
failure,
}
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use aither_core::llm::{Classify, Failure, FailureKind};
use super::AgentError;
#[derive(Debug)]
struct Overloaded;
impl std::fmt::Display for Overloaded {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("overloaded")
}
}
impl std::error::Error for Overloaded {}
impl Classify for Overloaded {
fn failure(&self) -> Failure {
Failure::transient().with_retry_after(Some(Duration::from_secs(5)))
}
}
#[test]
fn provider_failure_is_carried() {
let error = AgentError::from_model_error(&Overloaded);
assert_eq!(error.to_string(), "LLM error: overloaded");
let failure = error.failure().expect("an LLM error carries a failure");
assert_eq!(failure.kind, FailureKind::Transient);
assert_eq!(failure.retry_after, Some(Duration::from_secs(5)));
}
#[test]
fn non_llm_errors_have_no_failure() {
assert!(AgentError::Config("x".into()).failure().is_none());
assert_eq!(
AgentError::llm_local("x").failure().map(|f| f.kind),
Some(FailureKind::Permanent)
);
}
}
#[cfg(feature = "serde")]
#[derive(serde::Serialize, serde::Deserialize)]
enum AgentErrorRepr {
Llm {
message: String,
failure: Failure,
},
ToolExecution {
name: String,
error: String,
},
MaxIterations {
limit: usize,
},
HookRejected {
hook: String,
reason: String,
},
ToolNotFound {
name: String,
},
Config(String),
ApprovalNoApprover {
kind: aither_core::llm::tool::ApprovalKind,
},
ApprovalFailed {
kind: aither_core::llm::tool::ApprovalKind,
message: String,
},
ApprovalCancelled,
Cancelled,
Paused(crate::RunPause),
}
#[cfg(feature = "serde")]
impl From<&AgentError> for AgentErrorRepr {
fn from(error: &AgentError) -> Self {
use aither_core::llm::tool::ApprovalError;
match error {
AgentError::Llm { message, failure } => Self::Llm {
message: message.clone(),
failure: *failure,
},
AgentError::ToolExecution { name, error } => Self::ToolExecution {
name: name.clone(),
error: error.clone(),
},
AgentError::MaxIterations { limit } => Self::MaxIterations { limit: *limit },
AgentError::HookRejected { hook, reason } => Self::HookRejected {
hook: hook.to_string(),
reason: reason.clone(),
},
AgentError::ToolNotFound { name } => Self::ToolNotFound { name: name.clone() },
AgentError::Config(message) => Self::Config(message.clone()),
AgentError::Approval(approval) => match &**approval {
ApprovalError::NoApprover { kind } => {
Self::ApprovalNoApprover { kind: kind.clone() }
}
ApprovalError::Failed { kind, failure } => Self::ApprovalFailed {
kind: kind.clone(),
message: std::error::Error::source(failure)
.map_or_else(|| failure.to_string(), ToString::to_string),
},
ApprovalError::Cancelled => Self::ApprovalCancelled,
},
AgentError::Cancelled => Self::Cancelled,
AgentError::Paused(pause) => Self::Paused(*pause),
}
}
}
#[cfg(feature = "serde")]
impl From<AgentErrorRepr> for AgentError {
fn from(repr: AgentErrorRepr) -> Self {
use aither_core::llm::tool::{ApprovalError, ApproverFailure};
let approval = |error| Self::Approval(std::sync::Arc::new(error));
match repr {
AgentErrorRepr::Llm { message, failure } => Self::Llm { message, failure },
AgentErrorRepr::ToolExecution { name, error } => Self::ToolExecution { name, error },
AgentErrorRepr::MaxIterations { limit } => Self::MaxIterations { limit },
AgentErrorRepr::HookRejected { hook, reason } => Self::HookRejected {
hook: hook.into(),
reason,
},
AgentErrorRepr::ToolNotFound { name } => Self::ToolNotFound { name },
AgentErrorRepr::Config(message) => Self::Config(message),
AgentErrorRepr::ApprovalNoApprover { kind } => {
approval(ApprovalError::NoApprover { kind })
}
AgentErrorRepr::ApprovalFailed { kind, message } => approval(ApprovalError::Failed {
kind,
failure: ApproverFailure::new(std::io::Error::other(message)),
}),
AgentErrorRepr::ApprovalCancelled => approval(ApprovalError::Cancelled),
AgentErrorRepr::Cancelled => Self::Cancelled,
AgentErrorRepr::Paused(pause) => Self::Paused(pause),
}
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for AgentError {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
AgentErrorRepr::from(self).serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for AgentError {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
AgentErrorRepr::deserialize(deserializer).map(Self::from)
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("tool '{name}' panicked: {message}")]
pub struct ToolPanic {
pub name: String,
pub message: String,
}
impl ToolPanic {
pub(crate) fn caught(name: &str, payload: &(dyn std::any::Any + Send)) -> Self {
let message = payload
.downcast_ref::<&str>()
.map(|message| (*message).to_owned())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "the panic payload is not a string".to_owned());
Self {
name: name.to_owned(),
message,
}
}
}