sermo 0.2.4

A Rust client library for interacting with various LLM provider APIs
Documentation
use atlas_http::{HttpBody, HttpClient, HttpRequest, HttpResponse, HttpSyncClient};

const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0";

/// Browser-like request headers sent by `get`. Many sites (including search
/// engines) return an HTTP 202 bot-detection challenge when the request lacks a
/// normal browser `Accept` header, so we always send one.
const BROWSER_HEADERS: &[&str] = &[
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language: en-US,en;q=0.5",
    "Accept-Encoding: identity",
];

/// Build a synchronous HTTP client shared by the web tools.
///
/// Redirects are intentionally left disabled (atlas_http's default is to not
/// follow them; only `follow_location()` / `browser()` opt in). Callers that
/// need redirects (web_fetch) follow and re-validate each hop manually.
pub(crate) fn build_client() -> HttpSyncClient {
    HttpClient::builder().user_agent(USER_AGENT).timeout(30).read_timeout(20).build_sync()
}

/// Send a GET request with browser-like headers, without following redirects.
///
/// This is the single choke point for web_fetch; it keeps redirect handling
/// manual so each hop can be re-validated for SSRF safety.
pub(crate) fn get(client: &mut HttpSyncClient, url: &str) -> Result<HttpResponse, atlas_http::error::Error> {
    let headers: Vec<&str> = BROWSER_HEADERS.to_vec();
    let req = HttpRequest::new("GET", url, &headers, &HttpBody::empty());
    client.send(&req)
}

/// Build a synchronous HTTP client with browser-like headers and automatic
/// redirect following. Used by web_search, whose single known endpoint can be
/// followed safely without per-hop SSRF re-validation.
pub(crate) fn build_browser_client() -> HttpSyncClient {
    HttpClient::builder().user_agent(USER_AGENT).timeout(30).read_timeout(20).browser().build_sync()
}