siumai 0.10.3

A unified LLM interface library for Rust
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
//! Enhanced Multimodal Support
//!
//! This module provides comprehensive multimodal capabilities including
//! image processing, audio handling, document processing, and format conversion.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::error::LlmError;
use crate::types::{ContentPart, MessageContent};

/// Multimodal content processor
#[allow(dead_code)]
pub struct MultimodalProcessor {
    /// Supported image formats
    image_formats: Vec<ImageFormat>,
    /// Supported audio formats
    audio_formats: Vec<AudioFormat>,
    /// Supported document formats
    document_formats: Vec<DocumentFormat>,
    /// Processing configuration
    config: ProcessingConfig,
}

impl MultimodalProcessor {
    /// Create a new multimodal processor
    pub fn new() -> Self {
        Self {
            image_formats: ImageFormat::all_supported(),
            audio_formats: AudioFormat::all_supported(),
            document_formats: DocumentFormat::all_supported(),
            config: ProcessingConfig::default(),
        }
    }

    /// Process multimodal content
    pub fn process_content(&self, content: &MessageContent) -> Result<ProcessedContent, LlmError> {
        match content {
            MessageContent::Text(text) => Ok(ProcessedContent::Text(text.clone())),
            MessageContent::MultiModal(parts) => {
                let mut processed_parts = Vec::new();

                for part in parts {
                    let processed_part = self.process_content_part(part)?;
                    processed_parts.push(processed_part);
                }

                Ok(ProcessedContent::MultiModal(processed_parts))
            }
        }
    }

    /// Process a single content part
    fn process_content_part(&self, part: &ContentPart) -> Result<ProcessedContentPart, LlmError> {
        match part {
            ContentPart::Text { text } => Ok(ProcessedContentPart::Text {
                text: text.clone(),
                metadata: ContentMetadata::default(),
            }),
            ContentPart::Image { image_url, detail } => {
                let image_info = self.analyze_image(image_url)?;
                let format = match image_info.format {
                    MediaFormat::Image(fmt) => fmt,
                    _ => ImageFormat::Jpeg, // Fallback
                };
                Ok(ProcessedContentPart::Image {
                    data: image_url.clone(),
                    format,
                    detail: detail.clone(),
                    metadata: image_info.metadata,
                })
            }
            ContentPart::Audio { audio_url, format } => {
                let audio_info = self.analyze_audio(audio_url, Some(format.as_str()))?;
                let format = match audio_info.format {
                    MediaFormat::Audio(fmt) => fmt,
                    _ => AudioFormat::Wav, // Fallback
                };
                Ok(ProcessedContentPart::Audio {
                    data: audio_url.clone(),
                    format,
                    metadata: audio_info.metadata,
                })
            }
        }
    }

    /// Analyze image content
    fn analyze_image(&self, image_data: &str) -> Result<MediaInfo, LlmError> {
        // Detect format from data URL or base64 header
        let format = if image_data.starts_with("data:image/") {
            let mime_type = image_data
                .split(';')
                .next()
                .and_then(|s| s.strip_prefix("data:"))
                .unwrap_or("image/jpeg");

            ImageFormat::from_mime_type(mime_type)
        } else {
            // Try to detect from base64 header
            ImageFormat::detect_from_base64(image_data)
        };

        let mut metadata = ContentMetadata::default();
        metadata.insert(
            "original_format".to_string(),
            serde_json::Value::String(format.to_string()),
        );

        // Add size estimation if possible
        if let Ok(size) = self.estimate_data_size(image_data) {
            metadata.insert(
                "estimated_size_bytes".to_string(),
                serde_json::Value::Number(size.into()),
            );
        }

        Ok(MediaInfo {
            format: MediaFormat::Image(format),
            metadata,
        })
    }

    /// Analyze audio content
    fn analyze_audio(
        &self,
        audio_data: &str,
        format_hint: Option<&str>,
    ) -> Result<MediaInfo, LlmError> {
        let format = if let Some(hint) = format_hint {
            AudioFormat::from_extension(hint)
        } else if audio_data.starts_with("data:audio/") {
            let mime_type = audio_data
                .split(';')
                .next()
                .and_then(|s| s.strip_prefix("data:"))
                .unwrap_or("audio/wav");

            AudioFormat::from_mime_type(mime_type)
        } else {
            AudioFormat::Wav // Default
        };

        let mut metadata = ContentMetadata::default();
        metadata.insert(
            "original_format".to_string(),
            serde_json::Value::String(format.to_string()),
        );

        if let Ok(size) = self.estimate_data_size(audio_data) {
            metadata.insert(
                "estimated_size_bytes".to_string(),
                serde_json::Value::Number(size.into()),
            );
        }

        Ok(MediaInfo {
            format: MediaFormat::Audio(format),
            metadata,
        })
    }

