infernum-server 0.2.0-rc.2

HTTP API server for local LLM inference
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
//! Vision and multimodal content support.
//!
//! This module provides support for vision models that can process both text
//! and image inputs. It extends the standard chat message format to support
//! multimodal content.
//!
//! # Content Types
//!
//! Messages can contain multiple content parts:
//!
//! - **Text**: Plain text content
//! - **ImageUrl**: Image from a URL (with optional detail level)
//! - **ImageBase64**: Base64-encoded image data
//!
//! # Example
//!
//! ```ignore
//! use infernum_server::vision::{MessageContent, ContentPart, ImageDetail};
//!
//! let content = MessageContent::Parts(vec![
//!     ContentPart::Text { text: "What's in this image?".to_string() },
//!     ContentPart::ImageUrl {
//!         image_url: ImageUrl {
//!             url: "https://example.com/image.png".to_string(),
//!             detail: Some(ImageDetail::High),
//!         },
//!     },
//! ]);
//! ```
//!
//! # Image Validation
//!
//! The module provides validation for:
//! - Supported media types (JPEG, PNG, GIF, WebP)
//! - Base64 data format
//! - URL format
//! - Maximum image size

use std::fmt;

use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

/// Message content that can be text or multimodal.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(untagged)]
pub enum MessageContent {
    /// Simple text content (backward compatible).
    Text(String),

    /// Array of content parts for multimodal messages.
    Parts(Vec<ContentPart>),
}

impl Default for MessageContent {
    fn default() -> Self {
        Self::Text(String::new())
    }
}

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

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

impl MessageContent {
    /// Creates text content.
    pub fn text(s: impl Into<String>) -> Self {
        Self::Text(s.into())
    }

    /// Creates content with a single image URL.
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::Parts(vec![ContentPart::ImageUrl {
            image_url: ImageUrl::new(url),
        }])
    }

    /// Creates content with text and an image URL.
    pub fn text_and_image(text: impl Into<String>, url: impl Into<String>) -> Self {
        Self::Parts(vec![
            ContentPart::Text { text: text.into() },
            ContentPart::ImageUrl {
                image_url: ImageUrl::new(url),
            },
        ])
    }

    /// Creates content with a base64 image.
    pub fn image_base64(data: impl Into<String>, media_type: impl Into<String>) -> Self {
        Self::Parts(vec![ContentPart::ImageBase64 {
            image_base64: ImageBase64 {
                data: data.into(),
                media_type: media_type.into(),
            },
        }])
    }

    /// Returns true if this is text-only content.
    pub fn is_text(&self) -> bool {
        matches!(self, Self::Text(_))
    }

    /// Returns true if this content contains images.
    pub fn has_images(&self) -> bool {
        match self {
            Self::Text(_) => false,
            Self::Parts(parts) => parts.iter().any(|p| p.is_image()),
        }
    }

    /// Gets the text content if this is text-only.
    pub fn as_text(&self) -> Option<&str> {
        match self {
            Self::Text(s) => Some(s),
            Self::Parts(_) => None,
        }
    }

    /// Extracts all text from the content.
    pub fn extract_text(&self) -> String {
        match self {
            Self::Text(s) => s.clone(),
            Self::Parts(parts) => parts
                .iter()
                .filter_map(|p| match p {
                    ContentPart::Text { text } => Some(text.as_str()),
                    _ => None,
                })
                .collect::<Vec<_>>()
                .join(" "),
        }
    }

    /// Returns the number of content parts.
    pub fn part_count(&self) -> usize {
        match self {
            Self::Text(_) => 1,
            Self::Parts(parts) => parts.len(),
        }
    }

    /// Returns the number of images in the content.
    pub fn image_count(&self) -> usize {
        match self {
            Self::Text(_) => 0,
            Self::Parts(parts) => parts.iter().filter(|p| p.is_image()).count(),
        }
    }

    /// Validates the content.
    pub fn validate(&self, config: &VisionConfig) -> Result<(), VisionError> {
        match self {
            Self::Text(s) => {
                if s.len() > config.max_text_length {
                    return Err(VisionError::TextTooLong {
                        actual: s.len(),
                        max: config.max_text_length,
                    });
                }
            },
            Self::Parts(parts) => {
                if parts.is_empty() {
                    return Err(VisionError::EmptyContent);
                }

                let image_count = parts.iter().filter(|p| p.is_image()).count();
                if image_count > config.max_images_per_message {
                    return Err(VisionError::TooManyImages {
                        actual: image_count,
                        max: config.max_images_per_message,
                    });
                }

                for part in parts {
                    part.validate(config)?;
                }
            },
        }
        Ok(())
    }
}

