mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-07 01:47:47 +08:00
599 lines
25 KiB
Rust
599 lines
25 KiB
Rust
use super::{
|
|||
any, build_router_with_state, build_state_with_execution_runtime_override, json, start_server,
|
|||
strip_sse_keepalive_comments, to_bytes, Arc, Body, Bytes, HeaderName, HeaderValue, Infallible,
|
|||
|
|
Json, Mutex, Request, Response, Router, StatusCode, UsageRuntimeConfig, TRACE_ID_HEADER,
|
||
};
|
|||
use aether_crypto::{encrypt_python_fernet_plaintext, DEVELOPMENT_ENCRYPTION_KEY};
|
|||
|
|
use aether_data::repository::auth::{
|
||
|
|
InMemoryAuthApiKeySnapshotRepository, StoredAuthApiKeySnapshot,
|
||
|
|
};
|
||
use aether_data::repository::candidate_selection::InMemoryMinimalCandidateSelectionReadRepository;
|
|||
|
|
use aether_data::repository::candidates::InMemoryRequestCandidateRepository;
|
||
|
|
use aether_data::repository::provider_catalog::InMemoryProviderCatalogReadRepository;
|
||
use aether_data::repository::usage::InMemoryUsageReadRepository;
|
|||
use aether_data_contracts::repository::candidate_selection::{
|
|||
|
|
StoredMinimalCandidateSelectionRow, StoredProviderModelMapping,
|
||
};
|
|||
use aether_data_contracts::repository::candidates::{
|
|||
|
|
RequestCandidateReadRepository, RequestCandidateStatus,
|
||
};
|
|||
use aether_data_contracts::repository::provider_catalog::{
|
|||
|
|
StoredProviderCatalogEndpoint, StoredProviderCatalogKey, StoredProviderCatalogProvider,
|
||
};
|
|||
use aether_data_contracts::repository::usage::UsageReadRepository;
|
|||
use sha2::{Digest, Sha256};
|
|||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn gateway_executes_codex_cli_stream_via_local_decision_gate_after_oauth_refresh() {
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
struct SeenExecutionRuntimeStreamRequest {
|
||
|
|
trace_id: String,
|
||
|
|
url: String,
|
||
|
|
model: String,
|
||
|
|
stream: bool,
|
||
|
|
accept: String,
|
||
|
|
authorization: String,
|
||
x_client_request_id: String,
|
|||
}
|
|||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
struct SeenReportStreamRequest {
|
||
|
|
trace_id: String,
|
||
|
|
report_kind: String,
|
||
|
|
request_id: String,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
struct SeenRefreshRequest {
|
||
|
|
content_type: String,
|
||
|
|
body: String,
|
||
|
|
}
|
||
|
|
|
||
|
|
fn hash_api_key(value: &str) -> String {
|
||
|
|
let mut hasher = Sha256::new();
|
||
|
|
hasher.update(value.as_bytes());
|
||
|
|
format!("{:x}", hasher.finalize())
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_auth_snapshot(api_key_id: &str, user_id: &str) -> StoredAuthApiKeySnapshot {
|
||
|
|
StoredAuthApiKeySnapshot::new(
|
||
|
|
user_id.to_string(),
|
||
|
|
"alice".to_string(),
|
||
|
|
Some("[email protected]".to_string()),
|
||
|
|
"user".to_string(),
|
||
|
|
"local".to_string(),
|
||
|
|
true,
|
||
|
|
false,
|
||
|
|
Some(serde_json::json!(["openai", "codex"])),
|
||
Some(serde_json::json!(["openai:responses"])),
|
|||
Some(serde_json::json!(["gpt-5.4"])),
|
|||
|
|
api_key_id.to_string(),
|
||
|
|
Some("default".to_string()),
|
||
|
|
true,
|
||
|
|
false,
|
||
|
|
false,
|
||
|
|
Some(60),
|
||
|
|
Some(5),
|
||
|
|
Some(4_102_444_800_i64),
|
||
|
|
Some(serde_json::json!(["openai", "codex"])),
|
||
Some(serde_json::json!(["openai:responses"])),
|
|||
Some(serde_json::json!(["gpt-5.4"])),
|
|||
|
|
)
|
||
|
|
.expect("auth snapshot should build")
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_candidate_row() -> StoredMinimalCandidateSelectionRow {
|
||
|
|
StoredMinimalCandidateSelectionRow {
|
||
|
|
provider_id: "provider-codex-cli-stream-local-1".to_string(),
|
||
|
|
provider_name: "codex".to_string(),
|
||
|
|
provider_type: "codex".to_string(),
|
||
|
|
provider_priority: 10,
|
||
|
|
provider_is_active: true,
|
||
|
|
endpoint_id: "endpoint-codex-cli-stream-local-1".to_string(),
|
||
endpoint_api_format: "openai:responses".to_string(),
|
|||
endpoint_api_family: Some("openai".to_string()),
|
|||
|
|
endpoint_kind: Some("cli".to_string()),
|
||
|
|
endpoint_is_active: true,
|
||
|
|
key_id: "key-codex-cli-stream-local-1".to_string(),
|
||
|
|
key_name: "oauth".to_string(),
|
||
|
|
key_auth_type: "oauth".to_string(),
|
||
|
|
key_is_active: true,
|
||
key_api_formats: Some(vec!["openai:responses".to_string()]),
|
|||
key_allowed_models: None,
|
|||
|
|
key_capabilities: None,
|
||
|
|
key_internal_priority: 5,
|
||
key_global_priority_by_format: Some(serde_json::json!({"openai:responses": 1})),
|
|||
model_id: "model-codex-cli-stream-local-1".to_string(),
|
|||
|
|
global_model_id: "global-model-codex-cli-stream-local-1".to_string(),
|
||
|
|
global_model_name: "gpt-5.4".to_string(),
|
||
|
|
global_model_mappings: None,
|
||
|
|
global_model_supports_streaming: Some(true),
|
||
|
|
model_provider_model_name: "gpt-5.4".to_string(),
|
||
|
|
model_provider_model_mappings: Some(vec![StoredProviderModelMapping {
|
||
|
|
name: "gpt-5.4".to_string(),
|
||
|
|
priority: 1,
|
||
api_formats: Some(vec!["openai:responses".to_string()]),
|
|||
endpoint_ids: None,
|
|||
}]),
|
|||
|
|
model_supports_streaming: Some(true),
|
||
|
|
model_is_active: true,
|
||
|
|
model_is_available: true,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_provider_catalog_provider() -> StoredProviderCatalogProvider {
|
||
|
|
StoredProviderCatalogProvider::new(
|
||
|
|
"provider-codex-cli-stream-local-1".to_string(),
|
||
|
|
"codex".to_string(),
|
||
|
|
Some("https://chatgpt.com".to_string()),
|
||
|
|
"codex".to_string(),
|
||
|
|
)
|
||
|
|
.expect("provider should build")
|
||
|
|
.with_transport_fields(
|
||
|
|
true,
|
||
|
|
false,
|
||
|
|
false,
|
||
|
|
None,
|
||
|
|
Some(2),
|
||
|
|
None,
|
||
|
|
Some(20.0),
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_provider_catalog_endpoint() -> StoredProviderCatalogEndpoint {
|
||
|
|
StoredProviderCatalogEndpoint::new(
|
||
|
|
"endpoint-codex-cli-stream-local-1".to_string(),
|
||
|
|
"provider-codex-cli-stream-local-1".to_string(),
|
||
"openai:responses".to_string(),
|
|||
Some("openai".to_string()),
|
|||
|
|
Some("cli".to_string()),
|
||
|
|
true,
|
||
|
|
)
|
||
|
|
.expect("endpoint should build")
|
||
|
|
.with_transport_fields(
|
||
|
|
"https://chatgpt.com/backend-api/codex".to_string(),
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
Some(2),
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
)
|
||
|
|
.expect("endpoint transport should build")
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_provider_catalog_key() -> StoredProviderCatalogKey {
|
||
|
|
let encrypted_auth_config = encrypt_python_fernet_plaintext(
|
||
|
|
DEVELOPMENT_ENCRYPTION_KEY,
|
||
|
|
r#"{"provider_type":"codex","refresh_token":"rt-codex-stream-local-123"}"#,
|
||
|
|
)
|
||
|
|
.expect("auth config should encrypt");
|
||
|
|
StoredProviderCatalogKey::new(
|
||
|
|
"key-codex-cli-stream-local-1".to_string(),
|
||
|
|
"provider-codex-cli-stream-local-1".to_string(),
|
||
|
|
"oauth".to_string(),
|
||
|
|
"oauth".to_string(),
|
||
|
|
None,
|
||
|
|
true,
|
||
|
|
)
|
||
|
|
.expect("key should build")
|
||
|
|
.with_transport_fields(
|
||
Some(serde_json::json!(["openai:responses"])),
|
|||
encrypt_python_fernet_plaintext(DEVELOPMENT_ENCRYPTION_KEY, "__placeholder__")
|
|||
|
|
.expect("placeholder api key should encrypt"),
|
||
|
|
Some(encrypted_auth_config),
|
||
|
|
None,
|
||
Some(serde_json::json!({"openai:responses": 1})),
|
|||
None,
|
|||
|
|
None,
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
)
|
||
|
|
.expect("key transport should build")
|
||
|
|
}
|
||
|
|
|
||
|
|
let seen_execution_runtime = Arc::new(Mutex::new(None::<SeenExecutionRuntimeStreamRequest>));
|
||
|
|
let seen_execution_runtime_clone = Arc::clone(&seen_execution_runtime);
|
||
|
|
let seen_report = Arc::new(Mutex::new(None::<SeenReportStreamRequest>));
|
||
|
|
let seen_report_clone = Arc::clone(&seen_report);
|
||
|
|
let seen_refresh = Arc::new(Mutex::new(None::<SeenRefreshRequest>));
|
||
|
|
let seen_refresh_clone = Arc::clone(&seen_refresh);
|
||
|
|
let decision_hits = Arc::new(Mutex::new(0usize));
|
||
|
|
let decision_hits_clone = Arc::clone(&decision_hits);
|
||
|
|
let plan_hits = Arc::new(Mutex::new(0usize));
|
||
|
|
let plan_hits_clone = Arc::clone(&plan_hits);
|
||
|
|
let public_hits = Arc::new(Mutex::new(0usize));
|
||
|
|
let public_hits_clone = Arc::clone(&public_hits);
|
||
|
|
|
||
|
|
let upstream = Router::new()
|
||
|
|
.route(
|
||
|
|
"/api/internal/gateway/resolve",
|
||
|
|
any(|_request: Request| async move {
|
||
|
|
Json(json!({
|
||
|
|
"action": "proxy_public",
|
||
|
|
"route_class": "ai_public",
|
||
|
|
"route_family": "openai",
|
||
|
|
"route_kind": "cli",
|
||
"auth_endpoint_signature": "openai:responses",
|
|||
"execution_runtime_candidate": true,
|
|||
|
|
"auth_context": {
|
||
|
|
"user_id": "user-codex-cli-stream-local-123",
|
||
|
|
"api_key_id": "key-codex-cli-stream-local-123",
|
||
|
|
"access_allowed": true
|
||
|
|
},
|
||
|
|
"public_path": "/v1/responses"
|
||
|
|
}))
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.route(
|
||
|
|
"/api/internal/gateway/decision-stream",
|
||
|
|
any(move |_request: Request| {
|
||
|
|
let decision_hits_inner = Arc::clone(&decision_hits_clone);
|
||
|
|
async move {
|
||
|
|
*decision_hits_inner.lock().expect("mutex should lock") += 1;
|
||
|
|
Json(json!({"action": "proxy_public"}))
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.route(
|
||
|
|
"/api/internal/gateway/plan-stream",
|
||
|
|
any(move |_request: Request| {
|
||
|
|
let plan_hits_inner = Arc::clone(&plan_hits_clone);
|
||
|
|
async move {
|
||
|
|
*plan_hits_inner.lock().expect("mutex should lock") += 1;
|
||
|
|
Json(json!({"action": "proxy_public"}))
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.route(
|
||
|
|
"/api/internal/gateway/report-stream",
|
||
|
|
any(move |request: Request| {
|
||
|
|
let seen_report_inner = Arc::clone(&seen_report_clone);
|
||
|
|
async move {
|
||
|
|
let (parts, body) = request.into_parts();
|
||
|
|
let raw_body = to_bytes(body, usize::MAX).await.expect("body should read");
|
||
|
|
let payload: serde_json::Value =
|
||
|
|
serde_json::from_slice(&raw_body).expect("report payload should parse");
|
||
|
|
*seen_report_inner.lock().expect("mutex should lock") =
|
||
|
|
Some(SeenReportStreamRequest {
|
||
|
|
trace_id: parts
|
||
|
|
.headers
|
||
|
|
.get(TRACE_ID_HEADER)
|
||
|
|
.and_then(|value| value.to_str().ok())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
report_kind: payload
|
||
|
|
.get("report_kind")
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
request_id: payload
|
||
|
|
.get("report_context")
|
||
|
|
.and_then(|value| value.get("request_id"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
});
|
||
|
|
Json(json!({"ok": true}))
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
)
|
||
|
|
.route(
|
||
|
|
"/v1/responses",
|
||
|
|
any(move |_request: Request| {
|
||
|
|
let public_hits_inner = Arc::clone(&public_hits_clone);
|
||
|
|
async move {
|
||
|
|
*public_hits_inner.lock().expect("mutex should lock") += 1;
|
||
|
|
let stream = futures_util::stream::iter([Ok::<_, Infallible>(
|
||
|
|
Bytes::from_static(b"unexpected"),
|
||
|
|
)]);
|
||
|
|
let mut response = Response::builder()
|
||
|
|
.status(StatusCode::OK)
|
||
|
|
.body(Body::from_stream(stream))
|
||
|
|
.expect("response should build");
|
||
|
|
response.headers_mut().insert(
|
||
|
|
http::header::CONTENT_TYPE,
|
||
|
|
HeaderValue::from_static("text/plain"),
|
||
|
|
);
|
||
|
|
response
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
let refresh = Router::new().route(
|
||
|
|
"/oauth/token",
|
||
|
|
any(move |request: Request| {
|
||
|
|
let seen_refresh_inner = Arc::clone(&seen_refresh_clone);
|
||
|
|
async move {
|
||
|
|
let (parts, body) = request.into_parts();
|
||
|
|
let raw_body = to_bytes(body, usize::MAX).await.expect("body should read");
|
||
|
|
*seen_refresh_inner.lock().expect("mutex should lock") = Some(SeenRefreshRequest {
|
||
|
|
content_type: parts
|
||
|
|
.headers
|
||
|
|
.get(http::header::CONTENT_TYPE)
|
||
|
|
.and_then(|value| value.to_str().ok())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
body: String::from_utf8(raw_body.to_vec())
|
||
|
|
.expect("refresh body should be utf8"),
|
||
|
|
});
|
||
|
|
Json(json!({
|
||
|
|
"access_token": "refreshed-codex-stream-access-token",
|
||
|
|
"refresh_token": "rt-codex-stream-local-456",
|
||
|
|
"token_type": "Bearer",
|
||
|
|
"expires_in": 3600
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
let execution_runtime = Router::new().route(
|
||
|
|
"/v1/execute/stream",
|
||
|
|
any(move |request: Request| {
|
||
|
|
let seen_execution_runtime_inner = Arc::clone(&seen_execution_runtime_clone);
|
||
|
|
async move {
|
||
|
|
let (parts, body) = request.into_parts();
|
||
|
|
let raw_body = to_bytes(body, usize::MAX).await.expect("body should read");
|
||
|
|
let payload: serde_json::Value =
|
||
|
|
serde_json::from_slice(&raw_body).expect("execution runtime payload should parse");
|
||
|
|
*seen_execution_runtime_inner.lock().expect("mutex should lock") =
|
||
|
|
Some(SeenExecutionRuntimeStreamRequest {
|
||
|
|
trace_id: parts
|
||
|
|
.headers
|
||
|
|
.get(TRACE_ID_HEADER)
|
||
|
|
.and_then(|value| value.to_str().ok())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
url: payload
|
||
|
|
.get("url")
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
model: payload
|
||
|
|
.get("body")
|
||
|
|
.and_then(|value| value.get("json_body"))
|
||
|
|
.and_then(|value| value.get("model"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
stream: payload
|
||
|
|
.get("stream")
|
||
|
|
.and_then(|value| value.as_bool())
|
||
|
|
.unwrap_or(false),
|
||
|
|
accept: payload
|
||
|
|
.get("headers")
|
||
|
|
.and_then(|value| value.get("accept"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
|
|
authorization: payload
|
||
|
|
.get("headers")
|
||
|
|
.and_then(|value| value.get("authorization"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
x_client_request_id: payload
|
|||
|
|
.get("headers")
|
||
|
|
.and_then(|value| value.get("x-client-request-id"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
});
|
|||
|
|
let frames = concat!(
|
||
|
|
"{\"type\":\"headers\",\"payload\":{\"kind\":\"headers\",\"status_code\":200,\"headers\":{\"content-type\":\"text/event-stream\"}}}\n",
|
||
"{\"type\":\"data\",\"payload\":{\"kind\":\"data\",\"text\":\"event: response.completed\\ndata: {\\\"type\\\":\\\"response.completed\\\",\\\"response\\\":{\\\"id\\\":\\\"resp_codex_cli_stream_local_123\\\",\\\"object\\\":\\\"response\\\",\\\"model\\\":\\\"gpt-5.4\\\",\\\"status\\\":\\\"completed\\\",\\\"usage\\\":{\\\"input_tokens\\\":1,\\\"output_tokens\\\":2,\\\"total_tokens\\\":3}}}\\n\\n\"}}\n",
|
|||
"{\"type\":\"telemetry\",\"payload\":{\"kind\":\"telemetry\",\"telemetry\":{\"elapsed_ms\":41}}}\n",
|
|||
|
|
"{\"type\":\"eof\",\"payload\":{\"kind\":\"eof\"}}\n"
|
||
|
|
);
|
||
|
|
let mut response = Response::builder()
|
||
|
|
.status(StatusCode::OK)
|
||
|
|
.body(Body::from(frames))
|
||
|
|
.expect("response should build");
|
||
|
|
response.headers_mut().insert(
|
||
|
|
http::header::CONTENT_TYPE,
|
||
|
|
HeaderValue::from_static("application/x-ndjson"),
|
||
|
|
);
|
||
|
|
response
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
let client_api_key = "sk-client-codex-cli-stream-local";
|
||
|
|
let auth_repository = Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![(
|
||
|
|
Some(hash_api_key(client_api_key)),
|
||
|
|
sample_auth_snapshot(
|
||
|
|
"key-codex-cli-stream-local-123",
|
||
|
|
"user-codex-cli-stream-local-123",
|
||
|
|
),
|
||
|
|
)]));
|
||
|
|
let candidate_selection_repository =
|
||
|
|
Arc::new(InMemoryMinimalCandidateSelectionReadRepository::seed(vec![
|
||
|
|
sample_candidate_row(),
|
||
|
|
]));
|
||
|
|
let provider_catalog_repository = Arc::new(InMemoryProviderCatalogReadRepository::seed(
|
||
|
|
vec![sample_provider_catalog_provider()],
|
||
|
|
vec![sample_provider_catalog_endpoint()],
|
||
|
|
vec![sample_provider_catalog_key()],
|
||
|
|
));
|
||
|
|
let request_candidate_repository = Arc::new(InMemoryRequestCandidateRepository::default());
|
||
let usage_repository = Arc::new(InMemoryUsageReadRepository::default());
|
|||
|
|||
|
|
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
||
|
|
let (refresh_url, refresh_handle) = start_server(refresh).await;
|
||
|
|
let (execution_runtime_url, execution_runtime_handle) = start_server(execution_runtime).await;
|
||
|
|
let oauth_refresh =
|
||
crate::provider_transport::LocalOAuthRefreshCoordinator::with_adapters_for_tests(vec![
|
|||
|
|
Arc::new(
|
||
crate::provider_transport::oauth_refresh::GenericOAuthRefreshAdapter::default()
|
|||
.with_token_url_for_tests("codex", format!("{refresh_url}/oauth/token")),
|
|||
),
|
|||
|
|
]);
|
||
let gateway_state = build_state_with_execution_runtime_override(execution_runtime_url.clone())
|
|||
.with_data_state_for_tests(
|
|||
crate::data::GatewayDataState::with_auth_candidate_selection_provider_catalog_request_candidates_and_usage_for_tests(
|
|||
auth_repository,
|
|||
|
|
candidate_selection_repository,
|
||
|
|
provider_catalog_repository,
|
||
|
|
Arc::clone(&request_candidate_repository),
|
||
Arc::clone(&usage_repository),
|
|||
DEVELOPMENT_ENCRYPTION_KEY,
|
|||
)
|
|||
|
|
.with_system_config_values_for_tests([(
|
||
|
|
"request_record_level".to_string(),
|
||
|
|
json!("base"),
|
||
|
|
)]),
|
||
)
|
|||
.with_oauth_refresh_coordinator_for_tests(oauth_refresh)
|
|||
|
|
.with_usage_runtime_for_tests(UsageRuntimeConfig {
|
||
|
|
enabled: true,
|
||
|
|
..UsageRuntimeConfig::default()
|
||
|
|
});
|
||
let gateway = build_router_with_state(gateway_state);
|
|||
|
|
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
||
|
|
|
||
|
|
let response = reqwest::Client::new()
|
||
|
|
.post(format!("{gateway_url}/v1/responses"))
|
||
|
|
.header(http::header::CONTENT_TYPE, "application/json")
|
||
|
|
.header(
|
||
|
|
http::header::AUTHORIZATION,
|
||
|
|
format!("Bearer {client_api_key}"),
|
||
|
|
)
|
||
|
|
.header(TRACE_ID_HEADER, "trace-codex-cli-stream-local-123")
|
||
|
|
.body("{\"model\":\"gpt-5.4\",\"input\":\"hello\",\"stream\":true}")
|
||
|
|
.send()
|
||
|
|
.await
|
||
|
|
.expect("request should succeed");
|
||
|
|
|
||
|
|
assert_eq!(response.status(), StatusCode::OK);
|
||
let response_body =
|
|||
|
|
strip_sse_keepalive_comments(&response.text().await.expect("body should read"));
|
||
|
|
let data_line = response_body
|
||
|
|
.lines()
|
||
|
|
.find_map(|line| line.strip_prefix("data: "))
|
||
|
|
.expect("completed event data should exist");
|
||
|
|
let completed_event: serde_json::Value =
|
||
|
|
serde_json::from_str(data_line).expect("completed event should parse");
|
||
|
|
let created_at = completed_event["response"]["created_at"]
|
||
|
|
.as_i64()
|
||
|
|
.expect("created_at should be a unix timestamp");
|
||
|
|
|
||
assert_eq!(
|
|||
completed_event,
|
|||
|
|
json!({
|
||
|
|
"type": "response.completed",
|
||
|
|
"response": {
|
||
|
|
"id": "resp_codex_cli_stream_local_123",
|
||
|
|
"object": "response",
|
||
|
|
"model": "gpt-5.4",
|
||
|
|
"status": "completed",
|
||
|
|
"usage": {
|
||
|
|
"input_tokens": 1,
|
||
|
|
"output_tokens": 2,
|
||
|
|
"total_tokens": 3
|
||
|
|
},
|
||
|
|
"output": [],
|
||
|
|
"created_at": created_at,
|
||
|
|
"completed_at": created_at,
|
||
|
|
"output_text": ""
|
||
|
|
}
|
||
|
|
})
|
||
);
|
|||
|
|
|
||
|
|
let seen_refresh_request = seen_refresh
|
||
|
|
.lock()
|
||
|
|
.expect("mutex should lock")
|
||
|
|
.clone()
|
||
|
|
.expect("refresh request should be captured");
|
||
|
|
assert_eq!(
|
||
|
|
seen_refresh_request.content_type,
|
||
|
|
"application/x-www-form-urlencoded"
|
||
|
|
);
|
||
|
|
assert!(seen_refresh_request
|
||
|
|
.body
|
||
|
|
.contains("grant_type=refresh_token"));
|
||
|
|
assert!(seen_refresh_request
|
||
|
|
.body
|
||
|
|
.contains("client_id=app_EMoamEEZ73f0CkXaXp7hrann"));
|
||
|
|
assert!(seen_refresh_request
|
||
|
|
.body
|
||
|
|
.contains("refresh_token=rt-codex-stream-local-123"));
|
||
|
|
|
||
|
|
let seen_execution_runtime_request = seen_execution_runtime
|
||
|
|
.lock()
|
||
|
|
.expect("mutex should lock")
|
||
|
|
.clone()
|
||
|
|
.expect("execution runtime stream should be captured");
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.trace_id,
|
||
|
|
"trace-codex-cli-stream-local-123"
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.url,
|
||
|
|
"https://chatgpt.com/backend-api/codex/responses"
|
||
|
|
);
|
||
|
|
assert_eq!(seen_execution_runtime_request.model, "gpt-5.4");
|
||
|
|
assert!(seen_execution_runtime_request.stream);
|
||
|
|
assert_eq!(seen_execution_runtime_request.accept, "text/event-stream");
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.authorization,
|
||
|
|
"Bearer refreshed-codex-stream-access-token"
|
||
|
|
);
|
||
assert_eq!(
|
|||
|
|
seen_execution_runtime_request.x_client_request_id,
|
||
|
|
"trace-codex-cli-stream-local-123"
|
||
|
|
);
|
||
|
|||
|
|
let stored_candidates = request_candidate_repository
|
||
|
|
.list_by_request_id("trace-codex-cli-stream-local-123")
|
||
|
|
.await
|
||
|
|
.expect("request candidate trace should read");
|
||
|
|
assert_eq!(stored_candidates.len(), 1);
|
||
|
|
assert_eq!(stored_candidates[0].status, RequestCandidateStatus::Success);
|
||
|
|
|
||
let mut stored_usage = None;
|
|||
|
|
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(60);
|
||
|
|
loop {
|
||
|
|
stored_usage = usage_repository
|
||
|
|
.find_by_request_id("trace-codex-cli-stream-local-123")
|
||
|
|
.await
|
||
|
|
.expect("usage lookup should succeed");
|
||
|
|
if stored_usage
|
||
|
|
.as_ref()
|
||
|
|
.is_some_and(|usage| usage.status == "completed")
|
||
|
|
{
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
assert!(
|
||
|
|
tokio::time::Instant::now() < deadline,
|
||
|
|
"usage should reach completed status"
|
||
|
|
);
|
||
|
|
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||
|
|
}
|
||
|
|
let stored_usage = stored_usage.expect("usage should be recorded");
|
||
assert_eq!(stored_usage.input_tokens, 1);
|
|||
|
|
assert_eq!(stored_usage.output_tokens, 2);
|
||
assert_eq!(stored_usage.total_tokens, 3);
|
|||
|
|
assert!(stored_usage.request_body.is_none());
|
||
|
|
assert!(stored_usage.provider_request_body.is_none());
|
||
|
|
assert!(stored_usage.response_body.is_none());
|
||
|
|
assert!(stored_usage.client_response_body.is_none());
|
||
|
|
|
||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
|||
|
|
assert!(
|
||
|
|
seen_report.lock().expect("mutex should lock").is_none(),
|
||
|
|
"report-stream should stay local when request candidate persistence is available"
|
||
|
|
);
|
||
|
|
|
||
|
|
assert_eq!(*decision_hits.lock().expect("mutex should lock"), 0);
|
||
|
|
assert_eq!(*plan_hits.lock().expect("mutex should lock"), 0);
|
||
|
|
assert_eq!(*public_hits.lock().expect("mutex should lock"), 0);
|
||
|
|
|
||
|
|
gateway_handle.abort();
|
||
|
|
execution_runtime_handle.abort();
|
||
|
|
refresh_handle.abort();
|
||
|
|
upstream_handle.abort();
|
||
|
|
}
|