    /// Estimate data size from base64 string
    fn estimate_data_size(&self, data: &str) -> Result<u64, LlmError> {
        // Remove data URL prefix if present
        let base64_data = if data.contains(',') {
            data.split(',').nth(1).unwrap_or(data)
        } else {
            data
        };

        // Estimate size: base64 is ~4/3 the size of original data
        let base64_len = base64_data.len() as u64;
        Ok((base64_len * 3) / 4)
    }
}

impl Default for MultimodalProcessor {
    fn default() -> Self {
        Self::new()
    }
}

/// Processing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessingConfig {
    /// Maximum image size in bytes
    pub max_image_size: Option<u64>,
    /// Maximum audio duration in seconds
    pub max_audio_duration: Option<u32>,
    /// Whether to compress large files
    pub auto_compress: bool,
    /// Target compression quality (0.0-1.0)
    pub compression_quality: f32,
}

impl Default for ProcessingConfig {
    fn default() -> Self {
        Self {
            max_image_size: Some(20 * 1024 * 1024), // 20MB
            max_audio_duration: Some(300),          // 5 minutes
            auto_compress: true,
            compression_quality: 0.8,
        }
    }
}

/// Processed content
#[derive(Debug, Clone)]
pub enum ProcessedContent {
    /// Text content
    Text(String),
    /// Multimodal content
    MultiModal(Vec<ProcessedContentPart>),
}

/// Processed content part
#[derive(Debug, Clone)]
pub enum ProcessedContentPart {
    /// Text part
    Text {
        text: String,
        metadata: ContentMetadata,
    },
    /// Image part
    Image {
        data: String,
        format: ImageFormat,
        detail: Option<String>,
        metadata: ContentMetadata,
    },
    /// Audio part
    Audio {
        data: String,
        format: AudioFormat,
        metadata: ContentMetadata,
    },
    /// Document part
    Document {
        data: String,
        format: DocumentFormat,
        metadata: ContentMetadata,
    },
}

/// Content metadata
pub type ContentMetadata = HashMap<String, serde_json::Value>;

/// Media information
#[derive(Debug, Clone)]
pub struct MediaInfo {
    /// Media format
    pub format: MediaFormat,
    /// Metadata
    pub metadata: ContentMetadata,
}

/// Media format enumeration
#[derive(Debug, Clone)]
pub enum MediaFormat {
    /// Image format
    Image(ImageFormat),
    /// Audio format
    Audio(AudioFormat),
    /// Document format
    Document(DocumentFormat),
}

/// Supported image formats
#[derive(Debug, Clone, PartialEq)]
pub enum ImageFormat {
    Jpeg,
    Png,
    Gif,
    WebP,
    Bmp,
    Tiff,
    Svg,
}

impl ImageFormat {
    /// Get all supported image formats
    pub fn all_supported() -> Vec<Self> {
        vec![
            Self::Jpeg,
            Self::Png,
            Self::Gif,
            Self::WebP,
            Self::Bmp,
            Self::Tiff,
            Self::Svg,
        ]
    }

    /// Get format from MIME type
    pub fn from_mime_type(mime_type: &str) -> Self {
        match mime_type {
            "image/jpeg" | "image/jpg" => Self::Jpeg,
            "image/png" => Self::Png,
            "image/gif" => Self::Gif,
            "image/webp" => Self::WebP,
            "image/bmp" => Self::Bmp,
            "image/tiff" => Self::Tiff,
            "image/svg+xml" => Self::Svg,
            _ => Self::Jpeg, // Default
        }
    }

    /// Detect format from base64 data
    pub fn detect_from_base64(data: &str) -> Self {
        // Simple magic number detection
        if data.starts_with("/9j/") || data.starts_with("iVBOR") {
            Self::Jpeg
        } else if data.starts_with("iVBOR") {
            Self::Png
        } else if data.starts_with("R0lGOD") {
            Self::Gif
        } else if data.starts_with("UklGR") {
            Self::WebP
        } else {
            Self::Jpeg // Default
        }
    }

    /// Get MIME type
    pub const fn mime_type(&self) -> &'static str {
        match self {
            Self::Jpeg => "image/jpeg",
            Self::Png => "image/png",
            Self::Gif => "image/gif",
            Self::WebP => "image/webp",
            Self::Bmp => "image/bmp",
            Self::Tiff => "image/tiff",
            Self::Svg => "image/svg+xml",
        }
    }
}

