rsllm 0.1.0

Rust-native LLM client library with multi-provider support and streaming capabilities
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
//! # RSLLM Streaming Support
//! 
//! Streaming response handling with proper async Stream traits.
//! Supports real-time token streaming with backpressure and error handling.

use crate::{RsllmError, RsllmResult, StreamChunk, ChatResponse, CompletionResponse};
use futures_util::Stream;
use pin_project_lite::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_util::Future;

/// Type alias for chat streaming responses
pub type ChatStream = Pin<Box<dyn Stream<Item = RsllmResult<StreamChunk>> + Send>>;

/// Type alias for completion streaming responses  
pub type CompletionStream = Pin<Box<dyn Stream<Item = RsllmResult<StreamChunk>> + Send>>;

/// Stream collector for assembling complete responses from chunks
pin_project! {
    pub struct StreamCollector<S> {
        #[pin]
        stream: S,
        accumulated_content: String,
        model: Option<String>,
        finish_reason: Option<String>,
        usage: Option<crate::Usage>,
        metadata: std::collections::HashMap<String, serde_json::Value>,
        tool_calls: Vec<crate::ToolCall>,
        is_done: bool,
    }
}

impl<S> StreamCollector<S>
where
    S: Stream<Item = RsllmResult<StreamChunk>>,
{
    /// Create a new stream collector
    pub fn new(stream: S) -> Self {
        Self {
            stream,
            accumulated_content: String::new(),
            model: None,
            finish_reason: None,
            usage: None,
            metadata: std::collections::HashMap::new(),
            tool_calls: Vec::new(),
            is_done: false,
        }
    }
    
    /// Collect all chunks into a complete chat response
    pub async fn collect_chat_response(mut self) -> RsllmResult<ChatResponse>
    where
        S: Unpin,
    {
        use futures_util::StreamExt;
        while let Some(chunk_result) = self.next().await {
            let _chunk = chunk_result?;
            // Process chunk - this updates internal state
        }
        
        let model = self.model.unwrap_or_else(|| "unknown".to_string());
        
        let mut response = ChatResponse::new(self.accumulated_content, model);
        
        if let Some(reason) = self.finish_reason {
            response = response.with_finish_reason(reason);
        }
        
        if let Some(usage) = self.usage {
            response = response.with_usage(usage);
        }
        
        if !self.tool_calls.is_empty() {
            response = response.with_tool_calls(self.tool_calls);
        }
        
        for (key, value) in self.metadata {
            response = response.with_metadata(key, value);
        }
        
        Ok(response)
    }
    
    /// Collect all chunks into a complete completion response
    pub async fn collect_completion_response(mut self) -> RsllmResult<CompletionResponse>
    where
        S: Unpin,
    {
        use futures_util::StreamExt;
        while let Some(chunk_result) = self.next().await {
            let _chunk = chunk_result?;
            // Process chunk - this updates internal state
        }
        
        let model = self.model.unwrap_or_else(|| "unknown".to_string());
        
        let mut response = CompletionResponse::new(self.accumulated_content, model);
        
        if let Some(reason) = self.finish_reason {
            response = response.with_finish_reason(reason);
        }
        
        if let Some(usage) = self.usage {
            response = response.with_usage(usage);
        }
        
        for (key, value) in self.metadata {
            response = response.with_metadata(key, value);
        }
        
        Ok(response)
    }
}

