rig-ai-sdk 0.1.0

AI SDK Data Stream Protocol adapter for rig
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
//! AI SDK UIMessage to rig::message::Message conversion
//!
//! Converts frontend AI SDK UIMessage format to rig backend Message format.
//! Supports AI SDK 5.x with streaming states, dynamic tools, and rich file types.
//!
//! # Overview
//!
//! This module provides conversion functions to transform AI SDK messages into rig's
//! internal message format. The conversion supports:
//!
//! - Text, reasoning, and image content
//! - Legacy tool calls and AI SDK 5.x dynamic tools
//! - Tool results (including errors)
//! - Multi-part messages
//!
//! # Conversion Mapping
//!
//! | AI SDK Part | Rig Content |
//! |--------------|-------------|
//! | `Text` | `UserContent::Text` / `AssistantContent::Text` |
//! | `Reasoning` | `AssistantContent::Reasoning` |
//! | `File` (image) | `UserContent::Image` |
//! | `ToolCall` | `AssistantContent::ToolCall` |
//! | `DynamicTool` (InputAvailable) | `AssistantContent::ToolCall` |
//! | `DynamicTool` (OutputAvailable) | `UserContent::ToolResult` |
//! | `DynamicTool` (OutputError) | `UserContent::ToolResult` |
//! | `ToolResult` | `UserContent::ToolResult` |
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```ignore
//! use rig_ai_sdk::{convert_messages, ChatRequest};
//!
//! let messages = convert_messages(&request.messages)?;
//! ```
//!
//! ## Extracting Prompt and History
//!
//! ```ignore
//! use rig_ai_sdk::extract_prompt_and_history;
//!
//! let (prompt, history) = extract_prompt_and_history(&request)?;
//! // prompt = last message
//! // history = all previous messages
//! ```

use anyhow::{Result, anyhow};
use rig::OneOrMany;
use rig::message::{
    AssistantContent, DocumentSourceKind, Image, ImageMediaType, Message, Reasoning, ToolCall,
    ToolFunction, ToolResult, ToolResultContent, UserContent,
};
use serde_json::Value;

use crate::message::{DynamicToolState, UIMessage, UIMessagePart};

/// Extracts the prompt (last message) and history (previous messages) from a message list.
///
/// This is a convenience function that splits the message list into the final prompt
/// and the conversation history.
///
/// # Errors
///
/// Returns an error if the message list is empty.
///
/// # Example
///
/// ```ignore
/// use rig_ai_sdk::extract_prompt_and_history;
///
/// let (prompt, history) = extract_prompt_and_history(&messages)?;
/// assert_eq!(history.len(), 2);  // First two messages
/// ```
pub fn extract_prompt_and_history(messages: &[UIMessage]) -> Result<(Message, Vec<Message>)> {
    let messages = convert_messages(messages)?;
    let (prompt, history) = messages
        .split_last()
        .ok_or_else(|| anyhow!("Message list is empty"))?;
    Ok((prompt.clone(), history.to_vec()))
}

/// Converts multiple [`UIMessage`]s to [`Message`]s.
///
/// Returns an error if any message conversion fails.
pub fn convert_messages(messages: &[UIMessage]) -> Result<Vec<Message>> {
    messages.iter().map(convert_message).collect()
}

/// Converts a single [`UIMessage`] to a [`Message`].
///
/// The conversion depends on the message role:
///
/// - `"user"` or `"system"` → [`Message::User`]
/// - `"assistant"` → [`Message::Assistant`]
///
/// # Errors
///
/// Returns an error if the role is not supported.
pub fn convert_message(msg: &UIMessage) -> Result<Message> {
    match msg.role.as_str() {
        "user" | "system" => {
            let contents: Vec<_> = msg.parts.iter().filter_map(to_user_content).collect();
            Ok(Message::User {
                content: OneOrMany::many(contents)
                    .unwrap_or_else(|_| OneOrMany::one(UserContent::text(""))),
            })
        }
        "assistant" => {
            let contents: Vec<_> = msg.parts.iter().filter_map(to_assistant_content).collect();
            Ok(Message::Assistant {
                id: Some(msg.id.clone()),
                content: OneOrMany::many(contents)
                    .unwrap_or_else(|_| OneOrMany::one(AssistantContent::text(""))),
            })
        }
        role => Err(anyhow!("Unsupported message role: {}", role)),
    }
}

