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
//! Cloud client implementation.

use super::completion::{CompletionRequest, CompletionResponse};
use super::config::{CloudBackend, CloudConfig};
use super::error::CloudError;
use crate::http::{with_retry, CircuitBreaker, CircuitConfig, RetryPolicy, RetryResult};
use serde_json::json;
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Default timeout for HTTP connections (10 seconds).
const DEFAULT_CONNECT_TIMEOUT_MS: u64 = 10_000;

/// Cloud client for completions.
///
/// Routes requests through the configured backend:
/// - Gateway (default): Xybrid's managed gateway
/// - Direct: Direct API calls (development only)
///
/// For local/on-device inference, use `target: device` in your pipeline YAML,
/// which routes to [`crate::execution::TemplateExecutor`] instead.
///
/// # Resilience Features
///
/// The client includes production-hardening features:
/// - **Circuit breaker**: Prevents hammering failing gateway endpoints
/// - **Automatic retry**: Exponential backoff with jitter for transient failures
/// - **Connection timeouts**: Fail fast on unresponsive endpoints
///
/// # Example
///
/// ```no_run
/// # fn _example() -> Result<(), Box<dyn std::error::Error>> {
/// use xybrid_core::cloud::Cloud;
///
/// let cloud = Cloud::new()?;
/// let response = cloud.prompt("Hello, world!")?;
/// println!("Response: {}", response);
/// # Ok(())
/// # }
/// ```
pub struct Cloud {
    config: CloudConfig,
    agent: ureq::Agent,
    /// Circuit breaker for gateway endpoint.
    gateway_circuit: Arc<CircuitBreaker>,
    /// Retry policy for gateway requests.
    retry_policy: RetryPolicy,
}

impl Cloud {
    /// Create a new cloud client with default configuration.
    /// Uses gateway backend by default.
    pub fn new() -> Result<Self, CloudError> {
        Self::with_config(CloudConfig::default())
    }

    /// Create a new cloud client with custom configuration.
    pub fn with_config(config: CloudConfig) -> Result<Self, CloudError> {
        // Configure HTTP agent with both connection and request timeouts
        let agent = ureq::AgentBuilder::new()
            .timeout_connect(Duration::from_millis(DEFAULT_CONNECT_TIMEOUT_MS))
            .timeout(Duration::from_millis(config.timeout_ms as u64))
            .build();

        // Circuit breaker: open after 3 failures, stay open for 30s
        let gateway_circuit = Arc::new(CircuitBreaker::new(CircuitConfig::default()));

        // Retry policy: 3 attempts with exponential backoff (conservative for LLM calls)
        let retry_policy = RetryPolicy::conservative();

        Ok(Self {
            config,
            agent,
            gateway_circuit,
            retry_policy,
        })
    }

    /// Create a client that uses the gateway.
    pub fn gateway() -> Result<Self, CloudError> {
        Self::with_config(CloudConfig::gateway())
    }

    /// Create a client that uses direct API calls (development only).
    pub fn direct(provider: &str) -> Result<Self, CloudError> {
        Self::with_config(CloudConfig::direct(provider))
    }

    /// Check if the gateway circuit breaker is open.
    pub fn is_circuit_open(&self) -> bool {
        self.gateway_circuit.is_open()
    }

    /// Reset the gateway circuit breaker (use with caution).
    pub fn reset_circuit(&self) {
        self.gateway_circuit.reset();
    }

    /// Send a completion request.
    ///
    /// For gateway backend, this wraps the call with retry logic and circuit breaker.
    /// For direct backend, retries are not applied (direct calls are for development only).
    pub fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse, CloudError> {
        let start = Instant::now();

        let mut response = match self.config.backend {
            CloudBackend::Gateway => self.complete_via_gateway(request)?,
            CloudBackend::Direct => self.call_direct(request)?,
        };

        response.latency_ms = Some(start.elapsed().as_millis() as u32);
        Ok(response)
    }

