sombrax_agentic_core 0.1.1

SombraX Agentic Core (SAC) — a provider-agnostic Rust library for building LLM agents: content-modifying hooks, tool/MCP integration, and context optimization.
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
//! Message types for conversation content
//!
//! Provides the Message enum and content types for user and assistant messages,
//! along with validation helpers to enforce message integrity.

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt;

/// Error returned when message validation fails
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationError {
    /// Message content is empty
    EmptyContent {
        /// Role of the message with empty content
        role: &'static str,
    },
    /// Text content is empty or whitespace-only
    EmptyText {
        /// Role of the message containing empty text
        role: &'static str,
    },
    /// Tool call references an unknown tool
    UnknownTool {
        /// The tool call ID
        tool_call_id: String,
        /// The unknown tool name
        tool_name: String,
    },
    /// Tool result ID doesn't match any previous tool call
    UnmatchedToolResult {
        /// The tool result ID that doesn't match
        tool_result_id: String,
    },
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ValidationError::EmptyContent { role } => {
                write!(f, "{} message has empty content", role)
            }
            ValidationError::EmptyText { role } => {
                write!(f, "{} message contains empty or whitespace-only text", role)
            }
            ValidationError::UnknownTool {
                tool_call_id,
                tool_name,
            } => {
                write!(
                    f,
                    "tool call '{}' references unknown tool '{}'",
                    tool_call_id, tool_name
                )
            }
            ValidationError::UnmatchedToolResult { tool_result_id } => {
                write!(
                    f,
                    "tool result '{}' does not match any previous tool call",
                    tool_result_id
                )
            }
        }
    }
}

impl std::error::Error for ValidationError {}

/// A message in a conversation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "role", rename_all = "lowercase")]
pub enum Message {
    /// A user message
    User {
        /// Message content items
        content: Vec<UserContent>,
        /// Optional message ID
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
    },
    /// An assistant message
    Assistant {
        /// Message content items
        content: Vec<AssistantContent>,
        /// Optional message ID
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// Optional reasoning/thinking content (for models that return it as separate field)
        #[serde(skip_serializing_if = "Option::is_none")]
        reasoning: Option<String>,
    },
}

impl Message {
    /// Create a new user message with text content
    pub fn user(text: impl Into<String>) -> Self {
        Message::User {
            content: vec![UserContent::Text { text: text.into() }],
            id: None,
        }
    }

    /// Create a new user message with text content and an ID
    pub fn user_with_id(text: impl Into<String>, id: impl Into<String>) -> Self {
        Message::User {
            content: vec![UserContent::Text { text: text.into() }],
            id: Some(id.into()),
        }
    }

    /// Create a new assistant message with text content
    pub fn assistant(text: impl Into<String>) -> Self {
        Message::Assistant {
            content: vec![AssistantContent::Text { text: text.into() }],
            id: None,
            reasoning: None,
        }
    }

    /// Create a new assistant message with text content and an ID
    pub fn assistant_with_id(text: impl Into<String>, id: impl Into<String>) -> Self {
        Message::Assistant {
            content: vec![AssistantContent::Text { text: text.into() }],
            id: Some(id.into()),
            reasoning: None,
        }
    }

    /// Create a new assistant message with text content and reasoning
    pub fn assistant_with_reasoning(text: impl Into<String>, reasoning: impl Into<String>) -> Self {
        Message::Assistant {
            content: vec![AssistantContent::Text { text: text.into() }],
            id: None,
            reasoning: Some(reasoning.into()),
        }
    }

    /// Create a tool result message
    pub fn tool_result(tool_call_id: impl Into<String>, content: impl Into<String>) -> Self {
        Message::User {
            content: vec![UserContent::ToolResult {
                id: tool_call_id.into(),
                content: content.into(),
            }],
            id: None,
        }
    }

    /// Get the message ID if present
    pub fn id(&self) -> Option<&str> {
        match self {
            Message::User { id, .. } => id.as_deref(),
            Message::Assistant { id, .. } => id.as_deref(),
        }
    }

    /// Set the message ID
    pub fn with_id(mut self, new_id: impl Into<String>) -> Self {
        match &mut self {
            Message::User { id, .. } => *id = Some(new_id.into()),
            Message::Assistant { id, .. } => *id = Some(new_id.into()),
        }
        self
    }

