simple-agent-type 0.5.0

Core types and traits for SimpleAgents
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
//! Message types for LLM interactions.
//!
//! Provides role-based messages compatible with OpenAI's message format.

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::str::FromStr;
use thiserror::Error;

use crate::tool::ToolCall;

/// Role of a message in a conversation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// User message
    User,
    /// Assistant (LLM) message
    Assistant,
    /// System instruction message
    System,
    /// Tool/function call result
    #[serde(rename = "tool")]
    Tool,
}

#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("invalid message role '{role}' (expected: system|user|assistant|tool)")]
/// Error returned when parsing an unknown message role string.
pub struct ParseRoleError {
    /// Original role string that failed to parse.
    pub role: String,
}

impl Role {
    /// Returns this role as its canonical lowercase string value.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::System => "system",
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Tool => "tool",
        }
    }
}

impl FromStr for Role {
    type Err = ParseRoleError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "system" => Ok(Self::System),
            "user" => Ok(Self::User),
            "assistant" => Ok(Self::Assistant),
            "tool" => Ok(Self::Tool),
            _ => Err(ParseRoleError {
                role: s.to_string(),
            }),
        }
    }
}

/// Content of a message — either plain text or a list of multimodal parts.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum MessageContent {
    /// Plain text content.
    Text(String),
    /// Multimodal content parts (text, images, video, etc.).
    Parts(Vec<ContentPart>),
}

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

impl From<&str> for MessageContent {
    fn from(s: &str) -> Self {
        Self::Text(s.to_string())
    }
}

impl MessageContent {
    /// Returns the text content length for validation purposes.
    /// For Parts, sums the text lengths of all text parts.
    pub fn text_len(&self) -> usize {
        match self {
            Self::Text(s) => s.len(),
            Self::Parts(parts) => parts
                .iter()
                .map(|p| match p {
                    ContentPart::Text { text } => text.len(),
                    _ => 0,
                })
                .sum(),
        }
    }

    /// Returns true if the message has no usable content (empty string, empty parts,
    /// or every part is empty).
    pub fn is_empty_content(&self) -> bool {
        match self {
            Self::Text(s) => s.is_empty(),
            Self::Parts(parts) => {
                parts.is_empty()
                    || parts.iter().all(|p| match p {
                        ContentPart::Text { text } => text.is_empty(),
                        ContentPart::ImageUrl { image_url } => image_url.url.is_empty(),
                        ContentPart::Audio { input_audio } => input_audio.data.is_empty(),
                        ContentPart::Video { video } => video.data.is_empty(),
                    })
            }
        }
    }

    /// Returns true if the content contains a null byte.
    pub fn contains_null(&self) -> bool {
        match self {
            Self::Text(s) => s.contains('\0'),
            Self::Parts(parts) => parts.iter().any(|p| match p {
                ContentPart::Text { text } => text.contains('\0'),
                ContentPart::ImageUrl { image_url } => image_url.url.contains('\0'),
                ContentPart::Audio { input_audio } => {
                    input_audio.data.contains('\0') || input_audio.media_type.contains('\0')
                }
                ContentPart::Video { video } => {
                    video.data.contains('\0') || video.media_type.contains('\0')
                }
            }),
        }
    }
}

/// Well-known MIME type constants for multimodal content.
pub mod mime {
    /// PNG image.
    pub const IMAGE_PNG: &str = "image/png";
    /// JPEG image.
    pub const IMAGE_JPEG: &str = "image/jpeg";
    /// WebP image.
    pub const IMAGE_WEBP: &str = "image/webp";
    /// GIF image.
    pub const IMAGE_GIF: &str = "image/gif";
    /// MP3 audio.
    pub const AUDIO_MP3: &str = "audio/mpeg";
    /// WAV audio.
    pub const AUDIO_WAV: &str = "audio/wav";
    /// FLAC audio.
    pub const AUDIO_FLAC: &str = "audio/flac";
    /// OGG audio.
    pub const AUDIO_OGG: &str = "audio/ogg";
    /// MP4 video.
    pub const VIDEO_MP4: &str = "video/mp4";
    /// WebM video.
    pub const VIDEO_WEBM: &str = "video/webm";
    /// MOV video.
    pub const VIDEO_MOV: &str = "video/quicktime";
    /// MKV video.
    pub const VIDEO_MKV: &str = "video/x-matroska";
}

