objectiveai 0.1.4

ObjectiveAI SDK, definitions, and utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
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
//! Input types for Function expressions.
//!
//! Defines the data structures that can be passed as input to Functions,
//! along with schema types for validation.

use crate::chat;
use indexmap::IndexMap;
use serde::de::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Expressions that produce the 2D array used for mapped tasks.
///
/// Can be a single expression (producing one sub-array) or multiple
/// expressions (each producing a sub-array).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InputMaps {
    /// A single expression producing one sub-array.
    One(super::Expression),
    /// Multiple expressions, each producing a sub-array.
    Many(Vec<super::Expression>),
}

impl InputMaps {
    /// Compiles the input maps expressions into concrete 2D arrays.
    pub fn compile(
        self,
        params: &super::Params,
    ) -> Result<Vec<Vec<Input>>, super::ExpressionError> {
        match self {
            InputMaps::One(expression) => {
                match expression.compile_one_or_many::<Vec<Input>>(params)? {
                    super::OneOrMany::One(one) => Ok(vec![one]),
                    super::OneOrMany::Many(many) => Ok(many),
                }
            }
            InputMaps::Many(expressions) => {
                let mut compiled = Vec::with_capacity(expressions.len());
                for expression in expressions {
                    match expression
                        .compile_one_or_many::<Vec<Input>>(params)?
                    {
                        super::OneOrMany::One(one) => compiled.push(one),
                        super::OneOrMany::Many(many) => {
                            for item in many {
                                compiled.push(item);
                            }
                        }
                    }
                }
                Ok(compiled)
            }
        }
    }
}

/// A concrete input value (post-compilation).
///
/// Represents any JSON-like value that can be passed to a Function,
/// including rich content types (images, audio, video, files).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Input {
    /// Rich content (image, audio, video, file).
    RichContentPart(chat::completions::request::RichContentPart),
    /// An object with string keys.
    Object(IndexMap<String, Input>),
    /// An array of values.
    Array(Vec<Input>),
    /// A string value.
    String(String),
    /// An integer value.
    Integer(i64),
    /// A floating-point number.
    Number(f64),
    /// A boolean value.
    Boolean(bool),
}

