(
&self,
messages: &[ToolMessage],
tools: &[ToolDefinition],
max_tokens: u32,
)
| 178 | // ── Anthropic ─────────────────────────────────────────────── |
| 179 | |
| 180 | async fn chat_with_tools_anthropic( |
| 181 | &self, |
| 182 | messages: &[ToolMessage], |
| 183 | tools: &[ToolDefinition], |
| 184 | max_tokens: u32, |
| 185 | ) -> Result<ToolAwareResponse, AiError> { |
| 186 | let url = format!("{}/messages", self.base_url); |
| 187 | |
| 188 | // Extract system prompt (Anthropic uses a top-level field). |
| 189 | let system_text: String = messages |
| 190 | .iter() |
| 191 | .filter_map(|m| match m { |
| 192 | ToolMessage::System { content } => Some(content.as_str()), |
| 193 | _ => None, |
| 194 | }) |
| 195 | .collect::<Vec<_>>() |
| 196 | .join("\n\n"); |
| 197 | |
| 198 | // Build message array (skip System messages). |
| 199 | let api_messages = anthropic_messages(messages); |
| 200 | |
| 201 | // Build tools array. |
| 202 | let api_tools: Vec<serde_json::Value> = tools |
| 203 | .iter() |
| 204 | .map(|t| { |
| 205 | json!({ |
| 206 | "name": t.name, |
| 207 | "description": t.description, |
| 208 | "input_schema": t.parameters, |
| 209 | }) |
| 210 | }) |
| 211 | .collect(); |
| 212 | |
| 213 | let mut body = json!({ |
| 214 | "model": self.model, |
| 215 | "max_tokens": max_tokens, |
| 216 | "messages": api_messages, |
| 217 | }); |
| 218 | |
| 219 | if !system_text.is_empty() { |
| 220 | // Use content block format with cache_control for prompt caching. |
| 221 | // The system prompt + tools are static across turns — caching them |
| 222 | // means turns 2+ pay ~0 for the system prompt. |
| 223 | body["system"] = json!([ |
| 224 | { |
| 225 | "type": "text", |
| 226 | "text": system_text, |
| 227 | "cache_control": { "type": "ephemeral" } |
| 228 | } |
| 229 | ]); |
| 230 | } |
| 231 | if !api_tools.is_empty() { |
| 232 | body["tools"] = json!(api_tools); |
| 233 | } |
| 234 | |
| 235 | let client = reqwest::Client::new(); |
| 236 | let resp = client |
| 237 | .post(&url) |
no test coverage detected