mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-10 21:20:20 +08:00
fix: tighten pr 566 claude and deepseek handling
This commit is contained in:
@@ -45,6 +45,8 @@ use super::payload::{
|
||||
};
|
||||
use super::{LocalStandardCandidateAttempt, LocalStandardDecisionInput, LocalStandardSpec};
|
||||
|
||||
const OMITTED_THINKING_TEXT: &str = "Previous thinking omitted.";
|
||||
|
||||
pub(crate) struct LocalStandardCandidatePayloadParts {
|
||||
pub(super) auth_header: String,
|
||||
pub(super) auth_value: String,
|
||||
@@ -78,7 +80,6 @@ fn provider_preserves_claude_thinking_signatures(provider_type: &str, base_url:
|
||||
"anthropic" | "claude_code" | "bedrock" | "aws_bedrock" | "amazon_bedrock"
|
||||
) || base_url.contains("api.anthropic.com")
|
||||
|| is_bedrock_runtime_url
|
||||
|| is_deepseek_provider(provider_type.as_str(), base_url.as_str())
|
||||
}
|
||||
|
||||
fn sanitize_claude_thinking_block(block: Value) -> (Option<Value>, bool) {
|
||||
@@ -116,8 +117,6 @@ fn sanitize_claude_thinking_block(block: Value) -> (Option<Value>, bool) {
|
||||
}
|
||||
|
||||
fn sanitize_claude_message_content_for_non_native_thinking(content: &mut Value) -> bool {
|
||||
const OMITTED_THINKING_TEXT: &str = "Previous thinking omitted.";
|
||||
|
||||
if content.is_object() {
|
||||
let original = std::mem::take(content);
|
||||
let (sanitized, changed) = sanitize_claude_thinking_block(original);
|
||||
@@ -177,6 +176,81 @@ fn sanitize_claude_request_thinking_signatures_for_non_native(body_json: &mut Va
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn remove_claude_redacted_thinking_block(block: Value) -> (Option<Value>, bool) {
|
||||
let Some(object) = block.as_object() else {
|
||||
return (Some(block), false);
|
||||
};
|
||||
let block_type = object
|
||||
.get("type")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.unwrap_or_default();
|
||||
if block_type == "redacted_thinking" {
|
||||
return (None, true);
|
||||
}
|
||||
(Some(block), false)
|
||||
}
|
||||
|
||||
fn sanitize_claude_message_content_for_deepseek_thinking(content: &mut Value) -> bool {
|
||||
if content.is_object() {
|
||||
let original = std::mem::take(content);
|
||||
let (sanitized, changed) = remove_claude_redacted_thinking_block(original);
|
||||
if changed {
|
||||
*content = sanitized.unwrap_or_else(|| {
|
||||
serde_json::json!({
|
||||
"type": "text",
|
||||
"text": OMITTED_THINKING_TEXT,
|
||||
})
|
||||
});
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
let Some(blocks) = content.as_array_mut() else {
|
||||
return false;
|
||||
};
|
||||
let original_blocks = std::mem::take(blocks);
|
||||
let mut changed = false;
|
||||
let mut sanitized_blocks = Vec::with_capacity(original_blocks.len());
|
||||
for block in original_blocks {
|
||||
let (sanitized, block_changed) = remove_claude_redacted_thinking_block(block);
|
||||
changed |= block_changed;
|
||||
if let Some(sanitized) = sanitized {
|
||||
sanitized_blocks.push(sanitized);
|
||||
}
|
||||
}
|
||||
if changed && sanitized_blocks.is_empty() {
|
||||
sanitized_blocks.push(serde_json::json!({
|
||||
"type": "text",
|
||||
"text": OMITTED_THINKING_TEXT,
|
||||
}));
|
||||
}
|
||||
*blocks = sanitized_blocks;
|
||||
changed
|
||||
}
|
||||
|
||||
fn sanitize_claude_request_redacted_thinking_for_deepseek(body_json: &mut Value) -> bool {
|
||||
body_json
|
||||
.get_mut("messages")
|
||||
.and_then(Value::as_array_mut)
|
||||
.map(|messages| {
|
||||
messages.iter_mut().fold(false, |changed, message| {
|
||||
let is_assistant = message
|
||||
.get("role")
|
||||
.and_then(Value::as_str)
|
||||
.is_some_and(|role| role.trim().eq_ignore_ascii_case("assistant"));
|
||||
if !is_assistant {
|
||||
return changed;
|
||||
}
|
||||
let content_changed = message
|
||||
.get_mut("content")
|
||||
.is_some_and(sanitize_claude_message_content_for_deepseek_thinking);
|
||||
changed || content_changed
|
||||
})
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn apply_non_native_claude_thinking_signature_compat(
|
||||
provider_request_body: &mut Value,
|
||||
provider_api_format: &str,
|
||||
@@ -185,6 +259,13 @@ fn apply_non_native_claude_thinking_signature_compat(
|
||||
if crate::ai_serving::normalize_api_format_alias(provider_api_format) != "claude:messages" {
|
||||
return;
|
||||
}
|
||||
if is_deepseek_provider(
|
||||
transport.provider.provider_type.as_str(),
|
||||
transport.endpoint.base_url.as_str(),
|
||||
) {
|
||||
let _ = sanitize_claude_request_redacted_thinking_for_deepseek(provider_request_body);
|
||||
return;
|
||||
}
|
||||
if provider_preserves_claude_thinking_signatures(
|
||||
transport.provider.provider_type.as_str(),
|
||||
transport.endpoint.base_url.as_str(),
|
||||
@@ -1102,6 +1183,7 @@ async fn build_kiro_cross_format_payload_parts(
|
||||
mod tests {
|
||||
use super::{
|
||||
provider_preserves_claude_thinking_signatures,
|
||||
sanitize_claude_request_redacted_thinking_for_deepseek,
|
||||
sanitize_claude_request_thinking_signatures_for_non_native,
|
||||
};
|
||||
use serde_json::json;
|
||||
@@ -1165,6 +1247,46 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deepseek_sanitizer_preserves_plain_thinking_but_removes_redacted() {
|
||||
let mut body = json!({
|
||||
"model": "claude-opus-4-1",
|
||||
"messages": [{
|
||||
"role": "assistant",
|
||||
"content": [
|
||||
{
|
||||
"type": "thinking",
|
||||
"thinking": "I should keep this short.",
|
||||
"signature": "sig_123"
|
||||
},
|
||||
{
|
||||
"type": "redacted_thinking",
|
||||
"data": "opaque"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Done."
|
||||
}
|
||||
]
|
||||
}]
|
||||
});
|
||||
|
||||
assert!(sanitize_claude_request_redacted_thinking_for_deepseek(
|
||||
&mut body
|
||||
));
|
||||
assert_eq!(body["messages"][0]["content"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(body["messages"][0]["content"][0]["type"], json!("thinking"));
|
||||
assert_eq!(
|
||||
body["messages"][0]["content"][0]["thinking"],
|
||||
json!("I should keep this short.")
|
||||
);
|
||||
assert_eq!(
|
||||
body["messages"][0]["content"][0]["signature"],
|
||||
json!("sig_123")
|
||||
);
|
||||
assert_eq!(body["messages"][0]["content"][1]["text"], json!("Done."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn official_claude_providers_preserve_thinking_signatures() {
|
||||
assert!(provider_preserves_claude_thinking_signatures(
|
||||
@@ -1183,11 +1305,11 @@ mod tests {
|
||||
"amazon_bedrock",
|
||||
"https://relay.example.com"
|
||||
));
|
||||
assert!(provider_preserves_claude_thinking_signatures(
|
||||
assert!(!provider_preserves_claude_thinking_signatures(
|
||||
"deepseek",
|
||||
"https://relay.example.com"
|
||||
));
|
||||
assert!(provider_preserves_claude_thinking_signatures(
|
||||
assert!(!provider_preserves_claude_thinking_signatures(
|
||||
"custom",
|
||||
"https://api.deepseek.com"
|
||||
));
|
||||
|
||||
@@ -20,6 +20,7 @@ use crate::formats::shared::stream_core::common::{
|
||||
use crate::formats::shared::stream_core::{
|
||||
CanonicalStreamFrame, StreamingStandardFormatMatrix, StreamingStandardTerminalObserver,
|
||||
};
|
||||
use crate::formats::shared::stream_rewrite::maybe_build_ai_surface_stream_rewriter;
|
||||
use crate::formats::shared::AiSurfaceFinalizeError;
|
||||
|
||||
pub struct SyncToStreamBridgeOutcome {
|
||||
@@ -667,17 +668,20 @@ fn maybe_bridge_aether_sse_response_capture_to_stream(
|
||||
captured_api_format.as_str(),
|
||||
client_api_format,
|
||||
);
|
||||
let sse_body =
|
||||
if captured_api_format == client_api_format && captured_api_format != "claude:messages" {
|
||||
body_text.as_bytes().to_vec()
|
||||
let sse_body = if captured_api_format == client_api_format {
|
||||
if captured_api_format == "claude:messages" {
|
||||
sanitize_same_format_claude_sse_body(body_text.as_bytes(), report_context)?
|
||||
} else {
|
||||
rewrite_sse_body_between_formats(
|
||||
body_text.as_bytes(),
|
||||
captured_api_format.as_str(),
|
||||
client_api_format,
|
||||
&bridge_context,
|
||||
)?
|
||||
};
|
||||
body_text.as_bytes().to_vec()
|
||||
}
|
||||
} else {
|
||||
rewrite_sse_body_between_formats(
|
||||
body_text.as_bytes(),
|
||||
captured_api_format.as_str(),
|
||||
client_api_format,
|
||||
&bridge_context,
|
||||
)?
|
||||
};
|
||||
let terminal_summary = observe_sse_terminal_summary(
|
||||
body_text.as_bytes(),
|
||||
captured_api_format.as_str(),
|
||||
@@ -690,6 +694,34 @@ fn maybe_bridge_aether_sse_response_capture_to_stream(
|
||||
}))
|
||||
}
|
||||
|
||||
fn sanitize_same_format_claude_sse_body(
|
||||
body: &[u8],
|
||||
report_context: Option<&Value>,
|
||||
) -> Result<Vec<u8>, AiSurfaceFinalizeError> {
|
||||
let mut context = report_context
|
||||
.cloned()
|
||||
.filter(Value::is_object)
|
||||
.unwrap_or_else(|| json!({}));
|
||||
let object = context
|
||||
.as_object_mut()
|
||||
.expect("same-format Claude context should stay object");
|
||||
object.insert(
|
||||
"provider_api_format".to_string(),
|
||||
Value::String("claude:messages".to_string()),
|
||||
);
|
||||
object.insert(
|
||||
"client_api_format".to_string(),
|
||||
Value::String("claude:messages".to_string()),
|
||||
);
|
||||
|
||||
let Some(mut rewriter) = maybe_build_ai_surface_stream_rewriter(Some(&context)) else {
|
||||
return Ok(body.to_vec());
|
||||
};
|
||||
let mut out = rewriter.push_chunk(body)?;
|
||||
out.extend(rewriter.finish()?);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn response_capture_header<'a>(headers: &'a Map<String, Value>, name: &str) -> Option<&'a str> {
|
||||
headers
|
||||
.iter()
|
||||
@@ -1359,6 +1391,10 @@ mod tests {
|
||||
"data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_read_1\",\"name\":\"Read\",\"input\":{\"file_path\":\"D:/projects/UIAutoTest/docs/prd/msr.md\",\"offset\":0,\"limit\":2000,\"pages\":\"\"}}}\n\n",
|
||||
"event: content_block_stop\n",
|
||||
"data: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
|
||||
"event: content_block_start\n",
|
||||
"data: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"server_tool_use\",\"id\":\"srv_1\",\"name\":\"web_search\",\"input\":{\"query\":\"rust\"}}}\n\n",
|
||||
"event: content_block_stop\n",
|
||||
"data: {\"type\":\"content_block_stop\",\"index\":1}\n\n",
|
||||
"event: message_delta\n",
|
||||
"data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"input_tokens\":1,\"output_tokens\":2}}\n\n",
|
||||
"event: message_stop\n",
|
||||
@@ -1382,7 +1418,10 @@ mod tests {
|
||||
|
||||
let output = utf8(outcome.sse_body);
|
||||
assert!(output.contains("\"name\":\"Read\""));
|
||||
assert!(output.contains("\\\"limit\\\":2000"));
|
||||
assert!(output.contains("\"limit\":2000"));
|
||||
assert!(output.contains("\"type\":\"server_tool_use\""));
|
||||
assert!(output.contains("\"name\":\"web_search\""));
|
||||
assert!(!output.contains("\"pages\":\"\""));
|
||||
assert!(!output.contains("\\\"pages\\\":\\\"\\\""));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user