troupekit-providers 0.4.0

Model providers for the Troupekit LLM agent framework: wire protocols, catalog, transports and middleware.
Documentation
use serde_json::Value;
use troupekit_core::error::{ProviderError, ProviderErrorKind};

const MAX_DETAIL_CHARS: usize = 300;
const REDACTED: &str = "[redacted]";

/// Builds a `BadRequest` error with `message`: the request cannot be sent
/// as given. Used by request validation and every protocol encoder.
/// Returns the error.
pub fn bad_request(message: impl Into<String>) -> ProviderError {
    ProviderError::new(ProviderErrorKind::BadRequest, message)
}

/// Builds a `Decode` error with `message`: a reply did not have the shape
/// its protocol promises. Used by every decoder and the SSE parser.
/// Returns the error.
pub fn decode_error(message: impl Into<String>) -> ProviderError {
    ProviderError::new(ProviderErrorKind::Decode, message)
}

/// Maps an HTTP `status` to the kind of failure it means.
/// 401 and 403 are auth, 408 a stall, 429 a rate limit, other 4xx a bad
/// request, and every other status a server error.
pub fn kind_for_status(status: u16) -> ProviderErrorKind {
    match status {
        401 | 403 => ProviderErrorKind::Auth,
        408 => ProviderErrorKind::Stalled,
        429 => ProviderErrorKind::RateLimited,
        400..=499 => ProviderErrorKind::BadRequest,
        _ => ProviderErrorKind::Server,
    }
}

/// Reads the retry delay a provider sent, in milliseconds.
/// `retry-after-ms` wins over `retry-after`, which counts seconds and may
/// be fractional. Names match case-insensitively; dates give `None`.
pub fn retry_after_ms(headers: &[(String, String)]) -> Option<u64> {
    let find = |name: &str| {
        headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(name))
            .map(|(_, v)| v.trim())
    };
    if let Some(ms) = find("retry-after-ms").and_then(|v| v.parse::<u64>().ok()) {
        return Some(ms);
    }
    let secs: f64 = find("retry-after")?.parse().ok()?;
    if !secs.is_finite() || secs < 0.0 {
        return None;
    }
    Some((secs * 1000.0).ceil().min(u64::MAX as f64) as u64)
}

/// Pulls a short readable message out of an error reply `body`.
/// Prefers JSON `error.message`, then `message`, then the raw text, cut
/// to 300 characters. Returns `"empty body"` when there is nothing.
pub fn error_detail(body: &[u8]) -> String {
    redacted_detail(body, None)
}

/// Replaces every `secret` in `text` with `[redacted]`. When a marker
/// edge and the text beside it form `secret` again, as `]a` does in
/// `]aa`, the whole text becomes `[redacted]`. Input: text and a non-empty
/// secret. Output: text without `secret`, unless the marker contains it.
pub(crate) fn redact_secret(text: &str, secret: &str) -> String {
    assert!(!secret.is_empty());
    let once = text.replace(secret, REDACTED);
    let out = if once.contains(secret) && !REDACTED.contains(secret) {
        REDACTED.to_owned()
    } else {
        once
    };
    assert!(!out.contains(secret) || REDACTED.contains(secret));
    out
}

/// `error_detail` with every whole `secret` replaced by `[redacted]`
/// before the cut, so a secret crossing the 300-character mark never
/// leaves a prefix behind. Input: body and optional non-empty secret.
/// Output: the detail.
fn redacted_detail(body: &[u8], secret: Option<&str>) -> String {
    assert!(secret.is_none_or(|s| !s.is_empty()));
    let clean = |text: &str| match secret {
        Some(s) => cut(&redact_secret(text, s)),
        None => cut(text),
    };
    if let Ok(v) = serde_json::from_slice::<Value>(body) {
        if let Some(m) = v
            .pointer("/error/message")
            .or_else(|| v.get("message"))
            .and_then(Value::as_str)
        {
            return clean(m);
        }
    }
    let text = String::from_utf8_lossy(body);
    let text = text.trim();
    if text.is_empty() {
        "empty body".into()
    } else {
        clean(text)
    }
}

/// Builds the error for a non-success HTTP reply from its `status`,
/// `headers` and `body`: kind from the status, message from the body,
/// retry delay from the headers.
pub fn status_error(status: u16, headers: &[(String, String)], body: &[u8]) -> ProviderError {
    redacted_status_error(status, headers, body, None)
}

/// `status_error` with every whole `secret` in the body replaced by
/// `[redacted]` before the detail is cut. Input: status, headers, body
/// and an optional non-empty secret. Output: the error.
pub(crate) fn redacted_status_error(
    status: u16,
    headers: &[(String, String)],
    body: &[u8],
    secret: Option<&str>,
) -> ProviderError {
    let err = ProviderError::new(
        kind_for_status(status),
        format!("HTTP {status}: {}", redacted_detail(body, secret)),
    );
    match retry_after_ms(headers) {
        Some(ms) => err.with_retry_after(ms),
        None => err,
    }
}

fn cut(text: &str) -> String {
    text.chars().take(MAX_DETAIL_CHARS).collect()
}