/// A part of multimodal content.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPart {
    /// Text content.
    Text {
        /// The text content.
        text: String,
    },

    /// Image from URL.
    #[serde(rename = "image_url")]
    ImageUrl {
        /// The image URL specification.
        image_url: ImageUrl,
    },

    /// Base64-encoded image.
    #[serde(rename = "image_base64")]
    ImageBase64 {
        /// The base64 image specification.
        image_base64: ImageBase64,
    },
}

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

    /// Creates an image URL part.
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: ImageUrl::new(url),
        }
    }

    /// Creates a base64 image part.
    pub fn image_base64(data: impl Into<String>, media_type: impl Into<String>) -> Self {
        Self::ImageBase64 {
            image_base64: ImageBase64 {
                data: data.into(),
                media_type: media_type.into(),
            },
        }
    }

    /// Returns true if this is an image part.
    pub fn is_image(&self) -> bool {
        matches!(self, Self::ImageUrl { .. } | Self::ImageBase64 { .. })
    }

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

    /// Validates the content part.
    pub fn validate(&self, config: &VisionConfig) -> Result<(), VisionError> {
        match self {
            Self::Text { text } => {
                if text.len() > config.max_text_length {
                    return Err(VisionError::TextTooLong {
                        actual: text.len(),
                        max: config.max_text_length,
                    });
                }
            },
            Self::ImageUrl { image_url } => {
                image_url.validate(config)?;
            },
            Self::ImageBase64 { image_base64 } => {
                image_base64.validate(config)?;
            },
        }
        Ok(())
    }
}

/// Image URL specification.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct ImageUrl {
    /// The URL of the image.
    pub url: String,

    /// Detail level for image processing.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub detail: Option<ImageDetail>,
}

impl ImageUrl {
    /// Creates a new image URL.
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            detail: None,
        }
    }

    /// Builder method to set detail level.
    pub fn with_detail(mut self, detail: ImageDetail) -> Self {
        self.detail = Some(detail);
        self
    }

    /// Gets the effective detail level.
    pub fn effective_detail(&self) -> ImageDetail {
        self.detail.unwrap_or_default()
    }

    /// Validates the image URL.
    pub fn validate(&self, _config: &VisionConfig) -> Result<(), VisionError> {
        if self.url.is_empty() {
            return Err(VisionError::InvalidUrl("URL is empty".to_string()));
        }

        // Check for valid URL format
        if !self.url.starts_with("http://")
            && !self.url.starts_with("https://")
            && !self.url.starts_with("data:")
        {
            return Err(VisionError::InvalidUrl(
                "URL must start with http://, https://, or data:".to_string(),
            ));
        }

        // If it's a data URL, validate the format
        if self.url.starts_with("data:") {
            validate_data_url(&self.url)?;
        }

        Ok(())
    }
}

/// Base64-encoded image specification.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, ToSchema)]
pub struct ImageBase64 {
    /// The base64-encoded image data.
    pub data: String,

    /// The MIME type of the image.
    pub media_type: String,
}

