rig-ai-sdk 0.1.4

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
//! AI SDK UIMessage type definitions
//!
//! Implements the AI SDK UIMessage format for receiving messages from frontend clients
//! like assistant-ui.
//!
//! # Overview
//!
//! This module provides types for deserializing AI SDK messages from frontend clients.
//! The format supports:
//!
//! - Rich text content with streaming states
//! - Multi-modal messages (text, images, files)
//! - Tool calls and tool results (both legacy and AI SDK 5.x formats)
//! - Reasoning/model thinking blocks
//! - Source references (URLs, documents)
//! - Custom data attachments
//!
//! Reference: [AI SDK Transport](https://ai-sdk.dev/docs/ai-sdk-ui/transport)
//!
//! # Examples
//!
//! ```ignore
//! use rig_ai_sdk::{UIMessage, UIMessagePart};
//!
//! let msg = UIMessage {
//!     id: "msg-1".to_string(),
//!     role: "user".to_string(),
//!     parts: vec![
//!         UIMessagePart::Text {
//!             text: "Hello!".to_string(),
//!             state: None,
//!             provider_metadata: None,
//!         }
//!     ],
//!     metadata: None,
//! };
//! ```

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;

// ============================================================================
// Shared types
// ============================================================================

/// Provider metadata from AI SDK (simplified version).
///
/// Contains optional metadata fields that may be included by the AI provider.
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(default)]
pub struct ProviderMetadata {
    /// Model ID being used
    pub model_id: Option<String>,

    /// Request ID from the provider
    pub request_id: Option<String>,

    /// Timestamp of the request
    pub timestamp: Option<String>,

    /// Additional provider-specific metadata fields
    #[serde(flatten)]
    pub extra: Value,
}

/// Streaming state for message parts.
///
/// Indicates whether content is being streamed or is complete.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum PartState {
    /// Content is currently being streamed
    Streaming,

    /// Content is complete
    #[default]
    Done,
}

// ============================================================================
// AI SDK UIMessage Part types
// ============================================================================

/// Text content part
///
/// Plain text content with optional streaming state and provider metadata.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TextPart {
    pub text: String,
    pub state: Option<PartState>,
    pub provider_metadata: Option<ProviderMetadata>,
}

/// Reasoning/thinking part
///
/// Model reasoning or thinking block content (e.g., for o1-style models).
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ReasoningPart {
    /// Reasoning/thinking content
    pub text: String,
    pub state: Option<PartState>,
    pub provider_metadata: Option<ProviderMetadata>,
}

/// File attachment part (supports any media type)
///
/// Represents a file attachment with URL, media type, and optional filename.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FilePart {
    pub media_type: String,
    pub url: String,
    pub filename: Option<String>,
    pub provider_metadata: Option<ProviderMetadata>,
}

/// Tool state as string literal (AI SDK 5.x format)
///
/// Represents the current state of a tool invocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ToolState {
    /// Input is being streamed
    InputStreaming,
    /// Input is available
    InputAvailable,
    /// Output is available
    OutputAvailable,
    /// Output error
    OutputError,
}

/// Tool invocation part (AI SDK 5.x ToolUIPart format)
///
/// Modern tool call format with streaming state support and provider execution info.
/// The `type` field uses dynamic format: "tool-{toolName}"
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ToolPart {
    pub tool_call_id: String,
    #[serde(default)]
    pub tool_name: String,
    pub state: ToolState,
    pub title: Option<String>,
    pub provider_executed: Option<bool>,
    pub call_provider_metadata: Option<ProviderMetadata>,
    pub preliminary: Option<bool>,
    pub input: Option<Value>,
    pub output: Option<Value>,
    pub raw_input: Option<Value>,
    pub error_text: Option<String>,
}

/// URL source reference
///
/// References a URL as a source for the response.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceUrlPart {
    pub source_id: String,
    pub url: String,
    pub title: Option<String>,
    pub provider_metadata: Option<ProviderMetadata>,
}

/// Document source reference
///
/// References a document as a source with media type and title.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceDocumentPart {
    pub source_id: String,
    pub media_type: String,
    pub title: String,
    pub filename: Option<String>,
    pub provider_metadata: Option<ProviderMetadata>,
}

/// Custom data part (supports dynamic `data-{name}` tags)
///
/// Arbitrary custom data attachment with a type name.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DataPart {
    /// Data type name (the `{name}` in `data-{name}`)
    pub data_type: String,
    pub id: Option<String>,
    pub data: Value,
}

