diff --git a/src-tauri/src/proxy/forwarder.rs b/src-tauri/src/proxy/forwarder.rs index 72e681996..44ff4584a 100644 --- a/src-tauri/src/proxy/forwarder.rs +++ b/src-tauri/src/proxy/forwarder.rs @@ -2146,17 +2146,17 @@ impl RequestForwarder { )?; } - // Codex 客户端私有的 `include` 标记只对官方 ChatGPT 后端有效:第三方与本地原生 - // Responses 网关收到它会整条 400(#9042)。 - if should_drop_codex_private_include_markers( + // Codex 以为在跟内置 openai 说话时才发的字段(ChatGPT 后端私有):第三方与本地原生 + // Responses 网关收到会整条 400(#9042),去掉后和 Codex 对自定义表发的一样。 + if should_strip_codex_openai_only_fields( app_type, provider, codex_responses_to_chat, codex_responses_to_anthropic, - ) && super::providers::strip_codex_private_include_markers(&mut request_body) + ) && super::providers::strip_codex_openai_only_request_fields(&mut request_body) { log::debug!( - "[Codex] Dropped Codex-private include markers for a third-party upstream (provider={})", + "[Codex] Dropped OpenAI-only request fields for a third-party upstream (provider={})", provider.id ); } @@ -3871,10 +3871,12 @@ fn is_chatgpt_identity_header(key_str: &str) -> bool { || key_str.starts_with("x-responsesapi-") } -/// Codex 客户端私有的 `include` 标记(`output.encrypted_content`)只对官方 ChatGPT 后端 -/// 有效。第三方与本地原生 Responses 网关收到它会把整条请求 400 掉(#9042)。转 Chat / -/// Anthropic 的路径本来就不带它,官方那条路逐字节不变。 -fn should_drop_codex_private_include_markers( +/// Codex 只对内置 openai 发的字段(`output.encrypted_content`、 +/// `internal_chat_message_metadata_passthrough` 等,见 +/// `strip_codex_openai_only_request_fields`)只有 ChatGPT 后端认。第三方与本地原生 +/// Responses 网关收到会把整条请求 400 掉(#9042)。官方卡和 ChatGPT 托管账号那条路逐字节 +/// 不变;转 Chat / Anthropic 的路径重新组装请求,本来就不带这些字段。 +fn should_strip_codex_openai_only_fields( app_type: &AppType, provider: &Provider, codex_responses_to_chat: bool, @@ -3884,6 +3886,7 @@ fn should_drop_codex_private_include_markers( && !codex_responses_to_chat && !codex_responses_to_anthropic && !super::providers::is_codex_official_provider(provider) + && !provider.is_codex_oauth() } fn codex_anthropic_error_envelope_message(body: &[u8]) -> Option { @@ -8008,10 +8011,86 @@ mod tests { assert!(seen.body.get("include").is_none(), "{}", seen.body); } - /// 官方 ChatGPT 后端认这个标记;转 Chat / Anthropic 的路径本来就不带它。三条都跳过。 + /// 第三方路由写成内置 openai 后,Codex 把只发给 OpenAI 的字段也带进第三方请求: + /// 条目上的 `internal_chat_message_metadata_passthrough`、非推理条目的密文、 + /// `configuration_update` 条目、`stream_options` 与 `access_programs`(#9042)。 + /// 去掉后应与 Codex 对自定义表发的一样,推理与压缩的密文、其余字段原样留着。 + #[tokio::test] + async fn third_party_requests_drop_openai_only_fields() { + let upstream = upstream().await; + let metadata = json!({ "turn_id": "t1" }); + let mut request = body("listed", json!([])); + request["input"] = json!([ + { + "type": "message", + "role": "user", + "content": [{ "type": "input_text", "text": "hi" }], + "internal_chat_message_metadata_passthrough": metadata + }, + { "type": "configuration_update", "reasoning": { "effort": "high" } }, + { + "type": "reasoning", + "summary": [], + "encrypted_content": "gAAAA-reasoning", + "internal_chat_message_metadata_passthrough": metadata + }, + { + "type": "message", + "role": "assistant", + "content": [{ "type": "output_text", "text": "ok" }], + "encrypted_content": "gAAAA-message" + }, + { + "type": "function_call", + "call_id": "c1", + "name": "spawn_agent", + "arguments": "{}", + "encrypted_content": "gAAAA-call", + "encrypted_function_args": ["message"] + }, + { "type": "function_call_output", "call_id": "c1", "output": "done" } + ]); + request["include"] = json!(["reasoning.encrypted_content", "output.encrypted_content"]); + request["stream_options"] = + json!({ "reasoning_summary_delivery": "sequential_cutoff" }); + request["access_programs"] = json!({ "cyber": "standard" }); + + let seen = send( + &forwarder(false), + &upstream, + provider(&upstream, "openai_responses"), + "/responses", + request, + ) + .await; + + assert_eq!(seen.body["include"], json!(["reasoning.encrypted_content"])); + assert!(seen.body.get("stream_options").is_none(), "{}", seen.body); + assert!(seen.body.get("access_programs").is_none(), "{}", seen.body); + assert_eq!( + seen.body["input"], + json!([ + { + "type": "message", + "role": "user", + "content": [{ "type": "input_text", "text": "hi" }] + }, + { "type": "reasoning", "summary": [], "encrypted_content": "gAAAA-reasoning" }, + { + "type": "message", + "role": "assistant", + "content": [{ "type": "output_text", "text": "ok" }] + }, + { "type": "function_call", "call_id": "c1", "name": "spawn_agent", "arguments": "{}" }, + { "type": "function_call_output", "call_id": "c1", "output": "done" } + ]) + ); + } + + /// 官方 ChatGPT 后端认这些字段;转 Chat / Anthropic 的路径本来就不带它们。都跳过。 /// 官方那条路实测只能打到 chatgpt.com,这里直接验门控。 #[test] - fn private_include_marker_gate_skips_official_and_converted_requests() { + fn openai_only_fields_gate_skips_official_and_converted_requests() { let third_party = provider( &Upstream { base_url: "https://relay.example.com".to_string(), @@ -8023,33 +8102,45 @@ mod tests { official.id = crate::database::CODEX_OFFICIAL_PROVIDER_ID.to_string(); official.category = Some("official".to_string()); - assert!(should_drop_codex_private_include_markers( + assert!(should_strip_codex_openai_only_fields( &AppType::Codex, &third_party, false, false )); - assert!(!should_drop_codex_private_include_markers( + assert!(!should_strip_codex_openai_only_fields( &AppType::Codex, &official, false, false )); + // ChatGPT 托管账号发往 ChatGPT 后端,同样认这些字段。 + let mut managed = third_party.clone(); + managed.meta = Some(crate::provider::ProviderMeta { + provider_type: Some("codex_oauth".to_string()), + ..Default::default() + }); + assert!(!should_strip_codex_openai_only_fields( + &AppType::Codex, + &managed, + false, + false + )); // 转 Chat / 转 Anthropic 的路径不带原生 Responses 字段。 - assert!(!should_drop_codex_private_include_markers( + assert!(!should_strip_codex_openai_only_fields( &AppType::Codex, &third_party, true, false )); - assert!(!should_drop_codex_private_include_markers( + assert!(!should_strip_codex_openai_only_fields( &AppType::Codex, &third_party, false, true )); // Grok Build 走 xAI 自己的清洗,不归这里管。 - assert!(!should_drop_codex_private_include_markers( + assert!(!should_strip_codex_openai_only_fields( &AppType::GrokBuild, &third_party, false, diff --git a/src-tauri/src/proxy/providers/codex.rs b/src-tauri/src/proxy/providers/codex.rs index a178f7590..e472b64c6 100644 --- a/src-tauri/src/proxy/providers/codex.rs +++ b/src-tauri/src/proxy/providers/codex.rs @@ -638,6 +638,91 @@ pub fn strip_codex_private_include_markers(body: &mut JsonValue) -> bool { changed } +/// Codex 对非 OpenAI provider 会清掉 `encrypted_content` 的条目类型:codex-rs +/// `ResponseItem::clear_encrypted_content` 覆盖的类型,去掉它另外放行的推理与压缩条目。 +/// 按正向名单照抄,Codex 以后新加的条目类型不会被误删。 +const CODEX_OPENAI_ONLY_ENCRYPTED_CONTENT_ITEM_TYPES: &[&str] = &[ + "message", + "local_shell_call", + "function_call", + "tool_search_call", + "custom_tool_call", + "web_search_call", + "image_generation_call", +]; + +/// 去掉 Codex 只在 provider 是内置 openai 时才发的请求内容,让第三方上游收到的请求和 +/// Codex 自己对非 OpenAI provider 发的一样。照 codex-rs `core/src/client.rs` +/// `build_responses_request` 里 `is_openai()`(`name == "OpenAI"`)的分支: +/// +/// - `include` 里的 `output.encrypted_content`(`output_token_replay` 只对 OpenAI 开); +/// - `stream_options.reasoning_summary_delivery`(同上,`stream_options` 只有这一个字段); +/// - `input` 里的 `configuration_update` 条目(推理档位更新只对 OpenAI 开,否则整条滤掉); +/// - 每个条目的 `internal_chat_message_metadata_passthrough`; +/// - 推理、压缩以外条目的 `encrypted_content`,以及 `function_call` 的 `encrypted_function_args`。 +/// +/// 另加顶层 `access_programs`:Codex 按登录方式而非 provider 决定发不发,但只有带着 +/// ChatGPT 登录才会出现,自定义表(第三方 Key)永远不发。 +/// +/// 第三方路由写成内置 openai(有 ChatGPT 登录时)、聚合模式经内置 openai 走第三方成员, +/// Codex 都以为自己在跟 OpenAI 说话,这些字段就会到达第三方,严格网关会整条 400 +/// (#9042)。每次删得一样,不影响缓存前缀;请求里没有这些字段时逐字节不变。返回是否 +/// 改了请求。 +pub fn strip_codex_openai_only_request_fields(body: &mut JsonValue) -> bool { + let mut changed = strip_codex_private_include_markers(body); + let Some(obj) = body.as_object_mut() else { + return changed; + }; + + let mut stream_options_gone = false; + if let Some(stream_options) = obj + .get_mut("stream_options") + .and_then(JsonValue::as_object_mut) + { + if stream_options + .remove("reasoning_summary_delivery") + .is_some() + { + changed = true; + stream_options_gone = stream_options.is_empty(); + } + } + if stream_options_gone { + obj.remove("stream_options"); + } + + changed |= obj.remove("access_programs").is_some(); + + if let Some(input) = obj.get_mut("input").and_then(JsonValue::as_array_mut) { + let before = input.len(); + input.retain(|item| { + item.get("type").and_then(JsonValue::as_str) != Some("configuration_update") + }); + changed |= input.len() != before; + + for item in input.iter_mut() { + let Some(item) = item.as_object_mut() else { + continue; + }; + changed |= item + .remove("internal_chat_message_metadata_passthrough") + .is_some(); + let item_type = item.get("type").and_then(JsonValue::as_str); + let drops_encrypted_content = item_type + .is_some_and(|t| CODEX_OPENAI_ONLY_ENCRYPTED_CONTENT_ITEM_TYPES.contains(&t)); + let is_function_call = item_type == Some("function_call"); + if drops_encrypted_content { + changed |= item.remove("encrypted_content").is_some(); + } + if is_function_call { + changed |= item.remove("encrypted_function_args").is_some(); + } + } + } + + changed +} + pub fn resolve_codex_chat_reasoning_config( provider: &Provider, body: &JsonValue, @@ -2746,4 +2831,85 @@ wire_api = "responses" assert!(!strip_codex_private_include_markers(&mut body)); assert_eq!(body["include"], json!(["reasoning.encrypted_content"])); } + + #[test] + fn strip_openai_only_fields_leaves_custom_table_requests_byte_identical() { + // 和 Codex 对自定义表发的请求一样:没有任何只发给 OpenAI 的字段,什么都不动。 + let mut body = json!({ + "model": "m", + "include": ["reasoning.encrypted_content"], + "input": [ + { "type": "message", "role": "user", "content": [] }, + { "type": "reasoning", "summary": [], "encrypted_content": "r" }, + { "type": "compaction", "encrypted_content": "c" }, + { "type": "context_compaction", "encrypted_content": "cc" } + ] + }); + let before = body.clone(); + + assert!(!strip_codex_openai_only_request_fields(&mut body)); + assert_eq!(body, before); + } + + #[test] + fn strip_openai_only_fields_keeps_other_stream_options() { + let mut body = json!({ + "stream_options": { "reasoning_summary_delivery": "sequential_cutoff", "include_usage": true } + }); + + assert!(strip_codex_openai_only_request_fields(&mut body)); + assert_eq!(body["stream_options"], json!({ "include_usage": true })); + } + + #[test] + fn strip_openai_only_fields_keeps_compaction_ciphertext() { + // 压缩条目的密文 Codex 对非 OpenAI 也照发,第三方压缩靠 `codex_compaction` 处理。 + let mut body = json!({ + "input": [ + { + "type": "compaction_summary", + "encrypted_content": "c", + "internal_chat_message_metadata_passthrough": { "turn_id": "t" } + }, + { "type": "web_search_call", "status": "completed", "encrypted_content": "w" } + ] + }); + + assert!(strip_codex_openai_only_request_fields(&mut body)); + assert_eq!( + body["input"], + json!([ + { "type": "compaction_summary", "encrypted_content": "c" }, + { "type": "web_search_call", "status": "completed" } + ]) + ); + } + + #[test] + fn strip_openai_only_fields_ignores_shapes_it_does_not_own() { + let mut body = json!(["output.encrypted_content"]); + assert!(!strip_codex_openai_only_request_fields(&mut body)); + + // input 不是数组、条目不是对象:不动。 + let mut body = json!({ "input": "hi", "stream_options": "x" }); + assert!(!strip_codex_openai_only_request_fields(&mut body)); + let mut body = json!({ "input": ["hi", 1] }); + assert!(!strip_codex_openai_only_request_fields(&mut body)); + assert_eq!(body["input"], json!(["hi", 1])); + } + + #[test] + fn strip_openai_only_fields_is_idempotent() { + let mut body = json!({ + "include": ["output.encrypted_content"], + "access_programs": { "cyber": "standard" }, + "input": [{ "type": "configuration_update", "reasoning": { "effort": "high" } }] + }); + + assert!(strip_codex_openai_only_request_fields(&mut body)); + let once = body.clone(); + assert!(!strip_codex_openai_only_request_fields(&mut body)); + assert_eq!(body, once); + assert_eq!(body, json!({ "input": [] })); + } } diff --git a/src-tauri/src/proxy/providers/mod.rs b/src-tauri/src/proxy/providers/mod.rs index 1e63d6adf..4657d330e 100644 --- a/src-tauri/src/proxy/providers/mod.rs +++ b/src-tauri/src/proxy/providers/mod.rs @@ -72,7 +72,7 @@ pub use codex::{ provider_needs_responses_late_arguments_repair, provider_needs_responses_namespace_flatten, resolve_codex_catalog_tool_profile, resolve_codex_chat_reasoning_config, should_convert_codex_responses_to_anthropic, should_convert_codex_responses_to_chat, - strip_codex_hosted_web_search, strip_codex_private_include_markers, + strip_codex_hosted_web_search, strip_codex_openai_only_request_fields, }; pub use gemini::GeminiAdapter;