    /// Complete via gateway with retry logic and circuit breaker.
    fn complete_via_gateway(
        &self,
        request: CompletionRequest,
    ) -> Result<CompletionResponse, CloudError> {
        // Check circuit breaker before attempting
        if !self.gateway_circuit.can_execute() {
            return Err(CloudError::CircuitOpen(
                "Gateway circuit breaker is open due to recent failures. Try again later.".into(),
            ));
        }

        // Clone request data needed for retry closure
        let request_clone = request.clone();

        let result: RetryResult<CompletionResponse, CloudError> =
            with_retry(&self.retry_policy, Some(&self.gateway_circuit), || {
                self.call_gateway(request_clone.clone())
            });

        result.into_result()
    }

    /// Simple prompt completion (convenience method).
    pub fn prompt(&self, prompt: &str) -> Result<String, CloudError> {
        let request = CompletionRequest::new(prompt);
        let response = self.complete(request)?;
        Ok(response.text)
    }

    /// Chat completion with system prompt (convenience method).
    pub fn chat(&self, system: &str, user_message: &str) -> Result<String, CloudError> {
        let request = CompletionRequest::new(user_message).with_system(system);
        let response = self.complete(request)?;
        Ok(response.text)
    }

    /// Complete through Xybrid Gateway.
    fn call_gateway(&self, request: CompletionRequest) -> Result<CompletionResponse, CloudError> {
        let api_key = self.config.resolve_api_key();

        // Build OpenAI-compatible request body
        let messages = request.to_messages();
        let openai_messages: Vec<serde_json::Value> = messages
            .iter()
            .map(|m| {
                json!({
                    "role": match m.role {
                        super::completion::Role::System => "system",
                        super::completion::Role::User => "user",
                        super::completion::Role::Assistant => "assistant",
                    },
                    "content": &m.content
                })
            })
            .collect();

        let model = request
            .model
            .clone()
            .or_else(|| self.config.default_model.clone())
            .unwrap_or_else(|| "gpt-4o-mini".to_string());

        let mut body = json!({
            "model": model,
            "messages": openai_messages,
        });

        if let Some(max_tokens) = request.max_tokens {
            body["max_tokens"] = json!(max_tokens);
        }
        if let Some(temperature) = request.temperature {
            body["temperature"] = json!(temperature);
        }
        if let Some(top_p) = request.top_p {
            body["top_p"] = json!(top_p);
        }
        if let Some(ref stop) = request.stop {
            body["stop"] = json!(stop);
        }
        if request.stream {
            body["stream"] = json!(true);
        }

        let url = format!("{}/chat/completions", self.config.gateway_url);

        if self.config.debug {
            eprintln!("[Cloud] Gateway request to: {}", url);
            eprintln!(
                "[Cloud] Body: {}",
                serde_json::to_string_pretty(&body).unwrap_or_default()
            );
        }

        let mut req = self
            .agent
            .post(&url)
            .set("Content-Type", "application/json");

        if let Some(ref key) = api_key {
            req = req.set("Authorization", &format!("Bearer {}", key));
        }

        let response = req.send_json(&body);

        match response {
            Ok(resp) => {
                let json_resp: serde_json::Value = resp
                    .into_json()
                    .map_err(|e| CloudError::ParseError(e.to_string()))?;

                if self.config.debug {
                    eprintln!(
                        "[Cloud] Response: {}",
                        serde_json::to_string_pretty(&json_resp).unwrap_or_default()
                    );
                }

                // Parse OpenAI-format response
                let text = json_resp["choices"][0]["message"]["content"]
                    .as_str()
                    .unwrap_or("")
                    .to_string();

                let model = json_resp["model"].as_str().unwrap_or("unknown").to_string();

                let finish_reason = json_resp["choices"][0]["finish_reason"]
                    .as_str()
                    .map(|s| s.to_string());

                let usage = json_resp.get("usage").map(parse_gateway_usage);

                let id = json_resp["id"].as_str().map(|s| s.to_string());

                Ok(CompletionResponse {
                    text,
                    model,
                    finish_reason,
                    usage,
                    id,
                    latency_ms: None,
                    backend: Some("gateway".to_string()),
                })
            }
            Err(ureq::Error::Status(status, resp)) => {
                // Parse Retry-After header for rate limiting (before consuming response)
                let retry_after_secs = resp
                    .header("Retry-After")
                    .and_then(|v| v.parse::<u64>().ok())
                    .unwrap_or(60); // Default to 60 seconds if not specified

                let error_body: Result<serde_json::Value, _> = resp.into_json();
                let message = error_body
                    .ok()
                    .and_then(|v| v["error"]["message"].as_str().map(|s| s.to_string()))
                    .unwrap_or_else(|| "Unknown error".into());

                match status {
                    429 => Err(CloudError::RateLimited { retry_after_secs }),
                    502..=504 => Err(CloudError::GatewayError(format!(
                        "Gateway returned {}: {}",
                        status, message
                    ))),
                    _ => Err(CloudError::ApiError { status, message }),
                }
            }
            Err(ureq::Error::Transport(transport)) => {
                let msg = transport.to_string();
                if msg.contains("timed out") || msg.contains("timeout") {
                    return Err(CloudError::Timeout {
                        timeout_ms: self.config.timeout_ms,
                    });
                }
                Err(CloudError::NetworkError(msg))
            }
        }
    }