impl ImageBase64 {
    /// Creates a new base64 image.
    pub fn new(data: impl Into<String>, media_type: impl Into<String>) -> Self {
        Self {
            data: data.into(),
            media_type: media_type.into(),
        }
    }

    /// Validates the base64 image.
    pub fn validate(&self, config: &VisionConfig) -> Result<(), VisionError> {
        // Validate media type
        if !is_supported_media_type(&self.media_type) {
            return Err(VisionError::UnsupportedMediaType(self.media_type.clone()));
        }

        // Validate base64 data is not empty
        if self.data.is_empty() {
            return Err(VisionError::InvalidBase64("Empty data".to_string()));
        }

        // Validate base64 format (rough check)
        if !is_valid_base64(&self.data) {
            return Err(VisionError::InvalidBase64(
                "Invalid base64 format".to_string(),
            ));
        }

        // Check approximate decoded size
        let estimated_size = estimate_base64_size(&self.data);
        if estimated_size > config.max_image_size {
            return Err(VisionError::ImageTooLarge {
                actual: estimated_size,
                max: config.max_image_size,
            });
        }

        Ok(())
    }

    /// Decodes the base64 data.
    pub fn decode(&self) -> Result<Vec<u8>, VisionError> {
        // Remove whitespace and padding issues
        let clean_data: String = self.data.chars().filter(|c| !c.is_whitespace()).collect();

        // Simple base64 decode
        decode_base64(&clean_data)
    }

    /// Converts to a data URL.
    pub fn to_data_url(&self) -> String {
        format!("data:{};base64,{}", self.media_type, self.data)
    }
}

/// Image processing detail level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum ImageDetail {
    /// Low resolution (512x512, faster, cheaper).
    Low,

    /// High resolution (up to 2048x2048, slower, more expensive).
    High,

    /// Automatic selection based on image size.
    #[default]
    Auto,
}

impl ImageDetail {
    /// Gets the maximum dimension for this detail level.
    pub fn max_dimension(&self) -> u32 {
        match self {
            Self::Low => 512,
            Self::High | Self::Auto => 2048,
        }
    }

    /// Gets the tile size for high-detail processing.
    pub fn tile_size(&self) -> u32 {
        match self {
            Self::Low => 512,
            Self::High | Self::Auto => 512, // High uses 512x512 tiles
        }
    }
}

impl fmt::Display for ImageDetail {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Low => write!(f, "low"),
            Self::High => write!(f, "high"),
            Self::Auto => write!(f, "auto"),
        }
    }
}

/// Supported image media types.
pub const SUPPORTED_MEDIA_TYPES: &[&str] = &[
    "image/jpeg",
    "image/jpg",
    "image/png",
    "image/gif",
    "image/webp",
];

/// Checks if a media type is supported.
pub fn is_supported_media_type(media_type: &str) -> bool {
    let normalized = media_type.to_lowercase();
    SUPPORTED_MEDIA_TYPES.contains(&normalized.as_str())
}

/// Vision configuration.
#[derive(Debug, Clone)]
pub struct VisionConfig {
    /// Maximum text length per part.
    pub max_text_length: usize,

    /// Maximum image size in bytes.
    pub max_image_size: usize,

    /// Maximum images per message.
    pub max_images_per_message: usize,

    /// Maximum total images per request.
    pub max_images_per_request: usize,

    /// Allow external URLs.
    pub allow_external_urls: bool,

    /// Allowed URL hosts (empty = allow all).
    pub allowed_hosts: Vec<String>,
}

impl Default for VisionConfig {
    fn default() -> Self {
        Self {
            max_text_length: 100_000,
            max_image_size: 20 * 1024 * 1024, // 20MB
            max_images_per_message: 10,
            max_images_per_request: 50,
            allow_external_urls: true,
            allowed_hosts: Vec::new(),
        }
    }
}

impl VisionConfig {
    /// Creates a new vision config with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Builder method to set max image size.
    pub fn with_max_image_size(mut self, size: usize) -> Self {
        self.max_image_size = size;
        self
    }

