mirror of
https://github.com/openai/codex.git
synced 2026-11-19 15:04:25 +00:00
Route backend requests through the HTTP client factory (#34490)
## Why Backend-backed features need to use the same outbound routing configuration as the rest of Codex. ## What changed - Build the backend client on a route-aware client pool supplied by `HttpClientFactory`. - Pass the configured factory through app-server account requests, cloud config loading, cloud tasks, and memory rate-limit checks. - Preserve task query encoding, authentication headers, JSON request bodies, and ChatGPT cookie handling during the migration. ## Testing - Add backend-client tests for proxy-policy propagation, task query encoding, bearer authentication, and JSON request bodies. GitOrigin-RevId: c17d162e0dce9cec758df7cdeecf982207c6512b
This commit is contained in:
1 parent
d5998e7452
commit
dc21b46aea
20 files changed
+365
-124
No files matched your search
@@ -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);
|
||||
@@ -664,6 +684,10 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "client_request_tests.rs"]
|
||||
mod request_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1004,7 +1028,10 @@ mod tests {
|
||||
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,
|
||||
|
||||
Reference in new issue
Block a user