impl<S> Stream for StreamCollector<S>
where
    S: Stream<Item = RsllmResult<StreamChunk>>,
{
    type Item = RsllmResult<StreamChunk>;
    
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        
        if *this.is_done {
            return Poll::Ready(None);
        }
        
        match this.stream.as_mut().poll_next(cx) {
            Poll::Ready(Some(Ok(chunk))) => {
                // Update accumulated state
                if chunk.has_content() {
                    this.accumulated_content.push_str(&chunk.content);
                }
                
                if this.model.is_none() && !chunk.model.is_empty() {
                    *this.model = Some(chunk.model.clone());
                }
                
                if let Some(reason) = &chunk.finish_reason {
                    *this.finish_reason = Some(reason.clone());
                }
                
                if let Some(usage) = &chunk.usage {
                    *this.usage = Some(usage.clone());
                }
                
                // Merge metadata
                for (key, value) in &chunk.metadata {
                    this.metadata.insert(key.clone(), value.clone());
                }
                
                // Handle tool calls delta (simplified - would need proper delta merging)
                if let Some(_tool_calls_delta) = &chunk.tool_calls_delta {
                    // TODO: Implement proper tool call delta merging
                }
                
                if chunk.is_done {
                    *this.is_done = true;
                }
                
                Poll::Ready(Some(Ok(chunk)))
            }
            Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
            Poll::Ready(None) => {
                *this.is_done = true;
                Poll::Ready(None)
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

/// Stream adapter for rate limiting
pin_project! {
    pub struct RateLimitedStream<S> {
        #[pin]
        stream: S,
        delay: std::time::Duration,
        last_emit: Option<std::time::Instant>,
    }
}

impl<S> RateLimitedStream<S> {
    /// Create a new rate-limited stream
    pub fn new(stream: S, max_chunks_per_second: f64) -> Self {
        let delay = std::time::Duration::from_secs_f64(1.0 / max_chunks_per_second);
        Self {
            stream,
            delay,
            last_emit: None,
        }
    }
}

impl<S> Stream for RateLimitedStream<S>
where
    S: Stream<Item = RsllmResult<StreamChunk>>,
{
    type Item = S::Item;
    
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        
        // Check if we need to delay
        if let Some(last) = this.last_emit {
            let elapsed = last.elapsed();
            if elapsed < *this.delay {
                let remaining = *this.delay - elapsed;
                
                // Set up a timer for the remaining delay
                let sleep = tokio::time::sleep(remaining);
                tokio::pin!(sleep);
                
                if sleep.as_mut().poll(cx).is_pending() {
                    return Poll::Pending;
                }
            }
        }
        
        match this.stream.as_mut().poll_next(cx) {
            Poll::Ready(Some(item)) => {
                *this.last_emit = Some(std::time::Instant::now());
                Poll::Ready(Some(item))
            }
            other => other,
        }
    }
}

/// Stream adapter for filtering chunks
pin_project! {
    pub struct FilteredStream<S, F> {
        #[pin]
        stream: S,
        filter: F,
    }
}

impl<S, F> FilteredStream<S, F>
where
    F: Fn(&StreamChunk) -> bool,
{
    /// Create a new filtered stream
    pub fn new(stream: S, filter: F) -> Self {
        Self { stream, filter }
    }
}

impl<S, F> Stream for FilteredStream<S, F>
where
    S: Stream<Item = RsllmResult<StreamChunk>>,
    F: Fn(&StreamChunk) -> bool,
{
    type Item = S::Item;
    
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        
        loop {
            match this.stream.as_mut().poll_next(cx) {
                Poll::Ready(Some(Ok(chunk))) => {
                    if (this.filter)(&chunk) {
                        return Poll::Ready(Some(Ok(chunk)));
                    }
                    // Continue polling if chunk was filtered out
                }
                other => return other,
            }
        }
    }
}

/// Stream adapter for mapping chunks
pin_project! {
    pub struct MappedStream<S, F> {
        #[pin]
        stream: S,
        mapper: F,
    }
}

impl<S, F> MappedStream<S, F>
where
    F: Fn(StreamChunk) -> StreamChunk,
{
    /// Create a new mapped stream
    pub fn new(stream: S, mapper: F) -> Self {
        Self { stream, mapper }
    }
}

impl<S, F> Stream for MappedStream<S, F>
where
    S: Stream<Item = RsllmResult<StreamChunk>>,
    F: Fn(StreamChunk) -> StreamChunk,
{
    type Item = S::Item;
    
    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut this = self.project();
        
        match this.stream.as_mut().poll_next(cx) {
            Poll::Ready(Some(Ok(chunk))) => {
                let mapped = (this.mapper)(chunk);
                Poll::Ready(Some(Ok(mapped)))
            }
            other => other,
        }
    }
}

/// Stream utilities
pub struct StreamUtils;

impl StreamUtils {
    /// Convert a vector of chunks into a stream
    pub fn from_chunks(chunks: Vec<StreamChunk>) -> ChatStream {
        let stream = tokio_stream::iter(chunks.into_iter().map(Ok));
        Box::pin(stream)
    }
    
    /// Create an empty stream
    pub fn empty() -> ChatStream {
        let stream = tokio_stream::empty();
        Box::pin(stream)
    }
    
    /// Create a stream that immediately returns an error
    pub fn error(error: RsllmError) -> ChatStream {
        use futures_util::stream;
        let stream = stream::once(async move { Err(error) });
        Box::pin(stream)
    }
    
    /// Collect stream into a vector of chunks
    pub async fn collect_chunks<S>(stream: S) -> RsllmResult<Vec<StreamChunk>>
    where
        S: Stream<Item = RsllmResult<StreamChunk>>,
    {
        tokio_stream::StreamExt::collect::<Vec<_>>(stream)
            .await
            .into_iter()
            .collect::<RsllmResult<Vec<_>>>()
    }
    
    /// Take only the first N chunks from a stream
    pub fn take<S>(stream: S, n: usize) -> impl Stream<Item = RsllmResult<StreamChunk>>
    where
        S: Stream<Item = RsllmResult<StreamChunk>>,
    {
        tokio_stream::StreamExt::take(stream, n)
    }
    
    /// Skip the first N chunks from a stream
    pub fn skip<S>(stream: S, n: usize) -> impl Stream<Item = RsllmResult<StreamChunk>>
    where
        S: Stream<Item = RsllmResult<StreamChunk>>,
    {
        tokio_stream::StreamExt::skip(stream, n)
    }
    
