api_ollama 0.2.0

Ollama local LLM runtime API client for HTTP communication.
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Stream control functionality for Ollama API.
//!
//! Provides pause/resume/cancel capabilities for streaming responses,
//! with buffering, metrics tracking, and control interfaces.

#[ cfg( feature = "streaming_control" ) ]
mod private
{
  use core::time::Duration;
  use std::sync::Arc;

  /// Streaming control functionality for pause/resume/cancel operations
  #[ cfg( feature = "streaming_control" ) ]
  #[ derive( Debug, Clone, Copy, PartialEq, Eq ) ]
  pub enum StreamState
  {
    /// Stream is ready to start
    Ready,
    /// Stream is actively streaming data
    Streaming,
    /// Stream is paused and buffering
    Paused,
    /// Stream is cancelled and cleaned up
    Cancelled,
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl core::fmt::Display for StreamState
  {
    fn fmt( &self, f : &mut core::fmt::Formatter< '_ > ) -> core::fmt::Result
    {
      match self
      {
        StreamState::Ready => write!( f, "Ready" ),
        StreamState::Streaming => write!( f, "Streaming" ),
        StreamState::Paused => write!( f, "Paused" ),
        StreamState::Cancelled => write!( f, "Cancelled" ),
      }
    }
  }

  /// Errors related to stream control operations
  #[ cfg( feature = "streaming_control" ) ]
  #[ derive( Debug, Clone ) ]
  pub enum StreamControlError
  {
    /// Invalid state transition attempted
    InvalidStateTransition {
      /// The state transitioning from
      from : StreamState,
      /// The state attempting to transition to
      to : StreamState
    },
    /// Stream operation timed out
    TimeoutError,
    /// Buffer overflow occurred
    BufferOverflow {
      /// The buffer size limit that was exceeded
      limit : usize
    },
    /// Stream was cancelled
    StreamCancelled,
    /// General error with message
    GeneralError( String ),
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl core::fmt::Display for StreamControlError
  {
    fn fmt( &self, f : &mut core::fmt::Formatter< '_ > ) -> core::fmt::Result
    {
      match self
      {
        StreamControlError::InvalidStateTransition { from, to } =>
        {
          write!( f, "Invalid state transition from {from} to {to}" )
        },
        StreamControlError::TimeoutError => write!( f, "Stream control operation timed out" ),
        StreamControlError::BufferOverflow { limit } =>
        {
          write!( f, "Buffer overflow : exceeded limit of {limit} bytes" )
        },
        StreamControlError::StreamCancelled => write!( f, "Stream was cancelled" ),
        StreamControlError::GeneralError( msg ) => write!( f, "Stream control error : {msg}" ),
      }
    }
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl std::error::Error for StreamControlError
  {}

  /// Metrics for stream control operations
  #[ cfg( feature = "streaming_control" ) ]
  #[ derive( Debug, Clone ) ]
  pub struct StreamMetrics
  {
    /// Number of times stream was paused
    pub pause_count : u64,
    /// Number of times stream was resumed
    pub resume_count : u64,
    /// Total time spent in paused state
    pub total_pause_duration : Duration,
    /// Time when last pause started
    pub last_pause_start : Option< std::time::Instant >,
    /// Total bytes buffered during pause
    pub total_buffered_bytes : u64,
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl StreamMetrics
  {
    /// Create new metrics instance
    #[ inline ]
    #[ must_use ]
    pub fn new() -> Self
    {
      Self
      {
        pause_count : 0,
        resume_count : 0,
        total_pause_duration : Duration::from_secs( 0 ),
        last_pause_start : None,
        total_buffered_bytes : 0,
      }
    }

    /// Record a pause operation
    #[ inline ]
    pub fn record_pause( &mut self )
    {
      self.pause_count += 1;
      self.last_pause_start = Some( std::time::Instant::now() );
    }

    /// Record a resume operation
    #[ inline ]
    pub fn record_resume( &mut self )
    {
      self.resume_count += 1;
      if let Some( pause_start ) = self.last_pause_start.take()
      {
        self.total_pause_duration += pause_start.elapsed();
      }
    }

    /// Record buffered bytes
    #[ inline ]
    pub fn record_buffered_bytes( &mut self, bytes : u64 )
    {
      self.total_buffered_bytes += bytes;
    }
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl Default for StreamMetrics
  {
    fn default() -> Self
    {
      Self::new()
    }
  }

  /// Buffer for managing streaming data during pause states
  #[ cfg( feature = "streaming_control" ) ]
  #[ derive( Debug ) ]
  pub struct StreamBuffer
  {
    /// Internal buffer storage
    buffer : Arc< tokio::sync::Mutex< Vec< u8 > > >,
    /// Maximum buffer capacity
    capacity : usize,
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl StreamBuffer
  {
    /// Create a new stream buffer with specified capacity
    #[ inline ]
    #[ must_use ]
    pub fn new( capacity : usize ) -> Self
    {
      Self
      {
        buffer : Arc::new( tokio::sync::Mutex::new( Vec::with_capacity( capacity ) ) ),
        capacity,
      }
    }

    /// Write data to the buffer
    #[ inline ]
    pub async fn write( &self, data : Vec< u8 > ) -> Result< (), StreamControlError >
    {
      let mut buffer = self.buffer.lock().await;

      if buffer.len() + data.len() > self.capacity
      {
        return Err( StreamControlError::BufferOverflow { limit : self.capacity } );
      }

      buffer.extend( data );
      Ok( () )
    }

    /// Read data from the buffer
    #[ inline ]
    pub async fn read( &self, size : usize ) -> Result< Vec< u8 >, StreamControlError >
    {
      let mut buffer = self.buffer.lock().await;

      if buffer.len() < size
      {
        return Ok( buffer.drain( .. ).collect() );
      }

      Ok( buffer.drain( ..size ).collect() )
    }

    /// Get current buffer length
    #[ inline ]
    pub async fn len( &self ) -> usize
    {
      self.buffer.lock().await.len()
    }

    /// Check if buffer is empty
    #[ inline ]
    pub async fn is_empty( &self ) -> bool
    {
      self.buffer.lock().await.is_empty()
    }

    /// Get buffer capacity
    #[ inline ]
    pub fn capacity( &self ) -> usize
    {
      self.capacity
    }

    /// Clear the buffer
    #[ inline ]
    pub async fn clear( &self )
    {
      self.buffer.lock().await.clear();
    }
  }

  /// Main stream control interface
  #[ cfg( feature = "streaming_control" ) ]
  pub struct StreamControl
  {
    /// Current stream state
    state : Arc< tokio::sync::RwLock< StreamState > >,
    /// Stream metrics
    metrics : Arc< tokio::sync::Mutex< StreamMetrics > >,
    /// Timeout for paused streams
    timeout : Option< Duration >,
    /// Cancellation token
    cancellation_token : Arc< tokio::sync::Mutex< Option< tokio_util::sync::CancellationToken > > >,
    /// State change callbacks
    state_callbacks : Arc< tokio::sync::Mutex< Vec< Box< dyn Fn( StreamState, StreamState ) + Send + Sync > > > >,
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl StreamControl
  {
    /// Create a new stream control instance
    #[ inline ]
    #[ must_use ]
    pub fn new() -> Self
    {
      Self
      {
        state : Arc::new( tokio::sync::RwLock::new( StreamState::Ready ) ),
        metrics : Arc::new( tokio::sync::Mutex::new( StreamMetrics::new() ) ),
        timeout : None,
        cancellation_token : Arc::new( tokio::sync::Mutex::new( None ) ),
        state_callbacks : Arc::new( tokio::sync::Mutex::new( Vec::new() ) ),
      }
    }

    /// Create a new stream control instance with timeout
    #[ inline ]
    #[ must_use ]
    pub fn with_timeout( timeout : Duration ) -> Self
    {
      Self
      {
        state : Arc::new( tokio::sync::RwLock::new( StreamState::Ready ) ),
        metrics : Arc::new( tokio::sync::Mutex::new( StreamMetrics::new() ) ),
        timeout : Some( timeout ),
        cancellation_token : Arc::new( tokio::sync::Mutex::new( None ) ),
        state_callbacks : Arc::new( tokio::sync::Mutex::new( Vec::new() ) ),
      }
    }

    /// Get current stream state
    #[ inline ]
    pub async fn state( &self ) -> StreamState
    {
      *self.state.read().await
    }

    /// Check if stream is paused
    #[ inline ]
    pub async fn is_paused( &self ) -> bool
    {
      *self.state.read().await == StreamState::Paused
    }

    /// Check if stream is cancelled
    #[ inline ]
    pub async fn is_cancelled( &self ) -> bool
    {
      *self.state.read().await == StreamState::Cancelled
    }

    /// Start the stream
    #[ inline ]
    pub async fn start( &self ) -> Result< (), StreamControlError >
    {
      let mut state = self.state.write().await;

      if *state != StreamState::Ready
      {
        return Err( StreamControlError::InvalidStateTransition {
          from : *state,
          to : StreamState::Streaming,
        } );
      }

      let old_state = *state;
      *state = StreamState::Streaming;

      // Initialize cancellation token
      {
        let mut token = self.cancellation_token.lock().await;
        *token = Some( tokio_util::sync::CancellationToken::new() );
      }

      drop( state );
      self.notify_state_change( old_state, StreamState::Streaming ).await;

      Ok( () )
    }

    /// Pause the stream
    #[ inline ]
    pub async fn pause( &self ) -> Result< (), StreamControlError >
    {
      let mut state = self.state.write().await;

      if *state != StreamState::Streaming
      {
        return Err( StreamControlError::InvalidStateTransition {
          from : *state,
          to : StreamState::Paused,
        } );
      }

      let old_state = *state;
      *state = StreamState::Paused;

      // Record pause in metrics
      {
        let mut metrics = self.metrics.lock().await;
        metrics.record_pause();
      }

      // Start timeout if configured
      if let Some( timeout ) = self.timeout
      {
        let control_clone = self.clone();
        tokio ::spawn( async move
        {
          tokio ::time::sleep( timeout ).await;
          let current_state = control_clone.state().await;
          if current_state == StreamState::Paused
          {
            let _ = control_clone.cancel().await;
          }
        } );
      }

      drop( state );
      self.notify_state_change( old_state, StreamState::Paused ).await;

      Ok( () )
    }

    /// Resume the stream
    #[ inline ]
    pub async fn resume( &self ) -> Result< (), StreamControlError >
    {
      let mut state = self.state.write().await;

      match *state
      {
        StreamState::Paused =>
        {
          let old_state = *state;
          *state = StreamState::Streaming;

          // Record resume in metrics
          {
            let mut metrics = self.metrics.lock().await;
            metrics.record_resume();
          }

          drop( state );
          self.notify_state_change( old_state, StreamState::Streaming ).await;
          Ok( () )
        },
        StreamState::Cancelled =>
        {
          Err( StreamControlError::StreamCancelled )
        },
        _ =>
        {
          Err( StreamControlError::InvalidStateTransition {
            from : *state,
            to : StreamState::Streaming,
          } )
        }
      }
    }

    /// Cancel the stream
    #[ inline ]
    pub async fn cancel( &self ) -> Result< (), StreamControlError >
    {
      let mut state = self.state.write().await;
      let old_state = *state;

      if *state == StreamState::Cancelled
      {
        return Ok( () );
      }

      *state = StreamState::Cancelled;

      // Signal cancellation
      {
        let token_guard = self.cancellation_token.lock().await;
        if let Some( token ) = token_guard.as_ref()
        {
          token.cancel();
        }
      }

      drop( state );
      self.notify_state_change( old_state, StreamState::Cancelled ).await;

      Ok( () )
    }

    /// Trigger cleanup when stream is cancelled
    #[ inline ]
    pub async fn cleanup_on_cancel( &self, buffer : &StreamBuffer ) -> Result< (), StreamControlError >
    {
      if self.is_cancelled().await
      {
        buffer.clear().await;
      }
      Ok( () )
    }

    /// Get stream metrics
    #[ inline ]
    pub async fn get_metrics( &self ) -> StreamMetrics
    {
      self.metrics.lock().await.clone()
    }

    /// Register callback for state changes
    #[ inline ]
    pub async fn on_state_change< F >( &self, callback : F )
    where
      F: Fn( StreamState, StreamState ) + Send + Sync + 'static,
    {
      let mut callbacks = self.state_callbacks.lock().await;
      callbacks.push( Box::new( callback ) );
    }

    /// Notify all callbacks of state change
    async fn notify_state_change( &self, old_state : StreamState, new_state : StreamState )
    {
      let callbacks = self.state_callbacks.lock().await;
      for callback in callbacks.iter()
      {
        callback( old_state, new_state );
      }
    }

    /// Get cancellation token
    #[ inline ]
    pub async fn cancellation_token( &self ) -> Option< tokio_util::sync::CancellationToken >
    {
      self.cancellation_token.lock().await.clone()
    }
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl core::fmt::Debug for StreamControl
  {
    #[ inline ]
    fn fmt( &self, f : &mut core::fmt::Formatter< '_ > ) -> core::fmt::Result
    {
      f.debug_struct( "StreamControl" )
        .field( "timeout", &self.timeout )
        .finish()
    }
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl Clone for StreamControl
  {
    #[ inline ]
    fn clone( &self ) -> Self
    {
      Self
      {
        state : self.state.clone(),
        metrics : self.metrics.clone(),
        timeout : self.timeout,
        cancellation_token : self.cancellation_token.clone(),
        state_callbacks : self.state_callbacks.clone(),
      }
    }
  }

  #[ cfg( feature = "streaming_control" ) ]
  impl Default for StreamControl
  {
    #[ inline ]
    fn default() -> Self
    {
      Self::new()
    }
  }

  /// Wrapper for streams with control capabilities
  #[ cfg( all( feature = "streaming", feature = "streaming_control" ) ) ]
  pub struct ControlledStream< T >
  {
    /// The underlying stream
    #[ allow( dead_code ) ]
    stream : std::pin::Pin< Box< dyn futures_core::Stream< Item = T > + Send > >,
    /// Control interface
    control : StreamControl,
    /// Buffer for pause/resume functionality
    #[ allow( dead_code ) ]
    buffer : StreamBuffer,
  }

  #[ cfg( all( feature = "streaming", feature = "streaming_control" ) ) ]
  impl< T > core::fmt::Debug for ControlledStream< T >
  {
    #[ inline ]
    fn fmt( &self, f : &mut core::fmt::Formatter< '_ > ) -> core::fmt::Result
    {
      f.debug_struct( "ControlledStream" )
        .field( "control", &self.control )
        .field( "buffer", &self.buffer )
        .finish()
    }
  }

  #[ cfg( all( feature = "streaming", feature = "streaming_control" ) ) ]
  impl< T > ControlledStream< T >
  {
    /// Create a new controlled stream
    #[ inline ]
    #[ must_use ]
    pub fn new(
      stream : std::pin::Pin< Box< dyn futures_core::Stream< Item = T > + Send > >,
      control : StreamControl
    ) -> Self
    {
      Self
      {
        stream,
        control,
        buffer : StreamBuffer::new( 1024 * 1024 ), // 1MB default buffer
      }
    }

    /// Get reference to the control interface
    #[ inline ]
    pub fn control( &self ) -> &StreamControl
    {
      &self.control
    }

    /// Check if stream is paused (synchronous)
    #[ inline ]
    pub fn is_paused_sync( &self ) -> bool
    {
      // This is a simplified check - in real implementation would need async
      false
    }

    /// Check if stream is cancelled (synchronous)
    #[ inline ]
    pub fn is_cancelled_sync( &self ) -> bool
    {
      // This is a simplified check - in real implementation would need async
      false
    }
  }

}

#[ cfg( feature = "streaming_control" ) ]
crate ::mod_interface!
{
  exposed use
  {
    StreamState,
    StreamControlError,
    StreamMetrics,
    StreamBuffer,
    StreamControl,
    ControlledStream,
  };
}