/// Base64-encoded media content with its MIME type.
///
/// Used by [`ContentPart::Audio`] and [`ContentPart::Video`] to carry inline
/// media data. The same struct works for both input (user uploads) and output
/// (model-generated audio/video).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MediaContent {
    /// MIME type (e.g. `"image/png"`, `"audio/wav"`). See [`mime`] for constants.
    pub media_type: String,
    /// Base64-encoded media bytes.
    pub data: String,
}

/// A single content part in a multimodal message.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")]
pub enum ContentPart {
    /// Text content.
    #[serde(rename = "text")]
    Text {
        /// The text string.
        text: String,
    },
    /// Image content (base64 data URI for OpenAI wire compatibility).
    #[serde(rename = "image_url")]
    ImageUrl {
        /// The image URL and optional detail level.
        image_url: ImageUrlContent,
    },
    /// Audio content (inline base64).
    #[serde(rename = "input_audio")]
    Audio {
        /// The audio media payload.
        input_audio: MediaContent,
    },
    /// Video content (inline base64).
    #[serde(rename = "video")]
    Video {
        /// The video media payload.
        video: MediaContent,
    },
}

impl ContentPart {
    /// Create a text content part.
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text { text: text.into() }
    }

    /// Create an image content part from base64 data.
    ///
    /// Wraps the data as a `data:` URI in [`ImageUrlContent`] for OpenAI wire
    /// compatibility.
    pub fn image(media_type: impl Into<String>, data: impl Into<String>) -> Self {
        let mt = media_type.into();
        let d = data.into();
        Self::ImageUrl {
            image_url: ImageUrlContent {
                url: format!("data:{mt};base64,{d}"),
                detail: None,
            },
        }
    }

    /// Create an audio content part from base64 data.
    pub fn audio(media_type: impl Into<String>, data: impl Into<String>) -> Self {
        Self::Audio {
            input_audio: MediaContent {
                media_type: media_type.into(),
                data: data.into(),
            },
        }
    }

    /// Create a video content part from base64 data.
    pub fn video(media_type: impl Into<String>, data: impl Into<String>) -> Self {
        Self::Video {
            video: MediaContent {
                media_type: media_type.into(),
                data: data.into(),
            },
        }
    }

    /// Create an image URL content part (legacy).
    ///
    /// Prefer [`ContentPart::image`] for inline base64 data.
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: ImageUrlContent {
                url: url.into(),
                detail: None,
            },
        }
    }
}

/// Image URL content with optional detail level.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ImageUrlContent {
    /// The image URL (or `data:` URI for inline base64 images).
    pub url: String,
    /// Optional detail level (e.g. "low", "high", "auto").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<String>,
}

/// A message in a conversation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Message {
    /// Role of the message sender
    pub role: Role,
    /// Content of the message
    pub content: MessageContent,
    /// Optional name (for multi-user conversations or tool calls)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Tool call ID (for tool role messages)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    /// Tool calls emitted by the assistant.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCall>>,
}

impl Message {
    /// Create a user message.
    ///
    /// # Example
    /// ```
    /// use simple_agent_type::message::{Message, Role};
    ///
    /// let msg = Message::user("Hello!");
    /// assert_eq!(msg.role, Role::User);
    /// assert_eq!(msg.content_text(), "Hello!");
    /// ```
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: Role::User,
            content: MessageContent::Text(content.into()),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }
    }

    /// Create an assistant message.
    ///
    /// # Example
    /// ```
    /// use simple_agent_type::message::{Message, Role};
    ///
    /// let msg = Message::assistant("Hi there!");
    /// assert_eq!(msg.role, Role::Assistant);
    /// ```
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: Role::Assistant,
            content: MessageContent::Text(content.into()),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }
    }

    /// Create a system message.
    ///
    /// # Example
    /// ```
    /// use simple_agent_type::message::{Message, Role};
    ///
    /// let msg = Message::system("You are a helpful assistant.");
    /// assert_eq!(msg.role, Role::System);
    /// ```
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: Role::System,
            content: MessageContent::Text(content.into()),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }
    }

    /// Create a tool message.
    ///
    /// # Example
    /// ```
    /// use simple_agent_type::message::{Message, Role};
    ///
    /// let msg = Message::tool("result", "call_123");
    /// assert_eq!(msg.role, Role::Tool);
    /// assert_eq!(msg.tool_call_id, Some("call_123".to_string()));
    /// ```
    pub fn tool(content: impl Into<String>, tool_call_id: impl Into<String>) -> Self {
        Self {
            role: Role::Tool,
            content: MessageContent::Text(content.into()),
            name: None,
            tool_call_id: Some(tool_call_id.into()),
            tool_calls: None,
        }
    }

    /// Create a user message with multimodal content parts.
    pub fn user_parts(parts: Vec<ContentPart>) -> Self {
        Self {
            role: Role::User,
            content: MessageContent::Parts(parts),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }
    }

    /// Extract the first text string from the message content.
    ///
    /// For `MessageContent::Text`, returns the string directly.
    /// For `MessageContent::Parts`, returns the text of the first `Text` part.
    /// Returns `""` if no text is found.
    pub fn content_text(&self) -> &str {
        match &self.content {
            MessageContent::Text(s) => s.as_str(),
            MessageContent::Parts(parts) => parts
                .iter()
                .find_map(|p| match p {
                    ContentPart::Text { text } => Some(text.as_str()),
                    _ => None,
                })
                .unwrap_or(""),
        }
    }

    /// Set the name field (builder pattern).
    ///
    /// # Example
    /// ```
    /// use simple_agent_type::message::Message;
    ///
    /// let msg = Message::user("Hello").with_name("Alice");
    /// assert_eq!(msg.name, Some("Alice".to_string()));
    /// ```
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set tool calls for assistant messages.
    pub fn with_tool_calls(mut self, tool_calls: Vec<ToolCall>) -> Self {
        self.tool_calls = Some(tool_calls);
        self
    }
}