    /// Filter chunks based on a predicate
    pub fn filter<S, F>(stream: S, filter: F) -> FilteredStream<S, F>
    where
        S: Stream<Item = RsllmResult<StreamChunk>>,
        F: Fn(&StreamChunk) -> bool,
    {
        FilteredStream::new(stream, filter)
    }
    
    /// Map chunks with a function
    pub fn map<S, F>(stream: S, mapper: F) -> MappedStream<S, F>
    where
        S: Stream<Item = RsllmResult<StreamChunk>>,
        F: Fn(StreamChunk) -> StreamChunk,
    {
        MappedStream::new(stream, mapper)
    }
    
    /// Rate limit a stream
    pub fn rate_limit<S>(stream: S, max_chunks_per_second: f64) -> RateLimitedStream<S>
    where
        S: Stream<Item = RsllmResult<StreamChunk>>,
    {
        RateLimitedStream::new(stream, max_chunks_per_second)
    }
    
    /// Buffer chunks to reduce API calls (simplified implementation)
    pub async fn buffer<S>(
        mut stream: S,
        max_size: usize,
    ) -> RsllmResult<Vec<StreamChunk>>
    where
        S: Stream<Item = RsllmResult<StreamChunk>> + Unpin,
    {
        let mut chunks = Vec::new();
        let mut count = 0;
        
        use futures_util::StreamExt;
        while let Some(chunk) = stream.next().await {
            chunks.push(chunk?);
            count += 1;
            
            if count >= max_size {
                break;
            }
        }
        
        Ok(chunks)
    }
}

/// Stream extension traits for additional functionality
pub trait RsllmStreamExt: Stream<Item = RsllmResult<StreamChunk>> + Sized {
    /// Collect stream into a complete chat response
    fn collect_chat_response(self) -> impl std::future::Future<Output = RsllmResult<ChatResponse>> + Send
    where
        Self: Send + Unpin,
    {
        StreamCollector::new(self).collect_chat_response()
    }
    
    /// Collect stream into a complete completion response
    fn collect_completion_response(self) -> impl std::future::Future<Output = RsllmResult<CompletionResponse>> + Send
    where
        Self: Send + Unpin,
    {
        StreamCollector::new(self).collect_completion_response()
    }
    
    /// Filter chunks that have content
    fn content_only(self) -> FilteredStream<Self, fn(&StreamChunk) -> bool> {
        FilteredStream::new(self, |chunk| chunk.has_content())
    }
    
    /// Filter out done chunks
    fn exclude_done(self) -> FilteredStream<Self, fn(&StreamChunk) -> bool> {
        FilteredStream::new(self, |chunk| !chunk.is_done)
    }
    
    /// Rate limit the stream
    fn rate_limit(self, max_chunks_per_second: f64) -> RateLimitedStream<Self> {
        RateLimitedStream::new(self, max_chunks_per_second)
    }
}

impl<S> RsllmStreamExt for S where S: Stream<Item = RsllmResult<StreamChunk>> {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{MessageRole, StreamChunk};
    
    #[tokio::test]
    async fn test_stream_collector() {
        let chunks = vec![
            StreamChunk::delta("Hello", "gpt-4").with_role(MessageRole::Assistant),
            StreamChunk::delta(" world", "gpt-4"),
            StreamChunk::done("gpt-4").with_finish_reason("stop"),
        ];
        
        let stream = StreamUtils::from_chunks(chunks);
        let response = stream.collect_chat_response().await.unwrap();
        
        assert_eq!(response.content, "Hello world");
        assert_eq!(response.model, "gpt-4");
        assert_eq!(response.finish_reason, Some("stop".to_string()));
    }
    
    #[tokio::test]
    async fn test_filter_stream() {
        let chunks = vec![
            StreamChunk::delta("Hello", "gpt-4"),
            StreamChunk::new("", "gpt-4", false, false), // Empty chunk
            StreamChunk::delta(" world", "gpt-4"),
        ];
        
        let stream = StreamUtils::from_chunks(chunks);
        use futures_util::StreamExt;
        let mut filtered_stream = stream.content_only();
        let mut filtered_chunks = Vec::new();
        while let Some(chunk) = filtered_stream.next().await {
            filtered_chunks.push(chunk.unwrap());
        }
        
        assert_eq!(filtered_chunks.len(), 2);
        assert_eq!(filtered_chunks[0].content, "Hello");
        assert_eq!(filtered_chunks[1].content, " world");
    }
    
    #[tokio::test]
    async fn test_map_stream() {
        let chunks = vec![
            StreamChunk::delta("hello", "gpt-4"),
            StreamChunk::delta(" world", "gpt-4"),
        ];
        
        let stream = StreamUtils::from_chunks(chunks);
        let mapped_stream = StreamUtils::map(stream, |mut chunk| {
            chunk.content = chunk.content.to_uppercase();
            chunk
        });
        
        let collected = StreamUtils::collect_chunks(mapped_stream).await.unwrap();
        
        assert_eq!(collected[0].content, "HELLO");
        assert_eq!(collected[1].content, " WORLD");
    }
}