    /// Builder method to set max images per message.
    pub fn with_max_images_per_message(mut self, max: usize) -> Self {
        self.max_images_per_message = max;
        self
    }

    /// Builder method to disable external URLs.
    pub fn with_external_urls_disabled(mut self) -> Self {
        self.allow_external_urls = false;
        self
    }

    /// Builder method to set allowed hosts.
    pub fn with_allowed_hosts(mut self, hosts: Vec<String>) -> Self {
        self.allowed_hosts = hosts;
        self
    }
}

/// Vision error types.
#[derive(Debug, Clone, PartialEq)]
pub enum VisionError {
    /// Text content is too long.
    TextTooLong {
        /// Actual length.
        actual: usize,
        /// Maximum allowed.
        max: usize,
    },

    /// Image is too large.
    ImageTooLarge {
        /// Actual size in bytes.
        actual: usize,
        /// Maximum allowed.
        max: usize,
    },

    /// Too many images in a message.
    TooManyImages {
        /// Actual count.
        actual: usize,
        /// Maximum allowed.
        max: usize,
    },

    /// Invalid image URL.
    InvalidUrl(String),

    /// Invalid base64 data.
    InvalidBase64(String),

    /// Unsupported media type.
    UnsupportedMediaType(String),

    /// Empty content.
    EmptyContent,

    /// External URLs not allowed.
    ExternalUrlNotAllowed,

    /// Host not in allowed list.
    HostNotAllowed(String),
}

impl fmt::Display for VisionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TextTooLong { actual, max } => {
                write!(f, "Text too long: {} characters (max {})", actual, max)
            },
            Self::ImageTooLarge { actual, max } => {
                write!(f, "Image too large: {} bytes (max {})", actual, max)
            },
            Self::TooManyImages { actual, max } => {
                write!(f, "Too many images: {} (max {})", actual, max)
            },
            Self::InvalidUrl(msg) => write!(f, "Invalid URL: {}", msg),
            Self::InvalidBase64(msg) => write!(f, "Invalid base64: {}", msg),
            Self::UnsupportedMediaType(mt) => write!(f, "Unsupported media type: {}", mt),
            Self::EmptyContent => write!(f, "Content is empty"),
            Self::ExternalUrlNotAllowed => write!(f, "External URLs are not allowed"),
            Self::HostNotAllowed(host) => write!(f, "Host not allowed: {}", host),
        }
    }
}

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

/// Validates a data URL format.
fn validate_data_url(url: &str) -> Result<(), VisionError> {
    // data:[<mediatype>][;base64],<data>
    if !url.starts_with("data:") {
        return Err(VisionError::InvalidUrl("Not a data URL".to_string()));
    }

    let rest = &url[5..];

    // Find the comma separating metadata from data
    let comma_pos = rest
        .find(',')
        .ok_or_else(|| VisionError::InvalidUrl("Missing comma in data URL".to_string()))?;

    let metadata = &rest[..comma_pos];

    // Check for base64 encoding
    if !metadata.contains(";base64") {
        return Err(VisionError::InvalidUrl(
            "Data URL must use base64 encoding".to_string(),
        ));
    }

    // Extract media type
    let media_type = metadata.split(';').next().unwrap_or("");
    if !media_type.is_empty() && !is_supported_media_type(media_type) {
        return Err(VisionError::UnsupportedMediaType(media_type.to_string()));
    }

    Ok(())
}

/// Checks if a string is valid base64.
fn is_valid_base64(data: &str) -> bool {
    // Check for valid base64 characters
    data.chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '/' || c == '=' || c.is_whitespace())
}

/// Estimates the decoded size of base64 data.
fn estimate_base64_size(data: &str) -> usize {
    // Base64 encoding uses 4 characters for every 3 bytes
    // Remove whitespace and padding for estimate
    let clean_len = data
        .chars()
        .filter(|c| !c.is_whitespace() && *c != '=')
        .count();
    (clean_len * 3) / 4
}

