api_xai 0.3.0

X.AI Grok API client for accessing large language models (LLMs).
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
mod private
{
  //! Automatic failover support for multi-endpoint deployments.
  //!
  //! This module provides health tracking and automatic rotation between
  //! multiple API endpoints to ensure high availability.
  //!
  //! # Design Decisions
  //!
  //! ## Why `Arc< Mutex< FailoverState > >`?
  //!
  //! The failover state must be shared between the Client and multiple concurrent
  //! requests while allowing mutation of health metrics. Using Arc< Mutex<> > instead
  //! of alternatives:
  //!
  //! - **`Arc< RwLock<> >`**: Would provide better read concurrency, but failover
  //!   operations are fast (just index increments) and contention is rare since
  //!   most operations are reads of `current_endpoint` which we cache.
  //!
  //! - **`AtomicUsize` for index**: Would avoid locks for index reads but wouldn't
  //!   solve the problem of updating health metrics (failure counts, timestamps),
  //!   which require coordinated updates.
  //!
  //! - **Message passing**: Would add async complexity and latency to every request.
  //!
  //! Mutex is simpler and the lock is held for microseconds (just updating counters).
  //!
  //! ## Health State Transitions
  //!
  //! Endpoints follow a 3-state model:
  //!
  //! ```text
  //! Healthy ──(1st failure)──> Degraded ──(max failures)──> Unhealthy
  //!    ↑                                                          │
  //!    └─────────────────(success or cooldown)────────────────────┘
  //! ```
  //!
  //! **Rationale**: The Degraded state allows the system to detect intermittent
  //! issues without immediately marking endpoints as unavailable. This reduces
  //! unnecessary failovers for transient network glitches.
  //!
  //! ## Auto-Rotation vs Manual Control
  //!
  //! Auto-rotation (`auto_rotate : true`) automatically switches to backup endpoints
  //! when the current one becomes unhealthy. This is disabled by default because:
  //!
  //! 1. **Explicit Control**: Aligns with "Thin Client, Rich API" principle - the
  //!    application decides when failover occurs, not the library.
  //!
  //! 2. **Testing**: Manual control makes failover behavior predictable in tests.
  //!
  //! 3. **Debugging**: Applications may want to observe failures before switching.
  //!
  //! However, production deployments typically enable auto-rotation for automatic
  //! recovery without application intervention.
  //!
  //! ## Cooldown Period
  //!
  //! Unhealthy endpoints cannot be retried until `retry_after` duration elapses.
  //! After cooldown, they transition to Degraded (not Healthy) to allow gradual
  //! recovery validation.
  //!
  //! **Rationale**: Prevents rapid retry storms that could overwhelm a recovering
  //! endpoint. The gradual recovery (Degraded first) allows the system to detect
  //! if the endpoint is truly recovered before fully trusting it again.

  use std::sync::{ Arc, Mutex };
  use std::time::{ Duration, Instant };

  /// Endpoint health status.
  #[ derive( Debug, Clone, Copy, PartialEq, Eq ) ]
  pub enum EndpointHealth
  {
    /// Endpoint is healthy and available.
    Healthy,

    /// Endpoint is degraded but usable.
    Degraded,

    /// Endpoint is unhealthy and should be avoided.
    Unhealthy,
  }

  /// Failover configuration.
  ///
  /// Configures automatic endpoint failover behavior.
  ///
  /// # Examples
  ///
  /// ```
  /// use api_xai::FailoverConfig;
  /// use std::time::Duration;
  ///
  /// let config = FailoverConfig::default()
  ///   .with_max_failures( 3 )
  ///   .with_retry_after( Duration::from_secs( 60 ) );
  /// ```
  #[ derive( Debug, Clone ) ]
  pub struct FailoverConfig
  {
    /// Maximum consecutive failures before marking endpoint unhealthy.
    pub max_failures : usize,

    /// Duration to wait before retrying unhealthy endpoint.
    pub retry_after : Duration,

    /// Whether to automatically rotate endpoints on failure.
    pub auto_rotate : bool,
  }