/// AI SDK UIMessage part types.
///
/// A message consists of one or more parts, each representing a different
/// type of content (text, file, tool call, reasoning, etc.).
///
/// The format uses a `type` field to distinguish between different part types:
/// ```json
/// { "type": "text", "text": "Hello" }
/// { "type": "file", "mediaType": "image/png", "url": "..." }
/// { "type": "tool-get_weather", "toolCallId": "...", "state": "input-available" }
/// { "type": "data-usage", "data": {...} }
/// ```
#[derive(Debug, Clone, Serialize)]
pub enum UIMessagePart {
    /// Text content
    Text(TextPart),
    /// Reasoning/thinking content
    Reasoning(ReasoningPart),
    /// File attachment
    File(FilePart),
    /// Tool invocation (AI SDK 5.x ToolUIPart format)
    Tool(ToolPart),
    /// URL source
    SourceUrl(SourceUrlPart),
    /// Document source
    SourceDocument(SourceDocumentPart),
    /// Step start marker
    StepStart,
    /// Dynamic data part (data-{name} pattern)
    Data(DataPart),
}

/// Helper struct for deserialization of UIMessagePart with type field
///
/// Uses `tag = "type"` to handle standard types: text, reasoning, file,
/// source-url, source-document, step-start. Tool parts are handled separately
/// due to dynamic type field (tool-{NAME}).
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
enum UIMessagePartTagged {
    Text(TextPart),
    Reasoning(ReasoningPart),
    File(FilePart),
    SourceUrl(SourceUrlPart),
    SourceDocument(SourceDocumentPart),
    StepStart,
}

impl<'de> Deserialize<'de> for UIMessagePart {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let raw = serde_json::Value::deserialize(deserializer)?;

        // Check if this is a data-{name} or tool-{NAME} type
        if let Some(t) = raw.get("type").and_then(|v| v.as_str()) {
            if t.starts_with("data-") {
                let data_part = DataPart {
                    data_type: t.strip_prefix("data-").unwrap_or(t).to_string(),
                    id: raw.get("id").and_then(|v| v.as_str()).map(String::from),
                    data: raw.get("data").cloned().unwrap_or(Value::Null),
                };
                return Ok(UIMessagePart::Data(data_part));
            }

            // Handle tool-{NAME} type (AI SDK 5.x ToolUIPart format)
            if t.starts_with("tool-") {
                let tool_name = t.strip_prefix("tool-").unwrap_or(t).to_string();
                let mut tool_part: ToolPart =
                    serde_json::from_value(raw.clone()).map_err(serde::de::Error::custom)?;
                // Set tool_name from the type field
                tool_part.tool_name = tool_name;
                return Ok(UIMessagePart::Tool(tool_part));
            }
        }

        // Otherwise, try tagged deserialization
        let tagged: Result<UIMessagePartTagged, _> =
            serde_json::from_value(raw.clone()).map_err(serde::de::Error::custom);

        match tagged {
            Ok(tagged) => Ok(match tagged {
                UIMessagePartTagged::Text(v) => UIMessagePart::Text(v),
                UIMessagePartTagged::Reasoning(v) => UIMessagePart::Reasoning(v),
                UIMessagePartTagged::File(v) => UIMessagePart::File(v),
                UIMessagePartTagged::SourceUrl(v) => UIMessagePart::SourceUrl(v),
                UIMessagePartTagged::SourceDocument(v) => UIMessagePart::SourceDocument(v),
                UIMessagePartTagged::StepStart => UIMessagePart::StepStart,
            }),
            Err(e) => Err(e),
        }
    }
}

/// Media type classification
///
/// Categorizes media types into broader categories for easier handling.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaType {
    /// Image media type
    Image,

    /// Audio media type
    Audio,

    /// Video media type
    Video,

    /// Document media type
    Document,

    /// Other/unknown media type
    Other,
}

impl UIMessagePart {
    /// Gets the text content if this is a `Text` part.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use rig_ai_sdk::UIMessagePart;
    ///
    /// let part = UIMessagePart::Text {
    ///     text: "Hello".to_string(),
    ///     state: None,
    ///     provider_metadata: None,
    /// };
    /// assert_eq!(part.as_text(), Some("Hello"));
    /// ```
    pub fn as_text(&self) -> Option<&str> {
        match self {
            UIMessagePart::Text(p) => Some(&p.text),
            _ => None,
        }
    }

    /// Returns `true` if this is a `Text` part.
    pub fn is_text(&self) -> bool {
        matches!(self, UIMessagePart::Text(_))
    }

    /// Returns `true` if this is a `Reasoning` part.
    pub fn is_reasoning(&self) -> bool {
        matches!(self, UIMessagePart::Reasoning(_))
    }

    /// Returns `true` if this is a tool invocation.
    pub fn is_tool(&self) -> bool {
        matches!(self, UIMessagePart::Tool(_))
    }

    /// Gets the tool part if this is a Tool.
    pub fn as_tool(&self) -> Option<&ToolPart> {
        match self {
            UIMessagePart::Tool(p) => Some(p),
            _ => None,
        }
    }

    /// Returns `true` if this is a `Data` part.
    pub fn is_data(&self) -> bool {
        matches!(self, UIMessagePart::Data(_))
    }

    /// Gets the data content if this is a `Data` part.
    pub fn as_data(&self) -> Option<&DataPart> {
        match self {
            UIMessagePart::Data(p) => Some(p),
            _ => None,
        }
    }

