xybrid-core 0.1.0

Core runtime for hybrid cloud-edge AI inference: model execution, pipeline orchestration, and routing primitives.
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
//! Integration provider configuration for third-party APIs.
//!
//! Supported providers:
//! - OpenAI (GPT, Whisper, DALL-E)
//! - Anthropic (Claude)
//! - Google (Gemini)
//! - ElevenLabs (TTS)
//! - OpenRouter (multi-provider routing)

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

/// Supported integration providers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IntegrationProvider {
    /// OpenAI API (GPT, Whisper, DALL-E, embeddings).
    OpenAI,

    /// Anthropic API (Claude models).
    Anthropic,

    /// Google AI (Gemini models).
    Google,

    /// DeepSeek (OpenAI-compatible chat & reasoning models).
    DeepSeek,

    /// ElevenLabs (TTS).
    ElevenLabs,

    /// OpenRouter (multi-provider).
    OpenRouter,

    /// Custom provider (user-defined).
    Custom,
}

impl IntegrationProvider {
    /// Get the environment variable name for the API key.
    pub fn api_key_env_var(&self) -> &'static str {
        match self {
            IntegrationProvider::OpenAI => "OPENAI_API_KEY",
            IntegrationProvider::Anthropic => "ANTHROPIC_API_KEY",
            IntegrationProvider::Google => "GOOGLE_API_KEY",
            IntegrationProvider::DeepSeek => "DEEPSEEK_API_KEY",
            IntegrationProvider::ElevenLabs => "ELEVENLABS_API_KEY",
            IntegrationProvider::OpenRouter => "OPENROUTER_API_KEY",
            IntegrationProvider::Custom => "CUSTOM_API_KEY",
        }
    }

    /// Get the default base URL for the provider.
    pub fn default_base_url(&self) -> &'static str {
        match self {
            IntegrationProvider::OpenAI => "https://api.openai.com/v1",
            IntegrationProvider::Anthropic => "https://api.anthropic.com/v1",
            IntegrationProvider::Google => "https://generativelanguage.googleapis.com/v1beta",
            IntegrationProvider::DeepSeek => "https://api.deepseek.com/v1",
            IntegrationProvider::ElevenLabs => "https://api.elevenlabs.io/v1",
            IntegrationProvider::OpenRouter => "https://openrouter.ai/api/v1",
            IntegrationProvider::Custom => "",
        }
    }

    /// Get the string representation.
    pub fn as_str(&self) -> &'static str {
        match self {
            IntegrationProvider::OpenAI => "openai",
            IntegrationProvider::Anthropic => "anthropic",
            IntegrationProvider::Google => "google",
            IntegrationProvider::DeepSeek => "deepseek",
            IntegrationProvider::ElevenLabs => "elevenlabs",
            IntegrationProvider::OpenRouter => "openrouter",
            IntegrationProvider::Custom => "custom",
        }
    }
}

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

impl std::str::FromStr for IntegrationProvider {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "openai" => Ok(IntegrationProvider::OpenAI),
            "anthropic" | "claude" => Ok(IntegrationProvider::Anthropic),
            "google" | "gemini" => Ok(IntegrationProvider::Google),
            "deepseek" | "deep_seek" => Ok(IntegrationProvider::DeepSeek),
            "elevenlabs" | "eleven" | "eleven_labs" => Ok(IntegrationProvider::ElevenLabs),
            "openrouter" | "open_router" => Ok(IntegrationProvider::OpenRouter),
            "custom" => Ok(IntegrationProvider::Custom),
            _ => Err(format!(
                "Unknown provider: '{}'. Valid values: openai, anthropic, google, deepseek, elevenlabs, openrouter, custom",
                s
            )),
        }
    }
}

/// Provider-specific configuration options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProviderConfig {
    /// The integration provider.
    pub provider: IntegrationProvider,

    /// Override base URL (optional).
    #[serde(default)]
    pub base_url: Option<String>,

    /// API key (usually read from environment).
    /// Can be:
    /// - Direct value (NOT recommended for production)
    /// - Environment variable reference: `$OPENAI_API_KEY`
    /// - Xybrid secrets reference: `xybrid://secrets/openai-key`
    #[serde(default)]
    pub api_key: Option<String>,

    /// Request timeout in milliseconds.
    #[serde(default = "default_timeout_ms")]
    pub timeout_ms: u32,

    /// Additional provider-specific options.
    #[serde(default)]
    pub options: HashMap<String, serde_json::Value>,
}

fn default_timeout_ms() -> u32 {
    30000
}

impl ProviderConfig {
    /// Create a new provider config.
    pub fn new(provider: IntegrationProvider) -> Self {
        Self {
            provider,
            base_url: None,
            api_key: None,
            timeout_ms: default_timeout_ms(),
            options: HashMap::new(),
        }
    }

    /// Get the effective base URL (custom or default).
    pub fn effective_base_url(&self) -> &str {
        self.base_url
            .as_deref()
            .unwrap_or_else(|| self.provider.default_base_url())
    }

