| |
| |
|
|
| use super::models::*; |
| use super::utils::to_claude_usage; |
|
|
| |
| pub struct NonStreamingProcessor { |
| content_blocks: Vec<ContentBlock>, |
| text_builder: String, |
| thinking_builder: String, |
| thinking_signature: Option<String>, |
| trailing_signature: Option<String>, |
| has_tool_call: bool, |
| } |
|
|
| impl NonStreamingProcessor { |
| pub fn new() -> Self { |
| Self { |
| content_blocks: Vec::new(), |
| text_builder: String::new(), |
| thinking_builder: String::new(), |
| thinking_signature: None, |
| trailing_signature: None, |
| has_tool_call: false, |
| } |
| } |
|
|
| |
| pub fn process(&mut self, gemini_response: &GeminiResponse) -> ClaudeResponse { |
| |
| let empty_parts = vec![]; |
| let parts = gemini_response |
| .candidates |
| .as_ref() |
| .and_then(|c| c.get(0)) |
| .and_then(|candidate| candidate.content.as_ref()) |
| .map(|content| &content.parts) |
| .unwrap_or(&empty_parts); |
|
|
| |
| for part in parts { |
| self.process_part(part); |
| } |
|
|
| |
| if let Some(candidate) = gemini_response.candidates.as_ref().and_then(|c| c.get(0)) { |
| if let Some(grounding) = &candidate.grounding_metadata { |
| self.process_grounding(grounding); |
| } |
| } |
|
|
| |
| self.flush_thinking(); |
| self.flush_text(); |
|
|
| |
| if let Some(signature) = self.trailing_signature.take() { |
| self.content_blocks.push(ContentBlock::Thinking { |
| thinking: String::new(), |
| signature: Some(signature), |
| cache_control: None, |
| }); |
| } |
|
|
| |
| self.build_response(gemini_response) |
| } |
|
|
| |
| fn process_part(&mut self, part: &GeminiPart) { |
| let signature = part.thought_signature.clone(); |
|
|
| |
| if let Some(fc) = &part.function_call { |
| self.flush_thinking(); |
| self.flush_text(); |
|
|
| |
| if let Some(trailing_sig) = self.trailing_signature.take() { |
| self.content_blocks.push(ContentBlock::Thinking { |
| thinking: String::new(), |
| signature: Some(trailing_sig), |
| cache_control: None, |
| }); |
| } |
|
|
| self.has_tool_call = true; |
|
|
| |
| let tool_id = fc.id.clone().unwrap_or_else(|| { |
| format!( |
| "{}-{}", |
| fc.name, |
| crate::proxy::common::utils::generate_random_id() |
| ) |
| }); |
|
|
| let mut tool_use = ContentBlock::ToolUse { |
| id: tool_id, |
| name: fc.name.clone(), |
| input: fc.args.clone().unwrap_or(serde_json::json!({})), |
| signature: None, |
| cache_control: None, |
| }; |
|
|
| |
| if let ContentBlock::ToolUse { signature: sig, .. } = &mut tool_use { |
| *sig = signature; |
| } |
|
|
| self.content_blocks.push(tool_use); |
| return; |
| } |
|
|
| |
| if let Some(text) = &part.text { |
| if part.thought.unwrap_or(false) { |
| |
| self.flush_text(); |
|
|
| |
| if let Some(trailing_sig) = self.trailing_signature.take() { |
| self.flush_thinking(); |
| self.content_blocks.push(ContentBlock::Thinking { |
| thinking: String::new(), |
| signature: Some(trailing_sig), |
| cache_control: None, |
| }); |
| } |
|
|
| self.thinking_builder.push_str(text); |
| if signature.is_some() { |
| self.thinking_signature = signature; |
| } |
| } else { |
| |
| if text.is_empty() { |
| |
| if signature.is_some() { |
| self.trailing_signature = signature; |
| } |
| return; |
| } |
|
|
| self.flush_thinking(); |
|
|
| |
| if let Some(trailing_sig) = self.trailing_signature.take() { |
| self.flush_text(); |
| self.content_blocks.push(ContentBlock::Thinking { |
| thinking: String::new(), |
| signature: Some(trailing_sig), |
| cache_control: None, |
| }); |
| } |
|
|
| self.text_builder.push_str(text); |
|
|
| |
| if let Some(sig) = signature { |
| self.flush_text(); |
| self.content_blocks.push(ContentBlock::Thinking { |
| thinking: String::new(), |
| signature: Some(sig), |
| cache_control: None, |
| }); |
| } |
| } |
| } |
|
|
| |
| if let Some(img) = &part.inline_data { |
| self.flush_thinking(); |
|
|
| let mime_type = &img.mime_type; |
| let data = &img.data; |
| if !data.is_empty() { |
| let markdown_img = format!("", mime_type, data); |
| self.text_builder.push_str(&markdown_img); |
| self.flush_text(); |
| } |
| } |
| } |
|
|
| |
| fn process_grounding(&mut self, grounding: &GroundingMetadata) { |
| let mut grounding_text = String::new(); |
|
|
| |
| if let Some(queries) = &grounding.web_search_queries { |
| if !queries.is_empty() { |
| grounding_text.push_str("\n\n---\n**🔍 已为您搜索:** "); |
| grounding_text.push_str(&queries.join(", ")); |
| } |
| } |
|
|
| |
| if let Some(chunks) = &grounding.grounding_chunks { |
| let mut links = Vec::new(); |
| for (i, chunk) in chunks.iter().enumerate() { |
| if let Some(web) = &chunk.web { |
| let title = web.title.as_deref().unwrap_or("网页来源"); |
| let uri = web.uri.as_deref().unwrap_or("#"); |
| links.push(format!("[{}] [{}]({})", i + 1, title, uri)); |
| } |
| } |
|
|
| if !links.is_empty() { |
| grounding_text.push_str("\n\n**🌐 来源引文:**\n"); |
| grounding_text.push_str(&links.join("\n")); |
| } |
| } |
|
|
| if !grounding_text.is_empty() { |
| |
| self.flush_thinking(); |
| self.flush_text(); |
| self.text_builder.push_str(&grounding_text); |
| self.flush_text(); |
| } |
| } |
|
|
| |
| fn flush_text(&mut self) { |
| if self.text_builder.is_empty() { |
| return; |
| } |
|
|
| self.content_blocks.push(ContentBlock::Text { |
| text: self.text_builder.clone(), |
| }); |
| self.text_builder.clear(); |
| } |
|
|
| |
| fn flush_thinking(&mut self) { |
| |
| if self.thinking_builder.is_empty() && self.thinking_signature.is_none() { |
| return; |
| } |
|
|
| let thinking = self.thinking_builder.clone(); |
| let signature = self.thinking_signature.take(); |
|
|
| self.content_blocks.push(ContentBlock::Thinking { |
| thinking, |
| signature, |
| cache_control: None, |
| }); |
| self.thinking_builder.clear(); |
| } |
|
|
| |
| fn build_response(&self, gemini_response: &GeminiResponse) -> ClaudeResponse { |
| let finish_reason = gemini_response |
| .candidates |
| .as_ref() |
| .and_then(|c| c.get(0)) |
| .and_then(|candidate| candidate.finish_reason.as_deref()); |
|
|
| let stop_reason = if self.has_tool_call { |
| "tool_use" |
| } else if finish_reason == Some("MAX_TOKENS") { |
| "max_tokens" |
| } else { |
| "end_turn" |
| }; |
|
|
| let usage = gemini_response |
| .usage_metadata |
| .as_ref() |
| .map(|u| to_claude_usage(u)) |
| .unwrap_or(Usage { |
| input_tokens: 0, |
| output_tokens: 0, |
| server_tool_use: None, |
| }); |
|
|
| ClaudeResponse { |
| id: gemini_response.response_id.clone().unwrap_or_else(|| { |
| format!("msg_{}", crate::proxy::common::utils::generate_random_id()) |
| }), |
| type_: "message".to_string(), |
| role: "assistant".to_string(), |
| model: gemini_response.model_version.clone().unwrap_or_default(), |
| content: self.content_blocks.clone(), |
| stop_reason: stop_reason.to_string(), |
| stop_sequence: None, |
| usage, |
| } |
| } |
| } |
|
|
| |
| pub fn transform_response(gemini_response: &GeminiResponse) -> Result<ClaudeResponse, String> { |
| let mut processor = NonStreamingProcessor::new(); |
| Ok(processor.process(gemini_response)) |
| } |
|
|
| #[cfg(test)] |
| mod tests { |
| use super::*; |
|
|
| #[test] |
| fn test_simple_text_response() { |
| let gemini_resp = GeminiResponse { |
| candidates: Some(vec![Candidate { |
| content: Some(GeminiContent { |
| role: "model".to_string(), |
| parts: vec![GeminiPart { |
| text: Some("Hello, world!".to_string()), |
| thought: None, |
| thought_signature: None, |
| function_call: None, |
| function_response: None, |
| inline_data: None, |
| }], |
| }), |
| finish_reason: Some("STOP".to_string()), |
| index: Some(0), |
| grounding_metadata: None, |
| }]), |
| usage_metadata: Some(UsageMetadata { |
| prompt_token_count: Some(10), |
| candidates_token_count: Some(5), |
| total_token_count: Some(15), |
| }), |
| model_version: Some("gemini-2.5-pro".to_string()), |
| response_id: Some("resp_123".to_string()), |
| }; |
|
|
| let result = transform_response(&gemini_resp); |
| assert!(result.is_ok()); |
|
|
| let claude_resp = result.unwrap(); |
| assert_eq!(claude_resp.role, "assistant"); |
| assert_eq!(claude_resp.stop_reason, "end_turn"); |
| assert_eq!(claude_resp.content.len(), 1); |
|
|
| match &claude_resp.content[0] { |
| ContentBlock::Text { text } => { |
| assert_eq!(text, "Hello, world!"); |
| } |
| _ => panic!("Expected Text block"), |
| } |
| } |
|
|
| #[test] |
| fn test_thinking_with_signature() { |
| let gemini_resp = GeminiResponse { |
| candidates: Some(vec![Candidate { |
| content: Some(GeminiContent { |
| role: "model".to_string(), |
| parts: vec![ |
| GeminiPart { |
| text: Some("Let me think...".to_string()), |
| thought: Some(true), |
| thought_signature: Some("sig123".to_string()), |
| function_call: None, |
| function_response: None, |
| inline_data: None, |
| }, |
| GeminiPart { |
| text: Some("The answer is 42".to_string()), |
| thought: None, |
| thought_signature: None, |
| function_call: None, |
| function_response: None, |
| inline_data: None, |
| }, |
| ], |
| }), |
| finish_reason: Some("STOP".to_string()), |
| index: Some(0), |
| grounding_metadata: None, |
| }]), |
| usage_metadata: None, |
| model_version: Some("gemini-2.5-pro".to_string()), |
| response_id: Some("resp_456".to_string()), |
| }; |
|
|
| let result = transform_response(&gemini_resp); |
| assert!(result.is_ok()); |
|
|
| let claude_resp = result.unwrap(); |
| assert_eq!(claude_resp.content.len(), 2); |
|
|
| match &claude_resp.content[0] { |
| ContentBlock::Thinking { |
| thinking, |
| signature, |
| .. |
| } => { |
| assert_eq!(thinking, "Let me think..."); |
| assert_eq!(signature.as_deref(), Some("sig123")); |
| } |
| _ => panic!("Expected Thinking block"), |
| } |
|
|
| match &claude_resp.content[1] { |
| ContentBlock::Text { text } => { |
| assert_eq!(text, "The answer is 42"); |
| } |
| _ => panic!("Expected Text block"), |
| } |
| } |
| } |
|
|