machi 0.8.1

A Web3-native AI Agent Framework
Documentation
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
//! Unified message types for LLM chat interactions.
//!
//! This module provides a clean, type-safe API for constructing and manipulating
//! chat messages compatible with OpenAI-style LLM APIs.
//!
//! # Design Principles
//!
//! - **Single Source of Truth**: One `Role` enum, one `Message` struct
//! - **Builder Pattern**: Fluent API for message construction
//! - **Type Safety**: Strong typing for content variants
//! - **Serialization Ready**: All types derive Serialize/Deserialize

use std::fmt;

use base64::Engine;
use serde::{Deserialize, Serialize};
use serde_json::Value;

pub use crate::audio::AudioFormat;

/// Role of a message participant in a conversation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Role {
    /// System message providing instructions to the model.
    System,
    /// User message from the human.
    #[default]
    User,
    /// Assistant message from the model.
    Assistant,
    /// Tool/function response message.
    Tool,
    /// Developer message for `OpenAI` o1/o3 models.
    Developer,
}

impl Role {
    /// Returns the string representation.
    #[inline]
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::System => "system",
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Tool => "tool",
            Self::Developer => "developer",
        }
    }

    /// Returns `true` if this is a system role.
    #[inline]
    #[must_use]
    pub const fn is_system(&self) -> bool {
        matches!(self, Self::System)
    }

    /// Returns `true` if this is a user role.
    #[inline]
    #[must_use]
    pub const fn is_user(&self) -> bool {
        matches!(self, Self::User)
    }

    /// Returns `true` if this is an assistant role.
    #[inline]
    #[must_use]
    pub const fn is_assistant(&self) -> bool {
        matches!(self, Self::Assistant)
    }

    /// Returns `true` if this is a tool role.
    #[inline]
    #[must_use]
    pub const fn is_tool(&self) -> bool {
        matches!(self, Self::Tool)
    }
}

impl fmt::Display for Role {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Supported MIME types for images.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum ImageMime {
    /// JPEG image.
    #[default]
    Jpeg,
    /// PNG image.
    Png,
    /// GIF image.
    Gif,
    /// `WebP` image.
    WebP,
}

impl ImageMime {
    /// Returns the MIME type string.
    #[inline]
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Jpeg => "image/jpeg",
            Self::Png => "image/png",
            Self::Gif => "image/gif",
            Self::WebP => "image/webp",
        }
    }

    /// Detects MIME type from file extension.
    #[must_use]
    pub fn from_extension(ext: &str) -> Option<Self> {
        match ext.to_ascii_lowercase().as_str() {
            "jpg" | "jpeg" => Some(Self::Jpeg),
            "png" => Some(Self::Png),
            "gif" => Some(Self::Gif),
            "webp" => Some(Self::WebP),
            _ => None,
        }
    }

    /// Detects MIME type from magic bytes.
    #[must_use]
    pub fn from_bytes(data: &[u8]) -> Option<Self> {
        if data.len() < 4 {
            return None;
        }
        match &data[..4] {
            [0xFF, 0xD8, 0xFF, ..] => Some(Self::Jpeg),
            [0x89, 0x50, 0x4E, 0x47] => Some(Self::Png),
            [0x47, 0x49, 0x46, 0x38] => Some(Self::Gif),
            [0x52, 0x49, 0x46, 0x46] if data.len() >= 12 && &data[8..12] == b"WEBP" => {
                Some(Self::WebP)
            }
            _ => None,
        }
    }
}

impl fmt::Display for ImageMime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Detail level for image processing in vision models.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ImageDetail {
    /// Low resolution processing (faster, cheaper).
    Low,
    /// High resolution processing (more accurate).
    High,
    /// Let the model decide based on image size.
    #[default]
    Auto,
}

/// Input audio data for audio messages.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputAudio {
    /// Base64-encoded audio data.
    pub data: String,
    /// Audio format.
    pub format: AudioFormat,
}