#[derive(Debug, Clone, Deserialize)]
struct MessageInputWire {
    role: Role,
    content: MessageContent,
    #[serde(default)]
    name: Option<String>,
    #[serde(default, alias = "toolCallId")]
    tool_call_id: Option<String>,
    #[serde(default)]
    tool_calls: Option<Vec<ToolCall>>,
}

/// Parses a JSON value containing an array of message objects into typed messages.
pub fn parse_messages_value(value: &Value) -> Result<Vec<Message>, String> {
    let wire_messages: Vec<MessageInputWire> = serde_json::from_value(value.clone())
        .map_err(|e| format!("messages must be a list of message objects: {e}"))?;
    if wire_messages.is_empty() {
        return Err("messages cannot be empty".to_string());
    }

    wire_messages
        .into_iter()
        .enumerate()
        .map(|(idx, wire)| {
            if wire.content.is_empty_content() {
                return Err(format!("message[{idx}].content cannot be empty"));
            }

            let content = wire.content;

            let mut msg = match wire.role {
                Role::System => Message {
                    role: Role::System,
                    content,
                    name: None,
                    tool_call_id: None,
                    tool_calls: None,
                },
                Role::User => Message {
                    role: Role::User,
                    content,
                    name: None,
                    tool_call_id: None,
                    tool_calls: None,
                },
                Role::Assistant => {
                    let mut m = Message {
                        role: Role::Assistant,
                        content,
                        name: None,
                        tool_call_id: None,
                        tool_calls: None,
                    };
                    if let Some(calls) = wire.tool_calls {
                        if !calls.is_empty() {
                            m = m.with_tool_calls(calls);
                        }
                    }
                    m
                }
                Role::Tool => {
                    let call_id = wire.tool_call_id.ok_or_else(|| {
                        format!("message[{idx}].tool_call_id is required for tool role")
                    })?;
                    Message {
                        role: Role::Tool,
                        content,
                        name: None,
                        tool_call_id: Some(call_id),
                        tool_calls: None,
                    }
                }
            };

            if let Some(name) = wire.name {
                if !name.is_empty() {
                    msg = msg.with_name(name);
                }
            }

            Ok(msg)
        })
        .collect()
}