impl std::fmt::Display for ImageFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

/// Supported audio formats
#[derive(Debug, Clone, PartialEq)]
pub enum AudioFormat {
    Mp3,
    Wav,
    Flac,
    Ogg,
    M4a,
    Webm,
}

impl AudioFormat {
    /// Get all supported audio formats
    pub fn all_supported() -> Vec<Self> {
        vec![
            Self::Mp3,
            Self::Wav,
            Self::Flac,
            Self::Ogg,
            Self::M4a,
            Self::Webm,
        ]
    }

    /// Get format from MIME type
    pub fn from_mime_type(mime_type: &str) -> Self {
        match mime_type {
            "audio/mpeg" | "audio/mp3" => Self::Mp3,
            "audio/wav" | "audio/wave" => Self::Wav,
            "audio/flac" => Self::Flac,
            "audio/ogg" => Self::Ogg,
            "audio/m4a" => Self::M4a,
            "audio/webm" => Self::Webm,
            _ => Self::Wav, // Default
        }
    }

    /// Get format from file extension
    pub fn from_extension(ext: &str) -> Self {
        match ext.to_lowercase().as_str() {
            "mp3" => Self::Mp3,
            "wav" => Self::Wav,
            "flac" => Self::Flac,
            "ogg" => Self::Ogg,
            "m4a" => Self::M4a,
            "webm" => Self::Webm,
            _ => Self::Wav, // Default
        }
    }

    /// Get MIME type
    pub const fn mime_type(&self) -> &'static str {
        match self {
            Self::Mp3 => "audio/mpeg",
            Self::Wav => "audio/wav",
            Self::Flac => "audio/flac",
            Self::Ogg => "audio/ogg",
            Self::M4a => "audio/m4a",
            Self::Webm => "audio/webm",
        }
    }
}

impl std::fmt::Display for AudioFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

/// Supported document formats
#[derive(Debug, Clone, PartialEq)]
pub enum DocumentFormat {
    Pdf,
    Docx,
    Txt,
    Md,
    Html,
    Csv,
    Json,
    Xml,
}

impl DocumentFormat {
    /// Get all supported document formats
    pub fn all_supported() -> Vec<Self> {
        vec![
            Self::Pdf,
            Self::Docx,
            Self::Txt,
            Self::Md,
            Self::Html,
            Self::Csv,
            Self::Json,
            Self::Xml,
        ]
    }

    /// Get format from MIME type
    pub fn from_mime_type(mime_type: &str) -> Self {
        match mime_type {
            "application/pdf" => Self::Pdf,
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => Self::Docx,
            "text/plain" => Self::Txt,
            "text/markdown" => Self::Md,
            "text/html" => Self::Html,
            "text/csv" => Self::Csv,
            "application/json" => Self::Json,
            "application/xml" | "text/xml" => Self::Xml,
            _ => Self::Txt, // Default
        }
    }

    /// Get MIME type
    pub const fn mime_type(&self) -> &'static str {
        match self {
            Self::Pdf => "application/pdf",
            Self::Docx => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
            Self::Txt => "text/plain",
            Self::Md => "text/markdown",
            Self::Html => "text/html",
            Self::Csv => "text/csv",
            Self::Json => "application/json",
            Self::Xml => "application/xml",
        }
    }
}

impl std::fmt::Display for DocumentFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self:?}")
    }
}

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

    #[test]
    fn test_image_format_detection() {
        assert_eq!(ImageFormat::from_mime_type("image/jpeg"), ImageFormat::Jpeg);
        assert_eq!(ImageFormat::from_mime_type("image/png"), ImageFormat::Png);
        assert_eq!(ImageFormat::from_mime_type("image/gif"), ImageFormat::Gif);
    }

    #[test]
    fn test_audio_format_detection() {
        assert_eq!(AudioFormat::from_mime_type("audio/mpeg"), AudioFormat::Mp3);
        assert_eq!(AudioFormat::from_extension("wav"), AudioFormat::Wav);
        assert_eq!(AudioFormat::from_extension("flac"), AudioFormat::Flac);
    }

    #[test]
    fn test_multimodal_processor() {
        let processor = MultimodalProcessor::new();

        let text_content = MessageContent::Text("Hello world".to_string());
        let processed = processor.process_content(&text_content).unwrap();

        match processed {
            ProcessedContent::Text(text) => assert_eq!(text, "Hello world"),
            _ => panic!("Expected text content"),
        }
    }
}