impl InputAudio {
    /// Creates a new input audio from base64-encoded data.
    #[must_use]
    pub fn new(data: impl Into<String>, format: AudioFormat) -> Self {
        Self {
            data: data.into(),
            format,
        }
    }

    /// Creates input audio from raw bytes.
    #[must_use]
    pub fn from_bytes(data: &[u8], format: AudioFormat) -> Self {
        let encoded = base64::engine::general_purpose::STANDARD.encode(data);
        Self::new(encoded, format)
    }
}

/// A single content part within a message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPart {
    /// Text content.
    Text {
        /// The text string.
        text: String,
    },
    /// Image URL content (including data URLs).
    ImageUrl {
        /// Image URL details.
        image_url: ImageUrl,
    },
    /// Input audio content (for audio-capable models).
    InputAudio {
        /// Audio input details.
        input_audio: InputAudio,
    },
}

/// Image URL with optional detail level.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImageUrl {
    /// The URL (http/https or data URL).
    pub url: String,
    /// Detail level for processing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<ImageDetail>,
}

impl ContentPart {
    /// Creates a text content part.
    #[inline]
    #[must_use]
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text { text: text.into() }
    }

    /// Creates an image URL content part.
    #[inline]
    #[must_use]
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: ImageUrl {
                url: url.into(),
                detail: None,
            },
        }
    }

    /// Creates an image URL content part with detail level.
    #[inline]
    #[must_use]
    pub fn image_url_with_detail(url: impl Into<String>, detail: ImageDetail) -> Self {
        Self::ImageUrl {
            image_url: ImageUrl {
                url: url.into(),
                detail: Some(detail),
            },
        }
    }

    /// Creates an image content part from raw bytes.
    #[must_use]
    pub fn image_bytes(data: &[u8], mime: ImageMime) -> Self {
        let encoded = base64::engine::general_purpose::STANDARD.encode(data);
        let data_url = format!("data:{};base64,{}", mime.as_str(), encoded);
        Self::image_url(data_url)
    }

    /// Creates an image content part from raw bytes with auto-detected MIME type.
    #[must_use]
    pub fn image_bytes_auto(data: &[u8]) -> Self {
        let mime = ImageMime::from_bytes(data).unwrap_or(ImageMime::Jpeg);
        Self::image_bytes(data, mime)
    }

    /// Creates an input audio content part.
    #[inline]
    #[must_use]
    pub fn input_audio(data: impl Into<String>, format: AudioFormat) -> Self {
        Self::InputAudio {
            input_audio: InputAudio::new(data, format),
        }
    }

    /// Creates an input audio content part from raw bytes.
    #[must_use]
    pub fn input_audio_bytes(data: &[u8], format: AudioFormat) -> Self {
        Self::InputAudio {
            input_audio: InputAudio::from_bytes(data, format),
        }
    }

    /// Returns the text content if this is a text part.
    #[must_use]
    pub fn as_text(&self) -> Option<&str> {
        match self {
            Self::Text { text } => Some(text),
            _ => None,
        }
    }

    /// Returns `true` if this is a text content part.
    #[inline]
    #[must_use]
    pub const fn is_text(&self) -> bool {
        matches!(self, Self::Text { .. })
    }

    /// Returns `true` if this is an image content part.
    #[inline]
    #[must_use]
    pub const fn is_image(&self) -> bool {
        matches!(self, Self::ImageUrl { .. })
    }

    /// Returns `true` if this is an audio content part.
    #[inline]
    #[must_use]
    pub const fn is_audio(&self) -> bool {
        matches!(self, Self::InputAudio { .. })
    }
}

/// Message content that can be either simple text or multipart.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Content {
    /// Simple text content (most common case).
    Text(String),
    /// Array of content parts (for multimodal messages).
    Parts(Vec<ContentPart>),
}