impl Input {
    /// Converts the input to a sequence of rich content parts.
    ///
    /// This is used to render structured input data as formatted JSON
    /// in multimodal messages.
    pub fn to_rich_content_parts(
        self,
        depth: usize,
    ) -> impl Iterator<Item = chat::completions::request::RichContentPart> {
        enum Iter {
            RichContentPart(RichContentPartIter),
            Object(Box<ObjectIter>),
            Array(Box<ArrayIter>),
            Primitive(Option<String>),
        }
        impl Iter {
            pub fn new(input: Input, depth: usize) -> Self {
                match input {
                    Input::RichContentPart(rich_content_part) => {
                        Iter::RichContentPart(RichContentPartIter {
                            first: true,
                            part: Some(rich_content_part),
                            last: true,
                        })
                    }
                    Input::Object(object) => {
                        Iter::Object(Box::new(ObjectIter {
                            object: object.into_iter(),
                            first: true,
                            child: None,
                            depth,
                        }))
                    }
                    Input::Array(array) => Iter::Array(Box::new(ArrayIter {
                        array: array.into_iter(),
                        first: true,
                        child: None,
                        depth,
                    })),
                    Input::String(string) => Iter::Primitive(Some(format!(
                        "\"{}\"",
                        json_escape::escape_str(&string),
                    ))),
                    Input::Integer(integer) => {
                        Iter::Primitive(Some(integer.to_string()))
                    }
                    Input::Number(number) => {
                        Iter::Primitive(Some(number.to_string()))
                    }
                    Input::Boolean(boolean) => {
                        Iter::Primitive(Some(boolean.to_string()))
                    }
                }
            }
        }
        impl Iterator for Iter {
            type Item = chat::completions::request::RichContentPart;
            fn next(&mut self) -> Option<Self::Item> {
                match self {
                    Iter::RichContentPart(rich_content_part_iter) => {
                        rich_content_part_iter.next()
                    }
                    Iter::Object(object_iter) => object_iter.next(),
                    Iter::Array(array_iter) => array_iter.next(),
                    Iter::Primitive(primitive_option) => {
                        primitive_option.take().map(|text| {
                            chat::completions::request::RichContentPart::Text {
                                text,
                            }
                        })
                    }
                }
            }
        }
        struct RichContentPartIter {
            first: bool,
            part: Option<chat::completions::request::RichContentPart>,
            last: bool,
        }
        impl Iterator for RichContentPartIter {
            type Item = chat::completions::request::RichContentPart;
            fn next(&mut self) -> Option<Self::Item> {
                if self.first {
                    self.first = false;
                    Some(chat::completions::request::RichContentPart::Text {
                        text: '"'.to_string(),
                    })
                } else if let Some(part) = self.part.take() {
                    Some(part)
                } else if self.last {
                    self.last = false;
                    Some(chat::completions::request::RichContentPart::Text {
                        text: '"'.to_string(),
                    })
                } else {
                    None
                }
            }
        }
        struct ObjectIter {
            object: indexmap::map::IntoIter<String, Input>,
            first: bool,
            child: Option<Iter>,
            depth: usize,
        }
        impl Iterator for ObjectIter {
            type Item = chat::completions::request::RichContentPart;
            fn next(&mut self) -> Option<Self::Item> {
                if self.first {
                    self.first = false;
                    if let Some((key, input)) = self.object.next() {
                        self.child = Some(Iter::new(input, self.depth + 1));
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!(
                                    "{{\n{}\"{}\": ",
                                    "    ".repeat(self.depth + 1),
                                    key,
                                ),
                            },
                        )
                    } else {
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!("{{}}"),
                            },
                        )
                    }
                } else if let Some(child) = &mut self.child {
                    if let Some(part) = child.next() {
                        Some(part)
                    } else if let Some((key, input)) = self.object.next() {
                        self.child = Some(Iter::new(input, self.depth + 1));
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!(
                                    ",\n{}\"{}\": ",
                                    "    ".repeat(self.depth + 1),
                                    key,
                                ),
                            },
                        )
                    } else {
                        self.child = None;
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!(
                                    "\n{}}}",
                                    "    ".repeat(self.depth)
                                ),
                            },
                        )
                    }
                } else {
                    None
                }
            }
        }
        struct ArrayIter {
            array: std::vec::IntoIter<Input>,
            first: bool,
            child: Option<Iter>,
            depth: usize,
        }
        impl Iterator for ArrayIter {
            type Item = chat::completions::request::RichContentPart;
            fn next(&mut self) -> Option<Self::Item> {
                if self.first {
                    self.first = false;
                    if let Some(input) = self.array.next() {
                        self.child = Some(Iter::new(input, self.depth + 1));
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!(
                                    "[\n{}",
                                    "    ".repeat(self.depth + 1)
                                ),
                            },
                        )
                    } else {
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!("[]"),
                            },
                        )
                    }
                } else if let Some(child) = &mut self.child {
                    if let Some(part) = child.next() {
                        Some(part)
                    } else if let Some(input) = self.array.next() {
                        self.child = Some(Iter::new(input, self.depth + 1));
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!(
                                    ",\n{}",
                                    "    ".repeat(self.depth + 1),
                                ),
                            },
                        )
                    } else {
                        self.child = None;
                        Some(
                            chat::completions::request::RichContentPart::Text {
                                text: format!(
                                    "\n{}]",
                                    "    ".repeat(self.depth)
                                ),
                            },
                        )
                    }
                } else {
                    None
                }
            }
        }
        Iter::new(self, depth)
    }
}

/// An input value that may contain JMESPath expressions (pre-compilation).
///
/// Similar to [`Input`] but object values and array elements can be
/// expressions that are evaluated during compilation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InputExpression {
    /// Rich content (image, audio, video, file).
    RichContentPart(chat::completions::request::RichContentPart),
    /// An object with values that may be expressions.
    Object(IndexMap<String, super::WithExpression<InputExpression>>),
    /// An array with elements that may be expressions.
    Array(Vec<super::WithExpression<InputExpression>>),
    /// A string value.
    String(String),
    /// An integer value.
    Integer(i64),
    /// A floating-point number.
    Number(f64),
    /// A boolean value.
    Boolean(bool),
}