/// Decodes base64 data.
fn decode_base64(data: &str) -> Result<Vec<u8>, VisionError> {
    // Simple base64 decoding
    let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let mut result = Vec::with_capacity((data.len() * 3) / 4);
    let mut buffer: u32 = 0;
    let mut bits: u32 = 0;

    for c in data.chars() {
        if c == '=' {
            break;
        }
        if c.is_whitespace() {
            continue;
        }

        let value = alphabet
            .iter()
            .position(|&x| x == c as u8)
            .ok_or_else(|| VisionError::InvalidBase64(format!("Invalid character: {}", c)))?
            as u32;

        buffer = (buffer << 6) | value;
        bits += 6;

        if bits >= 8 {
            bits -= 8;
            result.push((buffer >> bits) as u8);
            buffer &= (1 << bits) - 1;
        }
    }

    Ok(result)
}

/// Vision metrics for monitoring.
#[derive(Debug, Default)]
pub struct VisionMetrics {
    /// Total images processed.
    images_processed: std::sync::atomic::AtomicU64,

    /// Total images rejected.
    images_rejected: std::sync::atomic::AtomicU64,

    /// Total bytes processed.
    bytes_processed: std::sync::atomic::AtomicU64,
}

impl VisionMetrics {
    /// Creates new vision metrics.
    pub fn new() -> Self {
        Self::default()
    }

    /// Records an image processed.
    pub fn record_image_processed(&self, bytes: usize) {
        self.images_processed
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.bytes_processed
            .fetch_add(bytes as u64, std::sync::atomic::Ordering::Relaxed);
    }

    /// Records an image rejected.
    pub fn record_image_rejected(&self) {
        self.images_rejected
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    }