  impl Default for FailoverConfig
  {
    fn default() -> Self
    {
      Self
      {
        max_failures : 3,
        retry_after : Duration::from_secs( 60 ),
        auto_rotate : true,
      }
    }
  }

  impl FailoverConfig
  {
    /// Sets maximum failures before marking unhealthy.
    #[ must_use ]
    pub fn with_max_failures( mut self, failures : usize ) -> Self
    {
      self.max_failures = failures;
      self
    }

    /// Sets retry delay for unhealthy endpoints.
    #[ must_use ]
    pub fn with_retry_after( mut self, duration : Duration ) -> Self
    {
      self.retry_after = duration;
      self
    }

    /// Enables or disables automatic rotation.
    #[ must_use ]
    pub fn with_auto_rotate( mut self, enable : bool ) -> Self
    {
      self.auto_rotate = enable;
      self
    }
  }

  /// Endpoint state tracking.
  #[ derive( Debug, Clone ) ]
  struct EndpointState
  {
    base_url : String,
    consecutive_failures : usize,
    last_failure_time : Option< Instant >,
    health : EndpointHealth,
  }

  /// Failover manager for multiple XAI endpoints.
  ///
  /// Manages automatic failover between multiple XAI API endpoints,
  /// tracking health and rotating on failures.
  ///
  /// # Examples
  ///
  /// ```no_run
  /// use api_xai::{ FailoverManager, FailoverConfig };
  ///
  /// let manager = FailoverManager::new(
  ///   vec![
  ///     "https://api.x.ai/v1/".to_string(),
  ///     "https://api-backup.x.ai/v1/".to_string(),
  ///   ],
  ///   FailoverConfig::default()
  /// );
  ///
  /// // Get current endpoint
  /// let endpoint = manager.current_endpoint();
  /// ```
  #[ derive( Debug, Clone ) ]
  pub struct FailoverManager
  {
    config : FailoverConfig,
    state : Arc< Mutex< FailoverState > >,
  }

  #[ derive( Debug ) ]
  struct FailoverState
  {
    endpoints : Vec< EndpointState >,
    current_index : usize,
  }

  impl FailoverManager
  {
    /// Creates a new failover manager with multiple endpoints.
    ///
    /// # Arguments
    ///
    /// * `endpoints` - List of base URLs for XAI API endpoints
    /// * `config` - Failover configuration
    ///
    /// # Panics
    ///
    /// Panics if endpoints list is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use api_xai::{ FailoverManager, FailoverConfig };
    ///
    /// let manager = FailoverManager::new(
    ///   vec![ "https://api.x.ai/v1/".to_string() ],
    ///   FailoverConfig::default()
    /// );
    /// ```
    pub fn new( endpoints : Vec< String >, config : FailoverConfig ) -> Self
    {
      assert!( !endpoints.is_empty(), "Must provide at least one endpoint" );

      let endpoint_states = endpoints
        .into_iter()
        .map( | base_url | EndpointState
        {
          base_url,
          consecutive_failures : 0,
          last_failure_time : None,
          health : EndpointHealth::Healthy,
        } )
        .collect();

      Self
      {
        config,
        state : Arc::new( Mutex::new( FailoverState
        {
          endpoints : endpoint_states,
          current_index : 0,
        } ) ),
      }
    }

    /// Returns the current endpoint URL.
    ///
    /// # Examples
    ///
    /// ```
    /// use api_xai::{ FailoverManager, FailoverConfig };
    ///
    /// let manager = FailoverManager::new(
    ///   vec![ "https://api.x.ai/v1/".to_string() ],
    ///   FailoverConfig::default()
    /// );
    ///
    /// let endpoint = manager.current_endpoint();
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn current_endpoint( &self ) -> String
    {
      let state = self.state.lock().unwrap();
      state.endpoints[ state.current_index ].base_url.clone()
    }

    /// Records a successful request to current endpoint.
    ///
    /// Resets failure counter and marks endpoint as healthy.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn record_success( &self )
    {
      let mut state = self.state.lock().unwrap();
      let current_idx = state.current_index;
      let endpoint = &mut state.endpoints[ current_idx ];
      endpoint.consecutive_failures = 0;
      endpoint.health = EndpointHealth::Healthy;
    }

