Convert the lenient wire format into our internal `ChatResponse`.
(self)
| 84 | impl RawChatResponse { |
| 85 | /// Convert the lenient wire format into our internal `ChatResponse`. |
| 86 | fn into_chat_response(self) -> ChatResponse { |
| 87 | let choices = self |
| 88 | .choices |
| 89 | .into_iter() |
| 90 | .map(|c| { |
| 91 | let raw_msg = c.message.unwrap_or(RawMessage { |
| 92 | role: None, |
| 93 | content: None, |
| 94 | tool_calls: None, |
| 95 | _reasoning_text: None, |
| 96 | }); |
| 97 | |
| 98 | // Build content parts from the raw message. |
| 99 | let mut parts: Vec<crate::ContentPart> = Vec::new(); |
| 100 | |
| 101 | // Text content |
| 102 | if let Some(text) = &raw_msg.content { |
| 103 | if !text.is_empty() { |
| 104 | parts.push(crate::ContentPart { |
| 105 | content_type: "text".to_string(), |
| 106 | text: Some(text.clone()), |
| 107 | ..Default::default() |
| 108 | }); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Tool calls → ContentPart with tool_use |
| 113 | if let Some(tool_calls) = &raw_msg.tool_calls { |
| 114 | for tc in tool_calls { |
| 115 | let func = tc.function.as_ref(); |
| 116 | let name = func.and_then(|f| f.name.as_deref()).unwrap_or(""); |
| 117 | let args_str = func.and_then(|f| f.arguments.as_deref()).unwrap_or("{}"); |
| 118 | let input: serde_json::Value = |
| 119 | serde_json::from_str(args_str).unwrap_or(serde_json::json!({})); |
| 120 | let id = tc |
| 121 | .id |
| 122 | .clone() |
| 123 | .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()); |
| 124 | parts.push(crate::ContentPart { |
| 125 | content_type: "tool_use".to_string(), |
| 126 | tool_use: Some(crate::ToolUse { |
| 127 | id, |
| 128 | name: name.to_string(), |
| 129 | input, |
| 130 | }), |
| 131 | ..Default::default() |
| 132 | }); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | let content = if parts.is_empty() { |
| 137 | crate::Content::Text(raw_msg.content.unwrap_or_default()) |
| 138 | } else if parts.len() == 1 && parts[0].content_type == "text" { |
| 139 | crate::Content::Text(parts.remove(0).text.unwrap_or_default()) |
| 140 | } else { |
| 141 | crate::Content::Parts(parts) |
| 142 | }; |
| 143 |
no test coverage detected