impl Content {
    /// Creates simple text content.
    #[inline]
    #[must_use]
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text(text.into())
    }

    /// Creates multipart content from parts.
    #[inline]
    #[must_use]
    pub const fn parts(parts: Vec<ContentPart>) -> Self {
        Self::Parts(parts)
    }

    /// Extracts all text content, joining multiple text parts with newlines.
    #[must_use]
    pub fn as_text(&self) -> Option<String> {
        match self {
            Self::Text(text) => Some(text.clone()),
            Self::Parts(parts) => {
                let texts: Vec<&str> = parts.iter().filter_map(ContentPart::as_text).collect();
                if texts.is_empty() {
                    None
                } else {
                    Some(texts.join("\n"))
                }
            }
        }
    }

    /// Returns `true` if the content contains any images.
    #[must_use]
    pub fn has_images(&self) -> bool {
        match self {
            Self::Text(_) => false,
            Self::Parts(parts) => parts.iter().any(ContentPart::is_image),
        }
    }

    /// Returns `true` if the content is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        match self {
            Self::Text(text) => text.is_empty(),
            Self::Parts(parts) => parts.is_empty(),
        }
    }
}

impl Default for Content {
    fn default() -> Self {
        Self::Text(String::new())
    }
}

impl From<String> for Content {
    fn from(text: String) -> Self {
        Self::Text(text)
    }
}

impl From<&str> for Content {
    fn from(text: &str) -> Self {
        Self::Text(text.to_owned())
    }
}

impl From<Vec<ContentPart>> for Content {
    fn from(parts: Vec<ContentPart>) -> Self {
        Self::Parts(parts)
    }
}

/// A function call within a tool call.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionCall {
    /// Name of the function to call.
    pub name: String,
    /// Arguments as a JSON string.
    pub arguments: String,
}

impl FunctionCall {
    /// Creates a new function call.
    #[inline]
    #[must_use]
    pub fn new(name: impl Into<String>, arguments: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            arguments: arguments.into(),
        }
    }

    /// Parses arguments as a typed value.
    ///
    /// # Errors
    ///
    /// Returns an error if the arguments string is not valid JSON or cannot be deserialized into `T`.
    pub fn parse_arguments<T: for<'de> Deserialize<'de>>(&self) -> Result<T, serde_json::Error> {
        serde_json::from_str(&self.arguments)
    }

    /// Returns arguments as a JSON value.
    #[must_use]
    pub fn arguments_value(&self) -> Value {
        serde_json::from_str(&self.arguments).unwrap_or(Value::Null)
    }
}

/// A tool call made by the assistant.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ToolCall {
    /// Unique identifier for this tool call.
    pub id: String,
    /// Type of tool call (always "function" currently).
    #[serde(rename = "type", default = "default_tool_type")]
    pub call_type: String,
    /// The function to call.
    pub function: FunctionCall,
}

fn default_tool_type() -> String {
    "function".to_owned()
}

impl ToolCall {
    /// Creates a new function tool call.
    #[must_use]
    pub fn function(
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            call_type: "function".to_owned(),
            function: FunctionCall::new(name, arguments),
        }
    }

    /// Returns the function name.
    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        &self.function.name
    }

    /// Returns the function arguments.
    #[inline]
    #[must_use]
    pub fn arguments(&self) -> &str {
        &self.function.arguments
    }

    /// Parse arguments as a typed value.
    ///
    /// # Errors
    ///
    /// Returns an error if the arguments string is not valid JSON or cannot be deserialized into `T`.
    pub fn parse_arguments<T: for<'de> Deserialize<'de>>(&self) -> Result<T, serde_json::Error> {
        self.function.parse_arguments()
    }
}

impl fmt::Display for ToolCall {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({})", self.function.name, self.function.arguments)
    }
}

/// A thinking block from Anthropic Claude models.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ThinkingBlock {
    /// Standard thinking block with visible content.
    Thinking {
        /// The thinking/reasoning content.
        thinking: String,
        /// Signature for verification (if provided).
        #[serde(skip_serializing_if = "Option::is_none")]
        signature: Option<String>,
    },
    /// Redacted thinking block (content hidden).
    RedactedThinking {
        /// Opaque data representing redacted content.
        data: String,
    },
}

