use thiserror::Error;

/// Errors that can occur during agent loop execution.
#[derive(Debug, Error)]
pub enum AgentError {
    /// The LLM backend returned an error.
    #[error("llm error: {0}")]
    Llm(String),

    /// A tool invocation failed.
    #[error("tool error: {0}")]
    Tool(String),

    /// Serialization / deserialization failure.
    #[error("serde error: {0}")]
    Serde(#[from] serde_json::Error),

    /// I/O failure.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),

    /// The loop exceeded the configured maximum iterations.
    #[error("max iterations ({0}) exceeded")]
    MaxIterations(usize),

    /// The operation was cancelled via a [`CancellationToken`](tokio_util::sync::CancellationToken).
    #[cfg(feature = "cancellation")]
    #[error("cancelled")]
    Cancelled,

    /// An MCP server operation failed.
    #[cfg(feature = "mcp-client")]
    #[error("mcp error: {0}")]
    Mcp(String),
}

/// Convenience alias used throughout the crate.
pub type Result<T> = std::result::Result<T, AgentError>;