/// Converts a [`UIMessagePart`] to a [`UserContent`] if applicable.
///
/// Returns `None` for parts that don't map to user content:
///
/// - `Text` → `UserContent::Text`
/// - `ToolResult` → `UserContent::ToolResult`
/// - `DynamicTool` (OutputAvailable/Error) → `UserContent::ToolResult`
/// - `File` (image only) → `UserContent::Image`
fn to_user_content(part: &UIMessagePart) -> Option<UserContent> {
    match part {
        // Text part
        UIMessagePart::Text { text, .. } => Some(UserContent::text(text)),

        // Tool result
        UIMessagePart::ToolResult {
            tool_call_id,
            result,
            ..
        } => Some(UserContent::ToolResult(ToolResult {
            id: tool_call_id.clone(),
            call_id: Some(tool_call_id.clone()),
            content: OneOrMany::one(ToolResultContent::text(json_to_string(result))),
        })),

        // DynamicTool tool result (OutputAvailable and OutputError)
        UIMessagePart::DynamicTool {
            tool_call_id,
            state,
            ..
        } => {
            // Only convert completed dynamic-tools with output to tool result
            match state {
                DynamicToolState::OutputAvailable { input: _, output } => {
                    Some(UserContent::ToolResult(ToolResult {
                        id: tool_call_id.clone(),
                        call_id: Some(tool_call_id.clone()),
                        content: OneOrMany::one(ToolResultContent::text(json_to_string(output))),
                    }))
                }
                DynamicToolState::OutputError {
                    input: _,
                    error_text,
                } => {
                    // Use error text as tool result
                    Some(UserContent::ToolResult(ToolResult {
                        id: tool_call_id.clone(),
                        call_id: Some(tool_call_id.clone()),
                        content: OneOrMany::one(ToolResultContent::text(error_text.clone())),
                    }))
                }
                _ => None, // InputStreaming and InputAvailable don't convert to UserContent
            }
        }

        // Image file
        UIMessagePart::File {
            media_type, url, ..
        } => {
            if media_type.starts_with("image/") {
                Some(UserContent::Image(Image {
                    data: DocumentSourceKind::Url(url.clone()),
                    media_type: parse_image_media_type(media_type),
                    detail: None,
                    additional_params: None,
                }))
            } else {
                // Skip non-image files (rig currently doesn't support other file types)
                None
            }
        }

        // Other types not currently supported as UserContent
        // Can be extended here: SourceUrl, SourceDocument, Data, StepStart
        _ => None,
    }
}

/// Converts a [`UIMessagePart`] to an [`AssistantContent`] if applicable.
///
/// Returns `None` for parts that don't map to assistant content:
///
/// - `Text` → `AssistantContent::Text`
/// - `Reasoning` → `AssistantContent::Reasoning`
/// - `ToolCall` → `AssistantContent::ToolCall`
/// - `DynamicTool` (with input) → `AssistantContent::ToolCall`
fn to_assistant_content(part: &UIMessagePart) -> Option<AssistantContent> {
    match part {
        // Text part
        UIMessagePart::Text { text, .. } => Some(AssistantContent::text(text)),

        // Reasoning part
        UIMessagePart::Reasoning { text, .. } => {
            Some(AssistantContent::Reasoning(Reasoning::new(text)))
        }

        // Legacy tool call format
        UIMessagePart::ToolCall {
            tool_call_id,
            tool_name,
            args,
            ..
        } => Some(AssistantContent::ToolCall(
            ToolCall::new(
                tool_call_id.clone(),
                ToolFunction {
                    name: tool_name.clone(),
                    arguments: args.clone(),
                },
            )
            .with_call_id(tool_call_id.clone()),
        )),

        // DynamicTool tool call
        UIMessagePart::DynamicTool {
            tool_call_id,
            tool_name,
            state,
            ..
        } => {
            // Only convert to ToolCall when input is available
            match state {
                DynamicToolState::InputAvailable { input }
                | DynamicToolState::OutputAvailable { input, .. } => {
                    Some(AssistantContent::ToolCall(
                        ToolCall::new(
                            tool_call_id.clone(),
                            ToolFunction {
                                name: tool_name.clone(),
                                arguments: input.clone(),
                            },
                        )
                        .with_call_id(tool_call_id.clone()),
                    ))
                }
                DynamicToolState::InputStreaming { input } => {
                    // If there's partial input, create ToolCall anyway
                    input.as_ref().map(|i| {
                        AssistantContent::ToolCall(
                            ToolCall::new(
                                tool_call_id.clone(),
                                ToolFunction {
                                    name: tool_name.clone(),
                                    arguments: i.clone(),
                                },
                            )
                            .with_call_id(tool_call_id.clone()),
                        )
                    })
                }
                _ => None, // OutputError state doesn't convert to ToolCall
            }
        }

        // Other types not currently supported as AssistantContent
        _ => None,
    }
}