/// Parses a JSON string containing an array of message objects.
pub fn parse_messages_json(messages_json: &str) -> Result<Vec<Message>, String> {
    let value: Value =
        serde_json::from_str(messages_json).map_err(|e| format!("invalid messages json: {e}"))?;
    parse_messages_value(&value)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_message_user() {
        let msg = Message::user("test");
        assert_eq!(msg.role, Role::User);
        assert_eq!(msg.content, MessageContent::Text("test".to_string()));
        assert_eq!(msg.content_text(), "test");
        assert_eq!(msg.name, None);
        assert_eq!(msg.tool_call_id, None);
        assert_eq!(msg.tool_calls, None);
    }

    #[test]
    fn test_message_assistant() {
        let msg = Message::assistant("response");
        assert_eq!(msg.role, Role::Assistant);
        assert_eq!(msg.content_text(), "response");
        assert_eq!(msg.tool_calls, None);
    }

    #[test]
    fn test_message_system() {
        let msg = Message::system("instruction");
        assert_eq!(msg.role, Role::System);
        assert_eq!(msg.content_text(), "instruction");
        assert_eq!(msg.tool_calls, None);
    }

    #[test]
    fn test_message_tool() {
        let msg = Message::tool("result", "call_123");
        assert_eq!(msg.role, Role::Tool);
        assert_eq!(msg.content_text(), "result");
        assert_eq!(msg.tool_call_id, Some("call_123".to_string()));
        assert_eq!(msg.tool_calls, None);
    }

    #[test]
    fn test_message_with_name() {
        let msg = Message::user("test").with_name("Alice");
        assert_eq!(msg.name, Some("Alice".to_string()));
    }

    #[test]
    fn test_role_serialization() {
        let json = serde_json::to_string(&Role::User).unwrap();
        assert_eq!(json, "\"user\"");

        let json = serde_json::to_string(&Role::Assistant).unwrap();
        assert_eq!(json, "\"assistant\"");

        let json = serde_json::to_string(&Role::System).unwrap();
        assert_eq!(json, "\"system\"");

        let json = serde_json::to_string(&Role::Tool).unwrap();
        assert_eq!(json, "\"tool\"");
    }

    #[test]
    fn test_message_serialization() {
        let msg = Message::user("Hello");
        let json = serde_json::to_string(&msg).unwrap();
        let parsed: Message = serde_json::from_str(&json).unwrap();
        assert_eq!(msg, parsed);
    }

    #[test]
    fn test_message_optional_fields_not_serialized() {
        let msg = Message::user("test");
        let json = serde_json::to_value(&msg).unwrap();
        assert!(json.get("name").is_none());
        assert!(json.get("tool_call_id").is_none());
        assert!(json.get("tool_calls").is_none());
    }

    #[test]
    fn test_message_with_name_serialized() {
        let msg = Message::user("test").with_name("Alice");
        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json.get("name").and_then(|v| v.as_str()), Some("Alice"));
    }

    #[test]
    fn test_message_user_text() {
        let msg = Message::user("hello");
        assert_eq!(msg.role, Role::User);
        assert_eq!(msg.content_text(), "hello");
    }

    #[test]
    fn test_message_multimodal() {
        let msg = Message::user_parts(vec![
            ContentPart::text("what is this?"),
            ContentPart::image_url("https://example.com/img.jpg"),
        ]);
        assert_eq!(msg.content_text(), "what is this?");
    }

    #[test]
    fn test_content_part_image_inline_serde() {
        let part = ContentPart::image(mime::IMAGE_PNG, "abc");
        let v = serde_json::to_value(&part).unwrap();
        assert_eq!(v["type"], "image_url");
        assert!(v["image_url"]["url"]
            .as_str()
            .unwrap()
            .starts_with("data:image/png;base64,"));
        let parsed: ContentPart = serde_json::from_value(v).unwrap();
        assert_eq!(parsed, part);
    }

    #[test]
    fn test_content_part_audio_video_serde() {
        let audio = ContentPart::audio(mime::AUDIO_WAV, "dGVzdA==");
        let json = serde_json::to_string(&audio).unwrap();
        let parsed: ContentPart = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, audio);

        let video = ContentPart::video(mime::VIDEO_MP4, "dGVzdA==");
        let json = serde_json::to_string(&video).unwrap();
        let parsed: ContentPart = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, video);
    }

    #[test]
    fn test_message_parts_image_only_not_empty() {
        let msg = Message::user_parts(vec![ContentPart::image(mime::IMAGE_JPEG, "e30=")]);
        assert!(!msg.content.is_empty_content());
    }

    #[test]
    fn test_message_content_serialization() {
        let msg = Message::user("hello");
        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["content"], "hello");
        let msg2 = Message::user_parts(vec![ContentPart::text("hi")]);
        let json2 = serde_json::to_value(&msg2).unwrap();
        assert!(json2["content"].is_array());
    }

    #[test]
    fn test_message_content_from_string() {
        let content: MessageContent = "hello".into();
        assert_eq!(content, MessageContent::Text("hello".to_string()));

        let content: MessageContent = String::from("world").into();
        assert_eq!(content, MessageContent::Text("world".to_string()));
    }
}