artifactize-provider 0.27.1

Managed credentials, model discovery and direct HTTP turns for Artifactize.
Documentation
//! Classified, sanitized provider failures.

use std::fmt;

/// Why a provider request or its tool turn failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Code {
    /// Credentials are missing, expired or rejected (HTTP 401 or 403).
    Authentication,
    /// A billing, credit or plan usage limit.
    Quota,
    /// Rate limited (HTTP 429) after the retries, or asked to wait past the deadline.
    RateLimit,
    /// A connection failure, an overloaded or failing server, or an interrupted stream,
    /// after the retries.
    Transient,
    /// The caller's deadline passed.
    Timeout,
    Cancelled,
    /// A caller-supplied token or tool-call quota was exceeded.
    BudgetExceeded,
    /// The response's text degenerated into a long run of whitespace, after the retries; its
    /// stream was stopped.
    DegenerateOutput,
    /// Any other provider failure: a rejected request, an unknown or different model,
    /// an incomplete response or a malformed tool call.
    ProviderError,
    /// Anything else, such as unusable configuration or tools.
    Configuration,
}

impl Code {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Authentication => "AUTHENTICATION",
            Self::Quota => "QUOTA",
            Self::RateLimit => "RATE_LIMIT",
            Self::Transient => "TRANSIENT",
            Self::Timeout => "TIMEOUT",
            Self::Cancelled => "CANCELLED",
            Self::BudgetExceeded => "PROVIDER_BUDGET_EXCEEDED",
            Self::DegenerateOutput => "DEGENERATE_OUTPUT",
            Self::ProviderError => "PROVIDER_ERROR",
            Self::Configuration => "AGENT_ERROR",
        }
    }

    /// Failures that a turn without output may retry.
    pub fn retryable(self) -> bool {
        matches!(self, Self::RateLimit | Self::Transient)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Failure {
    pub code: Code,
    pub message: String,
    /// The provider's output failed once it had begun: it degenerated, stopped at the output
    /// cap or broke off. The request was sound, so its turn may be sent again.
    pub output_failed: bool,
}

impl Failure {
    pub fn new(code: Code, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
            output_failed: false,
        }
    }

    /// The same failure, marked as one of the provider's output.
    pub fn of_output(self) -> Self {
        Self {
            output_failed: true,
            ..self
        }
    }

    pub fn cancelled() -> Self {
        Self::new(Code::Cancelled, "Provider request was cancelled.")
    }

    pub fn timeout() -> Self {
        Self::new(Code::Timeout, "Provider request timed out.")
    }
}

/// An unclassified failure, such as a tool or configuration error.
impl From<String> for Failure {
    fn from(message: String) -> Self {
        Self::new(Code::Configuration, message)
    }
}

impl fmt::Display for Failure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}