Skip to main content

openai_protocol/
messages.rs

1//! Anthropic Messages API protocol definitions
2//!
3//! This module provides Rust types for the Anthropic Messages API.
4//! See: https://docs.anthropic.com/en/api/messages
5
6use std::collections::HashMap;
7
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10use validator::Validate;
11
12use crate::validated::Normalizable;
13
14// ============================================================================
15// Request Types
16// ============================================================================
17
18/// Request to create a message using the Anthropic Messages API.
19///
20/// This is the main request type for `/v1/messages` endpoint.
21#[serde_with::skip_serializing_none]
22#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
23#[validate(schema(function = "validate_mcp_config"))]
24pub struct CreateMessageRequest {
25    /// The model that will complete your prompt.
26    #[validate(length(min = 1, message = "model field is required and cannot be empty"))]
27    pub model: String,
28
29    /// Input messages for the conversation.
30    #[validate(length(min = 1, message = "messages array is required and cannot be empty"))]
31    pub messages: Vec<InputMessage>,
32
33    /// The maximum number of tokens to generate before stopping.
34    #[validate(range(min = 1, message = "max_tokens must be greater than 0"))]
35    pub max_tokens: u32,
36
37    /// An object describing metadata about the request.
38    pub metadata: Option<Metadata>,
39
40    /// Service tier for the request (auto or standard_only).
41    pub service_tier: Option<ServiceTier>,
42
43    /// Custom text sequences that will cause the model to stop generating.
44    pub stop_sequences: Option<Vec<String>>,
45
46    /// Whether to incrementally stream the response using server-sent events.
47    pub stream: Option<bool>,
48
49    /// System prompt for providing context and instructions.
50    pub system: Option<SystemContent>,
51
52    /// Amount of randomness injected into the response (0.0 to 1.0).
53    pub temperature: Option<f64>,
54
55    /// Configuration for extended thinking.
56    pub thinking: Option<ThinkingConfig>,
57
58    /// How the model should use the provided tools.
59    pub tool_choice: Option<ToolChoice>,
60
61    /// Definitions of tools that the model may use.
62    pub tools: Option<Vec<Tool>>,
63
64    /// Only sample from the top K options for each subsequent token.
65    pub top_k: Option<u32>,
66
67    /// Use nucleus sampling.
68    pub top_p: Option<f64>,
69
70    // Beta features
71    /// Container configuration for code execution (beta).
72    pub container: Option<ContainerConfig>,
73
74    /// MCP servers to be utilized in this request (beta).
75    pub mcp_servers: Option<Vec<McpServerConfig>>,
76}
77
78impl Normalizable for CreateMessageRequest {
79    // Use default no-op implementation
80}
81
82impl CreateMessageRequest {
83    /// Check if the request is for streaming
84    pub fn is_stream(&self) -> bool {
85        self.stream.unwrap_or(false)
86    }
87
88    /// Get the model name
89    pub fn get_model(&self) -> &str {
90        &self.model
91    }
92
93    /// Check if the request contains any `mcp_toolset` tool entries.
94    pub fn has_mcp_toolset(&self) -> bool {
95        self.tools
96            .as_ref()
97            .is_some_and(|tools| tools.iter().any(|t| matches!(t, Tool::McpToolset(_))))
98    }
99
100    /// Return MCP server configs if present and non-empty.
101    pub fn mcp_server_configs(&self) -> Option<&[McpServerConfig]> {
102        self.mcp_servers
103            .as_deref()
104            .filter(|servers| !servers.is_empty())
105    }
106}
107
108/// Validate that `mcp_servers` is non-empty when `mcp_toolset` tools are present.
109fn validate_mcp_config(req: &CreateMessageRequest) -> Result<(), validator::ValidationError> {
110    if req.has_mcp_toolset() && req.mcp_server_configs().is_none() {
111        let mut e = validator::ValidationError::new("mcp_servers_required");
112        e.message = Some("mcp_servers is required when mcp_toolset tools are present".into());
113        return Err(e);
114    }
115    Ok(())
116}
117
118/// Request metadata
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct Metadata {
121    /// An external identifier for the user who is associated with the request.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub user_id: Option<String>,
124}
125
126/// Service tier options
127#[derive(Debug, Clone, Serialize, Deserialize)]
128#[serde(rename_all = "snake_case")]
129pub enum ServiceTier {
130    Auto,
131    StandardOnly,
132}
133
134/// System content can be a string or an array of text blocks
135#[derive(Debug, Clone, Serialize, Deserialize)]
136#[serde(untagged)]
137pub enum SystemContent {
138    String(String),
139    Blocks(Vec<TextBlock>),
140}
141
142/// A single input message in a conversation
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct InputMessage {
145    /// The role of the message sender (user or assistant)
146    pub role: Role,
147
148    /// The content of the message
149    pub content: InputContent,
150}
151
152/// Role of a message sender
153#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
154#[serde(rename_all = "lowercase")]
155pub enum Role {
156    User,
157    Assistant,
158}
159
160/// Input content can be a string or an array of content blocks
161#[derive(Debug, Clone, Serialize, Deserialize)]
162#[serde(untagged)]
163pub enum InputContent {
164    String(String),
165    Blocks(Vec<InputContentBlock>),
166}
167
168// ============================================================================
169// Input Content Blocks
170// ============================================================================
171
172/// Input content block types
173#[derive(Debug, Clone, Serialize, Deserialize)]
174#[serde(tag = "type", rename_all = "snake_case")]
175pub enum InputContentBlock {
176    /// Text content
177    Text(TextBlock),
178    /// Image content
179    Image(ImageBlock),
180    /// Document content
181    Document(DocumentBlock),
182    /// Tool use block (for assistant messages)
183    ToolUse(ToolUseBlock),
184    /// Tool result block (for user messages)
185    ToolResult(ToolResultBlock),
186    /// Thinking block
187    Thinking(ThinkingBlock),
188    /// Redacted thinking block
189    RedactedThinking(RedactedThinkingBlock),
190    /// Server tool use block
191    ServerToolUse(ServerToolUseBlock),
192    /// Search result block
193    SearchResult(SearchResultBlock),
194    /// Web search tool result block
195    WebSearchToolResult(WebSearchToolResultBlock),
196}
197
198/// Text content block
199#[serde_with::skip_serializing_none]
200#[derive(Debug, Clone, Serialize, Deserialize)]
201pub struct TextBlock {
202    /// The text content
203    pub text: String,
204
205    /// Cache control for this block
206    pub cache_control: Option<CacheControl>,
207
208    /// Citations for this text block
209    pub citations: Option<Vec<Citation>>,
210}
211
212/// Image content block
213#[derive(Debug, Clone, Serialize, Deserialize)]
214pub struct ImageBlock {
215    /// The image source
216    pub source: ImageSource,
217
218    /// Cache control for this block
219    #[serde(skip_serializing_if = "Option::is_none")]
220    pub cache_control: Option<CacheControl>,
221}
222
223/// Image source (base64 or URL)
224#[derive(Debug, Clone, Serialize, Deserialize)]
225#[serde(tag = "type", rename_all = "snake_case")]
226pub enum ImageSource {
227    Base64 { media_type: String, data: String },
228    Url { url: String },
229}
230
231/// Document content block
232#[serde_with::skip_serializing_none]
233#[derive(Debug, Clone, Serialize, Deserialize)]
234pub struct DocumentBlock {
235    /// The document source
236    pub source: DocumentSource,
237
238    /// Cache control for this block
239    pub cache_control: Option<CacheControl>,
240
241    /// Optional title for the document
242    pub title: Option<String>,
243
244    /// Optional context for the document
245    pub context: Option<String>,
246
247    /// Citations configuration
248    pub citations: Option<CitationsConfig>,
249}
250
251/// Document source (base64, text, or URL)
252#[derive(Debug, Clone, Serialize, Deserialize)]
253#[serde(tag = "type", rename_all = "snake_case")]
254pub enum DocumentSource {
255    Base64 { media_type: String, data: String },
256    Text { data: String },
257    Url { url: String },
258    Content { content: Vec<InputContentBlock> },
259}
260
261/// Tool use block (in assistant messages)
262#[derive(Debug, Clone, Serialize, Deserialize)]
263pub struct ToolUseBlock {
264    /// Unique identifier for this tool use
265    pub id: String,
266
267    /// Name of the tool being used
268    pub name: String,
269
270    /// Input arguments for the tool
271    pub input: Value,
272
273    /// Cache control for this block
274    #[serde(skip_serializing_if = "Option::is_none")]
275    pub cache_control: Option<CacheControl>,
276}
277
278/// Tool result block (in user messages)
279#[serde_with::skip_serializing_none]
280#[derive(Debug, Clone, Serialize, Deserialize)]
281pub struct ToolResultBlock {
282    /// The ID of the tool use this is a result for
283    pub tool_use_id: String,
284
285    /// The result content (string or blocks)
286    pub content: Option<ToolResultContent>,
287
288    /// Whether this result indicates an error
289    pub is_error: Option<bool>,
290
291    /// Cache control for this block
292    pub cache_control: Option<CacheControl>,
293}
294
295/// Tool result content (string or blocks)
296#[derive(Debug, Clone, Serialize, Deserialize)]
297#[serde(untagged)]
298pub enum ToolResultContent {
299    String(String),
300    Blocks(Vec<ToolResultContentBlock>),
301}
302
303/// Content blocks allowed in tool results
304#[derive(Debug, Clone, Serialize, Deserialize)]
305#[serde(tag = "type", rename_all = "snake_case")]
306pub enum ToolResultContentBlock {
307    Text(TextBlock),
308    Image(ImageBlock),
309    Document(DocumentBlock),
310    SearchResult(SearchResultBlock),
311}
312
313/// Thinking block
314#[derive(Debug, Clone, Serialize, Deserialize)]
315pub struct ThinkingBlock {
316    /// The thinking content
317    pub thinking: String,
318
319    /// Signature for the thinking block
320    pub signature: String,
321}
322
323/// Redacted thinking block
324#[derive(Debug, Clone, Serialize, Deserialize)]
325pub struct RedactedThinkingBlock {
326    /// The encrypted/redacted data
327    pub data: String,
328}
329
330/// Server tool use block
331#[derive(Debug, Clone, Serialize, Deserialize)]
332pub struct ServerToolUseBlock {
333    /// Unique identifier for this tool use
334    pub id: String,
335
336    /// Name of the server tool
337    pub name: String,
338
339    /// Input arguments for the tool
340    pub input: Value,
341
342    /// Cache control for this block
343    #[serde(skip_serializing_if = "Option::is_none")]
344    pub cache_control: Option<CacheControl>,
345}
346
347/// Search result block
348#[serde_with::skip_serializing_none]
349#[derive(Debug, Clone, Serialize, Deserialize)]
350pub struct SearchResultBlock {
351    /// Source URL or identifier
352    pub source: String,
353
354    /// Title of the search result
355    pub title: String,
356
357    /// Content of the search result
358    pub content: Vec<TextBlock>,
359
360    /// Cache control for this block
361    pub cache_control: Option<CacheControl>,
362
363    /// Citations configuration
364    pub citations: Option<CitationsConfig>,
365}
366
367/// Web search tool result block
368#[derive(Debug, Clone, Serialize, Deserialize)]
369pub struct WebSearchToolResultBlock {
370    /// The tool use ID this result is for
371    pub tool_use_id: String,
372
373    /// The search results or error
374    pub content: WebSearchToolResultContent,
375
376    /// Cache control for this block
377    #[serde(skip_serializing_if = "Option::is_none")]
378    pub cache_control: Option<CacheControl>,
379}
380
381/// Web search tool result content
382#[derive(Debug, Clone, Serialize, Deserialize)]
383#[serde(untagged)]
384pub enum WebSearchToolResultContent {
385    Results(Vec<WebSearchResultBlock>),
386    Error(WebSearchToolResultError),
387}
388
389/// Web search result block
390#[derive(Debug, Clone, Serialize, Deserialize)]
391pub struct WebSearchResultBlock {
392    /// Title of the search result
393    pub title: String,
394
395    /// URL of the search result
396    pub url: String,
397
398    /// Encrypted content
399    pub encrypted_content: String,
400
401    /// Page age (if available)
402    #[serde(skip_serializing_if = "Option::is_none")]
403    pub page_age: Option<String>,
404}
405
406/// Web search tool result error
407#[derive(Debug, Clone, Serialize, Deserialize)]
408pub struct WebSearchToolResultError {
409    #[serde(rename = "type")]
410    pub error_type: String,
411    pub error_code: WebSearchToolResultErrorCode,
412}
413
414/// Web search tool result error codes
415#[derive(Debug, Clone, Serialize, Deserialize)]
416#[serde(rename_all = "snake_case")]
417pub enum WebSearchToolResultErrorCode {
418    InvalidToolInput,
419    Unavailable,
420    MaxUsesExceeded,
421    TooManyRequests,
422    QueryTooLong,
423}
424
425/// Cache control configuration
426#[derive(Debug, Clone, Serialize, Deserialize)]
427#[serde(tag = "type", rename_all = "snake_case")]
428pub enum CacheControl {
429    Ephemeral,
430}
431
432/// Citations configuration
433#[derive(Debug, Clone, Serialize, Deserialize)]
434pub struct CitationsConfig {
435    #[serde(skip_serializing_if = "Option::is_none")]
436    pub enabled: Option<bool>,
437}
438
439/// Citation types
440#[derive(Debug, Clone, Serialize, Deserialize)]
441#[serde(tag = "type", rename_all = "snake_case")]
442#[expect(
443    clippy::enum_variant_names,
444    reason = "variant names match the OpenAI API citation type discriminators (char_location, page_location, etc.)"
445)]
446pub enum Citation {
447    CharLocation(CharLocationCitation),
448    PageLocation(PageLocationCitation),
449    ContentBlockLocation(ContentBlockLocationCitation),
450    WebSearchResultLocation(WebSearchResultLocationCitation),
451    SearchResultLocation(SearchResultLocationCitation),
452}
453
454/// Character location citation
455#[derive(Debug, Clone, Serialize, Deserialize)]
456pub struct CharLocationCitation {
457    pub cited_text: String,
458    pub document_index: u32,
459    pub document_title: Option<String>,
460    pub start_char_index: u32,
461    pub end_char_index: u32,
462    #[serde(skip_serializing_if = "Option::is_none")]
463    pub file_id: Option<String>,
464}
465
466/// Page location citation
467#[derive(Debug, Clone, Serialize, Deserialize)]
468pub struct PageLocationCitation {
469    pub cited_text: String,
470    pub document_index: u32,
471    pub document_title: Option<String>,
472    pub start_page_number: u32,
473    pub end_page_number: u32,
474}
475
476/// Content block location citation
477#[derive(Debug, Clone, Serialize, Deserialize)]
478pub struct ContentBlockLocationCitation {
479    pub cited_text: String,
480    pub document_index: u32,
481    pub document_title: Option<String>,
482    pub start_block_index: u32,
483    pub end_block_index: u32,
484}
485
486/// Web search result location citation
487#[derive(Debug, Clone, Serialize, Deserialize)]
488pub struct WebSearchResultLocationCitation {
489    pub cited_text: String,
490    pub url: String,
491    pub title: Option<String>,
492    pub encrypted_index: String,
493}
494
495/// Search result location citation
496#[derive(Debug, Clone, Serialize, Deserialize)]
497pub struct SearchResultLocationCitation {
498    pub cited_text: String,
499    pub search_result_index: u32,
500    pub source: String,
501    pub title: Option<String>,
502    pub start_block_index: u32,
503    pub end_block_index: u32,
504}
505
506// ============================================================================
507// Tool Definitions
508// ============================================================================
509
510/// Tool definition
511#[derive(Debug, Clone, Serialize, Deserialize)]
512#[serde(untagged)]
513pub enum Tool {
514    /// MCP toolset definition
515    McpToolset(McpToolset),
516    /// Custom tool definition
517    Custom(CustomTool),
518    /// Bash tool (computer use)
519    Bash(BashTool),
520    /// Text editor tool (computer use)
521    TextEditor(TextEditorTool),
522    /// Web search tool
523    WebSearch(WebSearchTool),
524}
525
526/// Custom tool definition
527#[serde_with::skip_serializing_none]
528#[derive(Debug, Clone, Serialize, Deserialize)]
529pub struct CustomTool {
530    /// Name of the tool
531    pub name: String,
532
533    /// Optional type (defaults to "custom")
534    #[serde(rename = "type")]
535    pub tool_type: Option<String>,
536
537    /// Description of what this tool does
538    pub description: Option<String>,
539
540    /// JSON schema for the tool's input
541    pub input_schema: InputSchema,
542
543    /// Cache control for this tool
544    pub cache_control: Option<CacheControl>,
545}
546
547/// JSON Schema for tool input
548#[serde_with::skip_serializing_none]
549#[derive(Debug, Clone, Serialize, Deserialize)]
550pub struct InputSchema {
551    #[serde(rename = "type")]
552    pub schema_type: String,
553
554    pub properties: Option<HashMap<String, Value>>,
555
556    pub required: Option<Vec<String>>,
557
558    /// Additional properties can be stored here
559    #[serde(flatten)]
560    pub additional: HashMap<String, Value>,
561}
562
563/// Bash tool for computer use
564#[derive(Debug, Clone, Serialize, Deserialize)]
565pub struct BashTool {
566    #[serde(rename = "type")]
567    pub tool_type: String, // "bash_20250124"
568
569    pub name: String, // "bash"
570
571    #[serde(skip_serializing_if = "Option::is_none")]
572    pub cache_control: Option<CacheControl>,
573}
574
575/// Text editor tool for computer use
576#[derive(Debug, Clone, Serialize, Deserialize)]
577pub struct TextEditorTool {
578    #[serde(rename = "type")]
579    pub tool_type: String, // "text_editor_20250124", etc.
580
581    pub name: String, // "str_replace_editor"
582
583    #[serde(skip_serializing_if = "Option::is_none")]
584    pub cache_control: Option<CacheControl>,
585}
586
587/// Web search tool
588#[serde_with::skip_serializing_none]
589#[derive(Debug, Clone, Serialize, Deserialize)]
590pub struct WebSearchTool {
591    #[serde(rename = "type")]
592    pub tool_type: String, // "web_search_20250305"
593
594    pub name: String, // "web_search"
595
596    pub allowed_domains: Option<Vec<String>>,
597
598    pub blocked_domains: Option<Vec<String>>,
599
600    pub max_uses: Option<u32>,
601
602    pub user_location: Option<UserLocation>,
603
604    pub cache_control: Option<CacheControl>,
605}
606
607/// User location for web search
608#[serde_with::skip_serializing_none]
609#[derive(Debug, Clone, Serialize, Deserialize)]
610pub struct UserLocation {
611    #[serde(rename = "type")]
612    pub location_type: String, // "approximate"
613
614    pub city: Option<String>,
615
616    pub region: Option<String>,
617
618    pub country: Option<String>,
619
620    pub timezone: Option<String>,
621}
622
623// ============================================================================
624// Tool Choice
625// ============================================================================
626
627/// How the model should use the provided tools
628#[serde_with::skip_serializing_none]
629#[derive(Debug, Clone, Serialize, Deserialize)]
630#[serde(tag = "type", rename_all = "snake_case")]
631pub enum ToolChoice {
632    /// The model will automatically decide whether to use tools
633    Auto {
634        disable_parallel_tool_use: Option<bool>,
635    },
636    /// The model will use any available tools
637    Any {
638        disable_parallel_tool_use: Option<bool>,
639    },
640    /// The model will use the specified tool
641    Tool {
642        name: String,
643        disable_parallel_tool_use: Option<bool>,
644    },
645    /// The model will not use tools
646    None,
647}
648
649// ============================================================================
650// Thinking Configuration
651// ============================================================================
652
653/// Configuration for extended thinking
654#[derive(Debug, Clone, Serialize, Deserialize)]
655#[serde(tag = "type", rename_all = "snake_case")]
656pub enum ThinkingConfig {
657    /// Enable extended thinking
658    Enabled {
659        /// Budget in tokens for thinking (minimum 1024)
660        budget_tokens: u32,
661    },
662    /// Disable extended thinking
663    Disabled,
664}
665
666// ============================================================================
667// Response Types
668// ============================================================================
669
670/// Response message from the API
671#[derive(Debug, Clone, Serialize, Deserialize)]
672pub struct Message {
673    /// Unique object identifier
674    pub id: String,
675
676    /// Object type (always "message")
677    #[serde(rename = "type")]
678    pub message_type: String,
679
680    /// Conversational role (always "assistant")
681    pub role: String,
682
683    /// Content generated by the model
684    pub content: Vec<ContentBlock>,
685
686    /// The model that generated the message
687    pub model: String,
688
689    /// The reason the model stopped generating
690    pub stop_reason: Option<StopReason>,
691
692    /// Which custom stop sequence was generated (if any)
693    pub stop_sequence: Option<String>,
694
695    /// Billing and rate-limit usage
696    pub usage: Usage,
697}
698
699/// Output content block types
700#[derive(Debug, Clone, Serialize, Deserialize)]
701#[serde(tag = "type", rename_all = "snake_case")]
702pub enum ContentBlock {
703    /// Text content
704    Text {
705        text: String,
706        #[serde(skip_serializing_if = "Option::is_none")]
707        citations: Option<Vec<Citation>>,
708    },
709    /// Tool use by the model
710    ToolUse {
711        id: String,
712        name: String,
713        input: Value,
714    },
715    /// Thinking content
716    Thinking { thinking: String, signature: String },
717    /// Redacted thinking content
718    RedactedThinking { data: String },
719    /// Server tool use
720    ServerToolUse {
721        id: String,
722        name: String,
723        input: Value,
724    },
725    /// Web search tool result
726    WebSearchToolResult {
727        tool_use_id: String,
728        content: WebSearchToolResultContent,
729    },
730    /// MCP tool use (beta) - model requesting tool execution via MCP
731    McpToolUse {
732        id: String,
733        name: String,
734        server_name: String,
735        input: Value,
736    },
737    /// MCP tool result (beta) - result from MCP tool execution
738    McpToolResult {
739        tool_use_id: String,
740        content: Option<ToolResultContent>,
741        is_error: Option<bool>,
742    },
743}
744
745/// Stop reasons
746#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
747#[serde(rename_all = "snake_case")]
748pub enum StopReason {
749    /// The model reached a natural stopping point
750    EndTurn,
751    /// We exceeded the requested max_tokens
752    MaxTokens,
753    /// One of the custom stop_sequences was generated
754    StopSequence,
755    /// The model invoked one or more tools
756    ToolUse,
757    /// We paused a long-running turn
758    PauseTurn,
759    /// Streaming classifiers intervened
760    Refusal,
761}
762
763/// Billing and rate-limit usage
764#[serde_with::skip_serializing_none]
765#[derive(Debug, Clone, Serialize, Deserialize)]
766pub struct Usage {
767    /// The number of input tokens used
768    pub input_tokens: u32,
769
770    /// The number of output tokens used
771    pub output_tokens: u32,
772
773    /// The number of input tokens used to create the cache entry
774    pub cache_creation_input_tokens: Option<u32>,
775
776    /// The number of input tokens read from the cache
777    pub cache_read_input_tokens: Option<u32>,
778
779    /// Breakdown of cached tokens by TTL
780    pub cache_creation: Option<CacheCreation>,
781
782    /// Server tool usage information
783    pub server_tool_use: Option<ServerToolUsage>,
784
785    /// Service tier used for the request
786    pub service_tier: Option<String>,
787}
788
789/// Cache creation breakdown
790#[derive(Debug, Clone, Serialize, Deserialize)]
791pub struct CacheCreation {
792    #[serde(flatten)]
793    pub tokens_by_ttl: HashMap<String, u32>,
794}
795
796/// Server tool usage information
797#[derive(Debug, Clone, Serialize, Deserialize)]
798pub struct ServerToolUsage {
799    pub web_search_requests: u32,
800}
801
802// ============================================================================
803// Streaming Event Types
804// ============================================================================
805
806/// Server-sent event wrapper
807#[derive(Debug, Clone, Serialize, Deserialize)]
808#[serde(tag = "type", rename_all = "snake_case")]
809pub enum MessageStreamEvent {
810    /// Start of a new message
811    MessageStart { message: Message },
812    /// Update to a message
813    MessageDelta {
814        delta: MessageDelta,
815        usage: MessageDeltaUsage,
816    },
817    /// End of a message
818    MessageStop,
819    /// Start of a content block
820    ContentBlockStart {
821        index: u32,
822        content_block: ContentBlock,
823    },
824    /// Update to a content block
825    ContentBlockDelta {
826        index: u32,
827        delta: ContentBlockDelta,
828    },
829    /// End of a content block
830    ContentBlockStop { index: u32 },
831    /// Ping event (for keep-alive)
832    Ping,
833    /// Error event
834    Error { error: ErrorResponse },
835}
836
837/// Message delta for streaming updates
838#[serde_with::skip_serializing_none]
839#[derive(Debug, Clone, Serialize, Deserialize)]
840pub struct MessageDelta {
841    pub stop_reason: Option<StopReason>,
842
843    pub stop_sequence: Option<String>,
844}
845
846/// Usage delta for streaming updates
847#[serde_with::skip_serializing_none]
848#[derive(Debug, Clone, Serialize, Deserialize)]
849pub struct MessageDeltaUsage {
850    pub output_tokens: u32,
851
852    pub input_tokens: Option<u32>,
853
854    pub cache_creation_input_tokens: Option<u32>,
855
856    pub cache_read_input_tokens: Option<u32>,
857
858    pub server_tool_use: Option<ServerToolUsage>,
859}
860
861/// Content block delta for streaming updates
862#[derive(Debug, Clone, Serialize, Deserialize)]
863#[serde(tag = "type", rename_all = "snake_case")]
864#[expect(
865    clippy::enum_variant_names,
866    reason = "variant names match the OpenAI/Anthropic streaming delta type discriminators (text_delta, input_json_delta, etc.)"
867)]
868pub enum ContentBlockDelta {
869    /// Text delta
870    TextDelta { text: String },
871    /// JSON input delta (for tool use)
872    InputJsonDelta { partial_json: String },
873    /// Thinking delta
874    ThinkingDelta { thinking: String },
875    /// Signature delta
876    SignatureDelta { signature: String },
877    /// Citations delta
878    CitationsDelta { citation: Citation },
879}
880
881// ============================================================================
882// Error Types
883// ============================================================================
884
885/// Error response
886#[derive(Debug, Clone, Serialize, Deserialize)]
887pub struct ErrorResponse {
888    #[serde(rename = "type")]
889    pub error_type: String,
890
891    pub message: String,
892}
893
894/// API error types
895#[derive(Debug, Clone, Serialize, Deserialize)]
896#[serde(tag = "type", rename_all = "snake_case")]
897#[expect(
898    clippy::enum_variant_names,
899    reason = "variant names match the OpenAI API error type discriminators (invalid_request_error, authentication_error, etc.)"
900)]
901pub enum ApiError {
902    InvalidRequestError { message: String },
903    AuthenticationError { message: String },
904    BillingError { message: String },
905    PermissionError { message: String },
906    NotFoundError { message: String },
907    RateLimitError { message: String },
908    TimeoutError { message: String },
909    ApiError { message: String },
910    OverloadedError { message: String },
911}
912
913// ============================================================================
914// Count Tokens Types
915// ============================================================================
916
917/// Request to count tokens in a message
918#[serde_with::skip_serializing_none]
919#[derive(Debug, Clone, Serialize, Deserialize)]
920pub struct CountMessageTokensRequest {
921    /// The model to use for token counting
922    pub model: String,
923
924    /// Input messages
925    pub messages: Vec<InputMessage>,
926
927    /// System prompt
928    pub system: Option<SystemContent>,
929
930    /// Thinking configuration
931    pub thinking: Option<ThinkingConfig>,
932
933    /// Tool choice
934    pub tool_choice: Option<ToolChoice>,
935
936    /// Tool definitions
937    pub tools: Option<Vec<Tool>>,
938}
939
940/// Response from token counting
941#[derive(Debug, Clone, Serialize, Deserialize)]
942pub struct CountMessageTokensResponse {
943    pub input_tokens: u32,
944}
945
946// ============================================================================
947// Model Info Types
948// ============================================================================
949
950/// Model information
951#[derive(Debug, Clone, Serialize, Deserialize)]
952pub struct ModelInfo {
953    /// Object type (always "model")
954    #[serde(rename = "type")]
955    pub model_type: String,
956
957    /// Model ID
958    pub id: String,
959
960    /// Display name
961    pub display_name: String,
962
963    /// When the model was created
964    pub created_at: String,
965}
966
967/// List of models response
968#[derive(Debug, Clone, Serialize, Deserialize)]
969pub struct ListModelsResponse {
970    pub data: Vec<ModelInfo>,
971    pub has_more: bool,
972    pub first_id: Option<String>,
973    pub last_id: Option<String>,
974}
975
976// ============================================================================
977// Beta Features - Container & MCP Configuration
978// ============================================================================
979
980/// Container configuration for code execution (beta)
981#[serde_with::skip_serializing_none]
982#[derive(Debug, Clone, Serialize, Deserialize)]
983pub struct ContainerConfig {
984    /// Container ID for reuse across requests
985    pub id: Option<String>,
986
987    /// Skills to be loaded in the container
988    pub skills: Option<Vec<String>>,
989}
990
991/// MCP server configuration (beta)
992#[serde_with::skip_serializing_none]
993#[derive(Debug, Clone, Serialize, Deserialize)]
994pub struct McpServerConfig {
995    /// Name of the MCP server
996    pub name: String,
997
998    /// MCP server URL
999    pub url: String,
1000
1001    /// Authorization token (if required)
1002    pub authorization_token: Option<String>,
1003
1004    /// Tool configuration for this server
1005    pub tool_configuration: Option<McpToolConfiguration>,
1006}
1007
1008/// MCP tool configuration
1009#[serde_with::skip_serializing_none]
1010#[derive(Debug, Clone, Serialize, Deserialize)]
1011pub struct McpToolConfiguration {
1012    /// Whether to allow all tools
1013    pub enabled: Option<bool>,
1014
1015    /// Allowed tool names
1016    pub allowed_tools: Option<Vec<String>>,
1017}
1018
1019// ============================================================================
1020// Beta Features - MCP Tool Types
1021// ============================================================================
1022
1023/// MCP tool use block (beta) - for assistant messages
1024#[derive(Debug, Clone, Serialize, Deserialize)]
1025pub struct McpToolUseBlock {
1026    /// Unique identifier for this tool use
1027    pub id: String,
1028
1029    /// Name of the tool being used
1030    pub name: String,
1031
1032    /// Name of the MCP server
1033    pub server_name: String,
1034
1035    /// Input arguments for the tool
1036    pub input: Value,
1037
1038    /// Cache control for this block
1039    #[serde(skip_serializing_if = "Option::is_none")]
1040    pub cache_control: Option<CacheControl>,
1041}
1042
1043/// MCP tool result block (beta) - for user messages
1044#[serde_with::skip_serializing_none]
1045#[derive(Debug, Clone, Serialize, Deserialize)]
1046pub struct McpToolResultBlock {
1047    /// The ID of the tool use this is a result for
1048    pub tool_use_id: String,
1049
1050    /// The result content
1051    pub content: Option<ToolResultContent>,
1052
1053    /// Whether this result indicates an error
1054    pub is_error: Option<bool>,
1055
1056    /// Cache control for this block
1057    pub cache_control: Option<CacheControl>,
1058}
1059
1060/// MCP toolset definition (beta)
1061#[serde_with::skip_serializing_none]
1062#[derive(Debug, Clone, Serialize, Deserialize)]
1063pub struct McpToolset {
1064    #[serde(rename = "type")]
1065    pub toolset_type: String, // "mcp_toolset"
1066
1067    /// Name of the MCP server to configure tools for
1068    pub mcp_server_name: String,
1069
1070    /// Default configuration applied to all tools from this server
1071    pub default_config: Option<McpToolDefaultConfig>,
1072
1073    /// Configuration overrides for specific tools
1074    pub configs: Option<HashMap<String, McpToolConfig>>,
1075
1076    /// Cache control for this toolset
1077    pub cache_control: Option<CacheControl>,
1078}
1079
1080/// Default configuration for MCP tools
1081#[serde_with::skip_serializing_none]
1082#[derive(Debug, Clone, Serialize, Deserialize)]
1083pub struct McpToolDefaultConfig {
1084    /// Whether tools are enabled
1085    pub enabled: Option<bool>,
1086
1087    /// Whether to defer loading
1088    pub defer_loading: Option<bool>,
1089}
1090
1091/// Per-tool MCP configuration
1092#[serde_with::skip_serializing_none]
1093#[derive(Debug, Clone, Serialize, Deserialize)]
1094pub struct McpToolConfig {
1095    /// Whether this tool is enabled
1096    pub enabled: Option<bool>,
1097
1098    /// Whether to defer loading
1099    pub defer_loading: Option<bool>,
1100}
1101
1102// ============================================================================
1103// Beta Features - Code Execution Types
1104// ============================================================================
1105
1106/// Code execution tool (beta)
1107#[serde_with::skip_serializing_none]
1108#[derive(Debug, Clone, Serialize, Deserialize)]
1109pub struct CodeExecutionTool {
1110    #[serde(rename = "type")]
1111    pub tool_type: String, // "code_execution_20250522" or "code_execution_20250825"
1112
1113    pub name: String, // "code_execution"
1114
1115    /// Allowed callers for this tool
1116    pub allowed_callers: Option<Vec<String>>,
1117
1118    /// Whether to defer loading
1119    pub defer_loading: Option<bool>,
1120
1121    /// Whether to use strict mode
1122    pub strict: Option<bool>,
1123
1124    /// Cache control for this tool
1125    pub cache_control: Option<CacheControl>,
1126}
1127
1128/// Code execution result block (beta)
1129#[derive(Debug, Clone, Serialize, Deserialize)]
1130pub struct CodeExecutionResultBlock {
1131    /// Stdout output
1132    pub stdout: String,
1133
1134    /// Stderr output
1135    pub stderr: String,
1136
1137    /// Return code
1138    pub return_code: i32,
1139
1140    /// Output files
1141    pub content: Vec<CodeExecutionOutputBlock>,
1142}
1143
1144/// Code execution output file reference
1145#[derive(Debug, Clone, Serialize, Deserialize)]
1146pub struct CodeExecutionOutputBlock {
1147    #[serde(rename = "type")]
1148    pub block_type: String, // "code_execution_output"
1149
1150    /// File ID
1151    pub file_id: String,
1152}
1153
1154/// Code execution tool result block (beta)
1155#[derive(Debug, Clone, Serialize, Deserialize)]
1156pub struct CodeExecutionToolResultBlock {
1157    /// The ID of the tool use this is a result for
1158    pub tool_use_id: String,
1159
1160    /// The result content (success or error)
1161    pub content: CodeExecutionToolResultContent,
1162
1163    /// Cache control for this block
1164    #[serde(skip_serializing_if = "Option::is_none")]
1165    pub cache_control: Option<CacheControl>,
1166}
1167
1168/// Code execution tool result content
1169#[derive(Debug, Clone, Serialize, Deserialize)]
1170#[serde(untagged)]
1171pub enum CodeExecutionToolResultContent {
1172    Success(CodeExecutionResultBlock),
1173    Error(CodeExecutionToolResultError),
1174}
1175
1176/// Code execution tool result error
1177#[derive(Debug, Clone, Serialize, Deserialize)]
1178pub struct CodeExecutionToolResultError {
1179    #[serde(rename = "type")]
1180    pub error_type: String, // "code_execution_tool_result_error"
1181
1182    pub error_code: CodeExecutionToolResultErrorCode,
1183}
1184
1185/// Code execution error codes
1186#[derive(Debug, Clone, Serialize, Deserialize)]
1187#[serde(rename_all = "snake_case")]
1188pub enum CodeExecutionToolResultErrorCode {
1189    Unavailable,
1190    CodeExecutionExceededTimeout,
1191    ContainerExpired,
1192    InvalidToolInput,
1193}
1194
1195/// Bash code execution result block (beta)
1196#[derive(Debug, Clone, Serialize, Deserialize)]
1197pub struct BashCodeExecutionResultBlock {
1198    /// Stdout output
1199    pub stdout: String,
1200
1201    /// Stderr output
1202    pub stderr: String,
1203
1204    /// Return code
1205    pub return_code: i32,
1206
1207    /// Output files
1208    pub content: Vec<BashCodeExecutionOutputBlock>,
1209}
1210
1211/// Bash code execution output file reference
1212#[derive(Debug, Clone, Serialize, Deserialize)]
1213pub struct BashCodeExecutionOutputBlock {
1214    #[serde(rename = "type")]
1215    pub block_type: String, // "bash_code_execution_output"
1216
1217    /// File ID
1218    pub file_id: String,
1219}
1220
1221/// Bash code execution tool result block (beta)
1222#[derive(Debug, Clone, Serialize, Deserialize)]
1223pub struct BashCodeExecutionToolResultBlock {
1224    /// The ID of the tool use this is a result for
1225    pub tool_use_id: String,
1226
1227    /// The result content (success or error)
1228    pub content: BashCodeExecutionToolResultContent,
1229
1230    /// Cache control for this block
1231    #[serde(skip_serializing_if = "Option::is_none")]
1232    pub cache_control: Option<CacheControl>,
1233}
1234
1235/// Bash code execution tool result content
1236#[derive(Debug, Clone, Serialize, Deserialize)]
1237#[serde(untagged)]
1238pub enum BashCodeExecutionToolResultContent {
1239    Success(BashCodeExecutionResultBlock),
1240    Error(BashCodeExecutionToolResultError),
1241}
1242
1243/// Bash code execution tool result error
1244#[derive(Debug, Clone, Serialize, Deserialize)]
1245pub struct BashCodeExecutionToolResultError {
1246    #[serde(rename = "type")]
1247    pub error_type: String, // "bash_code_execution_tool_result_error"
1248
1249    pub error_code: BashCodeExecutionToolResultErrorCode,
1250}
1251
1252/// Bash code execution error codes
1253#[derive(Debug, Clone, Serialize, Deserialize)]
1254#[serde(rename_all = "snake_case")]
1255pub enum BashCodeExecutionToolResultErrorCode {
1256    Unavailable,
1257    CodeExecutionExceededTimeout,
1258    ContainerExpired,
1259    InvalidToolInput,
1260}
1261
1262/// Text editor code execution tool result block (beta)
1263#[derive(Debug, Clone, Serialize, Deserialize)]
1264pub struct TextEditorCodeExecutionToolResultBlock {
1265    /// The ID of the tool use this is a result for
1266    pub tool_use_id: String,
1267
1268    /// The result content
1269    pub content: TextEditorCodeExecutionToolResultContent,
1270
1271    /// Cache control for this block
1272    #[serde(skip_serializing_if = "Option::is_none")]
1273    pub cache_control: Option<CacheControl>,
1274}
1275
1276/// Text editor code execution result content
1277#[derive(Debug, Clone, Serialize, Deserialize)]
1278#[serde(untagged)]
1279pub enum TextEditorCodeExecutionToolResultContent {
1280    CreateResult(TextEditorCodeExecutionCreateResultBlock),
1281    StrReplaceResult(TextEditorCodeExecutionStrReplaceResultBlock),
1282    ViewResult(TextEditorCodeExecutionViewResultBlock),
1283    Error(TextEditorCodeExecutionToolResultError),
1284}
1285
1286/// Text editor create result block
1287#[derive(Debug, Clone, Serialize, Deserialize)]
1288pub struct TextEditorCodeExecutionCreateResultBlock {
1289    #[serde(rename = "type")]
1290    pub block_type: String, // "text_editor_code_execution_create_result"
1291}
1292
1293/// Text editor str_replace result block
1294#[derive(Debug, Clone, Serialize, Deserialize)]
1295pub struct TextEditorCodeExecutionStrReplaceResultBlock {
1296    #[serde(rename = "type")]
1297    pub block_type: String, // "text_editor_code_execution_str_replace_result"
1298
1299    /// Snippet of content around the replacement
1300    #[serde(skip_serializing_if = "Option::is_none")]
1301    pub snippet: Option<String>,
1302}
1303
1304/// Text editor view result block
1305#[derive(Debug, Clone, Serialize, Deserialize)]
1306pub struct TextEditorCodeExecutionViewResultBlock {
1307    #[serde(rename = "type")]
1308    pub block_type: String, // "text_editor_code_execution_view_result"
1309
1310    /// Content of the viewed file
1311    pub content: String,
1312}
1313
1314/// Text editor code execution tool result error
1315#[derive(Debug, Clone, Serialize, Deserialize)]
1316pub struct TextEditorCodeExecutionToolResultError {
1317    #[serde(rename = "type")]
1318    pub error_type: String,
1319
1320    pub error_code: TextEditorCodeExecutionToolResultErrorCode,
1321}
1322
1323/// Text editor code execution error codes
1324#[derive(Debug, Clone, Serialize, Deserialize)]
1325#[serde(rename_all = "snake_case")]
1326pub enum TextEditorCodeExecutionToolResultErrorCode {
1327    Unavailable,
1328    InvalidToolInput,
1329    FileNotFound,
1330    ContainerExpired,
1331}
1332
1333// ============================================================================
1334// Beta Features - Web Fetch Types
1335// ============================================================================
1336
1337/// Web fetch tool (beta)
1338#[serde_with::skip_serializing_none]
1339#[derive(Debug, Clone, Serialize, Deserialize)]
1340pub struct WebFetchTool {
1341    #[serde(rename = "type")]
1342    pub tool_type: String, // "web_fetch_20250305" or similar
1343
1344    pub name: String, // "web_fetch"
1345
1346    /// Allowed callers for this tool
1347    pub allowed_callers: Option<Vec<String>>,
1348
1349    /// Maximum number of uses
1350    pub max_uses: Option<u32>,
1351
1352    /// Cache control for this tool
1353    pub cache_control: Option<CacheControl>,
1354}
1355
1356/// Web fetch result block (beta)
1357#[derive(Debug, Clone, Serialize, Deserialize)]
1358pub struct WebFetchResultBlock {
1359    #[serde(rename = "type")]
1360    pub block_type: String, // "web_fetch_result"
1361
1362    /// The URL that was fetched
1363    pub url: String,
1364
1365    /// The document content
1366    pub content: DocumentBlock,
1367
1368    /// When the content was retrieved
1369    #[serde(skip_serializing_if = "Option::is_none")]
1370    pub retrieved_at: Option<String>,
1371}
1372
1373/// Web fetch tool result block (beta)
1374#[derive(Debug, Clone, Serialize, Deserialize)]
1375pub struct WebFetchToolResultBlock {
1376    /// The ID of the tool use this is a result for
1377    pub tool_use_id: String,
1378
1379    /// The result content (success or error)
1380    pub content: WebFetchToolResultContent,
1381
1382    /// Cache control for this block
1383    #[serde(skip_serializing_if = "Option::is_none")]
1384    pub cache_control: Option<CacheControl>,
1385}
1386
1387/// Web fetch tool result content
1388#[derive(Debug, Clone, Serialize, Deserialize)]
1389#[serde(untagged)]
1390pub enum WebFetchToolResultContent {
1391    Success(WebFetchResultBlock),
1392    Error(WebFetchToolResultError),
1393}
1394
1395/// Web fetch tool result error
1396#[derive(Debug, Clone, Serialize, Deserialize)]
1397pub struct WebFetchToolResultError {
1398    #[serde(rename = "type")]
1399    pub error_type: String, // "web_fetch_tool_result_error"
1400
1401    pub error_code: WebFetchToolResultErrorCode,
1402}
1403
1404/// Web fetch error codes
1405#[derive(Debug, Clone, Serialize, Deserialize)]
1406#[serde(rename_all = "snake_case")]
1407pub enum WebFetchToolResultErrorCode {
1408    InvalidToolInput,
1409    Unavailable,
1410    MaxUsesExceeded,
1411    TooManyRequests,
1412    UrlNotAllowed,
1413    FetchFailed,
1414    ContentTooLarge,
1415}
1416
1417// ============================================================================
1418// Beta Features - Tool Search Types
1419// ============================================================================
1420
1421/// Tool search tool (beta)
1422#[serde_with::skip_serializing_none]
1423#[derive(Debug, Clone, Serialize, Deserialize)]
1424pub struct ToolSearchTool {
1425    #[serde(rename = "type")]
1426    pub tool_type: String, // "tool_search_tool_regex" or "tool_search_tool_bm25"
1427
1428    pub name: String,
1429
1430    /// Allowed callers for this tool
1431    pub allowed_callers: Option<Vec<String>>,
1432
1433    /// Cache control for this tool
1434    pub cache_control: Option<CacheControl>,
1435}
1436
1437/// Tool reference block (beta) - returned by tool search
1438#[derive(Debug, Clone, Serialize, Deserialize)]
1439pub struct ToolReferenceBlock {
1440    #[serde(rename = "type")]
1441    pub block_type: String, // "tool_reference"
1442
1443    /// Tool name
1444    pub tool_name: String,
1445
1446    /// Tool description
1447    #[serde(skip_serializing_if = "Option::is_none")]
1448    pub description: Option<String>,
1449}
1450
1451/// Tool search result block (beta)
1452#[derive(Debug, Clone, Serialize, Deserialize)]
1453pub struct ToolSearchResultBlock {
1454    #[serde(rename = "type")]
1455    pub block_type: String, // "tool_search_tool_search_result"
1456
1457    /// Tool name
1458    pub tool_name: String,
1459
1460    /// Relevance score
1461    #[serde(skip_serializing_if = "Option::is_none")]
1462    pub score: Option<f64>,
1463}
1464
1465/// Tool search tool result block (beta)
1466#[derive(Debug, Clone, Serialize, Deserialize)]
1467pub struct ToolSearchToolResultBlock {
1468    /// The ID of the tool use this is a result for
1469    pub tool_use_id: String,
1470
1471    /// The search results
1472    pub content: Vec<ToolSearchResultBlock>,
1473
1474    /// Cache control for this block
1475    #[serde(skip_serializing_if = "Option::is_none")]
1476    pub cache_control: Option<CacheControl>,
1477}
1478
1479// ============================================================================
1480// Beta Features - Container Upload Types
1481// ============================================================================
1482
1483/// Container upload block (beta)
1484#[derive(Debug, Clone, Serialize, Deserialize)]
1485pub struct ContainerUploadBlock {
1486    #[serde(rename = "type")]
1487    pub block_type: String, // "container_upload"
1488
1489    /// File ID
1490    pub file_id: String,
1491
1492    /// File name
1493    pub file_name: String,
1494
1495    /// File path in container
1496    #[serde(skip_serializing_if = "Option::is_none")]
1497    pub file_path: Option<String>,
1498}
1499
1500// ============================================================================
1501// Beta Features - Memory Tool Types
1502// ============================================================================
1503
1504/// Memory tool (beta)
1505#[serde_with::skip_serializing_none]
1506#[derive(Debug, Clone, Serialize, Deserialize)]
1507pub struct MemoryTool {
1508    #[serde(rename = "type")]
1509    pub tool_type: String, // "memory_20250818"
1510
1511    pub name: String, // "memory"
1512
1513    /// Allowed callers for this tool
1514    pub allowed_callers: Option<Vec<String>>,
1515
1516    /// Whether to defer loading
1517    pub defer_loading: Option<bool>,
1518
1519    /// Whether to use strict mode
1520    pub strict: Option<bool>,
1521
1522    /// Input examples
1523    pub input_examples: Option<Vec<Value>>,
1524
1525    /// Cache control for this tool
1526    pub cache_control: Option<CacheControl>,
1527}
1528
1529// ============================================================================
1530// Beta Features - Computer Use Tool Types
1531// ============================================================================
1532
1533/// Computer use tool (beta)
1534#[serde_with::skip_serializing_none]
1535#[derive(Debug, Clone, Serialize, Deserialize)]
1536pub struct ComputerUseTool {
1537    #[serde(rename = "type")]
1538    pub tool_type: String, // "computer_20241022" or "computer_20250124"
1539
1540    pub name: String, // "computer"
1541
1542    /// Display width
1543    pub display_width_px: u32,
1544
1545    /// Display height
1546    pub display_height_px: u32,
1547
1548    /// Display number (optional)
1549    pub display_number: Option<u32>,
1550
1551    /// Allowed callers for this tool
1552    pub allowed_callers: Option<Vec<String>>,
1553
1554    /// Cache control for this tool
1555    pub cache_control: Option<CacheControl>,
1556}
1557
1558// ============================================================================
1559// Beta Features - Extended Input Content Block Enum
1560// ============================================================================
1561
1562/// Beta input content block types (extends InputContentBlock)
1563#[derive(Debug, Clone, Serialize, Deserialize)]
1564#[serde(tag = "type", rename_all = "snake_case")]
1565pub enum BetaInputContentBlock {
1566    // Standard types
1567    Text(TextBlock),
1568    Image(ImageBlock),
1569    Document(DocumentBlock),
1570    ToolUse(ToolUseBlock),
1571    ToolResult(ToolResultBlock),
1572    Thinking(ThinkingBlock),
1573    RedactedThinking(RedactedThinkingBlock),
1574    ServerToolUse(ServerToolUseBlock),
1575    SearchResult(SearchResultBlock),
1576    WebSearchToolResult(WebSearchToolResultBlock),
1577
1578    // Beta MCP types
1579    McpToolUse(McpToolUseBlock),
1580    McpToolResult(McpToolResultBlock),
1581
1582    // Beta code execution types
1583    CodeExecutionToolResult(CodeExecutionToolResultBlock),
1584    BashCodeExecutionToolResult(BashCodeExecutionToolResultBlock),
1585    TextEditorCodeExecutionToolResult(TextEditorCodeExecutionToolResultBlock),
1586
1587    // Beta web fetch types
1588    WebFetchToolResult(WebFetchToolResultBlock),
1589
1590    // Beta tool search types
1591    ToolSearchToolResult(ToolSearchToolResultBlock),
1592    ToolReference(ToolReferenceBlock),
1593
1594    // Beta container types
1595    ContainerUpload(ContainerUploadBlock),
1596}
1597
1598/// Beta output content block types (extends ContentBlock)
1599#[serde_with::skip_serializing_none]
1600#[derive(Debug, Clone, Serialize, Deserialize)]
1601#[serde(tag = "type", rename_all = "snake_case")]
1602pub enum BetaContentBlock {
1603    // Standard types
1604    Text {
1605        text: String,
1606        citations: Option<Vec<Citation>>,
1607    },
1608    ToolUse {
1609        id: String,
1610        name: String,
1611        input: Value,
1612    },
1613    Thinking {
1614        thinking: String,
1615        signature: String,
1616    },
1617    RedactedThinking {
1618        data: String,
1619    },
1620    ServerToolUse {
1621        id: String,
1622        name: String,
1623        input: Value,
1624    },
1625    WebSearchToolResult {
1626        tool_use_id: String,
1627        content: WebSearchToolResultContent,
1628    },
1629
1630    // Beta MCP types
1631    McpToolUse {
1632        id: String,
1633        name: String,
1634        server_name: String,
1635        input: Value,
1636    },
1637    McpToolResult {
1638        tool_use_id: String,
1639        content: Option<ToolResultContent>,
1640        is_error: Option<bool>,
1641    },
1642
1643    // Beta code execution types
1644    CodeExecutionToolResult {
1645        tool_use_id: String,
1646        content: CodeExecutionToolResultContent,
1647    },
1648    BashCodeExecutionToolResult {
1649        tool_use_id: String,
1650        content: BashCodeExecutionToolResultContent,
1651    },
1652    TextEditorCodeExecutionToolResult {
1653        tool_use_id: String,
1654        content: TextEditorCodeExecutionToolResultContent,
1655    },
1656
1657    // Beta web fetch types
1658    WebFetchToolResult {
1659        tool_use_id: String,
1660        content: WebFetchToolResultContent,
1661    },
1662
1663    // Beta tool search types
1664    ToolSearchToolResult {
1665        tool_use_id: String,
1666        content: Vec<ToolSearchResultBlock>,
1667    },
1668    ToolReference {
1669        tool_name: String,
1670        description: Option<String>,
1671    },
1672
1673    // Beta container types
1674    ContainerUpload {
1675        file_id: String,
1676        file_name: String,
1677        file_path: Option<String>,
1678    },
1679}
1680
1681/// Beta tool definition (extends Tool)
1682#[derive(Debug, Clone, Serialize, Deserialize)]
1683#[serde(untagged)]
1684pub enum BetaTool {
1685    // Standard tools
1686    Custom(CustomTool),
1687    Bash(BashTool),
1688    TextEditor(TextEditorTool),
1689    WebSearch(WebSearchTool),
1690
1691    // Beta tools
1692    CodeExecution(CodeExecutionTool),
1693    McpToolset(McpToolset),
1694    WebFetch(WebFetchTool),
1695    ToolSearch(ToolSearchTool),
1696    Memory(MemoryTool),
1697    ComputerUse(ComputerUseTool),
1698}
1699
1700/// Server tool names for beta features
1701#[derive(Debug, Clone, Serialize, Deserialize)]
1702#[serde(rename_all = "snake_case")]
1703pub enum BetaServerToolName {
1704    WebSearch,
1705    WebFetch,
1706    CodeExecution,
1707    BashCodeExecution,
1708    TextEditorCodeExecution,
1709    ToolSearchToolRegex,
1710    ToolSearchToolBm25,
1711}
1712
1713/// Server tool caller types (beta)
1714#[derive(Debug, Clone, Serialize, Deserialize)]
1715#[serde(tag = "type", rename_all = "snake_case")]
1716pub enum ServerToolCaller {
1717    /// Direct caller (the model itself)
1718    Direct,
1719    /// Code execution caller
1720    #[serde(rename = "code_execution_20250825")]
1721    CodeExecution20250825,
1722}