    /// Resolve the API key from environment or config.
    pub fn resolve_api_key(&self) -> Option<String> {
        if let Some(key) = &self.api_key {
            // Check if it's an environment variable reference
            if let Some(env_var) = key.strip_prefix('$') {
                return std::env::var(env_var).ok();
            }
            // Check if it's a Xybrid secrets reference
            if key.starts_with("xybrid://secrets/") {
                // TODO: Implement Xybrid secrets resolution
                return None;
            }
            // Direct value (not recommended)
            return Some(key.clone());
        }

        // Fall back to default environment variable
        std::env::var(self.provider.api_key_env_var()).ok()
    }

    /// Set an option value.
    pub fn with_option(mut self, key: &str, value: serde_json::Value) -> Self {
        self.options.insert(key.to_string(), value);
        self
    }
}

/// OpenAI-specific options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpenAIOptions {
    /// Temperature for generation (0.0 - 2.0).
    #[serde(default = "default_temperature")]
    pub temperature: f32,

    /// Maximum tokens to generate.
    #[serde(default)]
    pub max_tokens: Option<u32>,

    /// System prompt for chat models.
    #[serde(default)]
    pub system_prompt: Option<String>,

    /// Top-p sampling.
    #[serde(default)]
    pub top_p: Option<f32>,

    /// Frequency penalty.
    #[serde(default)]
    pub frequency_penalty: Option<f32>,

    /// Presence penalty.
    #[serde(default)]
    pub presence_penalty: Option<f32>,
}

fn default_temperature() -> f32 {
    0.7
}

impl Default for OpenAIOptions {
    fn default() -> Self {
        Self {
            temperature: default_temperature(),
            max_tokens: None,
            system_prompt: None,
            top_p: None,
            frequency_penalty: None,
            presence_penalty: None,
        }
    }
}

/// Anthropic-specific options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AnthropicOptions {
    /// Maximum tokens to generate.
    #[serde(default = "default_anthropic_max_tokens")]
    pub max_tokens: u32,

    /// Temperature for generation (0.0 - 1.0).
    #[serde(default = "default_temperature")]
    pub temperature: f32,

    /// System prompt.
    #[serde(default)]
    pub system: Option<String>,

    /// Top-p sampling.
    #[serde(default)]
    pub top_p: Option<f32>,

    /// Top-k sampling.
    #[serde(default)]
    pub top_k: Option<u32>,
}

fn default_anthropic_max_tokens() -> u32 {
    4096
}

impl Default for AnthropicOptions {
    fn default() -> Self {
        Self {
            max_tokens: default_anthropic_max_tokens(),
            temperature: default_temperature(),
            system: None,
            top_p: None,
            top_k: None,
        }
    }
}

/// ElevenLabs-specific options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct ElevenLabsOptions {
    /// Voice ID to use.
    #[serde(default)]
    pub voice_id: Option<String>,

    /// Model ID (e.g., "eleven_multilingual_v2").
    #[serde(default)]
    pub model_id: Option<String>,

    /// Stability (0.0 - 1.0).
    #[serde(default)]
    pub stability: Option<f32>,

    /// Similarity boost (0.0 - 1.0).
    #[serde(default)]
    pub similarity_boost: Option<f32>,

    /// Output format (e.g., "mp3_44100_128").
    #[serde(default)]
    pub output_format: Option<String>,
}

/// Google-specific options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GoogleOptions {
    /// Temperature for generation (0.0 - 1.0).
    #[serde(default = "default_temperature")]
    pub temperature: f32,

    /// Maximum output tokens.
    #[serde(default)]
    pub max_output_tokens: Option<u32>,

    /// Top-p sampling.
    #[serde(default)]
    pub top_p: Option<f32>,

    /// Top-k sampling.
    #[serde(default)]
    pub top_k: Option<u32>,

    /// Safety settings level.
    #[serde(default)]
    pub safety_level: Option<String>,
}

impl Default for GoogleOptions {
    fn default() -> Self {
        Self {
            temperature: default_temperature(),
            max_output_tokens: None,
            top_p: None,
            top_k: None,
            safety_level: None,
        }
    }
}

/// Provider configuration validation result.
#[derive(Debug, Clone)]
pub struct ProviderValidation {
    /// Whether the configuration is valid.
    pub valid: bool,
    /// Error messages if invalid.
    pub errors: Vec<String>,
    /// Warning messages.
    pub warnings: Vec<String>,
}

impl ProviderConfig {
    /// Validate the provider configuration.
    pub fn validate(&self) -> ProviderValidation {
        let mut errors = Vec::new();
        let mut warnings = Vec::new();

        // Check for API key
        if self.resolve_api_key().is_none() {
            warnings.push(format!(
                "No API key found for {}. Set {} environment variable or provide api_key in config.",
                self.provider,
                self.provider.api_key_env_var()
            ));
        }

        // Check for custom provider without base URL
        if self.provider == IntegrationProvider::Custom && self.base_url.is_none() {
            errors.push("Custom provider requires a base_url".to_string());
        }

        // Validate timeout
        if self.timeout_ms < 1000 {
            warnings.push(format!(
                "Timeout {}ms is very short, consider increasing to at least 5000ms",
                self.timeout_ms
            ));
        }

        ProviderValidation {
            valid: errors.is_empty(),
            errors,
            warnings,
        }
    }

