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
//! Provider-Specific Features
//!
//! This module provides a unified interface for managing provider-specific
//! features and capabilities across different AI providers.

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

use crate::error::LlmError;

/// Provider-specific feature configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderFeatures {
    /// Provider name
    pub provider: String,
    /// Feature configurations
    pub features: HashMap<String, FeatureConfig>,
}

impl ProviderFeatures {
    /// Create a new provider features configuration
    pub fn new<S: Into<String>>(provider: S) -> Self {
        Self {
            provider: provider.into(),
            features: HashMap::new(),
        }
    }

    /// Add a feature configuration
    pub fn with_feature<S: Into<String>>(mut self, name: S, config: FeatureConfig) -> Self {
        self.features.insert(name.into(), config);
        self
    }

    /// Enable a simple feature
    pub fn enable_feature<S: Into<String>>(mut self, name: S) -> Self {
        self.features
            .insert(name.into(), FeatureConfig::Boolean(true));
        self
    }

    /// Disable a feature
    pub fn disable_feature<S: Into<String>>(mut self, name: S) -> Self {
        self.features
            .insert(name.into(), FeatureConfig::Boolean(false));
        self
    }

    /// Get a feature configuration
    pub fn get_feature(&self, name: &str) -> Option<&FeatureConfig> {
        self.features.get(name)
    }

    /// Check if a feature is enabled
    pub fn is_feature_enabled(&self, name: &str) -> bool {
        match self.get_feature(name) {
            Some(FeatureConfig::Boolean(enabled)) => *enabled,
            Some(FeatureConfig::Object(obj)) => obj
                .get("enabled")
                .and_then(serde_json::Value::as_bool)
                .unwrap_or(false),
            _ => false,
        }
    }

    /// Convert to request parameters
    pub fn to_request_params(&self) -> HashMap<String, serde_json::Value> {
        let mut params = HashMap::new();

        for (name, config) in &self.features {
            match config {
                FeatureConfig::Boolean(enabled) => {
                    if *enabled {
                        params.insert(name.clone(), serde_json::Value::Bool(true));
                    }
                }
                FeatureConfig::String(value) => {
                    params.insert(name.clone(), serde_json::Value::String(value.clone()));
                }
                FeatureConfig::Number(value) => {
                    if let Some(num) = serde_json::Number::from_f64(*value) {
                        params.insert(name.clone(), serde_json::Value::Number(num));
                    }
                }
                FeatureConfig::Object(obj) => {
                    params.insert(name.clone(), obj.clone());
                }
            }
        }

        params
    }
}

/// Feature configuration types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FeatureConfig {
    /// Boolean feature (enabled/disabled)
    Boolean(bool),
    /// String configuration
    String(String),
    /// Numeric configuration
    Number(f64),
    /// Complex object configuration
    Object(serde_json::Value),
}

impl FeatureConfig {
    /// Create a boolean feature config
    pub const fn boolean(enabled: bool) -> Self {
        Self::Boolean(enabled)
    }

    /// Create a string feature config
    pub fn string<S: Into<String>>(value: S) -> Self {
        Self::String(value.into())
    }

    /// Create a numeric feature config
    pub const fn number(value: f64) -> Self {
        Self::Number(value)
    }

    /// Create an object feature config
    pub const fn object(value: serde_json::Value) -> Self {
        Self::Object(value)
    }
}

/// Provider-specific feature registry
pub struct ProviderFeatureRegistry {
    /// Registered features by provider
    features: HashMap<String, HashMap<String, FeatureDefinition>>,
}

impl ProviderFeatureRegistry {
    /// Create a new feature registry
    pub fn new() -> Self {
        let mut registry = Self {
            features: HashMap::new(),
        };
        registry.register_default_features();
        registry
    }

    /// Register a feature for a provider
    pub fn register_feature<S: Into<String>>(
        &mut self,
        provider: S,
        name: S,
        definition: FeatureDefinition,
    ) {
        let provider_key = provider.into();
        let feature_name = name.into();

        self.features
            .entry(provider_key)
            .or_default()
            .insert(feature_name, definition);
    }

    /// Get feature definition
    pub fn get_feature_definition(&self, provider: &str, name: &str) -> Option<&FeatureDefinition> {
        self.features
            .get(provider)
            .and_then(|provider_features| provider_features.get(name))
    }

    /// Get all features for a provider
    pub fn get_provider_features(
        &self,
        provider: &str,
    ) -> Option<&HashMap<String, FeatureDefinition>> {
        self.features.get(provider)
    }

    /// Validate feature configuration
    pub fn validate_feature_config(
        &self,
        provider: &str,
        name: &str,
        config: &FeatureConfig,
    ) -> Result<(), LlmError> {
        if let Some(definition) = self.get_feature_definition(provider, name) {
            definition.validate(config)
        } else {
            Err(LlmError::InvalidParameter(format!(
                "Unknown feature '{name}' for provider '{provider}'"
            )))
        }
    }

