mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
21c6d40a44
Adds a new feature `enable_request_compression` that will compress using zstd requests to the codex-backend. Currently only enabled for codex-backend so only enabled for openai providers when using chatgpt::auth even when the feature is enabled Added a new info log line too for evaluating the compression ratio and overhead off compressing before requesting. You can enable with `RUST_LOG=$RUST_LOG,codex_client::transport=info` ``` 2026-01-06T00:09:48.272113Z INFO codex_client::transport: Compressed request body with zstd pre_compression_bytes=28914 post_compression_bytes=11485 compression_duration_ms=0 ```
117 lines
3.8 KiB
Rust
117 lines
3.8 KiB
Rust
#![cfg(not(target_os = "windows"))]
|
|
|
|
use codex_core::CodexAuth;
|
|
use codex_core::features::Feature;
|
|
use codex_core::protocol::EventMsg;
|
|
use codex_core::protocol::Op;
|
|
use codex_protocol::user_input::UserInput;
|
|
use core_test_support::responses::ev_completed;
|
|
use core_test_support::responses::ev_response_created;
|
|
use core_test_support::responses::get_responses_requests;
|
|
use core_test_support::responses::mount_sse_once;
|
|
use core_test_support::responses::sse;
|
|
use core_test_support::responses::start_mock_server;
|
|
use core_test_support::skip_if_no_network;
|
|
use core_test_support::test_codex::test_codex;
|
|
use core_test_support::wait_for_event;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn request_body_is_zstd_compressed_for_codex_backend_when_enabled() -> anyhow::Result<()> {
|
|
skip_if_no_network!(Ok(()));
|
|
|
|
let server = start_mock_server().await;
|
|
mount_sse_once(
|
|
&server,
|
|
sse(vec![ev_response_created("resp-1"), ev_completed("resp-1")]),
|
|
)
|
|
.await;
|
|
|
|
let base_url = format!("{}/backend-api/codex/v1", server.uri());
|
|
let mut builder = test_codex()
|
|
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
|
|
.with_config(move |config| {
|
|
config.features.enable(Feature::EnableRequestCompression);
|
|
config.model_provider.base_url = Some(base_url);
|
|
});
|
|
let codex = builder.build(&server).await?.codex;
|
|
|
|
codex
|
|
.submit(Op::UserInput {
|
|
items: vec![UserInput::Text {
|
|
text: "compress me".into(),
|
|
}],
|
|
final_output_json_schema: None,
|
|
})
|
|
.await?;
|
|
|
|
// Wait until the task completes so the request definitely hit the server.
|
|
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await;
|
|
|
|
let requests = get_responses_requests(&server).await;
|
|
assert_eq!(requests.len(), 1);
|
|
|
|
let request = &requests[0];
|
|
let content_encoding = request
|
|
.headers
|
|
.get("content-encoding")
|
|
.and_then(|v| v.to_str().ok());
|
|
assert_eq!(content_encoding, Some("zstd"));
|
|
|
|
let decompressed = zstd::stream::decode_all(std::io::Cursor::new(request.body.clone()))?;
|
|
let json: serde_json::Value = serde_json::from_slice(&decompressed)?;
|
|
assert!(
|
|
json.get("input").is_some(),
|
|
"expected request body to decode as Responses API JSON"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn request_body_is_not_compressed_for_api_key_auth_even_when_enabled() -> anyhow::Result<()> {
|
|
skip_if_no_network!(Ok(()));
|
|
|
|
let server = start_mock_server().await;
|
|
mount_sse_once(
|
|
&server,
|
|
sse(vec![ev_response_created("resp-1"), ev_completed("resp-1")]),
|
|
)
|
|
.await;
|
|
|
|
let base_url = format!("{}/backend-api/codex/v1", server.uri());
|
|
let mut builder = test_codex().with_config(move |config| {
|
|
config.features.enable(Feature::EnableRequestCompression);
|
|
config.model_provider.base_url = Some(base_url);
|
|
});
|
|
let codex = builder.build(&server).await?.codex;
|
|
|
|
codex
|
|
.submit(Op::UserInput {
|
|
items: vec![UserInput::Text {
|
|
text: "do not compress".into(),
|
|
}],
|
|
final_output_json_schema: None,
|
|
})
|
|
.await?;
|
|
|
|
wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await;
|
|
|
|
let requests = get_responses_requests(&server).await;
|
|
assert_eq!(requests.len(), 1);
|
|
|
|
let request = &requests[0];
|
|
assert!(
|
|
request.headers.get("content-encoding").is_none(),
|
|
"did not expect request compression for API-key auth"
|
|
);
|
|
|
|
let json: serde_json::Value = serde_json::from_slice(&request.body)?;
|
|
assert!(
|
|
json.get("input").is_some(),
|
|
"expected request body to be plain Responses API JSON"
|
|
);
|
|
|
|
Ok(())
|
|
}
|