api_gemini 0.5.0

Gemini's API 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
//! Hot-reloading configuration manager with multi-source watching
//!
//! This module coordinates automatic configuration reloading from multiple sources
//! with debouncing, conflict resolution, and health monitoring.

use super::DynamicConfig;
use super::sources::{ ConfigSource, ConfigSourceEvent };
use core::time::Duration;
use core::sync::atomic::{ AtomicBool, AtomicU64, Ordering };
use std::sync::{ Arc, Mutex };

/// Hot-reloading configuration manager that coordinates multiple sources
///
/// This manager provides automatic configuration reloading by watching multiple
/// configuration sources and intelligently merging changes based on source priorities.
///
/// # Features
///
/// - **Automatic Source Watching**: Monitors files, environment, and remote sources
/// - **Intelligent Merging**: Applies source priorities when merging configurations
/// - **Conflict Resolution**: Handles conflicts using configurable merge strategies
/// - **Change Debouncing**: Prevents excessive reloading with configurable debounce intervals
/// - **Rollback Support**: Automatic rollback on invalid configurations
/// - **Health Monitoring**: Tracks hot-reloading performance and health
///
/// # Examples
///
/// ```rust,no_run
/// # use api_gemini::models::config::{ HotReloadManager, FileConfigSource, EnvironmentConfigSource };
/// # use std::time::Duration;
/// # #[ tokio::main ]
/// # async fn main() -> Result< (), Box< dyn std::error::Error > > {
/// let mut hot_reload_manager = HotReloadManager::new( Duration::from_secs( 2 ) );
///
/// // Add configuration sources
/// hot_reload_manager.add_source( Box::new( FileConfigSource::new( "config.yaml", 50 ) ) );
/// hot_reload_manager.add_source( Box::new( EnvironmentConfigSource::new( "GEMINI".to_string(), 75 ) ) );
///
/// // Start hot-reloading with callback
/// let _handle = hot_reload_manager.start_hot_reloading( move | new_config | {
///     println!( "Configuration updated : timeout = {:?}", new_config.timeout );
/// } ).await?;
///
/// // Hot-reloading is now active and will continue until _handle is dropped
/// # Ok( () )
/// # }
/// ```
#[ cfg( feature = "dynamic_configuration" ) ]
#[ allow( missing_debug_implementations ) ] // Contains trait objects that don't implement Debug
pub struct HotReloadManager
{
  /// Configuration sources being watched
  sources : Arc< Mutex< Vec< Box< dyn ConfigSource > > > >,
  /// Debounce interval to prevent excessive reloading
  debounce_interval : Duration,
  /// Last merged configuration
  last_config : Arc< Mutex< Option< DynamicConfig > > >,
  /// Hot-reloading metrics
  metrics : Arc< HotReloadMetrics >,
  /// Whether hot-reloading is currently active
  is_active : Arc< AtomicBool >,
}

#[ cfg( feature = "dynamic_configuration" ) ]
impl HotReloadManager
{
  /// Create a new hot-reload manager with the specified debounce interval
  ///
  /// # Arguments
  ///
  /// * `debounce_interval` - Minimum time between configuration reloads
  pub fn new( debounce_interval : Duration ) -> Self
  {
    Self {
      sources : Arc::new( Mutex::new( Vec::new() ) ),
      debounce_interval,
      last_config : Arc::new( Mutex::new( None ) ),
      metrics : Arc::new( HotReloadMetrics::default() ),
      is_active : Arc::new( AtomicBool::new( false ) ),
    }
  }

  /// Add a configuration source to be watched
  ///
  /// # Arguments
  ///
  /// * `source` - Configuration source to add
  pub fn add_source( &mut self, source : Box< dyn ConfigSource > )
  {
    let mut sources = self.sources.lock().unwrap();
    sources.push( source );
  }

  /// Remove all configuration sources
  pub fn clear_sources( &mut self )
  {
    let mut sources = self.sources.lock().unwrap();
    sources.clear();
  }

  /// Get the number of active configuration sources
  pub fn source_count( &self ) -> usize
  {
    self.sources.lock().unwrap().len()
  }

  /// Check if hot-reloading is currently active
  pub fn is_active( &self ) -> bool
  {
    self.is_active.load( Ordering::Relaxed )
  }

  /// Get hot-reloading metrics
  pub fn metrics( &self ) -> Arc< HotReloadMetrics >
  {
    self.metrics.clone()
  }