impl InputExpression {
    /// Compiles the expression into a concrete [`Input`].
    pub fn compile(
        self,
        params: &super::Params,
    ) -> Result<Input, super::ExpressionError> {
        match self {
            InputExpression::RichContentPart(rich_content_part) => {
                Ok(Input::RichContentPart(rich_content_part))
            }
            InputExpression::Object(object) => {
                let mut compiled_object = IndexMap::with_capacity(object.len());
                for (key, value) in object {
                    compiled_object.insert(
                        key,
                        value.compile_one(params)?.compile(params)?,
                    );
                }
                Ok(Input::Object(compiled_object))
            }
            InputExpression::Array(array) => {
                let mut compiled_array = Vec::with_capacity(array.len());
                for item in array {
                    match item.compile_one_or_many(params)? {
                        super::OneOrMany::One(one_item) => {
                            compiled_array.push(one_item.compile(params)?);
                        }
                        super::OneOrMany::Many(many_items) => {
                            for item in many_items {
                                compiled_array.push(item.compile(params)?);
                            }
                        }
                    }
                }
                Ok(Input::Array(compiled_array))
            }
            InputExpression::String(string) => Ok(Input::String(string)),
            InputExpression::Integer(integer) => Ok(Input::Integer(integer)),
            InputExpression::Number(number) => Ok(Input::Number(number)),
            InputExpression::Boolean(boolean) => Ok(Input::Boolean(boolean)),
        }
    }
}

/// Schema for validating Function input.
///
/// Defines the expected structure and constraints for input data.
/// Used by remote Functions to document and validate their inputs.
#[derive(Debug, Clone)]
pub enum InputSchema {
    /// An object with named properties.
    Object(ObjectInputSchema),
    /// An array of items.
    Array(ArrayInputSchema),
    /// A string value.
    String(StringInputSchema),
    /// An integer value.
    Integer(IntegerInputSchema),
    /// A floating-point number.
    Number(NumberInputSchema),
    /// A boolean value.
    Boolean(BooleanInputSchema),
    /// An image (URL or base64).
    Image(ImageInputSchema),
    /// Audio content.
    Audio(AudioInputSchema),
    /// Video content.
    Video(VideoInputSchema),
    /// A file.
    File(FileInputSchema),
    /// A union of schemas - input must match at least one.
    AnyOf(AnyOfInputSchema),
}

impl InputSchema {
    /// Validates that an input value conforms to this schema.
    pub fn validate_input(&self, input: &Input) -> bool {
        match self {
            InputSchema::Object(schema) => schema.validate_input(input),
            InputSchema::Array(schema) => schema.validate_input(input),
            InputSchema::String(schema) => schema.validate_input(input),
            InputSchema::Integer(schema) => schema.validate_input(input),
            InputSchema::Number(schema) => schema.validate_input(input),
            InputSchema::Boolean(schema) => schema.validate_input(input),
            InputSchema::Image(schema) => schema.validate_input(input),
            InputSchema::Audio(schema) => schema.validate_input(input),
            InputSchema::Video(schema) => schema.validate_input(input),
            InputSchema::File(schema) => schema.validate_input(input),
            InputSchema::AnyOf(schema) => schema.validate_input(input),
        }
    }
}

/// Helper enum for deserializing typed schemas (those with a `type` field).
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
enum TypedInputSchema {
    Object(ObjectInputSchema),
    Array(ArrayInputSchema),
    String(StringInputSchema),
    Integer(IntegerInputSchema),
    Number(NumberInputSchema),
    Boolean(BooleanInputSchema),
    Image(ImageInputSchema),
    Audio(AudioInputSchema),
    Video(VideoInputSchema),
    File(FileInputSchema),
}