    /// Gets images processed count.
    pub fn images_processed(&self) -> u64 {
        self.images_processed
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Gets images rejected count.
    pub fn images_rejected(&self) -> u64 {
        self.images_rejected
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Gets bytes processed.
    pub fn bytes_processed(&self) -> u64 {
        self.bytes_processed
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Renders metrics in Prometheus format.
    pub fn prometheus(&self) -> String {
        let mut output = String::new();

        output.push_str("# HELP infernum_vision_images_processed_total Total images processed\n");
        output.push_str("# TYPE infernum_vision_images_processed_total counter\n");
        output.push_str(&format!(
            "infernum_vision_images_processed_total {}\n",
            self.images_processed()
        ));

        output.push_str("# HELP infernum_vision_images_rejected_total Total images rejected\n");
        output.push_str("# TYPE infernum_vision_images_rejected_total counter\n");
        output.push_str(&format!(
            "infernum_vision_images_rejected_total {}\n",
            self.images_rejected()
        ));

        output.push_str("# HELP infernum_vision_bytes_processed_total Total bytes processed\n");
        output.push_str("# TYPE infernum_vision_bytes_processed_total counter\n");
        output.push_str(&format!(
            "infernum_vision_bytes_processed_total {}\n",
            self.bytes_processed()
        ));

        output
    }
}

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

    #[test]
    fn test_message_content_text() {
        let content = MessageContent::text("Hello");
        assert!(content.is_text());
        assert!(!content.has_images());
        assert_eq!(content.as_text(), Some("Hello"));
        assert_eq!(content.extract_text(), "Hello");
        assert_eq!(content.part_count(), 1);
        assert_eq!(content.image_count(), 0);
    }

    #[test]
    fn test_message_content_from_string() {
        let content: MessageContent = "Test".into();
        assert!(content.is_text());
        assert_eq!(content.as_text(), Some("Test"));
    }

    #[test]
    fn test_message_content_image_url() {
        let content = MessageContent::image_url("https://example.com/image.png");
        assert!(!content.is_text());
        assert!(content.has_images());
        assert_eq!(content.image_count(), 1);
    }

    #[test]
    fn test_message_content_text_and_image() {
        let content = MessageContent::text_and_image("What's this?", "https://example.com/img.jpg");
        assert!(!content.is_text());
        assert!(content.has_images());
        assert_eq!(content.part_count(), 2);
        assert_eq!(content.image_count(), 1);
        assert!(content.extract_text().contains("What's this?"));
    }

    #[test]
    fn test_message_content_base64() {
        let content = MessageContent::image_base64("SGVsbG8=", "image/png");
        assert!(content.has_images());
        assert_eq!(content.image_count(), 1);
    }

    #[test]
    fn test_message_content_serialization() {
        let content = MessageContent::text("Hello");
        let json = serde_json::to_string(&content).expect("serialize");
        assert_eq!(json, "\"Hello\"");

        let parsed: MessageContent = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(content, parsed);
    }

    #[test]
    fn test_message_content_parts_serialization() {
        let content = MessageContent::Parts(vec![
            ContentPart::text("Hello"),
            ContentPart::image_url("https://example.com/img.png"),
        ]);

        let json = serde_json::to_string(&content).expect("serialize");
        assert!(json.contains("\"type\":\"text\""));
        assert!(json.contains("\"type\":\"image_url\""));
    }

    #[test]
    fn test_content_part_text() {
        let part = ContentPart::text("Hello");
        assert!(part.is_text());
        assert!(!part.is_image());
    }

    #[test]
    fn test_content_part_image_url() {
        let part = ContentPart::image_url("https://example.com/img.png");
        assert!(!part.is_text());
        assert!(part.is_image());
    }

    #[test]
    fn test_content_part_image_base64() {
        let part = ContentPart::image_base64("SGVsbG8=", "image/png");
        assert!(part.is_image());
    }

    #[test]
    fn test_image_url_new() {
        let url = ImageUrl::new("https://example.com/img.png");
        assert_eq!(url.url, "https://example.com/img.png");
        assert!(url.detail.is_none());
        assert_eq!(url.effective_detail(), ImageDetail::Auto);
    }

    #[test]
    fn test_image_url_with_detail() {
        let url = ImageUrl::new("https://example.com/img.png").with_detail(ImageDetail::High);
        assert_eq!(url.detail, Some(ImageDetail::High));
        assert_eq!(url.effective_detail(), ImageDetail::High);
    }

    #[test]
    fn test_image_url_validation() {
        let config = VisionConfig::default();

        let valid = ImageUrl::new("https://example.com/img.png");
        assert!(valid.validate(&config).is_ok());

        let empty = ImageUrl::new("");
        assert!(empty.validate(&config).is_err());

        let invalid = ImageUrl::new("ftp://example.com/img.png");
        assert!(invalid.validate(&config).is_err());
    }

    #[test]
    fn test_image_url_data_url() {
        let config = VisionConfig::default();

        let valid = ImageUrl::new("data:image/png;base64,SGVsbG8=");
        assert!(valid.validate(&config).is_ok());

        let no_base64 = ImageUrl::new("data:image/png,SGVsbG8=");
        assert!(no_base64.validate(&config).is_err());
    }

    #[test]
    fn test_image_base64_new() {
        let img = ImageBase64::new("SGVsbG8=", "image/png");
        assert_eq!(img.data, "SGVsbG8=");
        assert_eq!(img.media_type, "image/png");
    }

    #[test]
    fn test_image_base64_validation() {
        let config = VisionConfig::default();

        let valid = ImageBase64::new("SGVsbG8=", "image/png");
        assert!(valid.validate(&config).is_ok());

        let empty = ImageBase64::new("", "image/png");
        assert!(empty.validate(&config).is_err());

        let bad_media = ImageBase64::new("SGVsbG8=", "image/bmp");
        assert!(bad_media.validate(&config).is_err());
    }

    #[test]
    fn test_image_base64_decode() {
        let img = ImageBase64::new("SGVsbG8=", "image/png");
        let decoded = img.decode().expect("decode");
        assert_eq!(decoded, b"Hello");
    }

    #[test]
    fn test_image_base64_to_data_url() {
        let img = ImageBase64::new("SGVsbG8=", "image/png");
        assert_eq!(img.to_data_url(), "data:image/png;base64,SGVsbG8=");
    }

    #[test]
    fn test_image_detail_defaults() {
        assert_eq!(ImageDetail::default(), ImageDetail::Auto);
    }

    #[test]
    fn test_image_detail_dimensions() {
        assert_eq!(ImageDetail::Low.max_dimension(), 512);
        assert_eq!(ImageDetail::High.max_dimension(), 2048);
        assert_eq!(ImageDetail::Auto.max_dimension(), 2048);
    }

    #[test]
    fn test_image_detail_display() {
        assert_eq!(ImageDetail::Low.to_string(), "low");
        assert_eq!(ImageDetail::High.to_string(), "high");
        assert_eq!(ImageDetail::Auto.to_string(), "auto");
    }

    #[test]
    fn test_supported_media_types() {
        assert!(is_supported_media_type("image/png"));
        assert!(is_supported_media_type("image/jpeg"));
        assert!(is_supported_media_type("image/JPEG")); // Case insensitive
        assert!(is_supported_media_type("image/gif"));
        assert!(is_supported_media_type("image/webp"));

        assert!(!is_supported_media_type("image/bmp"));
        assert!(!is_supported_media_type("image/tiff"));
        assert!(!is_supported_media_type("text/plain"));
    }

    #[test]
    fn test_vision_config_default() {
        let config = VisionConfig::default();

        assert_eq!(config.max_text_length, 100_000);
        assert_eq!(config.max_image_size, 20 * 1024 * 1024);
        assert_eq!(config.max_images_per_message, 10);
        assert!(config.allow_external_urls);
    }

    #[test]
    fn test_vision_config_builder() {
        let config = VisionConfig::new()
            .with_max_image_size(1024)
            .with_max_images_per_message(5)
            .with_external_urls_disabled();

        assert_eq!(config.max_image_size, 1024);
        assert_eq!(config.max_images_per_message, 5);
        assert!(!config.allow_external_urls);
    }

    #[test]
    fn test_vision_error_display() {
        let err = VisionError::TextTooLong {
            actual: 1000,
            max: 500,
        };
        assert!(err.to_string().contains("1000"));
        assert!(err.to_string().contains("500"));

        let err = VisionError::UnsupportedMediaType("image/bmp".to_string());
        assert!(err.to_string().contains("image/bmp"));
    }

    #[test]
    fn test_message_content_validation() {
        let config = VisionConfig::default();

        let valid = MessageContent::text("Hello");
        assert!(valid.validate(&config).is_ok());

        let empty = MessageContent::Parts(vec![]);
        assert!(empty.validate(&config).is_err());
    }

    #[test]
    fn test_message_content_validation_too_many_images() {
        let config = VisionConfig::new().with_max_images_per_message(2);

        let parts = vec![
            ContentPart::image_url("https://example.com/1.png"),
            ContentPart::image_url("https://example.com/2.png"),
            ContentPart::image_url("https://example.com/3.png"),
        ];

        let content = MessageContent::Parts(parts);
        let result = content.validate(&config);

        assert!(result.is_err());
        match result {
            Err(VisionError::TooManyImages { actual, max }) => {
                assert_eq!(actual, 3);
                assert_eq!(max, 2);
            },
            _ => panic!("Expected TooManyImages error"),
        }
    }

    #[test]
    fn test_is_valid_base64() {
        assert!(is_valid_base64("SGVsbG8="));
        assert!(is_valid_base64("SGVs bG8=")); // With whitespace
        assert!(!is_valid_base64("SGVs!bG8="));
    }

    #[test]
    fn test_estimate_base64_size() {
        // "Hello" encoded is "SGVsbG8=" (8 chars for 5 bytes)
        assert_eq!(estimate_base64_size("SGVsbG8="), 5);

        // Longer string
        let data = "SGVsbG8gV29ybGQh"; // "Hello World!" (12 bytes)
        assert_eq!(estimate_base64_size(data), 12);
    }

    #[test]
    fn test_decode_base64() {
        let result = decode_base64("SGVsbG8=").expect("decode");
        assert_eq!(result, b"Hello");

        let result = decode_base64("SGVsbG8gV29ybGQh").expect("decode");
        assert_eq!(result, b"Hello World!");
    }

    #[test]
    fn test_decode_base64_invalid() {
        let result = decode_base64("SGVs!bG8=");
        assert!(result.is_err());
    }

    #[test]
    fn test_vision_metrics_new() {
        let metrics = VisionMetrics::new();

        assert_eq!(metrics.images_processed(), 0);
        assert_eq!(metrics.images_rejected(), 0);
        assert_eq!(metrics.bytes_processed(), 0);
    }

    #[test]
    fn test_vision_metrics_record() {
        let metrics = VisionMetrics::new();

        metrics.record_image_processed(1024);
        metrics.record_image_processed(2048);
        metrics.record_image_rejected();

        assert_eq!(metrics.images_processed(), 2);
        assert_eq!(metrics.images_rejected(), 1);
        assert_eq!(metrics.bytes_processed(), 3072);
    }

    #[test]
    fn test_vision_metrics_prometheus() {
        let metrics = VisionMetrics::new();
        metrics.record_image_processed(1024);
        metrics.record_image_rejected();

        let output = metrics.prometheus();

        assert!(output.contains("infernum_vision_images_processed_total 1"));
        assert!(output.contains("infernum_vision_images_rejected_total 1"));
        assert!(output.contains("infernum_vision_bytes_processed_total 1024"));
    }

    #[test]
    fn test_content_part_serialization() {
        let part = ContentPart::Text {
            text: "Hello".to_string(),
        };
        let json = serde_json::to_string(&part).expect("serialize");
        assert!(json.contains("\"type\":\"text\""));
        assert!(json.contains("\"text\":\"Hello\""));
    }

    #[test]
    fn test_content_part_image_url_serialization() {
        let part = ContentPart::ImageUrl {
            image_url: ImageUrl::new("https://example.com/img.png"),
        };
        let json = serde_json::to_string(&part).expect("serialize");
        assert!(json.contains("\"type\":\"image_url\""));
        assert!(json.contains("\"url\":\"https://example.com/img.png\""));
    }

    #[test]
    fn test_content_part_deserialization() {
        let json = r#"{"type":"text","text":"Hello world"}"#;
        let part: ContentPart = serde_json::from_str(json).expect("deserialize");

        match part {
            ContentPart::Text { text } => assert_eq!(text, "Hello world"),
            _ => panic!("Expected Text part"),
        }
    }

    #[test]
    fn test_image_url_deserialization() {
        let json = r#"{"type":"image_url","image_url":{"url":"https://example.com/img.png","detail":"high"}}"#;
        let part: ContentPart = serde_json::from_str(json).expect("deserialize");

        match part {
            ContentPart::ImageUrl { image_url } => {
                assert_eq!(image_url.url, "https://example.com/img.png");
                assert_eq!(image_url.detail, Some(ImageDetail::High));
            },
            _ => panic!("Expected ImageUrl part"),
        }
    }

    #[test]
    fn test_image_detail_serialization() {
        let detail = ImageDetail::High;
        let json = serde_json::to_string(&detail).expect("serialize");
        assert_eq!(json, "\"high\"");

        let parsed: ImageDetail = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(parsed, ImageDetail::High);
    }
}