    /// Gets the file content if this is a `File` part.
    ///
    /// Returns a tuple of `(media_type, url, optional_filename)`.
    pub fn as_file(&self) -> Option<(&str, &str, Option<&String>)> {
        match self {
            UIMessagePart::File(p) => Some((&p.media_type, &p.url, p.filename.as_ref())),
            _ => None,
        }
    }

    /// Gets the streaming state if applicable.
    ///
    /// Returns `Some(state)` for `Text` and `Reasoning` parts, `None` otherwise.
    pub fn state(&self) -> Option<PartState> {
        match self {
            UIMessagePart::Text(p) => p.state,
            UIMessagePart::Reasoning(p) => p.state,
            _ => None,
        }
    }

    /// Parses the media type into a [`MediaType`] category.
    ///
    /// Returns `Some(MediaType)` for `File` and `SourceDocument` parts, `None` otherwise.
    pub fn media_type_kind(&self) -> Option<MediaType> {
        match self {
            UIMessagePart::File(p) => {
                if p.media_type.starts_with("image/") {
                    Some(MediaType::Image)
                } else if p.media_type.starts_with("audio/") {
                    Some(MediaType::Audio)
                } else if p.media_type.starts_with("video/") {
                    Some(MediaType::Video)
                } else if matches!(
                    p.media_type.as_str(),
                    "application/pdf"
                        | "application/msword"
                        | "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                        | "text/plain"
                        | "text/csv"
                        | "application/json"
                ) {
                    Some(MediaType::Document)
                } else {
                    Some(MediaType::Other)
                }
            }
            UIMessagePart::SourceDocument(p) => {
                if p.media_type.starts_with("image/") {
                    Some(MediaType::Image)
                } else if p.media_type.starts_with("audio/") {
                    Some(MediaType::Audio)
                } else if p.media_type.starts_with("video/") {
                    Some(MediaType::Video)
                } else if matches!(
                    p.media_type.as_str(),
                    "application/pdf"
                        | "application/msword"
                        | "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                        | "text/plain"
                        | "text/csv"
                        | "application/json"
                ) {
                    Some(MediaType::Document)
                } else {
                    Some(MediaType::Other)
                }
            }
            _ => None,
        }
    }
}

// ============================================================================
// AI SDK UIMessage
// ============================================================================

/// AI SDK UIMessage format
///
/// Represents a message in the AI SDK format with role, parts, and metadata.
///
/// Reference: [AI SDK Transport](https://ai-sdk.dev/docs/ai-sdk-ui/transport)
#[derive(Debug, Clone, Deserialize)]
pub struct UIMessage {
    /// Unique message ID
    pub id: String,

    /// Message role: "user", "assistant", or "system"
    pub role: String,

    /// Optional message metadata
    pub metadata: Option<Value>,

    /// Message parts (content items)
    pub parts: Vec<UIMessagePart>,
}

impl UIMessage {
    /// Gets the concatenated text content from all `Text` parts.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use rig_ai_sdk::UIMessage;
    ///
    /// let msg = UIMessage {
    ///     id: "1".to_string(),
    ///     role: "user".to_string(),
    ///     parts: vec![
    ///         UIMessagePart::Text { text: "Hello, ".to_string(), state: None, provider_metadata: None },
    ///         UIMessagePart::Text { text: "world!".to_string(), state: None, provider_metadata: None },
    ///     ],
    ///     metadata: None,
    /// };
    /// assert_eq!(msg.text(), "Hello, world!");
    /// ```
    pub fn text(&self) -> String {
        self.parts
            .iter()
            .filter_map(|p| p.as_text())
            .collect::<Vec<_>>()
            .join("")
    }

    /// Returns `true` if the message role is "user".
    pub fn is_user(&self) -> bool {
        self.role == "user"
    }

    /// Returns `true` if the message role is "assistant".
    pub fn is_assistant(&self) -> bool {
        self.role == "assistant"
    }

    /// Returns `true` if the message role is "system".
    pub fn is_system(&self) -> bool {
        self.role == "system"
    }

    /// Gets all parts matching the given predicate.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use rig_ai_sdk::UIMessage;
    ///
    /// let tool_parts = msg.get_parts_by_type(|p| p.is_tool_call());
    /// ```
    pub fn get_parts_by_type<F>(&self, predicate: F) -> Vec<&UIMessagePart>
    where
        F: Fn(&UIMessagePart) -> bool,
    {
        self.parts.iter().filter(|p| predicate(p)).collect()
    }

    /// Returns `true` if the message contains streaming content.
    ///
    /// Checks if any part has `PartState::Streaming`.
    pub fn has_streaming_content(&self) -> bool {
        self.parts
            .iter()
            .any(|p| p.state() == Some(PartState::Streaming))
    }

    /// Returns `true` if the message contains tool invocations.
    pub fn has_tool_calls(&self) -> bool {
        self.parts.iter().any(|p| p.is_tool())
    }

    /// Returns `true` if the message contains file attachments.
    pub fn has_files(&self) -> bool {
        self.parts.iter().any(|p| p.as_file().is_some())
    }
}