use std::sync::Arc;
#[cfg(feature = "reqwest-transport")]
use std::sync::LazyLock;
use std::time::Duration;
#[cfg(feature = "reqwest-transport")]
use reqwest::header::{HeaderMap, CONTENT_TYPE, RETRY_AFTER};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::config::{AiConfig, Thinking};
use crate::error::{content_fingerprint, summarize, AiError, Result};
#[cfg(feature = "reqwest-transport")]
use crate::transport::{BoxFuture, TransportError};
use crate::transport::{Transport, TransportRequest, TransportResponse};
use crate::usage::UsageStats;
#[cfg(feature = "reqwest-transport")]
static CLIENT: LazyLock<reqwest::Client> = LazyLock::new(reqwest::Client::new);
#[cfg(feature = "reqwest-transport")]
pub(crate) struct ReqwestTransport;
#[cfg(feature = "reqwest-transport")]
impl Transport for ReqwestTransport {
fn name(&self) -> &'static str {
"reqwest"
}
fn send(
&self,
request: TransportRequest,
) -> BoxFuture<'static, std::result::Result<TransportResponse, TransportError>> {
Box::pin(async move {
let response = CLIENT
.post(&request.url)
.bearer_auth(&request.bearer_token)
.header(CONTENT_TYPE, "application/json")
.body(request.body)
.timeout(request.timeout)
.send()
.await;
let response = response.map_err(|err| TransportError {
retryable: err.is_timeout() || err.is_connect(),
summary: summarize(&redact_transport_error(&err), 200),
})?;
let status = response.status().as_u16();
let request_id = extract_request_id(response.headers());
let retry_after = extract_retry_after(response.headers());
let body = response.bytes().await.unwrap_or_default().to_vec();
Ok(TransportResponse {
status,
retry_after,
request_id,
body,
})
})
}
fn sleep(&self, duration: Duration) -> BoxFuture<'static, ()> {
Box::pin(async move {
tokio::time::sleep(duration).await;
})
}
}
#[derive(Serialize, Clone)]
pub(crate) struct Message {
pub role: &'static str,
pub content: String,
}
impl Message {
pub(crate) fn system(content: String) -> Self {
Message {
role: "system",
content,
}
}
pub(crate) fn user(content: String) -> Self {
Message {
role: "user",
content,
}
}
pub(crate) fn assistant(content: String) -> Self {
Message {
role: "assistant",
content,
}
}
}
#[derive(Serialize)]
pub(crate) struct ResponseFormat {
pub r#type: &'static str,
}
#[derive(Serialize)]
pub(crate) struct ThinkingSpec {
pub r#type: &'static str,
}
#[derive(Serialize)]
pub(crate) struct ChatRequest {
pub model: String,
pub messages: Vec<Message>,
pub response_format: ResponseFormat,
pub max_tokens: u32,
pub temperature: f32,
pub stream: bool,
pub thinking: ThinkingSpec,
#[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_effort: Option<&'static str>,
}
impl ChatRequest {
pub(crate) fn build(
config: &AiConfig,
messages: Vec<Message>,
thinking: Thinking,
max_tokens: u32,
) -> Self {
ChatRequest {
model: config.model.clone(),
messages,
response_format: ResponseFormat {
r#type: "json_object",
},
max_tokens,
temperature: config.temperature,
stream: false,
thinking: ThinkingSpec {
r#type: thinking.wire_type(),
},
reasoning_effort: thinking.reasoning_effort(),
}
}
}
#[derive(Deserialize, Default)]
pub(crate) struct Usage {
pub prompt_tokens: Option<u64>,
pub completion_tokens: Option<u64>,
pub prompt_cache_hit_tokens: Option<u64>,
pub prompt_cache_miss_tokens: Option<u64>,
}
#[derive(Deserialize)]
pub(crate) struct ResponseMessage {
pub content: Option<String>,
pub tool_calls: Option<Value>,
}
#[derive(Deserialize)]
pub(crate) struct Choice {
pub message: Option<ResponseMessage>,
pub finish_reason: Option<String>,
}
#[derive(Deserialize)]
pub(crate) struct ChatResponse {
pub choices: Option<Vec<Choice>>,
pub usage: Option<Usage>,
}
pub(crate) struct LogicalResponse {
pub content: Option<String>,
pub finish_reason: Option<String>,
pub tool_calls_present: bool,
pub usage: Option<Usage>,
pub request_id: Option<String>,
pub http_retries_used: u32,
}
#[derive(Deserialize)]
struct ApiErrorBody {
error: Option<ApiErrorDetail>,
}
#[derive(Deserialize)]
struct ApiErrorDetail {
message: Option<String>,
}
enum AttemptOutcome {
Success(LogicalResponse),
Retryable {
summary: String,
status: Option<u16>,
request_id: Option<String>,
retry_after: Option<Duration>,
},
Fatal(AiError),
}
pub(crate) async fn send(
config: &AiConfig,
base_url: &str,
request: &ChatRequest,
http_retries: u32,
stats: &mut UsageStats,
) -> Result<LogicalResponse> {
let transport = config
.transport
.clone()
.ok_or_else(|| AiError::InvalidConfig("no HTTP transport configured".to_string()))?;
let body = serde_json::to_vec(request)
.map_err(|err| AiError::InvalidConfig(format!("failed to encode request: {err}")))?;
let url = format!("{base_url}/chat/completions");
let mut retries_used = 0u32;
loop {
match attempt(&transport, config, &url, &body).await {
AttemptOutcome::Success(mut response) => {
response.http_retries_used = retries_used;
stats.http_retries += retries_used;
if let Some(usage) = &response.usage {
stats.add_usage(usage);
}
if response.request_id.is_some() {
stats.request_id.clone_from(&response.request_id);
}
return Ok(response);
}
AttemptOutcome::Retryable {
summary,
status,
request_id,
retry_after,
} => {
if retries_used >= http_retries {
return Err(match status {
Some(status) => AiError::Api {
status: Some(status),
request_id,
summary,
},
None => AiError::Http {
attempts: retries_used + 1,
summary,
},
});
}
retries_used += 1;
transport.sleep(backoff(retries_used, retry_after)).await;
}
AttemptOutcome::Fatal(err) => return Err(err),
}
}
}
async fn attempt(
transport: &Arc<dyn Transport>,
config: &AiConfig,
url: &str,
body: &[u8],
) -> AttemptOutcome {
use secrecy::ExposeSecret;
let result = transport
.send(TransportRequest {
url: url.to_string(),
bearer_token: config.api_key.expose_secret().to_string(),
body: body.to_vec(),
timeout: Duration::from_secs(config.timeout_secs),
})
.await;
let response = match result {
Ok(response) => response,
Err(err) if err.retryable => {
return AttemptOutcome::Retryable {
summary: err.summary,
status: None,
request_id: None,
retry_after: None,
};
}
Err(err) => {
return AttemptOutcome::Fatal(AiError::Http {
attempts: 1,
summary: err.summary,
});
}
};
let TransportResponse {
status,
retry_after,
request_id,
body,
} = response;
let body_text = String::from_utf8_lossy(&body);
if !(200..300).contains(&status) {
let detail = serde_json::from_str::<ApiErrorBody>(&body_text)
.ok()
.and_then(|parsed| parsed.error)
.and_then(|error| error.message)
.map(|message| summarize(&message, 200));
let summary = match detail {
Some(detail) => format!("HTTP {status}: {detail}"),
None => format!("HTTP {status}"),
};
return match status {
429 | 500 | 503 => AttemptOutcome::Retryable {
summary,
status: Some(status),
request_id,
retry_after,
},
_ => AttemptOutcome::Fatal(AiError::Api {
status: Some(status),
request_id,
summary,
}),
};
}
let parsed = match serde_json::from_str::<ChatResponse>(&body_text) {
Ok(parsed) => parsed,
Err(err) => {
return AttemptOutcome::Fatal(AiError::Api {
status: Some(status),
request_id,
summary: format!(
"malformed success response ({} bytes, hash {:#x}): {}",
body_text.len(),
content_fingerprint(&body_text),
summarize(&err.to_string(), 200)
),
});
}
};
let Some(choice) = parsed.choices.and_then(|mut choices| {
if choices.is_empty() {
None
} else {
Some(choices.remove(0))
}
}) else {
return AttemptOutcome::Fatal(AiError::Api {
status: Some(status),
request_id,
summary: "response contains no choices".to_string(),
});
};
if choice.finish_reason.as_deref() == Some("insufficient_system_resource") {
return AttemptOutcome::Retryable {
summary: "finish_reason: insufficient_system_resource".to_string(),
status: Some(status),
request_id,
retry_after: None,
};
}
let message = choice.message.unwrap_or(ResponseMessage {
content: None,
tool_calls: None,
});
let tool_calls_present = message
.tool_calls
.as_ref()
.is_some_and(|calls| !calls.is_null());
AttemptOutcome::Success(LogicalResponse {
content: message.content,
finish_reason: choice.finish_reason,
tool_calls_present,
usage: parsed.usage,
request_id,
http_retries_used: 0,
})
}
#[cfg(feature = "reqwest-transport")]
fn extract_request_id(headers: &HeaderMap) -> Option<String> {
headers
.get("x-request-id")
.and_then(|value| value.to_str().ok())
.map(str::to_string)
}
#[cfg(feature = "reqwest-transport")]
fn extract_retry_after(headers: &HeaderMap) -> Option<Duration> {
let value = headers.get(RETRY_AFTER)?.to_str().ok()?.trim().to_string();
value.parse::<u64>().ok().map(Duration::from_secs)
}
fn backoff(retry_number: u32, retry_after: Option<Duration>) -> Duration {
if let Some(retry_after) = retry_after {
return retry_after;
}
let base_ms = 500u64.saturating_mul(1u64 << retry_number.saturating_sub(1).min(6));
let capped = base_ms.min(30_000);
let jitter = 0.5 + fastrand::f64() * 0.5;
Duration::from_millis((capped as f64 * jitter) as u64)
}
#[cfg(feature = "reqwest-transport")]
fn redact_transport_error(err: &reqwest::Error) -> String {
let text = err.to_string();
text.split(" for ").next().unwrap_or(&text).to_string()
}