impl ThinkingBlock {
    /// Creates a new thinking block.
    #[must_use]
    pub fn thinking(content: impl Into<String>) -> Self {
        Self::Thinking {
            thinking: content.into(),
            signature: None,
        }
    }

    /// Returns the thinking content if this is a standard thinking block.
    #[must_use]
    pub fn as_thinking(&self) -> Option<&str> {
        match self {
            Self::Thinking { thinking, .. } => Some(thinking),
            Self::RedactedThinking { .. } => None,
        }
    }
}

/// Annotation on an assistant message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Annotation {
    /// URL citation annotation.
    UrlCitation {
        /// Start index in the text.
        start_index: usize,
        /// End index in the text.
        end_index: usize,
        /// The cited URL.
        url: String,
        /// Title of the cited content.
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
    },
    /// File citation annotation.
    FileCitation {
        /// File ID reference.
        file_id: String,
        /// Quote from the file.
        #[serde(skip_serializing_if = "Option::is_none")]
        quote: Option<String>,
    },
}

/// A chat message in a conversation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
    /// Role of the message sender.
    pub role: Role,

    /// Content of the message.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<Content>,

    /// Refusal message if the model declined to respond.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refusal: Option<String>,

    /// Annotations on the message (citations, etc.).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub annotations: Vec<Annotation>,

    /// Tool calls made by the assistant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCall>>,

    /// Tool call ID this message responds to (for tool role).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,

    /// Name associated with the message (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Reasoning content from `OpenAI` o1/o3 models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning_content: Option<String>,

    /// Thinking blocks from Anthropic Claude models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking_blocks: Option<Vec<ThinkingBlock>>,
}

impl Message {
    /// Creates a new message with the given role and content.
    #[must_use]
    pub fn new(role: Role, content: impl Into<Content>) -> Self {
        Self {
            role,
            content: Some(content.into()),
            refusal: None,
            annotations: Vec::new(),
            tool_calls: None,
            tool_call_id: None,
            name: None,
            reasoning_content: None,
            thinking_blocks: None,
        }
    }

    /// Creates a system message.
    #[inline]
    #[must_use]
    pub fn system(content: impl Into<String>) -> Self {
        Self::new(Role::System, Content::text(content))
    }

    /// Creates a user message.
    #[inline]
    #[must_use]
    pub fn user(content: impl Into<String>) -> Self {
        Self::new(Role::User, Content::text(content))
    }

    /// Creates an assistant message.
    #[inline]
    #[must_use]
    pub fn assistant(content: impl Into<String>) -> Self {
        Self::new(Role::Assistant, Content::text(content))
    }

    /// Creates an assistant message with tool calls (no text content).
    #[must_use]
    pub const fn assistant_tool_calls(tool_calls: Vec<ToolCall>) -> Self {
        Self {
            role: Role::Assistant,
            content: None,
            refusal: None,
            annotations: Vec::new(),
            tool_calls: Some(tool_calls),
            tool_call_id: None,
            name: None,
            reasoning_content: None,
            thinking_blocks: None,
        }
    }

    /// Creates a tool response message.
    #[must_use]
    pub fn tool(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            role: Role::Tool,
            content: Some(Content::text(content)),
            refusal: None,
            annotations: Vec::new(),
            tool_calls: None,
            tool_call_id: Some(tool_call_id.into()),
            name: None,
            reasoning_content: None,
            thinking_blocks: None,
        }
    }

    /// Creates a builder for constructing a message.
    #[inline]
    #[must_use]
    pub const fn builder(role: Role) -> MessageBuilder {
        MessageBuilder::new(role)
    }

    /// Returns the text content of the message.
    #[must_use]
    pub fn text(&self) -> Option<String> {
        self.content.as_ref().and_then(Content::as_text)
    }

    /// Returns `true` if the message has tool calls.
    #[must_use]
    pub fn has_tool_calls(&self) -> bool {
        self.tool_calls.as_ref().is_some_and(|tc| !tc.is_empty())
    }

    /// Returns `true` if the message contains images.
    #[must_use]
    pub fn has_images(&self) -> bool {
        self.content.as_ref().is_some_and(Content::has_images)
    }

    /// Returns `true` if the message has no content and no tool calls.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        let no_content = self.content.as_ref().is_none_or(Content::is_empty);
        let no_tools = !self.has_tool_calls();
        no_content && no_tools
    }

    /// Sets the name field.
    #[must_use]
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }
}