    /// Check if the provider is ready for use (has API key).
    pub fn is_ready(&self) -> bool {
        self.resolve_api_key().is_some()
    }

    /// Get headers for API requests.
    pub fn auth_headers(&self) -> Option<Vec<(String, String)>> {
        let api_key = self.resolve_api_key()?;

        let headers = match self.provider {
            IntegrationProvider::OpenAI
            | IntegrationProvider::OpenRouter
            | IntegrationProvider::DeepSeek => {
                vec![("Authorization".to_string(), format!("Bearer {}", api_key))]
            }
            IntegrationProvider::Anthropic => {
                vec![
                    ("x-api-key".to_string(), api_key),
                    ("anthropic-version".to_string(), "2023-06-01".to_string()),
                ]
            }
            IntegrationProvider::Google => {
                // Google uses query parameter or API key header
                vec![("x-goog-api-key".to_string(), api_key)]
            }
            IntegrationProvider::ElevenLabs => {
                vec![("xi-api-key".to_string(), api_key)]
            }
            IntegrationProvider::Custom => {
                vec![("Authorization".to_string(), format!("Bearer {}", api_key))]
            }
        };

        Some(headers)
    }
}

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

    #[test]
    fn test_provider_from_str() {
        assert_eq!(
            "openai".parse::<IntegrationProvider>().unwrap(),
            IntegrationProvider::OpenAI
        );
        assert_eq!(
            "anthropic".parse::<IntegrationProvider>().unwrap(),
            IntegrationProvider::Anthropic
        );
        assert_eq!(
            "claude".parse::<IntegrationProvider>().unwrap(),
            IntegrationProvider::Anthropic
        );
        assert_eq!(
            "elevenlabs".parse::<IntegrationProvider>().unwrap(),
            IntegrationProvider::ElevenLabs
        );
    }

    #[test]
    fn test_provider_env_var() {
        assert_eq!(
            IntegrationProvider::OpenAI.api_key_env_var(),
            "OPENAI_API_KEY"
        );
        assert_eq!(
            IntegrationProvider::Anthropic.api_key_env_var(),
            "ANTHROPIC_API_KEY"
        );
    }

    #[test]
    fn test_provider_config_base_url() {
        let config = ProviderConfig::new(IntegrationProvider::OpenAI);
        assert_eq!(config.effective_base_url(), "https://api.openai.com/v1");

        let config = ProviderConfig {
            provider: IntegrationProvider::OpenAI,
            base_url: Some("https://custom.openai.azure.com".to_string()),
            api_key: None,
            timeout_ms: 30000,
            options: HashMap::new(),
        };
        assert_eq!(
            config.effective_base_url(),
            "https://custom.openai.azure.com"
        );
    }

    #[test]
    fn test_provider_config_serde() {
        let config = ProviderConfig::new(IntegrationProvider::OpenAI)
            .with_option("temperature", serde_json::json!(0.7));

        let yaml = serde_yaml::to_string(&config).unwrap();
        let parsed: ProviderConfig = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(config.provider, parsed.provider);
    }

    #[test]
    fn test_provider_validation() {
        let config = ProviderConfig::new(IntegrationProvider::Custom);
        let validation = config.validate();
        assert!(!validation.valid);
        assert!(validation.errors.iter().any(|e| e.contains("base_url")));

        let config = ProviderConfig::new(IntegrationProvider::OpenAI);
        let validation = config.validate();
        assert!(validation.valid); // No errors, just warnings about API key
    }

    #[test]
    fn test_provider_auth_headers() {
        // Set a test API key
        std::env::set_var("OPENAI_API_KEY", "test-key-123");

        let config = ProviderConfig::new(IntegrationProvider::OpenAI);
        let headers = config.auth_headers().unwrap();
        assert_eq!(headers.len(), 1);
        assert_eq!(headers[0].0, "Authorization");
        assert!(headers[0].1.contains("Bearer test-key-123"));

        // Clean up
        std::env::remove_var("OPENAI_API_KEY");
    }

    #[test]
    fn test_google_options_default() {
        let options = GoogleOptions::default();
        assert!((options.temperature - 0.7).abs() < f32::EPSILON);
        assert!(options.max_output_tokens.is_none());
    }

    #[test]
    fn test_provider_is_ready() {
        std::env::set_var("TEST_API_KEY", "key123");

        let mut config = ProviderConfig::new(IntegrationProvider::Custom);
        config.api_key = Some("$TEST_API_KEY".to_string());
        assert!(config.is_ready());

        config.api_key = Some("$NONEXISTENT_KEY".to_string());
        assert!(!config.is_ready());

        std::env::remove_var("TEST_API_KEY");
    }
}