impl From<TypedInputSchema> for InputSchema {
    fn from(typed: TypedInputSchema) -> Self {
        match typed {
            TypedInputSchema::Object(s) => InputSchema::Object(s),
            TypedInputSchema::Array(s) => InputSchema::Array(s),
            TypedInputSchema::String(s) => InputSchema::String(s),
            TypedInputSchema::Integer(s) => InputSchema::Integer(s),
            TypedInputSchema::Number(s) => InputSchema::Number(s),
            TypedInputSchema::Boolean(s) => InputSchema::Boolean(s),
            TypedInputSchema::Image(s) => InputSchema::Image(s),
            TypedInputSchema::Audio(s) => InputSchema::Audio(s),
            TypedInputSchema::Video(s) => InputSchema::Video(s),
            TypedInputSchema::File(s) => InputSchema::File(s),
        }
    }
}

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

        // Check if this is an AnyOf schema (has anyOf field, no type field)
        if value.get("anyOf").is_some() {
            let schema: AnyOfInputSchema =
                serde_json::from_value(value).map_err(D::Error::custom)?;
            Ok(InputSchema::AnyOf(schema))
        } else {
            // Deserialize as a typed schema
            let typed: TypedInputSchema =
                serde_json::from_value(value).map_err(D::Error::custom)?;
            Ok(typed.into())
        }
    }
}

impl Serialize for InputSchema {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            InputSchema::AnyOf(schema) => schema.serialize(serializer),
            InputSchema::Object(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a ObjectInputSchema,
                }
                Tagged {
                    r#type: "object",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Array(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a ArrayInputSchema,
                }
                Tagged {
                    r#type: "array",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::String(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a StringInputSchema,
                }
                Tagged {
                    r#type: "string",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Integer(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a IntegerInputSchema,
                }
                Tagged {
                    r#type: "integer",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Number(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a NumberInputSchema,
                }
                Tagged {
                    r#type: "number",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Boolean(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a BooleanInputSchema,
                }
                Tagged {
                    r#type: "boolean",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Image(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a ImageInputSchema,
                }
                Tagged {
                    r#type: "image",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Audio(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a AudioInputSchema,
                }
                Tagged {
                    r#type: "audio",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::Video(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a VideoInputSchema,
                }
                Tagged {
                    r#type: "video",
                    schema,
                }
                .serialize(serializer)
            }
            InputSchema::File(schema) => {
                #[derive(Serialize)]
                #[serde(rename_all = "camelCase")]
                struct Tagged<'a> {
                    r#type: &'static str,
                    #[serde(flatten)]
                    schema: &'a FileInputSchema,
                }
                Tagged {
                    r#type: "file",
                    schema,
                }
                .serialize(serializer)
            }
        }
    }
}

/// Schema for a union of possible types - input must match at least one.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AnyOfInputSchema {
    /// The possible schemas that the input can match.
    pub any_of: Vec<InputSchema>,
}

impl AnyOfInputSchema {
    /// Validates that an input matches at least one schema in the union.
    pub fn validate_input(&self, input: &Input) -> bool {
        self.any_of
            .iter()
            .any(|schema| schema.validate_input(input))
    }
}

/// Schema for an object input with named properties.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ObjectInputSchema {
    /// Human-readable description of the object.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Schema for each property in the object.
    pub properties: IndexMap<String, InputSchema>,
    /// List of property names that must be present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,
}

impl ObjectInputSchema {
    /// Validates that an input is an object matching this schema.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::Object(map) => {
                self.properties.iter().all(|(key, schema)| {
                    map.get(key)
                        .map(|value| schema.validate_input(value))
                        .unwrap_or(false)
                }) && {
                    if let Some(required) = &self.required {
                        required.iter().all(|key| map.contains_key(key))
                    } else {
                        true
                    }
                }
            }
            _ => false,
        }
    }
}

/// Schema for an array input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArrayInputSchema {
    /// Human-readable description of the array.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Minimum number of items required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u64>,
    /// Maximum number of items allowed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u64>,
    /// Schema for each item in the array.
    pub items: Box<InputSchema>,
}

impl ArrayInputSchema {
    /// Validates that an input is an array matching this schema.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::Array(array) => {
                if let Some(min_items) = self.min_items
                    && (array.len() as u64) < min_items
                {
                    false
                } else if let Some(max_items) = self.max_items
                    && (array.len() as u64) > max_items
                {
                    false
                } else {
                    array.iter().all(|item| self.items.validate_input(item))
                }
            }
            _ => false,
        }
    }
}

