mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
backend-client: route requests through HTTP client factory
This commit is contained in:
@@ -16,7 +16,8 @@ workspace = true
|
||||
anyhow = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||
http = { workspace = true }
|
||||
url = { workspace = true }
|
||||
codex-backend-openapi-models = { path = "../codex-backend-openapi-models" }
|
||||
codex-api = { workspace = true }
|
||||
codex-http-client = { workspace = true }
|
||||
@@ -26,3 +27,4 @@ codex-protocol = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1"
|
||||
tokio = { workspace = true, features = ["macros", "rt"] }
|
||||
|
||||
@@ -9,8 +9,10 @@ use crate::types::TokenUsageProfile;
|
||||
use crate::types::TurnAttemptsSiblingTurnsResponse;
|
||||
use anyhow::Result;
|
||||
use codex_api::SharedAuthProvider;
|
||||
use codex_http_client::build_reqwest_client_with_custom_ca;
|
||||
use codex_http_client::with_chatgpt_cloudflare_cookie_store;
|
||||
use codex_http_client::ClientRouteClass;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_http_client::RouteAwareClientPool;
|
||||
use codex_http_client::RouteAwareRequestBuilder;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_login::default_client::get_codex_user_agent;
|
||||
use codex_protocol::account::PlanType as AccountPlanType;
|
||||
@@ -19,13 +21,14 @@ use codex_protocol::protocol::RateLimitReachedType;
|
||||
use codex_protocol::protocol::RateLimitSnapshot;
|
||||
use codex_protocol::protocol::RateLimitWindow;
|
||||
use codex_protocol::protocol::SpendControlLimitSnapshot;
|
||||
use reqwest::StatusCode;
|
||||
use reqwest::header::CACHE_CONTROL;
|
||||
use reqwest::header::CONTENT_TYPE;
|
||||
use reqwest::header::HeaderMap;
|
||||
use reqwest::header::HeaderName;
|
||||
use reqwest::header::HeaderValue;
|
||||
use reqwest::header::USER_AGENT;
|
||||
use http::Method;
|
||||
use http::StatusCode;
|
||||
use http::header::CACHE_CONTROL;
|
||||
use http::header::CONTENT_TYPE;
|
||||
use http::header::HeaderMap;
|
||||
use http::header::HeaderName;
|
||||
use http::header::HeaderValue;
|
||||
use http::header::USER_AGENT;
|
||||
use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::fmt;
|
||||
@@ -123,7 +126,7 @@ impl PathStyle {
|
||||
#[derive(Clone)]
|
||||
pub struct Client {
|
||||
base_url: String,
|
||||
http: reqwest::Client,
|
||||
http: RouteAwareClientPool,
|
||||
auth_provider: SharedAuthProvider,
|
||||
user_agent: Option<HeaderValue>,
|
||||
chatgpt_account_id: Option<String>,
|
||||
@@ -148,7 +151,7 @@ impl fmt::Debug for Client {
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn new(base_url: impl Into<String>) -> Result<Self> {
|
||||
pub fn new(base_url: impl Into<String>, http_client_factory: HttpClientFactory) -> Self {
|
||||
let mut base_url = base_url.into();
|
||||
// Normalize common ChatGPT hostnames to include /backend-api so we hit the WHAM paths.
|
||||
// Also trim trailing slashes for consistent URL building.
|
||||
@@ -161,11 +164,12 @@ impl Client {
|
||||
{
|
||||
base_url = format!("{base_url}/backend-api");
|
||||
}
|
||||
let http = build_reqwest_client_with_custom_ca(with_chatgpt_cloudflare_cookie_store(
|
||||
reqwest::Client::builder(),
|
||||
))?;
|
||||
let http = RouteAwareClientPool::with_chatgpt_cloudflare_cookies_without_request_logging(
|
||||
http_client_factory,
|
||||
ClientRouteClass::Api,
|
||||
);
|
||||
let path_style = PathStyle::from_base_url(&base_url);
|
||||
Ok(Self {
|
||||
Self {
|
||||
base_url,
|
||||
http,
|
||||
auth_provider: codex_model_provider::unauthenticated_auth_provider(),
|
||||
@@ -173,13 +177,17 @@ impl Client {
|
||||
chatgpt_account_id: None,
|
||||
chatgpt_account_is_fedramp: false,
|
||||
path_style,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_auth(base_url: impl Into<String>, auth: &CodexAuth) -> Result<Self> {
|
||||
Ok(Self::new(base_url)?
|
||||
pub fn from_auth(
|
||||
base_url: impl Into<String>,
|
||||
auth: &CodexAuth,
|
||||
http_client_factory: HttpClientFactory,
|
||||
) -> Self {
|
||||
Self::new(base_url, http_client_factory)
|
||||
.with_user_agent(get_codex_user_agent())
|
||||
.with_auth_provider(codex_model_provider::auth_provider_from_auth(auth)))
|
||||
.with_auth_provider(codex_model_provider::auth_provider_from_auth(auth))
|
||||
}
|
||||
|
||||
pub fn with_auth_provider(mut self, auth: SharedAuthProvider) -> Self {
|
||||
@@ -231,9 +239,13 @@ impl Client {
|
||||
h
|
||||
}
|
||||
|
||||
fn request(&self, method: Method, url: &str) -> RouteAwareRequestBuilder {
|
||||
self.http.request(method, url)
|
||||
}
|
||||
|
||||
async fn exec_request(
|
||||
&self,
|
||||
req: reqwest::RequestBuilder,
|
||||
req: RouteAwareRequestBuilder,
|
||||
method: &str,
|
||||
url: &str,
|
||||
) -> Result<(String, String)> {
|
||||
@@ -254,7 +266,7 @@ impl Client {
|
||||
|
||||
async fn exec_request_detailed(
|
||||
&self,
|
||||
req: reqwest::RequestBuilder,
|
||||
req: RouteAwareRequestBuilder,
|
||||
method: &str,
|
||||
url: &str,
|
||||
) -> std::result::Result<(String, String), RequestError> {
|
||||
@@ -306,14 +318,14 @@ impl Client {
|
||||
PathStyle::CodexApi => format!("{}/api/codex/accounts/check", self.base_url),
|
||||
PathStyle::ChatGptApi => format!("{}/wham/accounts/check", self.base_url),
|
||||
};
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json(&url, &ct, &body)
|
||||
}
|
||||
|
||||
pub async fn get_token_usage_profile(&self) -> Result<TokenUsageProfile> {
|
||||
let url = self.token_usage_profile_url();
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json(&url, &ct, &body)
|
||||
}
|
||||
@@ -331,8 +343,7 @@ impl Client {
|
||||
) -> std::result::Result<(), RequestError> {
|
||||
let url = self.send_add_credits_nudge_email_url();
|
||||
let req = self
|
||||
.http
|
||||
.post(&url)
|
||||
.request(Method::POST, &url)
|
||||
.headers(self.headers())
|
||||
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
||||
.json(&SendAddCreditsNudgeEmailRequest { credit_type });
|
||||
@@ -347,33 +358,44 @@ impl Client {
|
||||
environment_id: Option<&str>,
|
||||
cursor: Option<&str>,
|
||||
) -> Result<PaginatedListTaskListItem> {
|
||||
let url = self.list_tasks_url(limit, task_filter, environment_id, cursor)?;
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json::<PaginatedListTaskListItem>(&url, &ct, &body)
|
||||
}
|
||||
|
||||
fn list_tasks_url(
|
||||
&self,
|
||||
limit: Option<i32>,
|
||||
task_filter: Option<&str>,
|
||||
environment_id: Option<&str>,
|
||||
cursor: Option<&str>,
|
||||
) -> Result<String> {
|
||||
let url = match self.path_style {
|
||||
PathStyle::CodexApi => format!("{}/api/codex/tasks/list", self.base_url),
|
||||
PathStyle::ChatGptApi => format!("{}/wham/tasks/list", self.base_url),
|
||||
};
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = if let Some(lim) = limit {
|
||||
req.query(&[("limit", lim)])
|
||||
} else {
|
||||
req
|
||||
};
|
||||
let req = if let Some(tf) = task_filter {
|
||||
req.query(&[("task_filter", tf)])
|
||||
} else {
|
||||
req
|
||||
};
|
||||
let req = if let Some(c) = cursor {
|
||||
req.query(&[("cursor", c)])
|
||||
} else {
|
||||
req
|
||||
};
|
||||
let req = if let Some(id) = environment_id {
|
||||
req.query(&[("environment_id", id)])
|
||||
} else {
|
||||
req
|
||||
};
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json::<PaginatedListTaskListItem>(&url, &ct, &body)
|
||||
if limit.is_none() && task_filter.is_none() && environment_id.is_none() && cursor.is_none()
|
||||
{
|
||||
return Ok(url);
|
||||
}
|
||||
let mut url = url::Url::parse(&url)?;
|
||||
{
|
||||
let mut query = url.query_pairs_mut();
|
||||
if let Some(limit) = limit {
|
||||
query.append_pair("limit", &limit.to_string());
|
||||
}
|
||||
if let Some(task_filter) = task_filter {
|
||||
query.append_pair("task_filter", task_filter);
|
||||
}
|
||||
if let Some(cursor) = cursor {
|
||||
query.append_pair("cursor", cursor);
|
||||
}
|
||||
if let Some(environment_id) = environment_id {
|
||||
query.append_pair("environment_id", environment_id);
|
||||
}
|
||||
}
|
||||
Ok(url.to_string())
|
||||
}
|
||||
|
||||
pub async fn get_task_details(&self, task_id: &str) -> Result<CodeTaskDetailsResponse> {
|
||||
@@ -389,7 +411,7 @@ impl Client {
|
||||
PathStyle::CodexApi => format!("{}/api/codex/tasks/{}", self.base_url, task_id),
|
||||
PathStyle::ChatGptApi => format!("{}/wham/tasks/{}", self.base_url, task_id),
|
||||
};
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
let parsed: CodeTaskDetailsResponse = self.decode_json(&url, &ct, &body)?;
|
||||
Ok((parsed, body, ct))
|
||||
@@ -410,7 +432,7 @@ impl Client {
|
||||
self.base_url, task_id, turn_id
|
||||
),
|
||||
};
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json::<TurnAttemptsSiblingTurnsResponse>(&url, &ct, &body)
|
||||
}
|
||||
@@ -426,7 +448,7 @@ impl Client {
|
||||
PathStyle::CodexApi => format!("{}/api/codex/config/bundle", self.base_url),
|
||||
PathStyle::ChatGptApi => format!("{}/wham/config/bundle", self.base_url),
|
||||
};
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request_detailed(req, "GET", &url).await?;
|
||||
self.decode_json::<ConfigBundleResponse>(&url, &ct, &body)
|
||||
.map_err(RequestError::from)
|
||||
@@ -437,8 +459,7 @@ impl Client {
|
||||
) -> std::result::Result<CodexWorkspaceMessagesResponse, RequestError> {
|
||||
let url = self.workspace_messages_url();
|
||||
let req = self
|
||||
.http
|
||||
.get(&url)
|
||||
.request(Method::GET, &url)
|
||||
.headers(self.headers())
|
||||
.header(CACHE_CONTROL, HeaderValue::from_static("no-store"));
|
||||
let (body, ct) = self.exec_request_detailed(req, "GET", &url).await?;
|
||||
@@ -454,8 +475,7 @@ impl Client {
|
||||
PathStyle::ChatGptApi => format!("{}/wham/tasks", self.base_url),
|
||||
};
|
||||
let req = self
|
||||
.http
|
||||
.post(&url)
|
||||
.request(Method::POST, &url)
|
||||
.headers(self.headers())
|
||||
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
||||
.json(&request_body);
|
||||
@@ -666,6 +686,11 @@ impl Client {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use super::*;
|
||||
use codex_backend_openapi_models::models::AdditionalRateLimitDetails;
|
||||
use codex_backend_openapi_models::models::RateLimitReachedKind;
|
||||
@@ -974,10 +999,132 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_tasks_url_omits_empty_query_and_encodes_all_parameters() {
|
||||
let client = test_client("https://example.test", PathStyle::CodexApi);
|
||||
|
||||
assert_eq!(
|
||||
client
|
||||
.list_tasks_url(
|
||||
/*limit*/ None, /*task_filter*/ None, /*environment_id*/ None,
|
||||
/*cursor*/ None,
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.test/api/codex/tasks/list"
|
||||
);
|
||||
assert_eq!(
|
||||
client
|
||||
.list_tasks_url(
|
||||
/*limit*/ Some(10),
|
||||
/*task_filter*/ Some("mine / shared"),
|
||||
/*environment_id*/ Some("env&one"),
|
||||
/*cursor*/ Some("next=page"),
|
||||
)
|
||||
.unwrap(),
|
||||
"https://example.test/api/codex/tasks/list?limit=10&task_filter=mine+%2F+shared&cursor=next%3Dpage&environment_id=env%26one"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn migrated_requests_preserve_query_auth_and_json_body() {
|
||||
let listener =
|
||||
std::net::TcpListener::bind("127.0.0.1:0").expect("HTTP listener should bind");
|
||||
let address = listener
|
||||
.local_addr()
|
||||
.expect("HTTP listener should have an address");
|
||||
let server = std::thread::spawn(move || {
|
||||
let mut requests = Vec::new();
|
||||
for body in [r#"{"items":[]}"#, r#"{"task":{"id":"task-created"}}"#] {
|
||||
let (mut stream, _) = listener.accept().expect("HTTP listener should accept");
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(2)))
|
||||
.expect("HTTP stream should get a read timeout");
|
||||
let mut request = Vec::new();
|
||||
let mut buffer = [0_u8; 4096];
|
||||
loop {
|
||||
let size = stream.read(&mut buffer).expect("HTTP request should read");
|
||||
if size == 0 {
|
||||
break;
|
||||
}
|
||||
request.extend_from_slice(&buffer[..size]);
|
||||
let Some(headers_end) = request.windows(4).position(|part| part == b"\r\n\r\n")
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let headers = String::from_utf8_lossy(&request[..headers_end]);
|
||||
let content_length = headers
|
||||
.lines()
|
||||
.find_map(|line| {
|
||||
let (name, value) = line.split_once(':')?;
|
||||
name.eq_ignore_ascii_case("content-length")
|
||||
.then(|| value.trim().parse::<usize>().ok())
|
||||
.flatten()
|
||||
})
|
||||
.unwrap_or(0);
|
||||
if request.len() >= headers_end + 4 + content_length {
|
||||
break;
|
||||
}
|
||||
}
|
||||
requests.push(String::from_utf8(request).expect("request should be UTF-8"));
|
||||
write!(
|
||||
stream,
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
)
|
||||
.expect("HTTP response should write");
|
||||
}
|
||||
requests
|
||||
});
|
||||
let client = Client::new(
|
||||
format!("http://{address}"),
|
||||
HttpClientFactory::new(codex_http_client::OutboundProxyPolicy::ReqwestDefault),
|
||||
)
|
||||
.with_auth_provider(Arc::new(codex_model_provider::BearerAuthProvider::new(
|
||||
"request-token".to_string(),
|
||||
)));
|
||||
|
||||
let tasks = client
|
||||
.list_tasks(
|
||||
Some(10),
|
||||
Some("mine / shared"),
|
||||
Some("env&one"),
|
||||
Some("next=page"),
|
||||
)
|
||||
.await
|
||||
.expect("list request should succeed");
|
||||
let task_id = client
|
||||
.create_task(serde_json::json!({ "prompt": "hello" }))
|
||||
.await
|
||||
.expect("create request should succeed");
|
||||
let requests = server.join().expect("HTTP server should finish");
|
||||
|
||||
assert_eq!(tasks, PaginatedListTaskListItem::new(Vec::new()));
|
||||
assert_eq!(task_id, "task-created");
|
||||
assert_eq!(requests.len(), 2);
|
||||
assert!(requests[0].starts_with(
|
||||
"GET /api/codex/tasks/list?limit=10&task_filter=mine+%2F+shared&cursor=next%3Dpage&environment_id=env%26one HTTP/1.1\r\n"
|
||||
));
|
||||
assert!(
|
||||
requests[0]
|
||||
.to_ascii_lowercase()
|
||||
.contains("authorization: bearer request-token\r\n")
|
||||
);
|
||||
assert!(requests[1].starts_with("POST /api/codex/tasks HTTP/1.1\r\n"));
|
||||
assert!(
|
||||
requests[1]
|
||||
.to_ascii_lowercase()
|
||||
.contains("authorization: bearer request-token\r\n")
|
||||
);
|
||||
assert!(requests[1].ends_with(r#"{"prompt":"hello"}"#));
|
||||
}
|
||||
|
||||
fn test_client(base_url: &str, path_style: PathStyle) -> Client {
|
||||
Client {
|
||||
base_url: base_url.to_string(),
|
||||
http: reqwest::Client::new(),
|
||||
http: RouteAwareClientPool::new(
|
||||
HttpClientFactory::new(codex_http_client::OutboundProxyPolicy::ReqwestDefault),
|
||||
ClientRouteClass::Api,
|
||||
),
|
||||
auth_provider: codex_model_provider::unauthenticated_auth_provider(),
|
||||
user_agent: None,
|
||||
chatgpt_account_id: None,
|
||||
|
||||
@@ -7,8 +7,9 @@ use crate::types::RateLimitResetCreditsDetails;
|
||||
use crate::types::RateLimitStatusWithResetCredits;
|
||||
use crate::types::RateLimitsWithResetCredits;
|
||||
use anyhow::Result;
|
||||
use reqwest::header::CONTENT_TYPE;
|
||||
use reqwest::header::HeaderValue;
|
||||
use http::Method;
|
||||
use http::header::CONTENT_TYPE;
|
||||
use http::header::HeaderValue;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -29,14 +30,14 @@ impl Client {
|
||||
|
||||
pub(super) async fn get_rate_limit_status(&self) -> Result<RateLimitStatusWithResetCredits> {
|
||||
let url = self.rate_limit_status_url();
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json(&url, &ct, &body)
|
||||
}
|
||||
|
||||
pub async fn list_rate_limit_reset_credits(&self) -> Result<RateLimitResetCreditsDetails> {
|
||||
let url = self.rate_limit_reset_credits_url();
|
||||
let req = self.http.get(&url).headers(self.headers());
|
||||
let req = self.request(Method::GET, &url).headers(self.headers());
|
||||
let (body, ct) = self.exec_request(req, "GET", &url).await?;
|
||||
self.decode_json(&url, &ct, &body)
|
||||
}
|
||||
@@ -65,8 +66,7 @@ impl Client {
|
||||
) -> Result<ConsumeRateLimitResetCreditResponse> {
|
||||
let url = self.consume_rate_limit_reset_credit_url();
|
||||
let req = self
|
||||
.http
|
||||
.post(&url)
|
||||
.request(Method::POST, &url)
|
||||
.headers(self.headers())
|
||||
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
||||
.json(&ConsumeRateLimitResetCreditRequest {
|
||||
|
||||
@@ -138,7 +138,12 @@ fn rate_limit_reset_contract_uses_expected_paths_and_payloads() {
|
||||
fn test_client(base_url: &str, path_style: PathStyle) -> Client {
|
||||
Client {
|
||||
base_url: base_url.to_string(),
|
||||
http: reqwest::Client::new(),
|
||||
http: codex_http_client::RouteAwareClientPool::new(
|
||||
codex_http_client::HttpClientFactory::new(
|
||||
codex_http_client::OutboundProxyPolicy::ReqwestDefault,
|
||||
),
|
||||
codex_http_client::ClientRouteClass::Api,
|
||||
),
|
||||
auth_provider: codex_model_provider::unauthenticated_auth_provider(),
|
||||
user_agent: None,
|
||||
chatgpt_account_id: None,
|
||||
|
||||
Reference in New Issue
Block a user