/// Converts a JSON [`Value`] to a string.
///
/// If the value is already a string, returns it directly.
/// Otherwise, serializes it to JSON.
fn json_to_string(value: &Value) -> String {
    match value {
        Value::String(s) => s.clone(),
        _ => serde_json::to_string(value).unwrap_or_default(),
    }
}

/// Parses an image MIME type string to an [`ImageMediaType`].
///
/// Returns `Some(ImageMediaType)` for supported types, `None` otherwise.
/// Note that BMP and TIFF are not supported by rig.
fn parse_image_media_type(media_type: &str) -> Option<ImageMediaType> {
    match media_type {
        "image/jpeg" | "image/jpg" => Some(ImageMediaType::JPEG),
        "image/png" => Some(ImageMediaType::PNG),
        "image/gif" => Some(ImageMediaType::GIF),
        "image/webp" => Some(ImageMediaType::WEBP),
        "image/heic" => Some(ImageMediaType::HEIC),
        "image/heif" => Some(ImageMediaType::HEIF),
        "image/svg+xml" => Some(ImageMediaType::SVG),
        // BMP and TIFF are not supported by rig, return None
        _ => None,
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::message::PartState;
    use serde_json::json;

    #[test]
    fn test_convert_user_text_message() {
        let msg = UIMessage {
            id: "1".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::Text {
                text: "Hello".to_string(),
                state: None,
                provider_metadata: None,
            }],
        };

        let result = convert_message(&msg).unwrap();

        println!("{:?}", result);
        match result {
            Message::User { content } => {
                assert_eq!(content.iter().count(), 1);
            }
            _ => panic!("Expected User message"),
        }
    }

    #[test]
    fn test_convert_assistant_text_message() {
        let msg = UIMessage {
            id: "2".to_string(),
            role: "assistant".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::Text {
                text: "Hi there!".to_string(),
                state: Some(PartState::Done),
                provider_metadata: None,
            }],
        };

        let result = convert_message(&msg).unwrap();
        println!("{:?}", result);
        match result {
            Message::Assistant { id, content } => {
                assert_eq!(id, Some("2".to_string()));
                assert_eq!(content.iter().count(), 1);
            }
            _ => panic!("Expected Assistant message"),
        }
    }

    #[test]
    fn test_convert_tool_call_message() {
        let msg = UIMessage {
            id: "3".to_string(),
            role: "assistant".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::ToolCall {
                tool_call_id: "call_123".to_string(),
                tool_name: "get_weather".to_string(),
                args: json!({"city": "北京"}),
                provider_metadata: None,
            }],
        };

        let result = convert_message(&msg).unwrap();
        println!("{:?}", result);
        match result {
            Message::Assistant { content, .. } => {
                let first = content.iter().next().unwrap();
                match first {
                    AssistantContent::ToolCall(tc) => {
                        assert_eq!(tc.function.name, "get_weather");
                    }
                    _ => panic!("Expected ToolCall"),
                }
            }
            _ => panic!("Expected Assistant message"),
        }
    }

    #[test]
    fn test_convert_dynamic_tool_message() {
        let msg = UIMessage {
            id: "4".to_string(),
            role: "assistant".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::DynamicTool {
                tool_name: "search".to_string(),
                tool_call_id: "call_456".to_string(),
                title: None,
                provider_executed: true,
                state: DynamicToolState::InputAvailable {
                    input: json!({"query": "Rust"}),
                },
                call_provider_metadata: None,
                preliminary: false,
            }],
        };

        let result = convert_message(&msg).unwrap();
        println!("{:?}", result);
        match result {
            Message::Assistant { content, .. } => {
                let first = content.iter().next().unwrap();
                match first {
                    AssistantContent::ToolCall(tc) => {
                        assert_eq!(tc.function.name, "search");
                    }
                    _ => panic!("Expected ToolCall"),
                }
            }
            _ => panic!("Expected Assistant message"),
        }
    }

    #[test]
    fn test_convert_tool_result_message() {
        let msg = UIMessage {
            id: "5".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::ToolResult {
                tool_call_id: "call_123".to_string(),
                tool_name: Some("get_weather".to_string()),
                result: json!({"temp": 25, "weather": ""}),
            }],
        };

        let result = convert_message(&msg).unwrap();
        println!("{:?}", result);
        match result {
            Message::User { content } => {
                let first = content.iter().next().unwrap();
                match first {
                    UserContent::ToolResult(tr) => {
                        assert_eq!(tr.id, "call_123");
                    }
                    _ => panic!("Expected ToolResult"),
                }
            }
            _ => panic!("Expected User message"),
        }
    }

    #[test]
    fn test_convert_dynamic_tool_result_message() {
        let msg = UIMessage {
            id: "6".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::DynamicTool {
                tool_name: "search".to_string(),
                tool_call_id: "call_789".to_string(),
                title: None,
                provider_executed: true,
                state: DynamicToolState::OutputAvailable {
                    input: json!({"query": "Rust"}),
                    output: json!({"results": ["Rust", "Cargo"]}),
                },
                call_provider_metadata: None,
                preliminary: false,
            }],
        };

        let result = convert_message(&msg).unwrap();
        println!("{:?}", result);
        match result {
            Message::User { content } => {
                let first = content.iter().next().unwrap();
                match first {
                    UserContent::ToolResult(tr) => {
                        assert_eq!(tr.id, "call_789");
                    }
                    _ => panic!("Expected ToolResult"),
                }
            }
            _ => panic!("Expected User message"),
        }
    }

    #[test]
    fn test_convert_image_file() {
        let msg = UIMessage {
            id: "7".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::File {
                media_type: "image/png".to_string(),
                url: "https://example.com/image.png".to_string(),
                filename: Some("screenshot.png".to_string()),
                provider_metadata: None,
            }],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::User { content } => {
                let first = content.iter().next().unwrap();
                match first {
                    UserContent::Image(img) => {
                        assert_eq!(img.media_type, Some(ImageMediaType::PNG));
                    }
                    _ => panic!("Expected Image"),
                }
            }
            _ => panic!("Expected User message"),
        }
    }

    #[test]
    fn test_convert_reasoning_part() {
        let msg = UIMessage {
            id: "8".to_string(),
            role: "assistant".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::Reasoning {
                text: "Let me think about this...".to_string(),
                state: Some(PartState::Done),
                provider_metadata: None,
            }],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::Assistant { content, .. } => {
                let first = content.iter().next().unwrap();
                match first {
                    AssistantContent::Reasoning(r) => {
                        // Reasoning 结构有 reasoning 字段 (Vec<String>) 而不是 text
                        assert!(r.reasoning.iter().any(|s| s.contains("think")));
                    }
                    _ => panic!("Expected Reasoning"),
                }
            }
            _ => panic!("Expected Assistant message"),
        }
    }

    #[test]
    fn test_dynamic_tool_streaming_with_empty_input() {
        let msg = UIMessage {
            id: "9".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![UIMessagePart::DynamicTool {
                tool_name: "search".to_string(),
                tool_call_id: "call_999".to_string(),
                title: None,
                provider_executed: true,
                state: DynamicToolState::InputStreaming { input: None },
                call_provider_metadata: None,
                preliminary: false,
            }],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::User { content } => {
                // InputStreaming 状态没有输入时不产生转换内容,会返回空文本
                let first = content.iter().next().unwrap();
                match first {
                    UserContent::Text(text) => {
                        assert!(text.text.is_empty());
                    }
                    _ => panic!("Expected empty Text content"),
                }
            }
            _ => panic!("Expected User message"),
        }
    }

    /// 测试用户消息:文本 + 图片
    #[test]
    fn test_multi_part_user_text_and_image() {
        let msg = UIMessage {
            id: "10".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![
                UIMessagePart::Text {
                    text: "请查看这张图片".to_string(),
                    state: None,
                    provider_metadata: None,
                },
                UIMessagePart::File {
                    media_type: "image/jpeg".to_string(),
                    url: "https://example.com/photo.jpg".to_string(),
                    filename: Some("photo.jpg".to_string()),
                    provider_metadata: None,
                },
            ],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::User { content } => {
                let contents: Vec<_> = content.iter().collect();
                assert_eq!(contents.len(), 2);
                // 第一个应该是文本
                match &contents[0] {
                    UserContent::Text(t) => {
                        assert_eq!(t.text, "请查看这张图片");
                    }
                    _ => panic!("Expected Text first"),
                }
                // 第二个应该是图片
                match &contents[1] {
                    UserContent::Image(img) => {
                        assert_eq!(img.media_type, Some(ImageMediaType::JPEG));
                    }
                    _ => panic!("Expected Image second"),
                }
            }
            _ => panic!("Expected User message"),
        }
    }

    /// 测试助手消息:文本 + 工具调用
    #[test]
    fn test_multi_part_assistant_text_and_tool_call() {
        let msg = UIMessage {
            id: "11".to_string(),
            role: "assistant".to_string(),
            metadata: None,
            parts: vec![
                UIMessagePart::Text {
                    text: "我来帮你查询天气".to_string(),
                    state: Some(PartState::Done),
                    provider_metadata: None,
                },
                UIMessagePart::DynamicTool {
                    tool_name: "get_weather".to_string(),
                    tool_call_id: "call_111".to_string(),
                    title: None,
                    provider_executed: true,
                    state: DynamicToolState::InputAvailable {
                        input: json!({"city": "北京"}),
                    },
                    call_provider_metadata: None,
                    preliminary: false,
                },
            ],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::Assistant { content, id } => {
                assert_eq!(id, Some("11".to_string()));
                let contents: Vec<_> = content.iter().collect();
                assert_eq!(contents.len(), 2);
                match &contents[0] {
                    AssistantContent::Text(t) => {
                        assert_eq!(t.text, "我来帮你查询天气");
                    }
                    _ => panic!("Expected Text first"),
                }
                match &contents[1] {
                    AssistantContent::ToolCall(tc) => {
                        assert_eq!(tc.function.name, "get_weather");
                    }
                    _ => panic!("Expected ToolCall second"),
                }
            }
            _ => panic!("Expected Assistant message"),
        }
    }

    /// 测试用户消息:工具结果 + 文本
    #[test]
    fn test_multi_part_user_tool_result_and_text() {
        let msg = UIMessage {
            id: "12".to_string(),
            role: "user".to_string(),
            metadata: None,
            parts: vec![
                UIMessagePart::DynamicTool {
                    tool_name: "search".to_string(),
                    tool_call_id: "call_222".to_string(),
                    title: None,
                    provider_executed: true,
                    state: DynamicToolState::OutputAvailable {
                        input: json!({"query": "Rust"}),
                        output: json!({"results": ["Rust Programming Language"]}),
                    },
                    call_provider_metadata: None,
                    preliminary: false,
                },
                UIMessagePart::Text {
                    text: "搜索结果怎么样?".to_string(),
                    state: None,
                    provider_metadata: None,
                },
            ],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::User { content } => {
                let contents: Vec<_> = content.iter().collect();
                assert_eq!(contents.len(), 2);
                // 第一个是工具结果
                match &contents[0] {
                    UserContent::ToolResult(tr) => {
                        assert_eq!(tr.id, "call_222");
                    }
                    _ => panic!("Expected ToolResult first"),
                }
                // 第二个是文本
                match &contents[1] {
                    UserContent::Text(t) => {
                        assert_eq!(t.text, "搜索结果怎么样?");
                    }
                    _ => panic!("Expected Text second"),
                }
            }
            _ => panic!("Expected User message"),
        }
    }

    /// 测试助手消息:推理 + 文本 + 工具调用
    #[test]
    fn test_multi_part_assistant_reasoning_text_and_tool() {
        let msg = UIMessage {
            id: "13".to_string(),
            role: "assistant".to_string(),
            metadata: None,
            parts: vec![
                UIMessagePart::Reasoning {
                    text: "需要分析用户的需求".to_string(),
                    state: Some(PartState::Done),
                    provider_metadata: None,
                },
                UIMessagePart::Text {
                    text: "我理解了,让我来处理".to_string(),
                    state: Some(PartState::Done),
                    provider_metadata: None,
                },
                UIMessagePart::ToolCall {
                    tool_call_id: "call_333".to_string(),
                    tool_name: "calculate".to_string(),
                    args: json!({"expression": "1+1"}),
                    provider_metadata: None,
                },
            ],
        };

        let result = convert_message(&msg).unwrap();
        match result {
            Message::Assistant { content, .. } => {
                let contents: Vec<_> = content.iter().collect();
                assert_eq!(contents.len(), 3);
                match &contents[0] {
                    AssistantContent::Reasoning(r) => {
                        assert!(r.reasoning.iter().any(|s| s.contains("用户的需求")));
                    }
                    _ => panic!("Expected Reasoning first"),
                }
                match &contents[1] {
                    AssistantContent::Text(t) => {
                        assert_eq!(t.text, "我理解了,让我来处理");
                    }
                    _ => panic!("Expected Text second"),
                }
                match &contents[2] {
                    AssistantContent::ToolCall(tc) => {
                        assert_eq!(tc.function.name, "calculate");
                    }
                    _ => panic!("Expected ToolCall third"),
                }
            }
            _ => panic!("Expected Assistant message"),
        }
    }

    /// 测试批量消息转换
    #[test]
    fn test_convert_multiple_messages() {
        let messages = vec![
            UIMessage {
                id: "14".to_string(),
                role: "user".to_string(),
                metadata: None,
                parts: vec![UIMessagePart::Text {
                    text: "你好".to_string(),
                    state: None,
                    provider_metadata: None,
                }],
            },
            UIMessage {
                id: "15".to_string(),
                role: "assistant".to_string(),
                metadata: None,
                parts: vec![UIMessagePart::Text {
                    text: "你好!".to_string(),
                    state: Some(PartState::Done),
                    provider_metadata: None,
                }],
            },
            UIMessage {
                id: "16".to_string(),
                role: "user".to_string(),
                metadata: None,
                parts: vec![UIMessagePart::Text {
                    text: "再见".to_string(),
                    state: None,
                    provider_metadata: None,
                }],
            },
        ];

        let result = convert_messages(&messages).unwrap();
        assert_eq!(result.len(), 3);

        // 验证第一条
        match &result[0] {
            Message::User { content } => {
                assert_eq!(content.iter().count(), 1);
            }
            _ => panic!("Expected User message at index 0"),
        }

        // 验证第二条
        match &result[1] {
            Message::Assistant { id, content } => {
                assert_eq!(id, &Some("15".to_string()));
                assert_eq!(content.iter().count(), 1);
            }
            _ => panic!("Expected Assistant message at index 1"),
        }

        // 验证第三条
        match &result[2] {
            Message::User { content } => {
                assert_eq!(content.iter().count(), 1);
            }
            _ => panic!("Expected User message at index 2"),
        }
    }
}