1use super::http::{default_http_client, normalize_base_url, HttpClient};
4use super::structured;
5use super::types::*;
6use super::LlmClient;
7use crate::retry::{AttemptOutcome, RetryConfig};
8use anyhow::{Context, Result};
9use async_trait::async_trait;
10use futures::StreamExt;
11use serde::Deserialize;
12use std::sync::Arc;
13use std::time::Instant;
14use tokio::sync::mpsc;
15use tokio_util::sync::CancellationToken;
16
17pub(crate) const DEFAULT_MAX_TOKENS: usize = 8192;
19
20pub struct AnthropicClient {
22 pub(crate) provider_name: String,
23 pub(crate) api_key: SecretString,
24 pub(crate) model: String,
25 pub(crate) base_url: String,
26 pub(crate) max_tokens: usize,
27 pub(crate) temperature: Option<f32>,
28 pub(crate) thinking_budget: Option<usize>,
29 pub(crate) http: Arc<dyn HttpClient>,
30 pub(crate) retry_config: RetryConfig,
31}
32
33impl AnthropicClient {
34 pub fn new(api_key: String, model: String) -> Self {
35 Self {
36 provider_name: "anthropic".to_string(),
37 api_key: SecretString::new(api_key),
38 model,
39 base_url: "https://api.anthropic.com".to_string(),
40 max_tokens: DEFAULT_MAX_TOKENS,
41 temperature: None,
42 thinking_budget: None,
43 http: default_http_client(),
44 retry_config: RetryConfig::default(),
45 }
46 }
47
48 pub fn with_base_url(mut self, base_url: String) -> Self {
49 self.base_url = normalize_base_url(&base_url);
50 self
51 }
52
53 pub fn with_provider_name(mut self, provider_name: impl Into<String>) -> Self {
54 self.provider_name = provider_name.into();
55 self
56 }
57
58 pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
59 self.max_tokens = max_tokens;
60 self
61 }
62
63 pub fn with_temperature(mut self, temperature: f32) -> Self {
64 self.temperature = Some(temperature);
65 self
66 }
67
68 pub fn with_thinking_budget(mut self, budget: usize) -> Self {
69 self.thinking_budget = Some(budget);
70 self
71 }
72
73 pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
74 self.retry_config = retry_config;
75 self
76 }
77
78 pub fn with_http_client(mut self, http: Arc<dyn HttpClient>) -> Self {
79 self.http = http;
80 self
81 }
82
83 fn initial_tool_input_json(input: &serde_json::Value) -> Option<String> {
84 match input {
85 serde_json::Value::Object(map) if map.is_empty() => None,
86 serde_json::Value::Null => None,
87 value => serde_json::to_string(value).ok(),
88 }
89 }
90
91 pub(crate) fn build_request(
92 &self,
93 messages: &[Message],
94 system: Option<&str>,
95 tools: &[ToolDefinition],
96 ) -> serde_json::Value {
97 let mut request = serde_json::json!({
98 "model": self.model,
99 "max_tokens": self.max_tokens,
100 "messages": messages,
101 });
102
103 if let Some(sys) = system {
107 request["system"] = serde_json::json!([
108 {
109 "type": "text",
110 "text": sys,
111 "cache_control": { "type": "ephemeral" }
112 }
113 ]);
114 }
115
116 if !tools.is_empty() {
117 let mut tool_defs: Vec<serde_json::Value> = tools
118 .iter()
119 .map(|t| {
120 serde_json::json!({
121 "name": t.name,
122 "description": t.description,
123 "input_schema": t.parameters,
124 })
125 })
126 .collect();
127
128 if let Some(last) = tool_defs.last_mut() {
131 last["cache_control"] = serde_json::json!({ "type": "ephemeral" });
132 }
133
134 request["tools"] = serde_json::json!(tool_defs);
135 }
136
137 if let Some(temp) = self.temperature {
139 request["temperature"] = serde_json::json!(temp);
140 }
141
142 if let Some(budget) = self.thinking_budget {
144 request["thinking"] = serde_json::json!({
145 "type": "enabled",
146 "budget_tokens": budget
147 });
148 request["temperature"] = serde_json::json!(1.0);
150 }
151
152 request
153 }
154}
155
156impl AnthropicClient {
157 fn apply_directive(
162 request: &mut serde_json::Value,
163 directive: &structured::StructuredDirective,
164 ) {
165 if let Some(tool) = &directive.force_tool {
166 request["tool_choice"] = serde_json::json!({ "type": "tool", "name": tool });
167 }
168 }
169
170 async fn send_request(&self, request_body: serde_json::Value) -> Result<LlmResponse> {
172 {
173 let request_started_at = Instant::now();
174 let url = format!("{}/v1/messages", self.base_url);
175
176 let headers = vec![
177 ("x-api-key", self.api_key.expose()),
178 ("anthropic-version", "2023-06-01"),
179 ("anthropic-beta", "prompt-caching-2024-07-31"),
180 ];
181
182 let response = crate::retry::with_retry(&self.retry_config, |_attempt| {
183 let http = &self.http;
184 let url = &url;
185 let headers = headers.clone();
186 let request_body = &request_body;
187 async move {
188 match http
189 .post(url, headers, request_body, CancellationToken::new())
190 .await
191 {
192 Ok(resp) => {
193 let status = reqwest::StatusCode::from_u16(resp.status)
194 .unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
195 if status.is_success() {
196 AttemptOutcome::Success(resp.body)
197 } else if self.retry_config.is_retryable_status(status) {
198 AttemptOutcome::Retryable {
199 status,
200 body: resp.body,
201 retry_after: None,
202 }
203 } else {
204 AttemptOutcome::Fatal(anyhow::anyhow!(
205 "Anthropic API error at {} ({}): {}",
206 url,
207 status,
208 resp.body
209 ))
210 }
211 }
212 Err(e) => AttemptOutcome::Fatal(e),
213 }
214 }
215 })
216 .await?;
217
218 let parsed: AnthropicResponse =
219 serde_json::from_str(&response).context("Failed to parse Anthropic response")?;
220
221 tracing::debug!("Anthropic response: {:?}", parsed);
222
223 let content: Vec<ContentBlock> = parsed
224 .content
225 .into_iter()
226 .map(|block| match block {
227 AnthropicContentBlock::Text { text } => ContentBlock::Text { text },
228 AnthropicContentBlock::ToolUse { id, name, input } => {
229 ContentBlock::ToolUse { id, name, input }
230 }
231 })
232 .collect();
233
234 let llm_response = LlmResponse {
235 message: Message {
236 role: "assistant".to_string(),
237 content,
238 reasoning_content: None,
239 },
240 usage: TokenUsage {
241 prompt_tokens: parsed.usage.input_tokens,
242 completion_tokens: parsed.usage.output_tokens,
243 total_tokens: parsed.usage.input_tokens + parsed.usage.output_tokens,
244 cache_read_tokens: parsed.usage.cache_read_input_tokens,
245 cache_write_tokens: parsed.usage.cache_creation_input_tokens,
246 },
247 stop_reason: Some(parsed.stop_reason),
248 meta: Some(LlmResponseMeta {
249 provider: Some(self.provider_name.clone()),
250 request_model: Some(self.model.clone()),
251 request_url: Some(url.clone()),
252 response_id: parsed.id,
253 response_model: parsed.model,
254 response_object: parsed.response_type,
255 first_token_ms: None,
256 duration_ms: Some(request_started_at.elapsed().as_millis() as u64),
257 }),
258 };
259
260 crate::telemetry::record_llm_usage(
261 llm_response.usage.prompt_tokens,
262 llm_response.usage.completion_tokens,
263 llm_response.usage.total_tokens,
264 llm_response.stop_reason.as_deref(),
265 );
266
267 Ok(llm_response)
268 }
269 }
270}
271
272#[async_trait]
273impl LlmClient for AnthropicClient {
274 async fn complete(
275 &self,
276 messages: &[Message],
277 system: Option<&str>,
278 tools: &[ToolDefinition],
279 ) -> Result<LlmResponse> {
280 self.send_request(self.build_request(messages, system, tools))
281 .await
282 }
283
284 async fn complete_structured(
285 &self,
286 messages: &[Message],
287 system: Option<&str>,
288 tools: &[ToolDefinition],
289 directive: &structured::StructuredDirective,
290 ) -> Result<LlmResponse> {
291 let mut request_body = self.build_request(messages, system, tools);
292 Self::apply_directive(&mut request_body, directive);
293 self.send_request(request_body).await
294 }
295
296 fn native_structured_support(&self) -> structured::NativeStructuredSupport {
297 structured::NativeStructuredSupport::ForcedTool
298 }
299
300 async fn complete_streaming(
301 &self,
302 messages: &[Message],
303 system: Option<&str>,
304 tools: &[ToolDefinition],
305 cancel_token: CancellationToken,
306 ) -> Result<mpsc::Receiver<StreamEvent>> {
307 self.send_streaming(self.build_request(messages, system, tools), cancel_token)
308 .await
309 }
310
311 async fn complete_streaming_structured(
312 &self,
313 messages: &[Message],
314 system: Option<&str>,
315 tools: &[ToolDefinition],
316 directive: &structured::StructuredDirective,
317 cancel_token: CancellationToken,
318 ) -> Result<mpsc::Receiver<StreamEvent>> {
319 let mut request_body = self.build_request(messages, system, tools);
320 Self::apply_directive(&mut request_body, directive);
321 self.send_streaming(request_body, cancel_token).await
322 }
323}
324
325impl AnthropicClient {
326 async fn send_streaming(
328 &self,
329 mut request_body: serde_json::Value,
330 cancel_token: CancellationToken,
331 ) -> Result<mpsc::Receiver<StreamEvent>> {
332 {
333 let request_started_at = Instant::now();
334 request_body["stream"] = serde_json::json!(true);
335
336 let url = format!("{}/v1/messages", self.base_url);
337
338 let headers = vec![
339 ("x-api-key", self.api_key.expose()),
340 ("anthropic-version", "2023-06-01"),
341 ("anthropic-beta", "prompt-caching-2024-07-31"),
342 ];
343
344 let streaming_resp = crate::retry::with_retry(&self.retry_config, |_attempt| {
345 let http = &self.http;
346 let url = &url;
347 let headers = headers.clone();
348 let request_body = &request_body;
349 let cancel_token = cancel_token.clone();
350 async move {
351 let resp = tokio::select! {
352 _ = cancel_token.cancelled() => {
353 return AttemptOutcome::Fatal(anyhow::anyhow!("HTTP request cancelled"));
354 }
355 result = http.post_streaming(url, headers, request_body, cancel_token.clone()) => {
356 match result {
357 Ok(r) => r,
358 Err(e) => {
359 return AttemptOutcome::Fatal(anyhow::anyhow!("HTTP request failed: {}", e));
360 }
361 }
362 }
363 };
364 let status = reqwest::StatusCode::from_u16(resp.status)
365 .unwrap_or(reqwest::StatusCode::INTERNAL_SERVER_ERROR);
366 if status.is_success() {
367 AttemptOutcome::Success(resp)
368 } else {
369 let retry_after = resp
370 .retry_after
371 .as_deref()
372 .and_then(|v| RetryConfig::parse_retry_after(Some(v)));
373 if self.retry_config.is_retryable_status(status) {
374 AttemptOutcome::Retryable {
375 status,
376 body: resp.error_body,
377 retry_after,
378 }
379 } else {
380 AttemptOutcome::Fatal(anyhow::anyhow!(
381 "Anthropic API error at {} ({}): {}",
382 url,
383 status,
384 resp.error_body
385 ))
386 }
387 }
388 }
389 })
390 .await?;
391
392 let (tx, rx) = mpsc::channel(100);
393
394 let mut stream = streaming_resp.byte_stream;
395 let provider_name = self.provider_name.clone();
396 let request_model = self.model.clone();
397 let request_url = url.clone();
398 tokio::spawn(async move {
399 let mut buffer = String::new();
400 let mut content_blocks: Vec<ContentBlock> = Vec::new();
401 let mut text_content = String::new();
402 let mut current_tool_id = String::new();
403 let mut current_tool_name = String::new();
404 let mut current_tool_input = String::new();
405 let mut usage = TokenUsage::default();
406 let mut stop_reason = None;
407 let mut response_id = None;
408 let mut response_model = None;
409 let mut response_object = Some("message".to_string());
410 let mut first_token_ms = None;
411
412 while let Some(chunk_result) = stream.next().await {
413 let chunk = match chunk_result {
414 Ok(c) => c,
415 Err(e) => {
416 tracing::error!("Stream error: {}", e);
417 break;
418 }
419 };
420
421 buffer.push_str(&String::from_utf8_lossy(&chunk));
422
423 while let Some(event_end) = buffer.find("\n\n") {
424 let event_data: String = buffer.drain(..event_end).collect();
425 buffer.drain(..2);
426
427 for line in event_data.lines() {
428 if let Some(data) = line.strip_prefix("data: ") {
429 if data == "[DONE]" {
430 continue;
431 }
432
433 if let Ok(event) =
434 serde_json::from_str::<AnthropicStreamEvent>(data)
435 {
436 match event {
437 AnthropicStreamEvent::ContentBlockStart {
438 index: _,
439 content_block,
440 } => match content_block {
441 AnthropicContentBlock::Text { .. } => {}
442 AnthropicContentBlock::ToolUse { id, name, input } => {
443 if !text_content.is_empty() {
444 content_blocks.push(ContentBlock::Text {
445 text: std::mem::take(&mut text_content),
446 });
447 }
448 current_tool_id = id.clone();
449 current_tool_name = name.clone();
450 current_tool_input =
451 Self::initial_tool_input_json(&input)
452 .unwrap_or_default();
453 let _ = tx
454 .send(StreamEvent::ToolUseStart { id, name })
455 .await;
456 if !current_tool_input.is_empty() {
457 if first_token_ms.is_none() {
458 first_token_ms = Some(
459 request_started_at.elapsed().as_millis()
460 as u64,
461 );
462 }
463 let _ = tx
464 .send(StreamEvent::ToolUseInputDelta(
465 current_tool_input.clone(),
466 ))
467 .await;
468 }
469 }
470 },
471 AnthropicStreamEvent::ContentBlockDelta {
472 index: _,
473 delta,
474 } => match delta {
475 AnthropicDelta::TextDelta { text } => {
476 if first_token_ms.is_none() {
477 first_token_ms = Some(
478 request_started_at.elapsed().as_millis()
479 as u64,
480 );
481 }
482 text_content.push_str(&text);
483 let _ = tx.send(StreamEvent::TextDelta(text)).await;
484 }
485 AnthropicDelta::InputJsonDelta { partial_json } => {
486 if first_token_ms.is_none() {
487 first_token_ms = Some(
488 request_started_at.elapsed().as_millis()
489 as u64,
490 );
491 }
492 current_tool_input.push_str(&partial_json);
493 let _ = tx
494 .send(StreamEvent::ToolUseInputDelta(
495 partial_json,
496 ))
497 .await;
498 }
499 },
500 AnthropicStreamEvent::ContentBlockStop { index: _ }
501 if !current_tool_id.is_empty() =>
502 {
503 let input: serde_json::Value = if current_tool_input
504 .trim()
505 .is_empty()
506 {
507 serde_json::Value::Object(Default::default())
508 } else {
509 serde_json::from_str(¤t_tool_input)
510 .unwrap_or_else(|e| {
511 tracing::warn!(
512 "Failed to parse tool input JSON for tool '{}': {}",
513 current_tool_name, e
514 );
515 serde_json::json!({
516 "__parse_error": format!(
517 "Malformed tool arguments: {}. Raw input: {}",
518 e, ¤t_tool_input
519 )
520 })
521 })
522 };
523 content_blocks.push(ContentBlock::ToolUse {
524 id: current_tool_id.clone(),
525 name: current_tool_name.clone(),
526 input,
527 });
528 current_tool_id.clear();
529 current_tool_name.clear();
530 current_tool_input.clear();
531 }
532 AnthropicStreamEvent::MessageStart { message } => {
533 response_id = message.id;
534 response_model = message.model;
535 response_object = message.message_type;
536 usage.prompt_tokens = message.usage.input_tokens;
537 }
538 AnthropicStreamEvent::MessageDelta {
539 delta,
540 usage: msg_usage,
541 } => {
542 stop_reason = Some(delta.stop_reason);
543 usage.completion_tokens = msg_usage.output_tokens;
544 usage.total_tokens =
545 usage.prompt_tokens + usage.completion_tokens;
546 }
547 AnthropicStreamEvent::MessageStop => {
548 if !text_content.is_empty() {
549 content_blocks.push(ContentBlock::Text {
550 text: std::mem::take(&mut text_content),
551 });
552 }
553 crate::telemetry::record_llm_usage(
554 usage.prompt_tokens,
555 usage.completion_tokens,
556 usage.total_tokens,
557 stop_reason.as_deref(),
558 );
559
560 let response = LlmResponse {
561 message: Message {
562 role: "assistant".to_string(),
563 content: std::mem::take(&mut content_blocks),
564 reasoning_content: None,
565 },
566 usage: usage.clone(),
567 stop_reason: stop_reason.clone(),
568 meta: Some(LlmResponseMeta {
569 provider: Some(provider_name.clone()),
570 request_model: Some(request_model.clone()),
571 request_url: Some(request_url.clone()),
572 response_id: response_id.clone(),
573 response_model: response_model.clone(),
574 response_object: response_object.clone(),
575 first_token_ms,
576 duration_ms: Some(
577 request_started_at.elapsed().as_millis()
578 as u64,
579 ),
580 }),
581 };
582 let _ = tx.send(StreamEvent::Done(response)).await;
583 }
584 _ => {}
585 }
586 }
587 }
588 }
589 }
590 }
591 });
592
593 Ok(rx)
594 }
595 }
596}
597
598#[derive(Debug, Deserialize)]
600pub(crate) struct AnthropicResponse {
601 #[serde(default)]
602 pub(crate) id: Option<String>,
603 #[serde(default)]
604 pub(crate) model: Option<String>,
605 #[serde(rename = "type", default)]
606 pub(crate) response_type: Option<String>,
607 pub(crate) content: Vec<AnthropicContentBlock>,
608 pub(crate) stop_reason: String,
609 pub(crate) usage: AnthropicUsage,
610}
611
612#[derive(Debug, Deserialize)]
613#[serde(tag = "type")]
614pub(crate) enum AnthropicContentBlock {
615 #[serde(rename = "text")]
616 Text { text: String },
617 #[serde(rename = "tool_use")]
618 ToolUse {
619 id: String,
620 name: String,
621 input: serde_json::Value,
622 },
623}
624
625#[derive(Debug, Deserialize)]
626pub(crate) struct AnthropicUsage {
627 pub(crate) input_tokens: usize,
628 pub(crate) output_tokens: usize,
629 pub(crate) cache_read_input_tokens: Option<usize>,
630 pub(crate) cache_creation_input_tokens: Option<usize>,
631}
632
633#[derive(Debug, Deserialize)]
634#[serde(tag = "type")]
635#[allow(dead_code)]
636pub(crate) enum AnthropicStreamEvent {
637 #[serde(rename = "message_start")]
638 MessageStart { message: AnthropicMessageStart },
639 #[serde(rename = "content_block_start")]
640 ContentBlockStart {
641 index: usize,
642 content_block: AnthropicContentBlock,
643 },
644 #[serde(rename = "content_block_delta")]
645 ContentBlockDelta { index: usize, delta: AnthropicDelta },
646 #[serde(rename = "content_block_stop")]
647 ContentBlockStop { index: usize },
648 #[serde(rename = "message_delta")]
649 MessageDelta {
650 delta: AnthropicMessageDeltaData,
651 usage: AnthropicOutputUsage,
652 },
653 #[serde(rename = "message_stop")]
654 MessageStop,
655 #[serde(rename = "ping")]
656 Ping,
657 #[serde(rename = "error")]
658 Error { error: AnthropicError },
659}
660
661#[derive(Debug, Deserialize)]
662pub(crate) struct AnthropicMessageStart {
663 #[serde(default)]
664 pub(crate) id: Option<String>,
665 #[serde(default)]
666 pub(crate) model: Option<String>,
667 #[serde(rename = "type", default)]
668 pub(crate) message_type: Option<String>,
669 pub(crate) usage: AnthropicUsage,
670}
671
672#[derive(Debug, Deserialize)]
673#[serde(tag = "type")]
674pub(crate) enum AnthropicDelta {
675 #[serde(rename = "text_delta")]
676 TextDelta { text: String },
677 #[serde(rename = "input_json_delta")]
678 InputJsonDelta { partial_json: String },
679}
680
681#[derive(Debug, Deserialize)]
682pub(crate) struct AnthropicMessageDeltaData {
683 pub(crate) stop_reason: String,
684}
685
686#[derive(Debug, Deserialize)]
687pub(crate) struct AnthropicOutputUsage {
688 pub(crate) output_tokens: usize,
689}
690
691#[derive(Debug, Deserialize)]
692#[allow(dead_code)]
693pub(crate) struct AnthropicError {
694 #[serde(rename = "type")]
695 pub(crate) error_type: String,
696 pub(crate) message: String,
697}
698
699#[cfg(test)]
704mod tests {
705 use super::*;
706 use crate::llm::types::{Message, ToolDefinition};
707
708 fn make_client() -> AnthropicClient {
709 AnthropicClient::new("test-key".to_string(), "claude-opus-4-6".to_string())
710 }
711
712 #[test]
713 fn test_build_request_basic() {
714 let client = make_client();
715 let messages = vec![Message::user("Hello")];
716 let req = client.build_request(&messages, None, &[]);
717
718 assert_eq!(req["model"], "claude-opus-4-6");
719 assert_eq!(req["max_tokens"], DEFAULT_MAX_TOKENS);
720 assert!(req["thinking"].is_null());
721 }
722
723 #[test]
724 fn test_build_request_with_thinking_budget() {
725 let client = make_client().with_thinking_budget(10_000);
726 let messages = vec![Message::user("Think carefully.")];
727 let req = client.build_request(&messages, None, &[]);
728
729 assert_eq!(req["thinking"]["type"], "enabled");
731 assert_eq!(req["thinking"]["budget_tokens"], 10_000);
732 assert_eq!(req["temperature"], 1.0_f64);
734 }
735
736 #[test]
737 fn test_build_request_thinking_overrides_temperature() {
738 let client = make_client()
740 .with_temperature(0.5)
741 .with_thinking_budget(5_000);
742 let messages = vec![Message::user("Test")];
743 let req = client.build_request(&messages, None, &[]);
744
745 assert_eq!(req["temperature"], 1.0_f64);
746 assert_eq!(req["thinking"]["budget_tokens"], 5_000);
747 }
748
749 #[test]
750 fn test_build_request_no_thinking_uses_temperature() {
751 let client = make_client().with_temperature(0.7);
752 let messages = vec![Message::user("Test")];
753 let req = client.build_request(&messages, None, &[]);
754
755 let temp = req["temperature"].as_f64().unwrap();
757 assert!((temp - 0.7).abs() < 0.01);
758 assert!(req["thinking"].is_null());
759 }
760
761 #[test]
762 fn test_build_request_with_system_prompt() {
763 let client = make_client();
764 let messages = vec![Message::user("Hello")];
765 let req = client.build_request(&messages, Some("You are helpful."), &[]);
766
767 let system = &req["system"];
768 assert!(system.is_array());
769 assert_eq!(system[0]["type"], "text");
770 assert_eq!(system[0]["text"], "You are helpful.");
771 assert!(system[0]["cache_control"].is_object());
772 }
773
774 #[test]
775 fn test_build_request_with_tools() {
776 let client = make_client();
777 let messages = vec![Message::user("Use a tool")];
778 let tools = vec![ToolDefinition {
779 name: "read_file".to_string(),
780 description: "Read a file".to_string(),
781 parameters: serde_json::json!({"type": "object", "properties": {}}),
782 }];
783 let req = client.build_request(&messages, None, &tools);
784
785 assert!(req["tools"].is_array());
786 assert_eq!(req["tools"][0]["name"], "read_file");
787 assert!(req["tools"][0]["cache_control"].is_object());
789 }
790
791 #[test]
792 fn test_build_request_thinking_budget_sets_max_tokens() {
793 let client = make_client()
795 .with_max_tokens(16_000)
796 .with_thinking_budget(8_000);
797 let messages = vec![Message::user("Test")];
798 let req = client.build_request(&messages, None, &[]);
799
800 assert_eq!(req["max_tokens"], 16_000);
801 assert_eq!(req["thinking"]["budget_tokens"], 8_000);
802 }
803
804 #[test]
805 fn test_apply_directive_forces_tool_choice() {
806 let mut req = serde_json::json!({ "model": "m", "messages": [] });
807 let directive = structured::StructuredDirective {
808 force_tool: Some("emit_person".to_string()),
809 response_format: None,
810 };
811 AnthropicClient::apply_directive(&mut req, &directive);
812 assert_eq!(req["tool_choice"]["type"], "tool");
813 assert_eq!(req["tool_choice"]["name"], "emit_person");
814 }
815
816 #[test]
817 fn test_apply_directive_ignores_response_format() {
818 let mut req = serde_json::json!({ "model": "m" });
821 AnthropicClient::apply_directive(
822 &mut req,
823 &structured::StructuredDirective {
824 force_tool: None,
825 response_format: Some(structured::ResponseFormat::JsonObject),
826 },
827 );
828 assert!(req.get("response_format").is_none());
829 assert!(req.get("tool_choice").is_none());
830 }
831
832 #[test]
833 fn test_native_structured_support_is_forced_tool() {
834 assert_eq!(
835 make_client().native_structured_support(),
836 structured::NativeStructuredSupport::ForcedTool
837 );
838 }
839}