mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-07 18:07:47 +08:00
744 lines
31 KiB
Rust
744 lines
31 KiB
Rust
use super::{
|
|||
|
|
any, build_router_with_state, build_state_with_execution_runtime_override, json, start_server,
|
||
|
|
to_bytes, Arc, Body, Json, Mutex, Request, Router, StatusCode, TRACE_ID_HEADER,
|
||
|
|
};
|
||
use crate::ai_serving::CODEX_OPENAI_IMAGE_INTERNAL_MODEL;
|
|||
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_contracts::repository::candidate_selection::{
|
||
|
|
StoredMinimalCandidateSelectionRow, StoredProviderModelMapping,
|
||
|
|
};
|
||
|
|
use aether_data_contracts::repository::provider_catalog::{
|
||
|
|
StoredProviderCatalogEndpoint, StoredProviderCatalogKey, StoredProviderCatalogProvider,
|
||
|
|
};
|
||
|
|
use sha2::{Digest, Sha256};
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn gateway_executes_codex_image_stream_via_local_decision_gate_after_oauth_refresh() {
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
struct SeenExecutionRuntimeStreamRequest {
|
||
|
|
trace_id: String,
|
||
|
|
url: String,
|
||
|
|
model: String,
|
||
|
|
authorization: String,
|
||
|
|
x_client_request_id: String,
|
||
|
|
tool_type: String,
|
||
tool_action: String,
|
|||
tool_partial_images: Option<u64>,
|
|||
|
|
request_stream: bool,
|
||
|
|
plan_stream: bool,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[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:image"])),
|
||
|
|
Some(serde_json::json!(["gpt-image-2"])),
|
||
|
|
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:image"])),
|
||
|
|
Some(serde_json::json!(["gpt-image-2"])),
|
||
|
|
)
|
||
|
|
.expect("auth snapshot should build")
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_candidate_row() -> StoredMinimalCandidateSelectionRow {
|
||
|
|
StoredMinimalCandidateSelectionRow {
|
||
|
|
provider_id: "provider-codex-image-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-image-stream-local-1".to_string(),
|
||
|
|
endpoint_api_format: "openai:image".to_string(),
|
||
|
|
endpoint_api_family: Some("openai".to_string()),
|
||
|
|
endpoint_kind: Some("image".to_string()),
|
||
|
|
endpoint_is_active: true,
|
||
|
|
key_id: "key-codex-image-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:image".to_string()]),
|
||
|
|
key_allowed_models: None,
|
||
|
|
key_capabilities: None,
|
||
|
|
key_internal_priority: 5,
|
||
|
|
key_global_priority_by_format: Some(serde_json::json!({"openai:image": 1})),
|
||
|
|
model_id: "model-codex-image-stream-local-1".to_string(),
|
||
|
|
global_model_id: "global-model-codex-image-stream-local-1".to_string(),
|
||
|
|
global_model_name: "gpt-image-2".to_string(),
|
||
|
|
global_model_mappings: None,
|
||
|
|
global_model_supports_streaming: Some(true),
|
||
|
|
model_provider_model_name: "gpt-image-2".to_string(),
|
||
|
|
model_provider_model_mappings: Some(vec![StoredProviderModelMapping {
|
||
|
|
name: "gpt-image-2".to_string(),
|
||
|
|
priority: 1,
|
||
|
|
api_formats: Some(vec!["openai:image".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-image-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-image-stream-local-1".to_string(),
|
||
|
|
"provider-codex-image-stream-local-1".to_string(),
|
||
|
|
"openai:image".to_string(),
|
||
|
|
Some("openai".to_string()),
|
||
|
|
Some("image".to_string()),
|
||
|
|
true,
|
||
|
|
)
|
||
|
|
.expect("endpoint should build")
|
||
|
|
.with_transport_fields(
|
||
|
|
"https://chatgpt.com/backend-api/codex".to_string(),
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
Some(2),
|
||
|
|
None,
|
||
|
|
Some(serde_json::json!({"upstream_stream_policy":"force_stream"})),
|
||
|
|
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-image-stream-local-123"}"#,
|
||
|
|
)
|
||
|
|
.expect("auth config should encrypt");
|
||
|
|
StoredProviderCatalogKey::new(
|
||
|
|
"key-codex-image-stream-local-1".to_string(),
|
||
|
|
"provider-codex-image-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:image"])),
|
||
|
|
encrypt_python_fernet_plaintext(DEVELOPMENT_ENCRYPTION_KEY, "__placeholder__")
|
||
|
|
.expect("placeholder api key should encrypt"),
|
||
|
|
Some(encrypted_auth_config),
|
||
|
|
None,
|
||
|
|
Some(serde_json::json!({"openai:image": 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_refresh = Arc::new(Mutex::new(None::<SeenRefreshRequest>));
|
||
|
|
let seen_refresh_clone = Arc::clone(&seen_refresh);
|
||
|
|
|
||
|
|
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-image-stream-access-token",
|
||
|
|
"refresh_token": "rt-codex-image-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(),
|
||
|
|
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(),
|
||
|
|
tool_type: payload
|
||
|
|
.get("body")
|
||
|
|
.and_then(|value| value.get("json_body"))
|
||
|
|
.and_then(|value| value.get("tools"))
|
||
|
|
.and_then(|value| value.get(0))
|
||
|
|
.and_then(|value| value.get("type"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
tool_action: payload
|
|||
|
|
.get("body")
|
||
|
|
.and_then(|value| value.get("json_body"))
|
||
|
|
.and_then(|value| value.get("tools"))
|
||
|
|
.and_then(|value| value.get(0))
|
||
|
|
.and_then(|value| value.get("action"))
|
||
|
|
.and_then(|value| value.as_str())
|
||
|
|
.unwrap_or_default()
|
||
|
|
.to_string(),
|
||
tool_partial_images: payload
|
|||
|
|
.get("body")
|
||
|
|
.and_then(|value| value.get("json_body"))
|
||
|
|
.and_then(|value| value.get("tools"))
|
||
|
|
.and_then(|value| value.get(0))
|
||
|
|
.and_then(|value| value.get("partial_images"))
|
||
|
|
.and_then(|value| value.as_u64()),
|
||
|
|
request_stream: payload
|
||
|
|
.get("body")
|
||
|
|
.and_then(|value| value.get("json_body"))
|
||
|
|
.and_then(|value| value.get("stream"))
|
||
|
|
.and_then(|value| value.as_bool())
|
||
|
|
.unwrap_or(false),
|
||
|
|
plan_stream: payload
|
||
|
|
.get("stream")
|
||
|
|
.and_then(|value| value.as_bool())
|
||
|
|
.unwrap_or(false),
|
||
|
|
});
|
||
|
|
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.output_item.done\\ndata: {\\\"type\\\":\\\"response.output_item.done\\\",\\\"output_index\\\":0,\\\"item\\\":{\\\"id\\\":\\\"ig_123\\\",\\\"type\\\":\\\"image_generation_call\\\",\\\"result\\\":\\\"aGVsbG8=\\\"}}\\n\\n\"}}\n",
|
||
|
|
"{\"type\":\"data\",\"payload\":{\"kind\":\"data\",\"text\":\"event: response.completed\\ndata: {\\\"type\\\":\\\"response.completed\\\",\\\"response\\\":{\\\"tool_usage\\\":{\\\"image_gen\\\":{\\\"input_tokens\\\":11,\\\"output_tokens\\\":22,\\\"total_tokens\\\":33}}}}\\n\\n\"}}\n",
|
||
|
|
"{\"type\":\"telemetry\",\"payload\":{\"kind\":\"telemetry\",\"telemetry\":{\"elapsed_ms\":41}}}\n",
|
||
|
|
"{\"type\":\"eof\",\"payload\":{\"kind\":\"eof\"}}\n"
|
||
|
|
);
|
||
|
|
let mut response = http::Response::builder()
|
||
|
|
.status(StatusCode::OK)
|
||
|
|
.body(Body::from(frames))
|
||
|
|
.expect("response should build");
|
||
|
|
response.headers_mut().insert(
|
||
|
|
http::header::CONTENT_TYPE,
|
||
|
|
http::HeaderValue::from_static("application/x-ndjson"),
|
||
|
|
);
|
||
|
|
response
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
let client_api_key = "sk-client-codex-image-stream-local";
|
||
|
|
let auth_repository = Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![(
|
||
|
|
Some(hash_api_key(client_api_key)),
|
||
|
|
sample_auth_snapshot(
|
||
|
|
"key-codex-image-stream-client-123",
|
||
|
|
"user-codex-image-stream-client-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 (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_and_request_candidate_repository_for_tests(
|
||
|
|
auth_repository,
|
||
|
|
candidate_selection_repository,
|
||
|
|
provider_catalog_repository,
|
||
|
|
Arc::new(InMemoryRequestCandidateRepository::default()),
|
||
|
|
DEVELOPMENT_ENCRYPTION_KEY,
|
||
|
|
),
|
||
|
|
)
|
||
|
|
.with_oauth_refresh_coordinator_for_tests(oauth_refresh);
|
||
|
|
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/images/generations"))
|
||
|
|
.header(http::header::CONTENT_TYPE, "application/json")
|
||
|
|
.header(
|
||
|
|
http::header::AUTHORIZATION,
|
||
|
|
format!("Bearer {client_api_key}"),
|
||
|
|
)
|
||
|
|
.header(TRACE_ID_HEADER, "trace-codex-image-stream-local-123")
|
||
|
|
.body(
|
||
|
|
"{\"model\":\"gpt-image-2\",\"prompt\":\"生成一张中国历史视觉海报\",\"stream\":true,\"partial_images\":1}",
|
||
|
|
)
|
||
|
|
.send()
|
||
|
|
.await
|
||
|
|
.expect("request should succeed");
|
||
|
|
|
||
|
|
assert_eq!(response.status(), StatusCode::OK);
|
||
|
|
assert_eq!(
|
||
|
|
response
|
||
|
|
.headers()
|
||
|
|
.get(http::header::CONTENT_TYPE)
|
||
|
|
.and_then(|value| value.to_str().ok()),
|
||
|
|
Some("text/event-stream")
|
||
|
|
);
|
||
|
|
let response_text = response.text().await.expect("body should read");
|
||
|
|
assert!(response_text.contains("event: image_generation.partial_image"));
|
||
|
|
assert!(response_text.contains("\"type\":\"image_generation.partial_image\""));
|
||
|
|
assert!(response_text.contains("\"b64_json\":\"aGVsbG8=\""));
|
||
|
|
assert!(response_text.contains("event: image_generation.completed"));
|
||
|
|
assert!(response_text.contains("\"type\":\"image_generation.completed\""));
|
||
|
|
assert!(response_text.contains("\"total_tokens\":33"));
|
||
|
|
assert!(!response_text.contains("response.completed"));
|
||
|
|
|
||
|
|
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("refresh_token=rt-codex-image-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-image-stream-local-123"
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.url,
|
||
|
|
"https://chatgpt.com/backend-api/codex/responses"
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.model,
|
||
|
|
CODEX_OPENAI_IMAGE_INTERNAL_MODEL
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.authorization,
|
||
|
|
"Bearer refreshed-codex-image-stream-access-token"
|
||
|
|
);
|
||
|
|
assert_eq!(
|
||
|
|
seen_execution_runtime_request.x_client_request_id,
|
||
|
|
"trace-codex-image-stream-local-123"
|
||
|
|
);
|
||
|
|
assert_eq!(seen_execution_runtime_request.tool_type, "image_generation");
|
||
assert_eq!(seen_execution_runtime_request.tool_action, "generate");
|
|||
assert_eq!(seen_execution_runtime_request.tool_partial_images, Some(1));
|
|||
|
|
assert!(seen_execution_runtime_request.request_stream);
|
||
|
|
assert!(seen_execution_runtime_request.plan_stream);
|
||
|
|
|
||
|
|
gateway_handle.abort();
|
||
|
|
execution_runtime_handle.abort();
|
||
|
|
refresh_handle.abort();
|
||
|
|
}
|
||
|
|||
|
|
#[tokio::test]
|
||
|
|
async fn gateway_bridges_codex_image_sync_json_to_streaming_image_sse() {
|
||
|
|
#[derive(Debug, Clone)]
|
||
|
|
struct SeenExecutionRuntimeStreamRequest {
|
||
|
|
trace_id: String,
|
||
|
|
request_stream: bool,
|
||
|
|
plan_stream: bool,
|
||
|
|
}
|
||
|
|
|
||
|
|
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:image"])),
|
||
|
|
Some(serde_json::json!(["gpt-image-2"])),
|
||
|
|
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:image"])),
|
||
|
|
Some(serde_json::json!(["gpt-image-2"])),
|
||
|
|
)
|
||
|
|
.expect("auth snapshot should build")
|
||
|
|
}
|
||
|
|
|
||
|
|
fn sample_candidate_row() -> StoredMinimalCandidateSelectionRow {
|
||
|
|
StoredMinimalCandidateSelectionRow {
|
||
|
|
provider_id: "provider-codex-image-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-image-stream-local-1".to_string(),
|
||
|
|
endpoint_api_format: "openai:image".to_string(),
|
||
|
|
endpoint_api_family: Some("openai".to_string()),
|
||
|
|
endpoint_kind: Some("image".to_string()),
|
||
|
|
endpoint_is_active: true,
|
||
|
|
key_id: "key-codex-image-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:image".to_string()]),
|
||
|
|
key_allowed_models: None,
|
||
|
|
key_capabilities: None,
|
||
|
|
key_internal_priority: 5,
|
||
|
|
key_global_priority_by_format: Some(serde_json::json!({"openai:image": 1})),
|
||
|
|
model_id: "model-codex-image-stream-local-1".to_string(),
|
||
|
|
global_model_id: "global-model-codex-image-stream-local-1".to_string(),
|
||
|
|
global_model_name: "gpt-image-2".to_string(),
|
||
|
|
global_model_mappings: None,
|
||
|
|
global_model_supports_streaming: Some(true),
|
||
|
|
model_provider_model_name: "gpt-image-2".to_string(),
|
||
|
|
model_provider_model_mappings: Some(vec![StoredProviderModelMapping {
|
||
|
|
name: "gpt-image-2".to_string(),
|
||
|
|
priority: 1,
|
||
|
|
api_formats: Some(vec!["openai:image".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-image-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-image-stream-local-1".to_string(),
|
||
|
|
"provider-codex-image-stream-local-1".to_string(),
|
||
|
|
"openai:image".to_string(),
|
||
|
|
Some("openai".to_string()),
|
||
|
|
Some("image".to_string()),
|
||
|
|
true,
|
||
|
|
)
|
||
|
|
.expect("endpoint should build")
|
||
|
|
.with_transport_fields(
|
||
|
|
"https://chatgpt.com/backend-api/codex".to_string(),
|
||
|
|
None,
|
||
|
|
None,
|
||
|
|
Some(2),
|
||
|
|
None,
|
||
|
|
Some(serde_json::json!({"upstream_stream_policy":"force_stream"})),
|
||
|
|
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-image-stream-local-123"}"#,
|
||
|
|
)
|
||
|
|
.expect("auth config should encrypt");
|
||
|
|
StoredProviderCatalogKey::new(
|
||
|
|
"key-codex-image-stream-local-1".to_string(),
|
||
|
|
"provider-codex-image-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:image"])),
|
||
|
|
encrypt_python_fernet_plaintext(DEVELOPMENT_ENCRYPTION_KEY, "__placeholder__")
|
||
|
|
.expect("placeholder api key should encrypt"),
|
||
|
|
Some(encrypted_auth_config),
|
||
|
|
None,
|
||
|
|
Some(serde_json::json!({"openai:image": 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 refresh = Router::new().route(
|
||
|
|
"/oauth/token",
|
||
|
|
any(move |_request: Request| async move {
|
||
|
|
Json(json!({
|
||
|
|
"access_token": "refreshed-codex-image-stream-access-token",
|
||
|
|
"refresh_token": "rt-codex-image-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(),
|
||
|
|
request_stream: payload
|
||
|
|
.get("body")
|
||
|
|
.and_then(|value| value.get("json_body"))
|
||
|
|
.and_then(|value| value.get("stream"))
|
||
|
|
.and_then(|value| value.as_bool())
|
||
|
|
.unwrap_or(false),
|
||
|
|
plan_stream: payload
|
||
|
|
.get("stream")
|
||
|
|
.and_then(|value| value.as_bool())
|
||
|
|
.unwrap_or(false),
|
||
|
|
});
|
||
|
|
let frames = concat!(
|
||
|
|
"{\"type\":\"headers\",\"payload\":{\"kind\":\"headers\",\"status_code\":200,\"headers\":{\"content-type\":\"application/json\"}}}\n",
|
||
|
|
"{\"type\":\"data\",\"payload\":{\"kind\":\"data\",\"text\":\"{\\\"created\\\":1776991097,\\\"data\\\":[{\\\"b64_json\\\":\\\"aGVsbG8=\\\"}],\\\"usage\\\":{\\\"total_tokens\\\":100,\\\"input_tokens\\\":50,\\\"output_tokens\\\":50,\\\"input_tokens_details\\\":{\\\"text_tokens\\\":10,\\\"image_tokens\\\":40}}}\"}}\n",
|
||
|
|
"{\"type\":\"telemetry\",\"payload\":{\"kind\":\"telemetry\",\"telemetry\":{\"elapsed_ms\":41}}}\n",
|
||
|
|
"{\"type\":\"eof\",\"payload\":{\"kind\":\"eof\"}}\n"
|
||
|
|
);
|
||
|
|
let mut response = http::Response::builder()
|
||
|
|
.status(StatusCode::OK)
|
||
|
|
.body(Body::from(frames))
|
||
|
|
.expect("response should build");
|
||
|
|
response.headers_mut().insert(
|
||
|
|
http::header::CONTENT_TYPE,
|
||
|
|
http::HeaderValue::from_static("application/x-ndjson"),
|
||
|
|
);
|
||
|
|
response
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
let client_api_key = "sk-client-codex-image-stream-local";
|
||
|
|
let auth_repository = Arc::new(InMemoryAuthApiKeySnapshotRepository::seed(vec![(
|
||
|
|
Some(hash_api_key(client_api_key)),
|
||
|
|
sample_auth_snapshot(
|
||
|
|
"key-codex-image-stream-client-123",
|
||
|
|
"user-codex-image-stream-client-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 (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_and_request_candidate_repository_for_tests(
|
||
|
|
auth_repository,
|
||
|
|
candidate_selection_repository,
|
||
|
|
provider_catalog_repository,
|
||
|
|
Arc::new(InMemoryRequestCandidateRepository::default()),
|
||
|
|
DEVELOPMENT_ENCRYPTION_KEY,
|
||
|
|
),
|
||
|
|
)
|
||
|
|
.with_oauth_refresh_coordinator_for_tests(oauth_refresh);
|
||
|
|
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/images/generations"))
|
||
|
|
.header(http::header::CONTENT_TYPE, "application/json")
|
||
|
|
.header(
|
||
|
|
http::header::AUTHORIZATION,
|
||
|
|
format!("Bearer {client_api_key}"),
|
||
|
|
)
|
||
|
|
.header(TRACE_ID_HEADER, "trace-codex-image-stream-json-123")
|
||
|
|
.body("{\"model\":\"gpt-image-2\",\"prompt\":\"生成一张海报\",\"stream\":true}")
|
||
|
|
.send()
|
||
|
|
.await
|
||
|
|
.expect("request should succeed");
|
||
|
|
|
||
|
|
assert_eq!(response.status(), StatusCode::OK);
|
||
|
|
assert_eq!(
|
||
|
|
response
|
||
|
|
.headers()
|
||
|
|
.get(http::header::CONTENT_TYPE)
|
||
|
|
.and_then(|value| value.to_str().ok()),
|
||
|
|
Some("text/event-stream")
|
||
|
|
);
|
||
|
|
let response_text = response.text().await.expect("body should read");
|
||
|
|
assert!(response_text.contains("event: image_generation.completed"));
|
||
|
|
assert!(response_text.contains("\"type\":\"image_generation.completed\""));
|
||
|
|
assert!(response_text.contains("\"b64_json\":\"aGVsbG8=\""));
|
||
|
|
assert!(response_text.contains("\"total_tokens\":100"));
|
||
|
|
assert!(!response_text.trim_start().starts_with('{'));
|
||
|
|
assert!(!response_text.contains("\"created\": 1776991097"));
|
||
|
|
|
||
|
|
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-image-stream-json-123"
|
||
|
|
);
|
||
|
|
assert!(seen_execution_runtime_request.request_stream);
|
||
|
|
assert!(seen_execution_runtime_request.plan_stream);
|
||
|
|
|
||
|
|
gateway_handle.abort();
|
||
|
|
execution_runtime_handle.abort();
|
||
|
|
refresh_handle.abort();
|
||
|
|
}
|