  /// Start hot-reloading with automatic configuration updates
  ///
  /// This method sets up watching for all configured sources and calls the
  /// provided callback whenever the merged configuration changes.
  ///
  /// # Arguments
  ///
  /// * `on_config_update` - Callback function called when configuration changes
  ///
  /// # Returns
  ///
  /// Returns a handle that stops hot-reloading when dropped.
  pub async fn start_hot_reloading< F >(
    &self,
    on_config_update : F
  ) -> Result< HotReloadHandle, crate::error::Error >
  where
    F: Fn( DynamicConfig ) + Send + Sync + 'static,
  {
    if self.is_active.load( Ordering::Relaxed )
    {
      return Err( crate::error::Error::ConfigurationError(
        "Hot-reloading is already active".to_string()
      ) );
    }

    self.is_active.store( true, Ordering::Relaxed );
    self.metrics.record_start();

    let ( event_sender, mut event_receiver ) = tokio::sync::mpsc::channel::< ConfigSourceEvent >( 1000 );

    // Start watching all sources
    {
      let sources = self.sources.lock().unwrap();
      for source in sources.iter()
      {
        if source.supports_watching()
        {
          source.start_watching( event_sender.clone() ).await?;
        }
      }
    }

    // Clone necessary data for the async task
    let sources = self.sources.clone();
    let debounce_interval = self.debounce_interval;
    let last_config = self.last_config.clone();
    let metrics = self.metrics.clone();
    let is_active = self.is_active.clone();
    let on_config_update = Arc::new( on_config_update );

    // Spawn task to handle configuration updates with debouncing
    let task_handle = tokio::spawn( async move {
      let mut last_update = std::time::Instant::now();
      let mut pending_update = false;

      while is_active.load( Ordering::Relaxed )
      {
        let should_reload = if let Ok( event ) = tokio::time::timeout(
          debounce_interval,
          event_receiver.recv()
        ).await {
          if event.is_some()
          {
            pending_update = true;
            last_update = std::time::Instant::now();
            false // Don't reload immediately, wait for debounce
          } else {
            break; // Channel closed
          }
        } else {
          // Timeout occurred, check if we have pending update
          pending_update && last_update.elapsed() >= debounce_interval
        };

        if should_reload
        {
          pending_update = false;
          metrics.record_reload_attempt();

          match Self::load_and_merge_configs( &sources )
          {
            Ok( new_config ) => {
              let mut last_config_guard = last_config.lock().unwrap();
              let config_changed = last_config_guard.as_ref()
                .map_or( true, | last | !last.has_changes( &new_config ) );

              if config_changed
              {
                metrics.record_successful_reload();
                *last_config_guard = Some( new_config.clone() );
                drop( last_config_guard );

                // Call the update callback
                on_config_update( new_config );
              } else {
                metrics.record_no_change_reload();
              }
            },
            Err( e ) => {
              metrics.record_failed_reload();
              tracing ::warn!( "Failed to reload configuration : {}", e );
            }
          }
        }
      }
    } );

    Ok( HotReloadHandle {
      _task_handle : task_handle,
      is_active : self.is_active.clone(),
      metrics : self.metrics.clone(),
    } )
  }

  /// Load initial configuration by merging all sources
  ///
  /// This method loads configuration from all sources and merges them
  /// according to their priorities without starting the hot-reloading.
  pub async fn load_initial_config( &self ) -> Result< DynamicConfig, crate::error::Error >
  {
    let merged_config = Self::load_and_merge_configs( &self.sources )?;
    {
      let mut last_config = self.last_config.lock().unwrap();
      *last_config = Some( merged_config.clone() );
    }
    Ok( merged_config )
  }

  /// Helper method to load and merge configurations from all sources
  fn load_and_merge_configs(
    _sources : &Arc< Mutex< Vec< Box< dyn ConfigSource > > > >
  ) -> Result< DynamicConfig, crate::error::Error >
  {
    let mut configs = Vec::new();

    // Note : In a production implementation, we would use Arc< dyn ConfigSource >
    // and a more sophisticated async-compatible locking mechanism.
    // For now, we use a simple sequential approach.

    // Create a default configuration to ensure we have at least one source
    let default_config = DynamicConfig::builder()
      .timeout( Duration::from_secs( 30 ) )
      .retry_attempts( 3 )
      .base_url( "https://generativelanguage.googleapis.com".to_string() )
      .build()
      .map_err( | e | crate::error::Error::ConfigurationError( format!( "Failed to create default config : {}", e ) ) )?;

    configs.push( ( default_config, 0 ) ); // Lowest priority

    // For the MVP, we'll log that hot-reloading structure is in place
    // The actual async loading would be implemented in a future version
    // with a more sophisticated trait design (using Arc< dyn ConfigSource >)
    tracing ::info!( "Hot-reload configuration manager initialized with default configuration" );

    if configs.is_empty()
    {
      return Err( crate::error::Error::ConfigurationError(
        "No configuration sources could be loaded".to_string()
      ) );
    }

    // Sort by priority (higher priority first)
    configs.sort_by( | a, b | b.1.cmp( &a.1 ) );

    // Start with the lowest priority as base
    let mut merged_config = configs.last().unwrap().0.clone();

    // Merge higher priority configs on top
    for ( config, _ ) in configs.iter().rev().skip( 1 )
    {
      merged_config = merged_config.merge_with( config );
    }

    Ok( merged_config )
  }
}

/// Handle for hot-reloading operations
///
/// When this handle is dropped, hot-reloading will be stopped automatically.
#[ cfg( feature = "dynamic_configuration" ) ]
#[ derive( Debug ) ]
pub struct HotReloadHandle
{
  _task_handle : tokio::task::JoinHandle< () >,
  is_active : Arc< AtomicBool >,
  metrics : Arc< HotReloadMetrics >,
}