    /// Records a failed request to current endpoint.
    ///
    /// Increments failure counter and may trigger rotation if configured.
    ///
    /// # Returns
    ///
    /// `true` if endpoint was rotated, `false` otherwise.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn record_failure( &self ) -> bool
    {
      let mut state = self.state.lock().unwrap();
      let current_idx = state.current_index;
      let endpoint = &mut state.endpoints[ current_idx ];

      endpoint.consecutive_failures += 1;
      endpoint.last_failure_time = Some( Instant::now() );

      // Mark unhealthy if threshold reached
      if endpoint.consecutive_failures >= self.config.max_failures
      {
        endpoint.health = EndpointHealth::Unhealthy;
      }
      else if endpoint.consecutive_failures > 0
      {
        endpoint.health = EndpointHealth::Degraded;
      }

      // Auto-rotate if enabled and endpoint is unhealthy
      if self.config.auto_rotate && endpoint.health == EndpointHealth::Unhealthy
      {
        self.rotate_internal( &mut state );
        true
      }
      else
      {
        false
      }
    }

    /// Manually rotates to next available endpoint.
    ///
    /// Skips unhealthy endpoints unless all are unhealthy.
    ///
    /// # Examples
    ///
    /// ```
    /// use api_xai::{ FailoverManager, FailoverConfig };
    ///
    /// let manager = FailoverManager::new(
    ///   vec![
    ///     "https://api.x.ai/v1/".to_string(),
    ///     "https://backup.x.ai/v1/".to_string(),
    ///   ],
    ///   FailoverConfig::default()
    /// );
    ///
    /// manager.rotate();
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn rotate( &self )
    {
      let mut state = self.state.lock().unwrap();
      self.rotate_internal( &mut state );
    }

    fn rotate_internal( &self, state : &mut FailoverState )
    {
      let total = state.endpoints.len();
      let start_idx = state.current_index;

      // Try to find a healthy endpoint
      for offset in 1..=total
      {
        let idx = ( start_idx + offset ) % total;
        let endpoint = &mut state.endpoints[ idx ];

        // Check if unhealthy endpoint can be retried
        if endpoint.health == EndpointHealth::Unhealthy
        {
          if let Some( last_failure ) = endpoint.last_failure_time
          {
            if last_failure.elapsed() >= self.config.retry_after
            {
              // Retry period elapsed, mark as degraded
              endpoint.health = EndpointHealth::Degraded;
              endpoint.consecutive_failures = 0;
            }
            else
            {
              continue; // Still in cooldown
            }
          }
        }

        // Use this endpoint
        state.current_index = idx;
        return;
      }

      // All endpoints unhealthy, use next anyway (circular)
      state.current_index = ( start_idx + 1 ) % total;
    }

    /// Returns health status of all endpoints.
    ///
    /// # Examples
    ///
    /// ```
    /// use api_xai::{ FailoverManager, FailoverConfig };
    ///
    /// let manager = FailoverManager::new(
    ///   vec![ "https://api.x.ai/v1/".to_string() ],
    ///   FailoverConfig::default()
    /// );
    ///
    /// let health = manager.endpoint_health();
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn endpoint_health( &self ) -> Vec< ( String, EndpointHealth ) >
    {
      let state = self.state.lock().unwrap();
      state
        .endpoints
        .iter()
        .map( | e | ( e.base_url.clone(), e.health ) )
        .collect()
    }

    /// Returns index of current endpoint.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn current_index( &self ) -> usize
    {
      self.state.lock().unwrap().current_index
    }

    /// Returns total number of endpoints.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn endpoint_count( &self ) -> usize
    {
      self.state.lock().unwrap().endpoints.len()
    }

    /// Resets all endpoints to healthy state.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn reset( &self )
    {
      let mut state = self.state.lock().unwrap();
      for endpoint in &mut state.endpoints
      {
        endpoint.consecutive_failures = 0;
        endpoint.last_failure_time = None;
        endpoint.health = EndpointHealth::Healthy;
      }
      state.current_index = 0;
    }
  }
}

crate::mod_interface!
{
  exposed use
  {
    EndpointHealth,
    FailoverConfig,
    FailoverManager,
  };
}