use crate::provider::{ProviderError, REQUEST_ABORTED};
use jsrt::json_parse::parse_json;
use serde_json::Value;
use std::collections::BTreeMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use tokio::sync::Notify;
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub type HeaderMap = BTreeMap<String, String>;
#[derive(Clone, Debug, PartialEq)]
pub struct HttpRequest {
pub method: String,
pub url: String,
pub headers: HeaderMap,
pub body: String,
}
pub fn wire_body(request: &HttpRequest) -> Vec<u8> {
let zstd = request.headers.iter().any(|(name, value)| {
name.eq_ignore_ascii_case("content-encoding") && value.trim().eq_ignore_ascii_case("zstd")
});
if !zstd {
return request.body.clone().into_bytes();
}
ruzstd::encoding::compress_to_vec(request.body.as_bytes(), ruzstd::encoding::CompressionLevel::Fastest)
}
pub fn decode_wire_body(content_encoding: Option<&str>, bytes: &[u8]) -> Result<Vec<u8>, String> {
if !content_encoding.is_some_and(|e| e.trim().eq_ignore_ascii_case("zstd")) {
return Ok(bytes.to_vec());
}
let mut decoder = ruzstd::decoding::StreamingDecoder::new(bytes).map_err(|e| e.to_string())?;
let mut out = Vec::new();
std::io::Read::read_to_end(&mut decoder, &mut out).map_err(|e| e.to_string())?;
Ok(out)
}
pub trait Body: Send {
fn next_chunk(&mut self) -> BoxFuture<'_, Result<Option<Vec<u8>>, String>>;
}
pub struct HttpResponse {
pub status: u16,
pub headers: HeaderMap,
pub body: Box<dyn Body>,
}
pub trait Transport: Send + Sync {
fn send(&self, request: HttpRequest) -> BoxFuture<'_, Result<HttpResponse, String>>;
fn with_client_identity(&self, cert: &[u8], key: &[u8]) -> Result<Arc<dyn Transport>, String> {
let _ = (cert, key);
Err("this HTTP transport cannot present a TLS client certificate".into())
}
fn detached(&self) -> Option<Arc<dyn Transport>> {
None
}
fn http1_only(&self) -> Option<Arc<dyn Transport>> {
None
}
fn websocket_available(&self) -> bool {
false
}
fn websocket(
&self,
url: String,
headers: HeaderMap,
) -> BoxFuture<'_, Result<Box<dyn crate::websocket::WebSocketConnection>, String>> {
let _ = (url, headers);
Box::pin(async { Err(crate::websocket::NO_WEBSOCKET.to_string()) })
}
}
#[derive(Clone, Debug, Default)]
pub struct AbortSignal(Arc<SignalState>);
#[derive(Debug, Default)]
struct SignalState {
aborted: AtomicBool,
notify: Notify,
}
impl AbortSignal {
pub fn new() -> Self {
Self::default()
}
pub fn abort(&self) {
self.0.aborted.store(true, Ordering::SeqCst);
self.0.notify.notify_waiters();
}
pub fn aborted(&self) -> bool {
self.0.aborted.load(Ordering::SeqCst)
}
pub async fn cancelled(&self) {
loop {
let notified = self.0.notify.notified();
if self.aborted() {
return;
}
notified.await;
}
}
}
pub(crate) fn is_aborted(signal: Option<&AbortSignal>) -> bool {
signal.is_some_and(AbortSignal::aborted)
}
pub(crate) async fn cancelled(signal: Option<&AbortSignal>) {
match signal {
Some(signal) => signal.cancelled().await,
None => std::future::pending().await,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Sdk {
Anthropic,
OpenAi,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Failure {
pub message: String,
pub status: Option<u16>,
pub headers: Option<Box<HeaderMap>>,
pub error: Option<Box<Value>>,
pub api: bool,
pub raw_metadata: Option<String>,
}
impl ProviderError for Failure {
fn plain(message: String) -> Self {
Failure::plain(message)
}
}
impl Failure {
pub fn plain(message: impl Into<String>) -> Self {
Failure {
message: message.into(),
status: None,
headers: None,
error: None,
api: false,
raw_metadata: None,
}
}
pub fn api(
sdk: Sdk,
status: Option<u16>,
headers: Option<HeaderMap>,
body: Option<Value>,
text: Option<String>,
) -> Self {
let error = match sdk {
Sdk::Anthropic => body.clone(),
Sdk::OpenAi => body.as_ref().and_then(|b| b.get("error").cloned()),
};
let message_field = error
.as_ref()
.and_then(|e| e.get("message"))
.filter(|m| jsrt::value::truthy(Some(m)));
let msg = match (message_field, &error) {
(Some(Value::String(s)), _) => Some(s.clone()),
(Some(other), _) => Some(jsrt::json::stringify(other)),
(None, Some(e)) if jsrt::value::truthy(Some(e)) => Some(jsrt::json::stringify(e)),
_ => text.filter(|t| !t.is_empty()),
};
let message = match (status, msg) {
(Some(status), Some(msg)) if !msg.is_empty() => format!("{status} {msg}"),
(Some(status), _) => format!("{status} status code (no body)"),
(None, Some(msg)) if !msg.is_empty() => msg,
_ => "(no status code or body)".to_string(),
};
let raw_metadata = error
.as_ref()
.and_then(|e| e.pointer("/metadata/raw"))
.filter(|r| jsrt::value::truthy(Some(r)))
.map(|raw| jsrt::value::string(Some(raw)));
Failure {
message,
status,
headers: headers.map(Box::new),
error: error.map(Box::new),
api: true,
raw_metadata,
}
}
fn connection() -> Self {
Failure {
api: true,
..Failure::plain("Connection error.")
}
}
fn timed_out() -> Self {
Failure {
api: true,
..Failure::plain(REQUEST_TIMED_OUT)
}
}
fn header(&self, name: &str) -> Option<&str> {
self.headers.as_ref()?.get(name).map(String::as_str)
}
}
pub(crate) const MAX_PROVIDER_ERROR_BODY_CHARS: usize = 4000;
pub(crate) fn format_provider_error(failure: &Failure, prefix: Option<&str>) -> String {
let body = failure
.error
.as_ref()
.filter(|e| e.as_object().is_some_and(|o| !o.is_empty()))
.map(|e| jsrt::json::stringify(e))
.map(|text| text.trim().to_string())
.filter(|text| !text.is_empty())
.map(|text| truncate_error_text(&text, MAX_PROVIDER_ERROR_BODY_CHARS));
let carries_body = body.as_ref().is_none_or(|b| failure.message.contains(b.as_str()));
match (carries_body, failure.status, body) {
(false, Some(status), Some(body)) => match prefix {
Some(prefix) => format!("{prefix} ({status}): {body}"),
None => format!("{status}: {body}"),
},
(_, status, _) => match (prefix, status) {
(Some(prefix), Some(status)) => format!("{prefix} ({status}): {}", failure.message),
_ => failure.message.clone(),
},
}
}
pub(crate) fn truncate_error_text(text: &str, max: usize) -> String {
let len = jsrt::utf16::len(text);
if len <= max {
return text.to_string();
}
format!("{}... [truncated {} chars]", jsrt::utf16::head(text, max), len - max)
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct RetryOptions {
pub max_retries: Option<u32>,
pub max_retry_delay_ms: Option<f64>,
pub timeout_ms: Option<u64>,
}
pub(crate) const DEFAULT_MAX_RETRY_DELAY_MS: f64 = 60_000.0;
fn is_retryable(failure: &Failure) -> bool {
match failure.header("x-should-retry") {
Some("true") => return true,
Some("false") => return false,
_ => {}
}
match failure.status {
None => true,
Some(status) => matches!(status, 408 | 409 | 429) || status >= 500,
}
}
fn validate_delay(delay_ms: f64, max: Option<f64>, message: &str) -> Result<f64, Failure> {
let max = max.unwrap_or(DEFAULT_MAX_RETRY_DELAY_MS);
if max > 0.0 && delay_ms > max {
return Err(Failure::plain(format!(
"Server requested {}s retry delay (max: {}s). {message}",
(delay_ms / 1000.0).ceil(),
(max / 1000.0).ceil()
)));
}
Ok(delay_ms)
}
pub(crate) fn until_http_date_ms(text: &str) -> Option<f64> {
let date = parse_http_date(text)?;
Some((date - jsrt::date::now_ms()) as f64)
}
pub fn parse_http_date(text: &str) -> Option<i64> {
const MONTHS: [&str; 12] = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let parts: Vec<&str> = text.split_whitespace().collect();
let [_, day, month, year, time, "GMT"] = parts[..] else {
return None;
};
let month = MONTHS.iter().position(|m| m.eq_ignore_ascii_case(month))? as u32 + 1;
let hms: Vec<i64> = time.split(':').map(|p| p.parse().ok()).collect::<Option<_>>()?;
let [h, m, s] = hms[..] else {
return None;
};
let days = jsrt::date::days_from_civil(year.parse().ok()?, month, day.parse().ok()?);
Some((days * 86_400 + h * 3600 + m * 60 + s) * 1000)
}
fn retry_delay_ms(failure: &Failure, retry_index: u32, max: Option<f64>) -> Result<f64, Failure> {
if let Some(value) = failure
.header("retry-after-ms")
.filter(|v| !v.is_empty())
.map(jsrt::number::parse_float)
.filter(|v| !v.is_nan())
{
return validate_delay(value, max, &failure.message);
}
if let Some(retry_after) = failure.header("retry-after").filter(|v| !v.is_empty()) {
let seconds = jsrt::number::parse_float(retry_after);
let delay = if seconds.is_nan() {
until_http_date_ms(retry_after).unwrap_or(0.0)
} else {
seconds * 1000.0
};
return validate_delay(delay, max, &failure.message);
}
let exponential = (0.5 * 2f64.powi(retry_index as i32)).min(8.0) * 1000.0;
Ok(exponential * (1.0 - jitter() * 0.25))
}
fn jitter() -> f64 {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.subsec_nanos());
f64::from(nanos % 1_000_000) / 1_000_000.0
}
pub(crate) async fn send_with_retry(
transport: &dyn Transport,
request: &HttpRequest,
sdk: Sdk,
retry: RetryOptions,
signal: Option<&AbortSignal>,
) -> Result<HttpResponse, Failure> {
let max_retries = retry.max_retries.unwrap_or(0);
crate::provider::retry(
signal,
|| Failure::plain(REQUEST_ABORTED),
|_| send_once(transport, request, sdk, retry.timeout_ms, signal),
|failure, retry_index| {
if is_aborted(signal) {
return Err(Failure::plain(REQUEST_ABORTED));
}
if retry_index >= max_retries || !failure.api || !is_retryable(&failure) {
return Err(failure);
}
retry_delay_ms(&failure, retry_index, retry.max_retry_delay_ms)
},
)
.await
}
async fn send_once(
transport: &dyn Transport,
request: &HttpRequest,
sdk: Sdk,
timeout_ms: Option<u64>,
signal: Option<&AbortSignal>,
) -> Result<HttpResponse, Failure> {
if is_aborted(signal) {
return Err(Failure {
api: true,
..Failure::plain("Request was aborted.")
});
}
let timed_out = async {
match timeout_ms {
Some(ms) => tokio::time::sleep(Duration::from_millis(ms)).await,
None => std::future::pending().await,
}
};
let sent = tokio::select! {
sent = transport.send(request.clone()) => sent,
() = cancelled(signal) => return Err(Failure { api: true, ..Failure::plain("Request was aborted.") }),
() = timed_out => return Err(Failure { api: true, ..Failure::plain("Request timed out.") }),
};
let mut response = sent.map_err(|error| {
if error == REQUEST_TIMED_OUT {
Failure::timed_out()
} else {
Failure::connection()
}
})?;
if (200..300).contains(&response.status) {
return Ok(response);
}
let mut bytes = Vec::new();
while let Ok(Some(chunk)) = response.body.next_chunk().await {
bytes.extend_from_slice(&chunk);
}
let text = String::from_utf8_lossy(&bytes).into_owned();
let body = parse_json(&text).ok().filter(|body| jsrt::value::truthy(Some(body)));
let text = if body.is_some() { None } else { Some(text) };
Err(Failure::api(
sdk,
Some(response.status),
Some(response.headers),
body,
text,
))
}
pub(crate) const REQUEST_TIMED_OUT: &str = "Request timed out.";
pub(crate) struct TimeoutTransport<'a> {
pub inner: &'a dyn Transport,
pub timeout: Duration,
}
impl Transport for TimeoutTransport<'_> {
fn send(&self, request: HttpRequest) -> BoxFuture<'_, Result<HttpResponse, String>> {
Box::pin(async move {
tokio::time::timeout(self.timeout, self.inner.send(request))
.await
.map_err(|_| REQUEST_TIMED_OUT.to_string())?
})
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct HeaderBuilder(Vec<(String, Option<String>)>);
impl HeaderBuilder {
pub fn set(&mut self, name: &str, value: impl Into<String>) -> &mut Self {
self.0.push((name.to_string(), Some(value.into())));
self
}
pub fn remove(&mut self, name: &str) -> &mut Self {
self.0.push((name.to_string(), None));
self
}
pub fn extend<'a, I>(&mut self, headers: I) -> &mut Self
where
I: IntoIterator<Item = (&'a String, &'a Option<String>)>,
{
self.0.extend(headers.into_iter().map(|(k, v)| (k.clone(), v.clone())));
self
}
pub fn extend_set<'a, I>(&mut self, headers: I) -> &mut Self
where
I: IntoIterator<Item = (&'a String, &'a String)>,
{
self.0
.extend(headers.into_iter().map(|(k, v)| (k.clone(), Some(v.clone()))));
self
}
pub fn openai_sdk_identity(&mut self) -> &mut Self {
for (header, var) in [
("OpenAI-Organization", "OPENAI_ORG_ID"),
("OpenAI-Project", "OPENAI_PROJECT_ID"),
] {
if let Some(value) = std::env::var(var)
.ok()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
{
self.set(header, value);
}
}
self
}
pub fn build(&self) -> HeaderMap {
let mut map = HeaderMap::new();
for (name, value) in &self.0 {
let name = name.to_ascii_lowercase();
match value {
Some(value) => map.insert(name, value.clone()),
None => map.remove(&name),
};
}
map
}
}
pub(crate) fn has_header(headers: Option<&indexmap::IndexMap<String, Option<String>>>, name: &str) -> bool {
headers.is_some_and(|headers| {
headers
.iter()
.any(|(k, v)| k.eq_ignore_ascii_case(name) && v.as_ref().is_some_and(|v| !v.trim().is_empty()))
})
}
pub(crate) fn join_url(base: &str, path: &str) -> String {
match (base.ends_with('/'), path.strip_prefix('/')) {
(true, Some(rest)) => format!("{base}{rest}"),
_ => format!("{base}{path}"),
}
}
pub const DEFAULT_HTTP_IDLE_TIMEOUT: Duration = Duration::from_millis(config::keys::DEFAULT_HTTP_IDLE_TIMEOUT_MS);
pub fn http_idle_timeout(setting: Option<&Value>) -> Option<Duration> {
match setting.and_then(config::keys::parse_timeout_ms) {
Some(0.0) => None,
Some(ms) => Some(Duration::from_millis(ms as u64)),
None => Some(DEFAULT_HTTP_IDLE_TIMEOUT),
}
}
#[derive(Clone, Debug, Default)]
pub struct ReqwestTransport {
client: Arc<OnceLock<reqwest::Client>>,
timeout: Option<Duration>,
idle: Option<Duration>,
config: Option<crate::net::ClientConfig>,
}
impl ReqwestTransport {
pub fn new(timeout: Option<Duration>) -> Self {
Self::lazy(crate::net::ClientConfig::default(), timeout)
}
pub fn configured(config: crate::net::ClientConfig, timeout: Option<Duration>) -> Result<Self, String> {
let client = config.client()?;
Ok(ReqwestTransport {
config: Some(config),
..Self::with_client(client, timeout)
})
}
pub fn for_env(var: &dyn Fn(&str) -> Option<String>, timeout: Option<Duration>) -> Self {
Self::lazy(crate::net::ClientConfig::from_lookup(var), timeout)
}
fn lazy(config: crate::net::ClientConfig, timeout: Option<Duration>) -> Self {
ReqwestTransport {
client: Arc::default(),
timeout,
idle: None,
config: Some(config),
}
}
pub fn with_client(client: reqwest::Client, timeout: Option<Duration>) -> Self {
ReqwestTransport {
client: Arc::new(OnceLock::from(client)),
timeout,
idle: None,
config: None,
}
}
fn client(&self) -> &reqwest::Client {
self.client.get_or_init(|| {
self.config
.as_ref()
.and_then(|config| config.client().ok())
.or_else(|| crate::net::ClientConfig::default().client().ok())
.unwrap_or_default()
})
}
pub fn with_idle_timeout(mut self, idle: Option<Duration>) -> Self {
self.idle = idle;
self
}
}
struct ReqwestBody(reqwest::Response, Option<Duration>);
impl Body for ReqwestBody {
fn next_chunk(&mut self) -> BoxFuture<'_, Result<Option<Vec<u8>>, String>> {
Box::pin(async move {
let chunk = self.0.chunk();
let chunk = match self.1 {
Some(idle) => tokio::time::timeout(idle, chunk)
.await
.map_err(|_| "terminated".to_string())?,
None => chunk.await,
};
chunk.map(|c| c.map(|b| b.to_vec())).map_err(|e| e.to_string())
})
}
}
impl Transport for ReqwestTransport {
fn with_client_identity(&self, cert: &[u8], key: &[u8]) -> Result<Arc<dyn Transport>, String> {
let config = self
.config
.as_ref()
.ok_or("this HTTP transport cannot present a TLS client certificate")?;
let mut pem = cert.to_vec();
if !pem.ends_with(b"\n") {
pem.push(b'\n');
}
pem.extend_from_slice(key);
let identity = reqwest::Identity::from_pem(&pem).map_err(|e| e.to_string())?;
let client = config
.builder()?
.identity(identity)
.build()
.map_err(|e| e.to_string())?;
Ok(Arc::new(ReqwestTransport {
client: Arc::new(OnceLock::from(client)),
timeout: self.timeout,
idle: self.idle,
config: self.config.clone(),
}))
}
fn detached(&self) -> Option<Arc<dyn Transport>> {
let builder = match &self.config {
Some(config) => config.builder().ok()?,
None => reqwest::Client::builder().no_proxy(),
};
Some(Arc::new(ReqwestTransport {
client: Arc::new(OnceLock::from(builder.build().ok()?)),
timeout: self.timeout,
idle: self.idle,
config: self.config.clone(),
}))
}
fn http1_only(&self) -> Option<Arc<dyn Transport>> {
let client = self.config.as_ref()?.builder().ok()?.http1_only().build().ok()?;
Some(Arc::new(ReqwestTransport {
client: Arc::new(OnceLock::from(client)),
timeout: self.timeout,
idle: self.idle,
config: self.config.clone(),
}))
}
fn websocket_available(&self) -> bool {
true
}
fn websocket(
&self,
url: String,
headers: HeaderMap,
) -> BoxFuture<'_, Result<Box<dyn crate::websocket::WebSocketConnection>, String>> {
Box::pin(async move {
let builder = match &self.config {
Some(config) => config.builder()?,
None => reqwest::Client::builder().no_proxy(),
};
let client = builder.http1_only().build().map_err(|e| e.to_string())?;
crate::websocket::connect(&client, &url, &headers).await
})
}
fn send(&self, request: HttpRequest) -> BoxFuture<'_, Result<HttpResponse, String>> {
Box::pin(async move {
let method = reqwest::Method::from_bytes(request.method.as_bytes()).map_err(|e| e.to_string())?;
let mut builder = self.client().request(method, &request.url).body(wire_body(&request));
for (name, value) in &request.headers {
builder = builder.header(name, value);
}
if let Some(timeout) = self.timeout {
builder = builder.timeout(timeout);
}
let response = match self.idle {
Some(idle) => tokio::time::timeout(idle, builder.send())
.await
.map_err(|_| "Headers Timeout Error".to_string())?,
None => builder.send().await,
}
.map_err(|e| e.to_string())?;
let status = response.status().as_u16();
let mut headers = HeaderMap::new();
for (name, value) in response.headers() {
if let Ok(value) = value.to_str() {
headers.insert(name.as_str().to_string(), value.to_string());
}
}
Ok(HttpResponse {
status,
headers,
body: Box::new(ReqwestBody(response, self.idle)),
})
})
}
}
pub(crate) enum Read {
Chunk(Vec<u8>),
End,
Aborted,
Failed(String),
}
pub(crate) async fn read_chunk(body: &mut dyn Body, signal: Option<&AbortSignal>) -> Read {
tokio::select! {
chunk = body.next_chunk() => match chunk {
Ok(Some(bytes)) => Read::Chunk(bytes),
Ok(None) => Read::End,
Err(error) => Read::Failed(error),
},
() = cancelled(signal) => Read::Aborted,
}
}