vtcode-llm 0.136.7

LLM provider abstraction, client implementations, and streaming for VT Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum AnthropicFallback {
    #[serde(rename = "fallback")]
    Fallback {
        from: AnthropicFallbackModel,
        to: AnthropicFallbackModel,
    },
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicFallbackModel {
    pub model: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AnthropicRequest {
    pub model: String,
    pub messages: Vec<AnthropicMessage>,
    pub max_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<Value>, // Can be string or array of blocks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<AnthropicTool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<ThinkingConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<Value>, // Deprecated in favor of thinking, but kept for backward compat or direct effort passing
    #[serde(
        default,
        deserialize_with = "deserialize_boxed_output_config_opt",
        skip_serializing_if = "Option::is_none"
    )]
    pub output_config: Option<Box<AnthropicOutputConfig>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_management: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fallbacks: Option<Vec<AnthropicFallbackParam>>,
    /// Opaque credit token returned by a refused request's `stop_details.fallback_credit_token`.
    /// Echoed on the retry to avoid paying the prompt-cache cost twice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fallback_credit_token: Option<String>,
    pub stream: bool,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicFallbackParam {
    pub model: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<ThinkingConfig>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum ThinkingConfig {
    Enabled {
        budget_tokens: u32,
        #[serde(skip_serializing_if = "Option::is_none")]
        display: Option<ThinkingDisplay>,
    },
    Adaptive {
        #[serde(skip_serializing_if = "Option::is_none")]
        display: Option<ThinkingDisplay>,
    },
    Disabled,
    /// Catch-all for unknown thinking config types added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

/// Controls how thinking content is returned in API responses.
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ThinkingDisplay {
    /// Thinking blocks contain summarized thinking text (default on Claude 4 models).
    Summarized,
    /// Thinking blocks are returned with an empty `thinking` field; the `signature`
    /// still carries encrypted full thinking for multi-turn continuity
    /// (default on Claude Opus 4.7).
    Omitted,
    /// Catch-all for unknown display modes added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct AnthropicMessage {
    pub role: String,
    pub content: Vec<AnthropicContentBlock>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum AnthropicContentBlock {
    #[serde(rename = "text")]
    Text {
        text: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        citations: Option<Vec<TextCitation>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    #[serde(rename = "image")]
    Image {
        source: ImageSource,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    #[serde(rename = "tool_use")]
    ToolUse(Box<AnthropicToolUseBlock>),
    #[serde(rename = "tool_result")]
    ToolResult(Box<AnthropicToolResultBlock>),
    #[serde(rename = "thinking")]
    Thinking {
        thinking: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        signature: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    #[serde(rename = "redacted_thinking")]
    RedactedThinking {
        data: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    #[serde(rename = "compaction")]
    Compaction {
        content: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        cache_control: Option<CacheControl>,
    },
    /// Server-side tool use (e.g., tool search execution) - advanced-tool-use beta
    #[serde(rename = "server_tool_use")]
    ServerToolUse { id: String, name: String, input: Value },
    /// Tool search result containing discovered tool references - advanced-tool-use beta
    #[serde(rename = "tool_search_tool_result")]
    ToolSearchToolResult {
        tool_use_id: String,
        content: ToolSearchResultContent,
    },
    /// Files API upload reference used by Anthropic code execution.
    #[serde(rename = "container_upload")]
    ContainerUpload { file_id: String },
    /// Generic code execution result block used in some Anthropic responses.
    #[serde(rename = "code_execution_tool_result")]
    CodeExecutionToolResult { tool_use_id: String, content: Value },
    /// Bash code execution result block returned by Anthropic code execution.
    #[serde(rename = "bash_code_execution_tool_result")]
    BashCodeExecutionToolResult { tool_use_id: String, content: Value },
    /// Text editor code execution result block returned by Anthropic code execution.
    #[serde(rename = "text_editor_code_execution_tool_result")]
    TextEditorCodeExecutionToolResult { tool_use_id: String, content: Value },
    /// Native web search result blocks returned by Anthropic web search tools.
    #[serde(rename = "web_search_tool_result")]
    WebSearchToolResult { tool_use_id: String, content: Value },
    /// Advisor tool result block returned by the server-side advisor sub-inference.
    /// `content` is the verbatim result union (`advisor_result`,
    /// `advisor_redacted_result`, or `advisor_tool_result_error`).
    #[serde(rename = "advisor_tool_result")]
    AdvisorToolResult { tool_use_id: String, content: Value },
    /// Fallback content block marking model boundary in server-side fallback
    #[serde(rename = "fallback")]
    Fallback {
        from: AnthropicFallbackModel,
        to: AnthropicFallbackModel,
    },
    /// Catch-all for unknown content block types added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

/// Extracted struct for `AnthropicContentBlock::ToolUse` (boxed to reduce enum size).
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicToolUseBlock {
    pub id: String,
    pub name: String,
    pub input: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

/// Extracted struct for `AnthropicContentBlock::ToolResult` (boxed to reduce enum size).
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicToolResultBlock {
    pub tool_use_id: String,
    pub content: Value, // string or array of blocks
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_error: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

/// Content of a tool search result
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum ToolSearchResultContent {
    #[serde(rename = "tool_search_tool_search_result")]
    SearchResult { tool_references: Vec<ToolReference> },
    #[serde(rename = "tool_search_tool_result_error")]
    Error { error_code: String },
    /// Catch-all for unknown tool search result types.
    #[serde(other)]
    Unknown,
}

/// A reference to a discovered tool from tool search
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ToolReference {
    #[serde(rename = "type")]
    pub ref_type: Option<String>, // "tool_reference"
    pub tool_name: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum TextCitation {
    #[serde(rename = "char_location")]
    CharLocation {
        cited_text: String,
        document_index: usize,
        document_title: Option<String>,
        start_char_index: usize,
        end_char_index: usize,
    },
    #[serde(rename = "page_location")]
    PageLocation {
        cited_text: String,
        document_index: usize,
        document_title: Option<String>,
        start_page_number: usize,
        end_page_number: usize,
    },
    #[serde(rename = "content_block_location")]
    ContentBlockLocation {
        cited_text: String,
        document_index: usize,
        document_title: Option<String>,
        start_block_index: usize,
        end_block_index: usize,
    },
    #[serde(rename = "web_search_result_location")]
    WebSearchResultLocation {
        url: Option<String>,
        title: Option<String>,
        encrypted_index: Option<String>,
        cited_text: Option<String>,
    },
    /// Catch-all for unknown citation types.
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ImageSource {
    #[serde(rename = "type")]
    pub source_type: String, // "base64"
    pub media_type: String,
    pub data: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CacheControl {
    #[serde(rename = "type")]
    pub control_type: String, // "ephemeral"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ttl: Option<String>, // "5m" or "1h"
}

/// Anthropic tool definition
/// Supports both regular function tools and tool search tools (advanced-tool-use beta)
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum AnthropicTool {
    /// Tool search tool (regex or bm25)
    ToolSearch(AnthropicToolSearchTool),
    /// Native Anthropic code execution tool revision
    CodeExecution(AnthropicCodeExecutionTool),
    /// Native Anthropic memory tool revision
    Memory(AnthropicMemoryTool),
    /// Native Anthropic web search tool revision
    WebSearch(AnthropicWebSearchTool),
    /// Anthropic server-side advisor tool (beta `advisor-tool-2026-03-01`).
    Advisor(AnthropicAdvisorTool),
    /// Regular function tool
    Function(AnthropicFunctionTool),
}

/// Regular function tool definition for Anthropic API
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicFunctionTool {
    pub name: String,
    pub description: String,
    pub input_schema: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_examples: Option<Vec<Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_callers: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
    /// When true, the tool is deferred and only loaded when discovered via tool search
    #[serde(skip_serializing_if = "Option::is_none")]
    pub defer_loading: Option<bool>,
}

/// Native code execution tool definition for Anthropic API.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicCodeExecutionTool {
    /// Versioned code execution type (e.g. "code_execution_20250825")
    #[serde(rename = "type")]
    pub tool_type: String,
    /// Tool name (typically "code_execution")
    pub name: String,
}

/// Native memory tool definition for Anthropic API.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicMemoryTool {
    /// Versioned memory type (e.g. "memory_20250818")
    #[serde(rename = "type")]
    pub tool_type: String,
    /// Tool name (typically "memory")
    pub name: String,
}

/// Tool search tool definition for Anthropic's advanced-tool-use beta
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicToolSearchTool {
    /// The type of tool search: "tool_search_tool_regex_20251119" or "tool_search_tool_bm25_20251119"
    #[serde(rename = "type")]
    pub tool_type: String,
    /// Tool name (e.g., "tool_search_tool_regex" or "tool_search_tool_bm25")
    pub name: String,
}

/// Native web search tool definition for Anthropic API (PTC-enabled search revisions)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicWebSearchTool {
    /// Versioned web search type (e.g., "web_search_20260209")
    #[serde(rename = "type")]
    pub tool_type: String,
    /// Tool name (typically "web_search")
    pub name: String,
    /// Optional Anthropic-native web search configuration.
    #[serde(flatten, default, skip_serializing_if = "Map::is_empty")]
    pub options: Map<String, Value>,
}

/// Anthropic server-side advisor tool definition (beta `advisor-tool-2026-03-01`).
///
/// The executor model consults the advisor model for strategic guidance
/// mid-generation. The tool takes no client-supplied input; the server builds
/// the advisor's view from the full transcript.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicAdvisorTool {
    /// Fixed advisor tool type.
    #[serde(rename = "type")]
    pub tool_type: String, // "advisor_20260301"
    /// Fixed advisor tool name.
    pub name: String, // "advisor"
    /// Advisor model id (e.g. "claude-opus-4-8").
    pub model: String,
    /// Maximum number of advisor invocations per request.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_uses: Option<u32>,
    /// Caps the advisor's total output (thinking plus text) per call (min 1024).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
    /// Enables prompt caching for the advisor's own transcript across calls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub caching: Option<AnthropicAdvisorCaching>,
}

/// Prompt-caching configuration for the advisor tool.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicAdvisorCaching {
    /// Fixed cache type.
    #[serde(rename = "type")]
    pub cache_type: String, // "ephemeral"
    /// Cache lifetime ("5m" or "1h").
    pub ttl: String,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum AnthropicStreamEvent {
    #[serde(rename = "message_start")]
    MessageStart { message: AnthropicMessageResponse },
    #[serde(rename = "content_block_start")]
    ContentBlockStart {
        index: usize,
        content_block: AnthropicContentBlock,
    },
    #[serde(rename = "ping")]
    Ping,
    #[serde(rename = "content_block_delta")]
    ContentBlockDelta { index: usize, delta: AnthropicStreamDelta },
    #[serde(rename = "content_block_stop")]
    ContentBlockStop { index: usize },
    #[serde(rename = "message_delta")]
    MessageDelta {
        delta: AnthropicMessageDelta,
        usage: Option<AnthropicUsage>,
    },
    #[serde(rename = "message_stop")]
    MessageStop,
    #[serde(rename = "error")]
    Error { error: AnthropicErrorBody },
    /// Catch-all for unknown streaming event types added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum AnthropicStreamDelta {
    #[serde(rename = "text_delta")]
    TextDelta { text: String },
    #[serde(rename = "input_json_delta")]
    InputJsonDelta { partial_json: String },
    #[serde(rename = "thinking_delta")]
    ThinkingDelta { thinking: String },
    #[serde(rename = "signature_delta")]
    SignatureDelta { signature: String },
    #[serde(rename = "compaction_delta")]
    CompactionDelta { content: String },
    /// Catch-all for unknown delta types added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Deserialize)]
pub struct AnthropicMessageDelta {
    pub stop_reason: Option<String>,
    pub stop_sequence: Option<String>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct AnthropicStopDetails {
    #[serde(rename = "type")]
    pub stop_details_type: Option<String>,
    pub category: Option<String>,
    pub explanation: Option<String>,
    /// Opaque token that represents fallback credit when retrying on another model.
    /// Present when the refusal qualifies for fallback credit.
    pub fallback_credit_token: Option<String>,
    /// Whether the retry can append an assistant message continuing the refused model's
    /// partial output (`true`) or must use the unchanged request body (`false`).
    pub fallback_has_prefill_claim: Option<bool>,
}

#[derive(Debug, Deserialize)]
pub struct AnthropicMessageResponse {
    pub id: String,
    pub role: String,
    pub content: Vec<AnthropicContentBlock>,
    pub model: String,
    pub stop_reason: Option<String>,
    pub stop_sequence: Option<String>,
    pub stop_details: Option<AnthropicStopDetails>,
    pub usage: AnthropicUsage,
}

#[derive(Debug, Deserialize)]
pub struct AnthropicUsage {
    pub input_tokens: u32,
    pub output_tokens: u32,
    pub cache_creation_input_tokens: Option<u32>,
    pub cache_read_input_tokens: Option<u32>,
    /// Per-iteration token usage, populated when compaction triggers or server-side fallback runs.
    /// Each entry represents one sampling pass (compaction, message, or fallback_message).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub iterations: Option<Vec<AnthropicUsageIteration>>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AnthropicUsageIteration {
    Message {
        model: String,
        input_tokens: u32,
        output_tokens: u32,
        cache_creation_input_tokens: Option<u32>,
        cache_read_input_tokens: Option<u32>,
    },
    FallbackMessage {
        model: String,
        input_tokens: u32,
        output_tokens: u32,
        cache_creation_input_tokens: Option<u32>,
        cache_read_input_tokens: Option<u32>,
    },
    Compaction {
        input_tokens: u32,
        output_tokens: u32,
        cache_creation_input_tokens: Option<u32>,
        cache_read_input_tokens: Option<u32>,
    },
    /// Catch-all for unknown iteration types added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Deserialize)]
pub struct AnthropicErrorBody {
    #[serde(rename = "type")]
    pub error_type: String,
    pub message: String,
}

/// Output configuration for Anthropic API (e.g., effort parameter)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicOutputConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_budget: Option<AnthropicTaskBudget>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<AnthropicOutputFormat>,
}

impl AnthropicOutputConfig {
    fn is_empty(&self) -> bool {
        self.effort.is_none() && self.task_budget.is_none() && self.format.is_none()
    }

    fn into_boxed_if_non_empty(self) -> Option<Box<Self>> {
        (!self.is_empty()).then_some(Box::new(self))
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AnthropicTaskBudget {
    #[serde(rename = "type")]
    pub budget_type: String,
    pub total: u32,
}

/// Native structured output format for Anthropic responses.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AnthropicOutputFormat {
    JsonSchema {
        schema: Value,
    },
    /// Catch-all for unknown output format types added by the Anthropic API.
    #[serde(other)]
    Unknown,
}

/// Request body for Anthropic's count_tokens endpoint
/// <https://docs.anthropic.com/en/api/messages-count-tokens>
#[derive(Debug, Serialize, Deserialize)]
pub struct CountTokensRequest {
    pub model: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<Value>,
    pub messages: Vec<AnthropicMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<AnthropicTool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<ThinkingConfig>,
}

fn deserialize_boxed_output_config_opt<'de, D>(deserializer: D) -> Result<Option<Box<AnthropicOutputConfig>>, D::Error>
where
    D: Deserializer<'de>,
{
    Option::<AnthropicOutputConfig>::deserialize(deserializer)
        .map(|value| value.and_then(AnthropicOutputConfig::into_boxed_if_non_empty))
}

/// Response from Anthropic's count_tokens endpoint
#[derive(Debug, Deserialize)]
pub struct CountTokensResponse {
    pub input_tokens: u32,
}

#[cfg(test)]
mod tests {
    use super::{AnthropicContentBlock, AnthropicOutputConfig, AnthropicRequest, AnthropicStreamEvent, TextCitation};

    #[test]
    fn stream_content_block_start_thinking_allows_missing_signature() {
        let payload = r#"{
            "type": "content_block_start",
            "index": 0,
            "content_block": {
                "type": "thinking",
                "thinking": "Drafting plan"
            }
        }"#;

        let event: AnthropicStreamEvent = serde_json::from_str(payload).expect("should deserialize thinking block");
        match event {
            AnthropicStreamEvent::ContentBlockStart {
                content_block: AnthropicContentBlock::Thinking { thinking, signature, .. },
                ..
            } => {
                assert_eq!(thinking, "Drafting plan");
                assert!(signature.is_none());
            }
            other => panic!("expected thinking content_block_start, got {other:?}"),
        }
    }

    #[test]
    fn stream_content_block_start_accepts_web_search_tool_result() {
        let payload = r#"{
            "type": "content_block_start",
            "index": 1,
            "content_block": {
                "type": "web_search_tool_result",
                "tool_use_id": "srvtoolu_123",
                "content": [{
                    "type": "web_search_result",
                    "title": "Rust Releases",
                    "url": "https://blog.rust-lang.org"
                }]
            }
        }"#;

        let event: AnthropicStreamEvent =
            serde_json::from_str(payload).expect("should deserialize web search tool result");
        match event {
            AnthropicStreamEvent::ContentBlockStart {
                content_block: AnthropicContentBlock::WebSearchToolResult { tool_use_id, .. },
                ..
            } => assert_eq!(tool_use_id, "srvtoolu_123"),
            other => panic!("expected web_search_tool_result content_block_start, got {other:?}"),
        }
    }

    #[test]
    fn stream_content_block_start_accepts_bash_code_execution_result() {
        let payload = r#"{
            "type": "content_block_start",
            "index": 1,
            "content_block": {
                "type": "bash_code_execution_tool_result",
                "tool_use_id": "srvtoolu_456",
                "content": {
                    "type": "bash_code_execution_result",
                    "stdout": "Python 3.11.12",
                    "stderr": "",
                    "return_code": 0
                }
            }
        }"#;

        let event: AnthropicStreamEvent =
            serde_json::from_str(payload).expect("should deserialize bash code execution result");
        match event {
            AnthropicStreamEvent::ContentBlockStart {
                content_block: AnthropicContentBlock::BashCodeExecutionToolResult { tool_use_id, .. },
                ..
            } => assert_eq!(tool_use_id, "srvtoolu_456"),
            other => panic!("expected bash_code_execution_tool_result content_block_start, got {other:?}"),
        }
    }

    #[test]
    fn stream_content_block_start_accepts_text_editor_code_execution_result() {
        let payload = r#"{
            "type": "content_block_start",
            "index": 2,
            "content_block": {
                "type": "text_editor_code_execution_tool_result",
                "tool_use_id": "srvtoolu_789",
                "content": {
                    "type": "text_editor_code_execution_result",
                    "is_file_update": false
                }
            }
        }"#;

        let event: AnthropicStreamEvent =
            serde_json::from_str(payload).expect("should deserialize text editor code execution result");
        match event {
            AnthropicStreamEvent::ContentBlockStart {
                content_block: AnthropicContentBlock::TextEditorCodeExecutionToolResult { tool_use_id, .. },
                ..
            } => assert_eq!(tool_use_id, "srvtoolu_789"),
            other => panic!("expected text_editor_code_execution_tool_result content_block_start, got {other:?}"),
        }
    }

    #[test]
    fn text_block_accepts_web_search_result_citation() {
        let payload = r#"{
            "type": "text",
            "text": "Rust 1.82 shipped",
            "citations": [{
                "type": "web_search_result_location",
                "url": "https://blog.rust-lang.org",
                "title": "Rust Blog",
                "encrypted_index": "enc_123",
                "cited_text": "Rust 1.82 shipped"
            }]
        }"#;

        let block: AnthropicContentBlock = serde_json::from_str(payload).expect("should deserialize cited text block");
        match block {
            AnthropicContentBlock::Text { citations: Some(citations), .. } => {
                assert!(matches!(
                    &citations[0],
                    TextCitation::WebSearchResultLocation {
                        url: Some(url),
                        title: Some(title),
                        encrypted_index: Some(index),
                        cited_text: Some(cited_text),
                    } if url == "https://blog.rust-lang.org"
                        && title == "Rust Blog"
                        && index == "enc_123"
                        && cited_text == "Rust 1.82 shipped"
                ));
            }
            other => panic!("expected text block with citations, got {other:?}"),
        }
    }

    #[test]
    fn empty_output_config_deserializes_to_none() {
        let request: AnthropicRequest = serde_json::from_str(
            r#"{
                "model": "claude-sonnet",
                "messages": [],
                "max_tokens": 128,
                "output_config": {},
                "stream": false
            }"#,
        )
        .expect("should deserialize request");

        assert!(request.output_config.is_none());
    }

    #[test]
    fn boxed_output_config_is_smaller_than_inline_option() {
        use std::mem::size_of;

        assert!(size_of::<Option<Box<AnthropicOutputConfig>>>() < size_of::<Option<AnthropicOutputConfig>>());
    }
}