    /// Register default features for all providers
    fn register_default_features(&mut self) {
        // OpenAI features
        self.register_feature(
            "openai",
            "structured_output",
            FeatureDefinition::new("Structured Output")
                .with_description("Enable structured JSON output with schema validation")
                .with_config_type(FeatureConfigType::Object),
        );

        self.register_feature(
            "openai",
            "web_search",
            FeatureDefinition::new("Web Search")
                .with_description("Enable built-in web search via Responses API")
                .with_config_type(FeatureConfigType::Boolean),
        );

        self.register_feature(
            "openai",
            "file_search",
            FeatureDefinition::new("File Search")
                .with_description("Enable file search with vector stores")
                .with_config_type(FeatureConfigType::Object),
        );

        // Anthropic features
        self.register_feature(
            "anthropic",
            "prompt_caching",
            FeatureDefinition::new("Prompt Caching")
                .with_description("Enable prompt caching for cost reduction")
                .with_config_type(FeatureConfigType::Object),
        );

        self.register_feature(
            "anthropic",
            "thinking_mode",
            FeatureDefinition::new("Thinking Mode")
                .with_description("Enable access to Claude's reasoning process")
                .with_config_type(FeatureConfigType::Object),
        );

        // Gemini features
        self.register_feature(
            "gemini",
            "code_execution",
            FeatureDefinition::new("Code Execution")
                .with_description("Enable Python code execution")
                .with_config_type(FeatureConfigType::Object),
        );

        self.register_feature(
            "gemini",
            "search_grounding",
            FeatureDefinition::new("Search Grounding")
                .with_description("Enable search-augmented generation")
                .with_config_type(FeatureConfigType::Boolean),
        );
    }
}

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

/// Feature definition
#[derive(Debug, Clone)]
pub struct FeatureDefinition {
    /// Feature name
    pub name: String,
    /// Feature description
    pub description: String,
    /// Expected configuration type
    pub config_type: FeatureConfigType,
    /// Whether the feature is experimental
    pub experimental: bool,
    /// Required API version
    pub min_api_version: Option<String>,
}

impl FeatureDefinition {
    /// Create a new feature definition
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            config_type: FeatureConfigType::Boolean,
            experimental: false,
            min_api_version: None,
        }
    }

    /// Set description
    pub fn with_description<S: Into<String>>(mut self, description: S) -> Self {
        self.description = description.into();
        self
    }

    /// Set configuration type
    pub const fn with_config_type(mut self, config_type: FeatureConfigType) -> Self {
        self.config_type = config_type;
        self
    }

    /// Mark as experimental
    pub const fn experimental(mut self) -> Self {
        self.experimental = true;
        self
    }

    /// Set minimum API version
    pub fn with_min_api_version<S: Into<String>>(mut self, version: S) -> Self {
        self.min_api_version = Some(version.into());
        self
    }

    /// Validate feature configuration
    pub fn validate(&self, config: &FeatureConfig) -> Result<(), LlmError> {
        match (&self.config_type, config) {
            (FeatureConfigType::Boolean, FeatureConfig::Boolean(_)) => Ok(()),
            (FeatureConfigType::String, FeatureConfig::String(_)) => Ok(()),
            (FeatureConfigType::Number, FeatureConfig::Number(_)) => Ok(()),
            (FeatureConfigType::Object, FeatureConfig::Object(_)) => Ok(()),
            _ => Err(LlmError::InvalidParameter(format!(
                "Invalid configuration type for feature '{}'. Expected {:?}, got {:?}",
                self.name, self.config_type, config
            ))),
        }
    }
}

/// Feature configuration type
#[derive(Debug, Clone)]
pub enum FeatureConfigType {
    /// Boolean configuration
    Boolean,
    /// String configuration
    String,
    /// Numeric configuration
    Number,
    /// Object configuration
    Object,
}

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

    #[test]
    fn test_provider_features() {
        let features = ProviderFeatures::new("openai")
            .enable_feature("structured_output")
            .with_feature("web_search", FeatureConfig::boolean(true))
            .with_feature("temperature", FeatureConfig::number(0.7));

        assert!(features.is_feature_enabled("structured_output"));
        assert!(features.is_feature_enabled("web_search"));
        assert!(!features.is_feature_enabled("nonexistent"));

        let params = features.to_request_params();
        assert_eq!(
            params.get("structured_output"),
            Some(&serde_json::Value::Bool(true))
        );
        assert_eq!(
            params.get("temperature"),
            Some(&serde_json::Value::Number(
                serde_json::Number::from_f64(0.7).unwrap()
            ))
        );
    }

    #[test]
    fn test_feature_registry() {
        let registry = ProviderFeatureRegistry::new();

        let openai_features = registry.get_provider_features("openai").unwrap();
        assert!(openai_features.contains_key("structured_output"));
        assert!(openai_features.contains_key("web_search"));

        let anthropic_features = registry.get_provider_features("anthropic").unwrap();
        assert!(anthropic_features.contains_key("prompt_caching"));
        assert!(anthropic_features.contains_key("thinking_mode"));
    }

    #[test]
    fn test_feature_validation() {
        let registry = ProviderFeatureRegistry::new();

        // Valid configuration
        let config = FeatureConfig::boolean(true);
        assert!(
            registry
                .validate_feature_config("openai", "web_search", &config)
                .is_ok()
        );

        // Invalid configuration type
        let config = FeatureConfig::string("invalid");
        assert!(
            registry
                .validate_feature_config("openai", "web_search", &config)
                .is_err()
        );

        // Unknown feature
        let config = FeatureConfig::boolean(true);
        assert!(
            registry
                .validate_feature_config("openai", "unknown_feature", &config)
                .is_err()
        );
    }
}