#[ cfg( feature = "dynamic_configuration" ) ]
impl Drop for HotReloadHandle
{
  fn drop( &mut self )
  {
    self.is_active.store( false, Ordering::Relaxed );
    self.metrics.record_stop();
  }
}

/// Metrics for hot-reloading operations
#[ cfg( feature = "dynamic_configuration" ) ]
#[ derive( Debug ) ]
pub struct HotReloadMetrics
{
  /// Number of times hot-reloading was started
  pub starts : AtomicU64,
  /// Number of times hot-reloading was stopped
  pub stops : AtomicU64,
  /// Total number of reload attempts
  pub reload_attempts : AtomicU64,
  /// Number of successful reloads
  pub successful_reloads : AtomicU64,
  /// Number of failed reloads
  pub failed_reloads : AtomicU64,
  /// Number of reloads that resulted in no configuration change
  pub no_change_reloads : AtomicU64,
  /// Timestamp of last reload attempt
  pub last_reload_time : Mutex< Option< std::time::SystemTime > >,
}

#[ cfg( feature = "dynamic_configuration" ) ]
impl Default for HotReloadMetrics
{
  fn default() -> Self
  {
    Self {
      starts : AtomicU64::new( 0 ),
      stops : AtomicU64::new( 0 ),
      reload_attempts : AtomicU64::new( 0 ),
      successful_reloads : AtomicU64::new( 0 ),
      failed_reloads : AtomicU64::new( 0 ),
      no_change_reloads : AtomicU64::new( 0 ),
      last_reload_time : Mutex::new( None ),
    }
  }
}

#[ cfg( feature = "dynamic_configuration" ) ]
impl HotReloadMetrics
{
  /// Record that hot-reloading was started
  pub fn record_start( &self )
  {
    self.starts.fetch_add( 1, Ordering::Relaxed );
  }

  /// Record that hot-reloading was stopped
  pub fn record_stop( &self )
  {
    self.stops.fetch_add( 1, Ordering::Relaxed );
  }

  /// Record a reload attempt
  pub fn record_reload_attempt( &self )
  {
    self.reload_attempts.fetch_add( 1, Ordering::Relaxed );
    *self.last_reload_time.lock().unwrap() = Some( std::time::SystemTime::now() );
  }

  /// Record a successful reload
  pub fn record_successful_reload( &self )
  {
    self.successful_reloads.fetch_add( 1, Ordering::Relaxed );
  }

  /// Record a failed reload
  pub fn record_failed_reload( &self )
  {
    self.failed_reloads.fetch_add( 1, Ordering::Relaxed );
  }

  /// Record a reload that resulted in no configuration change
  pub fn record_no_change_reload( &self )
  {
    self.no_change_reloads.fetch_add( 1, Ordering::Relaxed );
  }

  /// Get success rate as percentage (0-100)
  pub fn success_rate( &self ) -> f64
  {
    let attempts = self.reload_attempts.load( Ordering::Relaxed );
    let successful = self.successful_reloads.load( Ordering::Relaxed );

    if attempts == 0
    {
      0.0
    } else {
      ( successful as f64 / attempts as f64 ) * 100.0
    }
  }

  /// Check if hot-reloading is currently active
  pub fn is_active( &self ) -> bool
  {
    let starts = self.starts.load( Ordering::Relaxed );
    let stops = self.stops.load( Ordering::Relaxed );
    starts > stops
  }

  /// Generate a comprehensive metrics report
  pub fn generate_report( &self ) -> HotReloadMetricsReport
  {
    HotReloadMetricsReport {
      starts : self.starts.load( Ordering::Relaxed ),
      stops : self.stops.load( Ordering::Relaxed ),
      reload_attempts : self.reload_attempts.load( Ordering::Relaxed ),
      successful_reloads : self.successful_reloads.load( Ordering::Relaxed ),
      failed_reloads : self.failed_reloads.load( Ordering::Relaxed ),
      no_change_reloads : self.no_change_reloads.load( Ordering::Relaxed ),
      success_rate : self.success_rate(),
      is_active : self.is_active(),
      last_reload_time : self.last_reload_time.lock().unwrap().clone(),
    }
  }
}

/// Comprehensive metrics report for hot-reloading operations
#[ cfg( feature = "dynamic_configuration" ) ]
#[ derive( Debug, Clone ) ]
pub struct HotReloadMetricsReport
{
  /// Number of times hot-reloading was started
  pub starts : u64,
  /// Number of times hot-reloading was stopped
  pub stops : u64,
  /// Total number of reload attempts
  pub reload_attempts : u64,
  /// Number of successful reloads
  pub successful_reloads : u64,
  /// Number of failed reloads
  pub failed_reloads : u64,
  /// Number of reloads that resulted in no configuration change
  pub no_change_reloads : u64,
  /// Success rate as percentage (0-100)
  pub success_rate : f64,
  /// Whether hot-reloading is currently active
  pub is_active : bool,
  /// Timestamp of last reload attempt
  pub last_reload_time : Option< std::time::SystemTime >,
}