    /// Complete using direct API calls (development only).
    fn call_direct(&self, request: CompletionRequest) -> Result<CompletionResponse, CloudError> {
        let provider =
            self.config.direct_provider.as_ref().ok_or_else(|| {
                CloudError::ConfigError("Direct provider not configured".to_string())
            })?;

        // Use cloud_llm for direct API calls
        let llm_provider: crate::pipeline::IntegrationProvider = provider
            .parse()
            .map_err(|e: String| CloudError::ConfigError(e))?;

        let client = crate::cloud_llm::LlmClient::new(llm_provider)?;
        let llm_request: crate::cloud_llm::LlmRequest = request.into();
        let response = client.complete(llm_request)?;

        let mut completion_response: CompletionResponse = response.into();
        completion_response.backend = Some(format!("direct:{}", provider));

        Ok(completion_response)
    }

    /// Get the current configuration.
    pub fn config(&self) -> &CloudConfig {
        &self.config
    }
}

impl Default for Cloud {
    fn default() -> Self {
        Self::new().expect("Failed to create default Cloud client")
    }
}

/// Parse a raw gateway `usage` JSON value into canonical `Usage`.
///
/// Maps DeepSeek's `prompt_cache_hit_tokens` / `prompt_cache_miss_tokens`
/// onto `cache_read_input_tokens` + derived uncached. The DeepSeek miss
/// count isn't stored — downstream code derives it as
/// `prompt_tokens - cache_read - cache_creation`. DeepSeek has no
/// cache-creation concept; Anthropic's creation field is plumbed via
/// the direct-path adapter in `cloud_llm::response`, not this function.
pub(crate) fn parse_gateway_usage(u: &serde_json::Value) -> super::completion::Usage {
    // Presence on either field surfaces as `Some(0)` for cold-cache
    // responses (where only the miss key is present). Preserves the
    // "provider didn't report" (None) vs "cold cache" (Some(0))
    // distinction downstream.
    let has_cache_fields =
        u.get("prompt_cache_hit_tokens").is_some() || u.get("prompt_cache_miss_tokens").is_some();
    let cache_read_input_tokens = if has_cache_fields {
        Some(
            u.get("prompt_cache_hit_tokens")
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as u32,
        )
    } else {
        None
    };
    super::completion::Usage {
        prompt_tokens: u["prompt_tokens"].as_u64().unwrap_or(0) as u32,
        completion_tokens: u["completion_tokens"].as_u64().unwrap_or(0) as u32,
        total_tokens: u["total_tokens"].as_u64().unwrap_or(0) as u32,
        cache_read_input_tokens,
        cache_creation_input_tokens: None,
    }
}

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

    #[test]
    fn test_cloud_new() {
        let cloud = Cloud::new();
        assert!(cloud.is_ok());
        let cloud = cloud.unwrap();
        assert_eq!(cloud.config().backend, CloudBackend::Gateway);
    }

    #[test]
    fn test_cloud_with_config() {
        let config = CloudConfig::gateway()
            .with_default_model("gpt-4o-mini")
            .with_timeout(60000);

        let cloud = Cloud::with_config(config).unwrap();
        assert_eq!(
            cloud.config().default_model,
            Some("gpt-4o-mini".to_string())
        );
        assert_eq!(cloud.config().timeout_ms, 60000);
    }

    #[test]
    fn test_cloud_direct() {
        // This will fail without API key, but should create the client
        std::env::set_var("OPENAI_API_KEY", "test");
        let cloud = Cloud::direct("openai");
        assert!(cloud.is_ok());
        std::env::remove_var("OPENAI_API_KEY");
    }

    #[test]
    fn test_cloud_circuit_breaker_initial_state() {
        let cloud = Cloud::new().unwrap();
        assert!(!cloud.is_circuit_open());
    }

    #[test]
    fn gateway_usage_maps_deepseek_cache_fields() {
        // Shape of a real DeepSeek `usage` block on a warm-cache call.
        let blob = serde_json::json!({
            "prompt_tokens": 1000,
            "completion_tokens": 120,
            "total_tokens": 1120,
            "prompt_cache_hit_tokens": 800,
            "prompt_cache_miss_tokens": 200,
        });
        let usage = parse_gateway_usage(&blob);
        assert_eq!(usage.prompt_tokens, 1000);
        assert_eq!(usage.completion_tokens, 120);
        assert_eq!(usage.cache_read_input_tokens, Some(800));
        assert_eq!(usage.cache_creation_input_tokens, None);
        // Derived uncached matches the provider's reported miss count.
        let derived_uncached = usage
            .prompt_tokens
            .saturating_sub(usage.cache_read_input_tokens.unwrap_or(0))
            .saturating_sub(usage.cache_creation_input_tokens.unwrap_or(0));
        assert_eq!(derived_uncached, 200);
    }

    #[test]
    fn gateway_usage_cold_cache_only_miss_field() {
        // First-call cold-cache response may include only the miss key.
        // Either-field presence detection should surface read as Some(0)
        // so the UI can render "0% cached" (vs hiding the receipt).
        let blob = serde_json::json!({
            "prompt_tokens": 500,
            "completion_tokens": 50,
            "total_tokens": 550,
            "prompt_cache_miss_tokens": 500,
        });
        let usage = parse_gateway_usage(&blob);
        assert_eq!(usage.cache_read_input_tokens, Some(0));
        assert_eq!(usage.cache_creation_input_tokens, None);
    }

    #[test]
    fn gateway_usage_no_cache_fields_stays_none() {
        // Providers that don't report caching at all (Groq, short-prompt
        // OpenAI) → both cache fields None.
        let blob = serde_json::json!({
            "prompt_tokens": 300,
            "completion_tokens": 30,
            "total_tokens": 330,
        });
        let usage = parse_gateway_usage(&blob);
        assert_eq!(usage.cache_read_input_tokens, None);
        assert_eq!(usage.cache_creation_input_tokens, None);
    }

    #[test]
    fn test_cloud_circuit_breaker_reset() {
        let cloud = Cloud::new().unwrap();

        // Manually trigger failures to open the circuit
        for _ in 0..3 {
            cloud.gateway_circuit.record_failure();
        }
        assert!(cloud.is_circuit_open());

        // Reset should close it
        cloud.reset_circuit();
        assert!(!cloud.is_circuit_open());
    }

    #[test]
    fn test_cloud_error_circuit_open() {
        let err = CloudError::CircuitOpen("test".to_string());
        assert!(matches!(err, CloudError::CircuitOpen(_)));
        assert_eq!(err.to_string(), "Circuit breaker open: test");
    }

    #[test]
    fn test_circuit_open_not_retryable() {
        use crate::http::RetryableError;

        let err = CloudError::CircuitOpen("test".to_string());
        assert!(!err.is_retryable());
    }
}