/// Schema for a string input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StringInputSchema {
    /// Human-readable description of the string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// If provided, the string must be one of these values.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#enum: Option<Vec<String>>,
}

impl StringInputSchema {
    /// Validates that an input is a string matching this schema.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::String(s) => {
                if let Some(r#enum) = &self.r#enum {
                    r#enum.contains(s)
                } else {
                    true
                }
            }
            _ => false,
        }
    }
}

/// Schema for an integer input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntegerInputSchema {
    /// Human-readable description of the integer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Minimum allowed value (inclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum: Option<i64>,
    /// Maximum allowed value (inclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum: Option<i64>,
}

impl IntegerInputSchema {
    /// Validates that an input is an integer matching this schema.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::Integer(integer) => {
                if let Some(minimum) = self.minimum
                    && *integer < minimum
                {
                    false
                } else if let Some(maximum) = self.maximum
                    && *integer > maximum
                {
                    false
                } else {
                    true
                }
            }
            Input::Number(number)
                if number.is_finite() && number.fract() == 0.0 =>
            {
                let integer = *number as i64;
                if let Some(minimum) = self.minimum
                    && integer < minimum
                {
                    false
                } else if let Some(maximum) = self.maximum
                    && integer > maximum
                {
                    false
                } else {
                    true
                }
            }
            _ => false,
        }
    }
}

/// Schema for a floating-point number input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NumberInputSchema {
    /// Human-readable description of the number.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Minimum allowed value (inclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum: Option<f64>,
    /// Maximum allowed value (inclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum: Option<f64>,
}

impl NumberInputSchema {
    /// Validates that an input is a number matching this schema.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::Integer(integer) => {
                let number = *integer as f64;
                if let Some(minimum) = self.minimum
                    && number < minimum
                {
                    false
                } else if let Some(maximum) = self.maximum
                    && number > maximum
                {
                    false
                } else {
                    true
                }
            }
            Input::Number(number) => {
                if let Some(minimum) = self.minimum
                    && *number < minimum
                {
                    false
                } else if let Some(maximum) = self.maximum
                    && *number > maximum
                {
                    false
                } else {
                    true
                }
            }
            _ => false,
        }
    }
}

/// Schema for a boolean input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BooleanInputSchema {
    /// Human-readable description of the boolean.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl BooleanInputSchema {
    /// Validates that an input is a boolean.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::Boolean(_) => true,
            _ => false,
        }
    }
}

/// Schema for an image input (URL or base64-encoded).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageInputSchema {
    /// Human-readable description of the expected image.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl ImageInputSchema {
    /// Validates that an input is an image.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::RichContentPart(
                chat::completions::request::RichContentPart::ImageUrl {
                    ..
                },
            ) => true,
            _ => false,
        }
    }
}

/// Schema for an audio input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AudioInputSchema {
    /// Human-readable description of the expected audio.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl AudioInputSchema {
    /// Validates that an input is audio content.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::RichContentPart(
                chat::completions::request::RichContentPart::InputAudio {
                    ..
                },
            ) => true,
            _ => false,
        }
    }
}

/// Schema for a video input (URL or base64-encoded).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VideoInputSchema {
    /// Human-readable description of the expected video.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl VideoInputSchema {
    /// Validates that an input is video content.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::RichContentPart(
                chat::completions::request::RichContentPart::InputVideo {
                    ..
                },
            ) => true,
            Input::RichContentPart(
                chat::completions::request::RichContentPart::VideoUrl {
                    ..
                },
            ) => true,
            _ => false,
        }
    }
}

/// Schema for a file input.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FileInputSchema {
    /// Human-readable description of the expected file.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

impl FileInputSchema {
    /// Validates that an input is a file.
    pub fn validate_input(&self, input: &Input) -> bool {
        match input {
            Input::RichContentPart(
                chat::completions::request::RichContentPart::File { .. },
            ) => true,
            _ => false,
        }
    }
}