From b6bacd6b4a78849f07cf439fd9f59b06dad20699 Mon Sep 17 00:00:00 2001 From: YoVinchen Date: Sat, 11 Apr 2026 16:46:41 +0800 Subject: [PATCH] Stop sending prompt cache keys on Claude chat conversions Responses conversions still use promptCacheKey, but chat completions now stay a pure shape transform. This keeps Claude -> chat requests aligned with providers that do not understand the field and keeps stream checks consistent with production behavior. Constraint: Issue #1919 requires removing prompt_cache_key from Claude -> OpenAI Chat requests Rejected: Add a runtime toggle for chat injection | requested behavior is unconditional removal Confidence: high Scope-risk: narrow Reversibility: clean Directive: Keep promptCacheKey limited to Claude -> Responses conversions unless a provider-specific contract is proven Tested: cargo test anthropic_to_openai Tested: cargo test anthropic_to_responses_with_cache_key Tested: cargo test transform_claude_request_for_api_format_responses Not-tested: Full src-tauri test suite Related: #1919 --- src-tauri/src/provider.rs | 6 +-- src-tauri/src/proxy/providers/claude.rs | 13 +++-- src-tauri/src/proxy/providers/transform.rs | 59 ++++++++-------------- src-tauri/src/services/stream_check.rs | 2 +- src/types.ts | 2 +- 5 files changed, 31 insertions(+), 51 deletions(-) diff --git a/src-tauri/src/provider.rs b/src-tauri/src/provider.rs index 67616853c..4170dd3f9 100644 --- a/src-tauri/src/provider.rs +++ b/src-tauri/src/provider.rs @@ -282,9 +282,9 @@ pub struct ProviderMeta { /// 是否将 base_url 视为完整 API 端点(不拼接 endpoint 路径) #[serde(rename = "isFullUrl", skip_serializing_if = "Option::is_none")] pub is_full_url: Option, - /// Prompt cache key for OpenAI-compatible endpoints. - /// When set, injected into converted requests to improve cache hit rate. - /// If not set, provider ID is used automatically during format conversion. + /// Prompt cache key for OpenAI Responses-compatible endpoints. + /// When set, injected into converted Responses requests to improve cache hit rate. + /// If not set, provider ID is used automatically during Claude -> Responses conversion. #[serde(rename = "promptCacheKey", skip_serializing_if = "Option::is_none")] pub prompt_cache_key: Option, /// 累加模式应用中,该 provider 是否已写入 live config。 diff --git a/src-tauri/src/proxy/providers/claude.rs b/src-tauri/src/proxy/providers/claude.rs index 52689d3b5..aa9949995 100644 --- a/src-tauri/src/proxy/providers/claude.rs +++ b/src-tauri/src/proxy/providers/claude.rs @@ -81,14 +81,13 @@ pub fn transform_claude_request_for_api_format( provider: &Provider, api_format: &str, ) -> Result { - let cache_key = provider - .meta - .as_ref() - .and_then(|m| m.prompt_cache_key.as_deref()) - .unwrap_or(&provider.id); - match api_format { "openai_responses" => { + let cache_key = provider + .meta + .as_ref() + .and_then(|m| m.prompt_cache_key.as_deref()) + .unwrap_or(&provider.id); // Codex OAuth (ChatGPT Plus/Pro 反代) 需要在请求体里强制 store: false // + include: ["reasoning.encrypted_content"],由 transform 层统一处理。 let is_codex_oauth = provider @@ -102,7 +101,7 @@ pub fn transform_claude_request_for_api_format( is_codex_oauth, ) } - "openai_chat" => super::transform::anthropic_to_openai(body, Some(cache_key)), + "openai_chat" => super::transform::anthropic_to_openai(body), _ => Ok(body), } } diff --git a/src-tauri/src/proxy/providers/transform.rs b/src-tauri/src/proxy/providers/transform.rs index 7380fb860..a41d18501 100644 --- a/src-tauri/src/proxy/providers/transform.rs +++ b/src-tauri/src/proxy/providers/transform.rs @@ -71,10 +71,8 @@ pub fn resolve_reasoning_effort(body: &Value) -> Option<&'static str> { } } -/// Anthropic 请求 → OpenAI 请求 -/// -/// `cache_key`: optional prompt_cache_key to inject for improved cache routing -pub fn anthropic_to_openai(body: Value, cache_key: Option<&str>) -> Result { +/// Anthropic 请求 → OpenAI Chat Completions 请求 +pub fn anthropic_to_openai(body: Value) -> Result { let mut result = json!({}); // NOTE: 模型映射由上游统一处理(proxy::model_mapper),格式转换层只做结构转换。 @@ -175,11 +173,6 @@ pub fn anthropic_to_openai(body: Value, cache_key: Option<&str>) -> Result