impl Default for Message {
    fn default() -> Self {
        Self {
            role: Role::User,
            content: None,
            refusal: None,
            annotations: Vec::new(),
            tool_calls: None,
            tool_call_id: None,
            name: None,
            reasoning_content: None,
            thinking_blocks: None,
        }
    }
}

impl fmt::Display for Message {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] ", self.role)?;
        if let Some(text) = self.text() {
            write!(f, "{text}")?;
        }
        if let Some(tool_calls) = &self.tool_calls {
            for tc in tool_calls {
                write!(f, " [{tc}]")?;
            }
        }
        Ok(())
    }
}

/// Builder for constructing messages with a fluent API.
#[derive(Debug, Clone)]
pub struct MessageBuilder {
    role: Role,
    parts: Vec<ContentPart>,
    tool_calls: Vec<ToolCall>,
    tool_call_id: Option<String>,
    name: Option<String>,
}

impl MessageBuilder {
    /// Creates a new builder with the specified role.
    #[inline]
    #[must_use]
    pub const fn new(role: Role) -> Self {
        Self {
            role,
            parts: Vec::new(),
            tool_calls: Vec::new(),
            tool_call_id: None,
            name: None,
        }
    }

    /// Adds text content.
    #[must_use]
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.parts.push(ContentPart::text(text));
        self
    }

    /// Adds an image URL.
    #[must_use]
    pub fn image_url(mut self, url: impl Into<String>) -> Self {
        self.parts.push(ContentPart::image_url(url));
        self
    }

    /// Adds an image URL with detail level.
    #[must_use]
    pub fn image_url_with_detail(mut self, url: impl Into<String>, detail: ImageDetail) -> Self {
        self.parts
            .push(ContentPart::image_url_with_detail(url, detail));
        self
    }

    /// Adds an image from raw bytes.
    #[must_use]
    pub fn image_bytes(mut self, data: &[u8], mime: ImageMime) -> Self {
        self.parts.push(ContentPart::image_bytes(data, mime));
        self
    }

    /// Adds a tool call.
    #[must_use]
    pub fn tool_call(
        mut self,
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: impl Into<String>,
    ) -> Self {
        self.tool_calls
            .push(ToolCall::function(id, name, arguments));
        self
    }

    /// Sets the tool call ID (for tool response messages).
    #[must_use]
    pub fn tool_call_id(mut self, id: impl Into<String>) -> Self {
        self.tool_call_id = Some(id.into());
        self
    }

    /// Sets the name field.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Builds the message.
    #[must_use]
    pub fn build(self) -> Message {
        let content = if self.parts.is_empty() {
            None
        } else if self.parts.len() == 1 && self.parts[0].is_text() {
            // Optimize single text to simple string
            self.parts.into_iter().next().and_then(|p| match p {
                ContentPart::Text { text } => Some(Content::Text(text)),
                ContentPart::ImageUrl { .. } | ContentPart::InputAudio { .. } => None,
            })
        } else {
            Some(Content::Parts(self.parts))
        };

        let tool_calls = if self.tool_calls.is_empty() {
            None
        } else {
            Some(self.tool_calls)
        };

        Message {
            role: self.role,
            content,
            refusal: None,
            annotations: Vec::new(),
            tool_calls,
            tool_call_id: self.tool_call_id,
            name: self.name,
            reasoning_content: None,
            thinking_blocks: None,
        }
    }
}