    /// Check if this is a user message
    pub fn is_user(&self) -> bool {
        matches!(self, Message::User { .. })
    }

    /// Check if this is an assistant message
    pub fn is_assistant(&self) -> bool {
        matches!(self, Message::Assistant { .. })
    }

    /// Get the message role as a string
    pub fn role(&self) -> &'static str {
        match self {
            Message::User { .. } => "user",
            Message::Assistant { .. } => "assistant",
        }
    }

    /// Get the text content of the message
    pub fn text(&self) -> String {
        match self {
            Message::User { content, .. } => content
                .iter()
                .filter_map(|c| match c {
                    UserContent::Text { text } => Some(text.as_str()),
                    _ => None,
                })
                .collect::<Vec<_>>()
                .join("\n"),
            Message::Assistant { content, .. } => content
                .iter()
                .filter_map(|c| match c {
                    AssistantContent::Text { text } => Some(text.as_str()),
                    _ => None,
                })
                .collect::<Vec<_>>()
                .join("\n"),
        }
    }

    /// Get the approximate content length (for token estimation)
    pub fn content_length(&self) -> usize {
        self.text().len()
    }

    /// Prepend text to the message content
    pub fn prepend_text(&mut self, prefix: &str) {
        match self {
            Message::User { content, .. } => {
                if let Some(UserContent::Text { text }) = content.first_mut() {
                    *text = format!("{}{}", prefix, text);
                } else {
                    content.insert(
                        0,
                        UserContent::Text {
                            text: prefix.to_string(),
                        },
                    );
                }
            }
            Message::Assistant { content, .. } => {
                if let Some(AssistantContent::Text { text }) = content.first_mut() {
                    *text = format!("{}{}", prefix, text);
                } else {
                    content.insert(
                        0,
                        AssistantContent::Text {
                            text: prefix.to_string(),
                        },
                    );
                }
            }
        }
    }

    /// Append text to the message content
    pub fn append_text(&mut self, suffix: &str) {
        match self {
            Message::User { content, .. } => {
                if let Some(UserContent::Text { text }) = content.last_mut() {
                    text.push_str(suffix);
                } else {
                    content.push(UserContent::Text {
                        text: suffix.to_string(),
                    });
                }
            }
            Message::Assistant { content, .. } => {
                if let Some(AssistantContent::Text { text }) = content.last_mut() {
                    text.push_str(suffix);
                } else {
                    content.push(AssistantContent::Text {
                        text: suffix.to_string(),
                    });
                }
            }
        }
    }

    /// Create a copy of this message with reasoning content set
    ///
    /// For assistant messages, this sets the reasoning field to preserve
    /// the model's thought process in conversation history for continuity
    /// after tool calls.
    pub fn with_reasoning(&self, reasoning_content: &str) -> Self {
        match self {
            Message::Assistant { content, id, .. } => Message::Assistant {
                content: content.clone(),
                id: id.clone(),
                reasoning: if reasoning_content.is_empty() {
                    None
                } else {
                    Some(reasoning_content.to_string())
                },
            },
            // For non-assistant messages, just return a clone
            _ => self.clone(),
        }
    }

    /// Get the reasoning content from an assistant message
    pub fn reasoning(&self) -> Option<&str> {
        match self {
            Message::Assistant { reasoning, .. } => reasoning.as_deref(),
            _ => None,
        }
    }

    /// Set reasoning on an assistant message (mutates in place)
    pub fn set_reasoning(&mut self, reasoning_content: Option<String>) {
        if let Message::Assistant { reasoning, .. } = self {
            *reasoning = reasoning_content;
        }
    }

    /// Get tool calls from an assistant message
    pub fn tool_calls(&self) -> Vec<&ToolCall> {
        match self {
            Message::Assistant { content, .. } => content
                .iter()
                .filter_map(|c| match c {
                    AssistantContent::ToolCall(tc) => Some(tc),
                    _ => None,
                })
                .collect(),
            _ => vec![],
        }
    }

    /// Check if this message contains tool calls
    pub fn has_tool_calls(&self) -> bool {
        !self.tool_calls().is_empty()
    }

    /// Remap all tool call IDs in this message using the provided generator function.
    ///
    /// The generator receives the old ID and returns the new ID.
    /// This is useful for normalizing provider-specific tool call IDs to a
    /// simpler incremental format (e.g., `tool_call_1`, `tool_call_2`).
    ///
    /// Only affects assistant messages containing tool calls.
    pub fn remap_tool_call_ids<F>(&mut self, mut id_generator: F)
    where
        F: FnMut(&str) -> String,
    {
        if let Message::Assistant { content, .. } = self {
            for item in content.iter_mut() {
                if let AssistantContent::ToolCall(tc) = item {
                    tc.id = id_generator(&tc.id);
                }
            }
        }
    }

    /// Get tool result IDs from a user message
    pub fn tool_result_ids(&self) -> Vec<&str> {
        match self {
            Message::User { content, .. } => content
                .iter()
                .filter_map(|c| match c {
                    UserContent::ToolResult { id, .. } => Some(id.as_str()),
                    _ => None,
                })
                .collect(),
            _ => vec![],
        }
    }

    /// Validate the message content
    ///
    /// Checks that:
    /// - The message has at least one content item
    /// - Text content is non-empty (not just whitespace)
    ///
    /// Returns `Ok(())` if valid, or a list of validation errors.
    pub fn validate(&self) -> Result<(), Vec<ValidationError>> {
        let mut errors = Vec::new();

        match self {
            Message::User { content, .. } => {
                if content.is_empty() {
                    errors.push(ValidationError::EmptyContent { role: "user" });
                }
                for item in content {
                    if let UserContent::Text { text } = item {
                        if text.trim().is_empty() {
                            errors.push(ValidationError::EmptyText { role: "user" });
                            break;
                        }
                    }
                }
            }
            Message::Assistant { content, .. } => {
                if content.is_empty() {
                    errors.push(ValidationError::EmptyContent { role: "assistant" });
                }
                for item in content {
                    if let AssistantContent::Text { text } = item {
                        if text.trim().is_empty() {
                            errors.push(ValidationError::EmptyText { role: "assistant" });
                            break;
                        }
                    }
                }
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Validate tool calls against a set of registered tool names
    ///
    /// Returns `Ok(())` if all tool calls reference registered tools,
    /// or a list of validation errors for unknown tools.
    pub fn validate_tool_calls(
        &self,
        registered_tools: &HashSet<&str>,
    ) -> Result<(), Vec<ValidationError>> {
        let mut errors = Vec::new();

        for tc in self.tool_calls() {
            if !registered_tools.contains(tc.function.name.as_str()) {
                errors.push(ValidationError::UnknownTool {
                    tool_call_id: tc.id.clone(),
                    tool_name: tc.function.name.clone(),
                });
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

/// Validate that tool result IDs in a message match previous tool call IDs
///
/// This function checks the conversation history to ensure that any
/// `ToolResult` in the given message has an ID that matches a previous
/// `ToolCall` in the history.
///
/// # Arguments
/// * `message` - The message to validate
/// * `history` - Previous messages in the conversation
///
/// # Returns
/// `Ok(())` if all tool result IDs match, or a list of unmatched IDs.
pub fn validate_tool_result_ids(
    message: &Message,
    history: &[Message],
) -> Result<(), Vec<ValidationError>> {
    // Collect all tool call IDs from history
    let tool_call_ids: HashSet<&str> = history
        .iter()
        .flat_map(|msg| msg.tool_calls())
        .map(|tc| tc.id.as_str())
        .collect();

    let mut errors = Vec::new();

    for result_id in message.tool_result_ids() {
        if !tool_call_ids.contains(result_id) {
            errors.push(ValidationError::UnmatchedToolResult {
                tool_result_id: result_id.to_string(),
            });
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(errors)
    }
}

impl From<&str> for Message {
    fn from(s: &str) -> Self {
        Message::user(s)
    }
}

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

/// Content types for user messages
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum UserContent {
    /// Text content
    Text {
        /// The text content
        text: String,
    },
    /// Tool result from a previous tool call
    ToolResult {
        /// The ID of the tool call this is a result for
        id: String,
        /// The tool result content
        content: String,
    },
    /// Image content
    Image {
        /// Base64-encoded image data
        data: String,
        /// MIME type of the image
        media_type: String,
    },
    /// Document content
    Document {
        /// Base64-encoded document data
        data: String,
        /// MIME type of the document
        media_type: String,
        /// Optional document name
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
    },
}

/// Content types for assistant messages
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AssistantContent {
    /// Text content
    Text {
        /// The text content
        text: String,
    },
    /// Tool call request
    ToolCall(ToolCall),
    /// Reasoning/thinking content (for models that support it)
    Reasoning {
        /// The reasoning steps
        reasoning: Vec<String>,
    },
}

/// A tool call request from the assistant
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ToolCall {
    /// Unique ID for this tool call
    pub id: String,
    /// The function to call
    pub function: ToolCallFunction,
}

/// Function details for a tool call
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ToolCallFunction {
    /// Name of the function to call
    pub name: String,
    /// JSON-encoded arguments
    pub arguments: String,
}

impl ToolCall {
    /// Create a new tool call
    pub fn new(
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            function: ToolCallFunction {
                name: name.into(),
                arguments: arguments.into(),
            },
        }
    }

    /// Parse the arguments as a specific type
    pub fn parse_arguments<T: serde::de::DeserializeOwned>(&self) -> Result<T, serde_json::Error> {
        serde_json::from_str(&self.function.arguments)
    }
}

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

    #[test]
    fn test_user_message() {
        let msg = Message::user("Hello, world!");
        assert!(msg.is_user());
        assert!(!msg.is_assistant());
        assert_eq!(msg.text(), "Hello, world!");
    }

    #[test]
    fn test_assistant_message() {
        let msg = Message::assistant("Hello back!");
        assert!(msg.is_assistant());
        assert!(!msg.is_user());
        assert_eq!(msg.text(), "Hello back!");
    }

    #[test]
    fn test_message_with_id() {
        let msg = Message::user_with_id("Test", "msg-123");
        assert_eq!(msg.id(), Some("msg-123"));
    }

    #[test]
    fn test_prepend_text() {
        let mut msg = Message::user("world");
        msg.prepend_text("Hello, ");
        assert_eq!(msg.text(), "Hello, world");
    }

    #[test]
    fn test_append_text() {
        let mut msg = Message::user("Hello");
        msg.append_text(", world!");
        assert_eq!(msg.text(), "Hello, world!");
    }

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

    #[test]
    fn test_tool_call() {
        let tc = ToolCall::new("call-1", "get_weather", r#"{"city": "NYC"}"#);
        assert_eq!(tc.id, "call-1");
        assert_eq!(tc.function.name, "get_weather");

        #[derive(Deserialize)]
        struct Args {
            city: String,
        }
        let args: Args = tc.parse_arguments().unwrap();
        assert_eq!(args.city, "NYC");
    }

    #[test]
    fn test_validate_valid_user_message() {
        let msg = Message::user("Hello, world!");
        assert!(msg.validate().is_ok());
    }

    #[test]
    fn test_validate_valid_assistant_message() {
        let msg = Message::assistant("Hello back!");
        assert!(msg.validate().is_ok());
    }

    #[test]
    fn test_validate_empty_user_content() {
        let msg = Message::User {
            content: vec![],
            id: None,
        };
        let result = msg.validate();
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert_eq!(errors.len(), 1);
        assert!(matches!(
            &errors[0],
            ValidationError::EmptyContent { role: "user" }
        ));
    }

    #[test]
    fn test_validate_empty_assistant_content() {
        let msg = Message::Assistant {
            content: vec![],
            id: None,
            reasoning: None,
        };
        let result = msg.validate();
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(matches!(
            &errors[0],
            ValidationError::EmptyContent { role: "assistant" }
        ));
    }

    #[test]
    fn test_validate_whitespace_only_text() {
        let msg = Message::User {
            content: vec![UserContent::Text {
                text: "   \t\n  ".to_string(),
            }],
            id: None,
        };
        let result = msg.validate();
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(matches!(
            &errors[0],
            ValidationError::EmptyText { role: "user" }
        ));
    }

    #[test]
    fn test_validate_tool_calls_known_tool() {
        let msg = Message::Assistant {
            content: vec![AssistantContent::ToolCall(ToolCall::new(
                "call-1",
                "get_weather",
                "{}",
            ))],
            id: None,
            reasoning: None,
        };
        let tools: HashSet<&str> = ["get_weather", "search"].into_iter().collect();
        assert!(msg.validate_tool_calls(&tools).is_ok());
    }

    #[test]
    fn test_validate_tool_calls_unknown_tool() {
        let msg = Message::Assistant {
            content: vec![AssistantContent::ToolCall(ToolCall::new(
                "call-1",
                "unknown_tool",
                "{}",
            ))],
            id: None,
            reasoning: None,
        };
        let tools: HashSet<&str> = ["get_weather", "search"].into_iter().collect();
        let result = msg.validate_tool_calls(&tools);
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(matches!(
            &errors[0],
            ValidationError::UnknownTool { tool_name, .. } if tool_name == "unknown_tool"
        ));
    }

    #[test]
    fn test_validate_tool_result_ids_matching() {
        // History with a tool call
        let history = vec![Message::Assistant {
            content: vec![AssistantContent::ToolCall(ToolCall::new(
                "call-123",
                "get_weather",
                "{}",
            ))],
            id: None,
            reasoning: None,
        }];

        // Tool result matching the call
        let msg = Message::tool_result("call-123", "Sunny");
        assert!(validate_tool_result_ids(&msg, &history).is_ok());
    }

    #[test]
    fn test_validate_tool_result_ids_unmatched() {
        // Empty history
        let history = vec![];

        // Tool result with no matching call
        let msg = Message::tool_result("call-unknown", "Result");
        let result = validate_tool_result_ids(&msg, &history);
        assert!(result.is_err());
        let errors = result.unwrap_err();
        assert!(matches!(
            &errors[0],
            ValidationError::UnmatchedToolResult { tool_result_id } if tool_result_id == "call-unknown"
        ));
    }

    #[test]
    fn test_tool_result_ids_extraction() {
        let msg = Message::tool_result("call-123", "Result");
        let ids = msg.tool_result_ids();
        assert_eq!(ids, vec!["call-123"]);
    }

    #[test]
    fn test_validation_error_display() {
        let err = ValidationError::EmptyContent { role: "user" };
        assert_eq!(err.to_string(), "user message has empty content");

        let err = ValidationError::EmptyText { role: "assistant" };
        assert_eq!(
            err.to_string(),
            "assistant message contains empty or whitespace-only text"
        );

        let err = ValidationError::UnknownTool {
            tool_call_id: "call-1".to_string(),
            tool_name: "foo".to_string(),
        };
        assert!(err.to_string().contains("call-1"));
        assert!(err.to_string().contains("foo"));

        let err = ValidationError::UnmatchedToolResult {
            tool_result_id: "result-1".to_string(),
        };
        assert!(err.to_string().contains("result-1"));
    }

    #[test]
    fn test_remap_tool_call_ids() {
        // Create assistant message with multiple tool calls
        let mut msg = Message::Assistant {
            content: vec![
                AssistantContent::Text {
                    text: "Let me help".to_string(),
                },
                AssistantContent::ToolCall(ToolCall::new(
                    "call_abc123xyz",
                    "get_weather",
                    r#"{"city": "NYC"}"#,
                )),
                AssistantContent::ToolCall(ToolCall::new(
                    "call_def456uvw",
                    "get_time",
                    r#"{"timezone": "EST"}"#,
                )),
            ],
            id: None,
            reasoning: None,
        };

        // Remap using an incrementing counter
        let mut counter = 0;
        msg.remap_tool_call_ids(|_old_id| {
            counter += 1;
            format!("tool_call_{}", counter)
        });

        // Verify tool calls have new IDs
        let tool_calls = msg.tool_calls();
        assert_eq!(tool_calls.len(), 2);
        assert_eq!(tool_calls[0].id, "tool_call_1");
        assert_eq!(tool_calls[1].id, "tool_call_2");

        // Verify other content is unchanged
        assert_eq!(msg.text(), "Let me help");
    }

    #[test]
    fn test_remap_tool_call_ids_user_message_noop() {
        // User messages should not be affected
        let mut msg = Message::user("Hello");
        msg.remap_tool_call_ids(|_| "should_not_be_called".to_string());

        // Should still be the same user message
        assert!(msg.is_user());
        assert_eq!(msg.text(), "Hello");
    }

    #[test]
    fn test_remap_tool_call_ids_no_tool_calls() {
        // Assistant message without tool calls
        let mut msg = Message::assistant("Just text");
        msg.remap_tool_call_ids(|_| "should_not_be_called".to_string());

        assert_eq!(msg.text(), "Just text");
        assert!(msg.tool_calls().is_empty());
    }
}