-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathstreaming.rs
More file actions
858 lines (744 loc) · 23.3 KB
/
streaming.rs
File metadata and controls
858 lines (744 loc) · 23.3 KB
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Streaming utilities.
//!
//! Provides utilities for handling streaming responses from LLMs
//! including delta processing, buffering, and event handling.
//!
//! All functions use safe array access with `.get()` and proper error handling
//! to prevent index out of bounds panics on malformed API responses.
use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use futures::Stream;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
/// Maximum number of events to buffer before dropping old ones.
/// Prevents unbounded memory growth if drain_events() is not called regularly.
const MAX_BUFFER_SIZE: usize = 10_000;
/// Token usage for streaming.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StreamTokenUsage {
/// Prompt tokens.
pub prompt_tokens: u32,
/// Completion tokens.
pub completion_tokens: u32,
/// Total tokens.
pub total_tokens: u32,
}
impl From<crate::client::TokenUsage> for StreamTokenUsage {
fn from(usage: crate::client::TokenUsage) -> Self {
Self {
prompt_tokens: usage.input_tokens as u32,
completion_tokens: usage.output_tokens as u32,
total_tokens: usage.total_tokens as u32,
}
}
}
/// Stream event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum StreamEvent {
/// Stream started.
Start,
/// Text delta (simple variant).
TextDelta { delta: String },
/// Text delta (shorthand).
Delta(String),
/// Reasoning delta.
Reasoning(String),
/// Tool call started.
ToolCallStart { id: String, name: String },
/// Tool call delta.
ToolCallDelta { id: String, arguments: String },
/// Tool call complete.
ToolCallComplete { id: String },
/// Tool call with full info.
ToolCall {
id: String,
name: String,
arguments: serde_json::Value,
},
/// Token usage update.
TokenUsage { prompt: u32, completion: u32 },
/// Stream complete (simple).
Complete,
/// Stream done with full info.
Done {
content: String,
reasoning: String,
tokens: Option<StreamTokenUsage>,
},
/// Error occurred (struct variant).
ErrorInfo { message: String },
/// Error occurred (tuple variant for convenience).
Error(String),
/// Empty response (no choices in API response).
/// This handles malformed/truncated API responses gracefully instead of panicking.
EmptyResponse { message: String },
}
/// Stream state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum StreamState {
/// Not started.
#[default]
Idle,
/// Streaming text.
StreamingText,
/// Streaming tool call.
StreamingToolCall,
/// Complete.
Complete,
/// Error.
Error,
}
/// Accumulated stream content.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StreamContent {
/// Accumulated text.
pub text: String,
/// Tool calls.
pub tool_calls: Vec<StreamToolCall>,
/// Token counts.
pub tokens: TokenCounts,
}
impl StreamContent {
/// Create new empty content.
pub fn new() -> Self {
Self::default()
}
/// Append text delta.
pub fn append_text(&mut self, delta: &str) {
self.text.push_str(delta);
}
/// Start a tool call.
pub fn start_tool_call(&mut self, id: &str, name: &str) {
self.tool_calls.push(StreamToolCall {
id: id.to_string(),
name: name.to_string(),
arguments: String::new(),
complete: false,
});
}
/// Append tool call arguments.
pub fn append_tool_call(&mut self, id: &str, arguments: &str) {
if let Some(tc) = self.tool_calls.iter_mut().find(|tc| tc.id == id) {
tc.arguments.push_str(arguments);
}
}
/// Complete a tool call.
pub fn complete_tool_call(&mut self, id: &str) {
if let Some(tc) = self.tool_calls.iter_mut().find(|tc| tc.id == id) {
tc.complete = true;
}
}
/// Check if has content.
pub fn has_content(&self) -> bool {
!self.text.is_empty() || !self.tool_calls.is_empty()
}
}
/// Stream tool call.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamToolCall {
/// Call ID.
pub id: String,
/// Tool name.
pub name: String,
/// Arguments (may be incomplete JSON).
pub arguments: String,
/// Is complete.
pub complete: bool,
}
impl StreamToolCall {
/// Try to parse arguments as JSON.
pub fn parse_arguments<T: for<'de> Deserialize<'de>>(&self) -> Option<T> {
if self.complete {
serde_json::from_str(&self.arguments).ok()
} else {
None
}
}
}
/// Token counts.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TokenCounts {
/// Prompt tokens.
pub prompt: u32,
/// Completion tokens.
pub completion: u32,
}
impl TokenCounts {
/// Total tokens.
pub fn total(&self) -> u32 {
self.prompt + self.completion
}
}
/// Stream processor.
pub struct StreamProcessor {
/// Current state.
state: StreamState,
/// Accumulated content.
content: StreamContent,
/// Event buffer.
buffer: VecDeque<StreamEvent>,
/// Start time.
start_time: Option<Instant>,
/// First token time.
first_token_time: Option<Instant>,
/// Last event time.
last_event_time: Option<Instant>,
/// Event count.
event_count: u64,
}
impl StreamProcessor {
/// Create a new stream processor.
pub fn new() -> Self {
Self {
state: StreamState::Idle,
content: StreamContent::new(),
buffer: VecDeque::with_capacity(1024), // Pre-allocate reasonable capacity
start_time: None,
first_token_time: None,
last_event_time: None,
event_count: 0,
}
}
/// Process an event.
pub fn process(&mut self, event: StreamEvent) {
let now = Instant::now();
if self.start_time.is_none() {
self.start_time = Some(now);
}
self.last_event_time = Some(now);
self.event_count += 1;
match &event {
StreamEvent::Start => {
self.state = StreamState::StreamingText;
}
StreamEvent::TextDelta { delta } => {
if self.first_token_time.is_none() {
self.first_token_time = Some(now);
}
self.content.append_text(delta);
self.state = StreamState::StreamingText;
}
StreamEvent::ToolCallStart { id, name } => {
self.content.start_tool_call(id, name);
self.state = StreamState::StreamingToolCall;
}
StreamEvent::ToolCallDelta { id, arguments } => {
self.content.append_tool_call(id, arguments);
}
StreamEvent::ToolCallComplete { id } => {
self.content.complete_tool_call(id);
}
StreamEvent::TokenUsage { prompt, completion } => {
self.content.tokens.prompt = *prompt;
self.content.tokens.completion = *completion;
}
StreamEvent::Complete => {
self.state = StreamState::Complete;
}
StreamEvent::Done { .. } => {
self.state = StreamState::Complete;
}
StreamEvent::Error(_)
| StreamEvent::ErrorInfo { .. }
| StreamEvent::EmptyResponse { .. } => {
self.state = StreamState::Error;
}
StreamEvent::Delta(delta) => {
if self.first_token_time.is_none() {
self.first_token_time = Some(now);
}
self.content.append_text(delta);
self.state = StreamState::StreamingText;
}
StreamEvent::Reasoning(_) => {
// Reasoning is tracked separately if needed
}
StreamEvent::ToolCall { id, name, .. } => {
self.content.start_tool_call(id, name);
self.state = StreamState::StreamingToolCall;
}
}
// Enforce buffer size limit to prevent unbounded memory growth
if self.buffer.len() >= MAX_BUFFER_SIZE {
self.buffer.pop_front();
}
self.buffer.push_back(event);
}
/// Get current state.
pub fn state(&self) -> StreamState {
self.state
}
/// Get accumulated content.
pub fn content(&self) -> &StreamContent {
&self.content
}
/// Take accumulated content.
pub fn take_content(self) -> StreamContent {
self.content
}
/// Get time to first token.
pub fn time_to_first_token(&self) -> Option<Duration> {
match (self.start_time, self.first_token_time) {
(Some(start), Some(first)) => Some(first.duration_since(start)),
_ => None,
}
}
/// Get total elapsed time.
pub fn elapsed(&self) -> Option<Duration> {
self.start_time.map(|s| s.elapsed())
}
/// Get event count.
pub fn event_count(&self) -> u64 {
self.event_count
}
/// Check if complete.
pub fn is_complete(&self) -> bool {
self.state == StreamState::Complete || self.state == StreamState::Error
}
/// Drain buffered events.
pub fn drain_events(&mut self) -> Vec<StreamEvent> {
self.buffer.drain(..).collect()
}
/// Get streaming stats.
pub fn stats(&self) -> StreamStats {
StreamStats {
state: self.state,
event_count: self.event_count,
text_length: self.content.text.len(),
tool_call_count: self.content.tool_calls.len(),
time_to_first_token: self.time_to_first_token(),
total_time: self.elapsed(),
tokens: self.content.tokens.clone(),
}
}
}
impl Default for StreamProcessor {
fn default() -> Self {
Self::new()
}
}
/// Streaming statistics.
#[derive(Debug, Clone, Serialize)]
pub struct StreamStats {
/// Current state.
pub state: StreamState,
/// Event count.
pub event_count: u64,
/// Text length.
pub text_length: usize,
/// Tool call count.
pub tool_call_count: usize,
/// Time to first token.
pub time_to_first_token: Option<Duration>,
/// Total time.
pub total_time: Option<Duration>,
/// Token counts.
pub tokens: TokenCounts,
}
/// Stream buffer for rate limiting output.
pub struct StreamBuffer {
/// Buffer.
buffer: String,
/// Minimum flush interval.
min_interval: Duration,
/// Last flush time.
last_flush: Instant,
}
impl StreamBuffer {
/// Create a new buffer.
pub fn new(min_interval: Duration) -> Self {
Self {
buffer: String::new(),
min_interval,
last_flush: Instant::now(),
}
}
/// Add content to buffer.
pub fn push(&mut self, text: &str) {
self.buffer.push_str(text);
}
/// Flush if ready.
pub fn flush_if_ready(&mut self) -> Option<String> {
if self.last_flush.elapsed() >= self.min_interval && !self.buffer.is_empty() {
self.last_flush = Instant::now();
Some(std::mem::take(&mut self.buffer))
} else {
None
}
}
/// Force flush.
pub fn flush(&mut self) -> String {
self.last_flush = Instant::now();
std::mem::take(&mut self.buffer)
}
/// Check if empty.
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
}
/// UTF-8 safe byte buffer for streaming that handles multi-byte characters
/// split across network packet boundaries (#2196).
///
/// This buffer accumulates raw bytes and only emits complete UTF-8 characters,
/// holding back incomplete multi-byte sequences until more data arrives.
pub struct Utf8StreamBuffer {
/// Accumulated bytes that may include incomplete UTF-8 sequences.
buffer: Vec<u8>,
}
impl Utf8StreamBuffer {
/// Create a new UTF-8 stream buffer.
pub fn new() -> Self {
Self { buffer: Vec::new() }
}
/// Push raw bytes into the buffer and return any complete UTF-8 string.
///
/// Incomplete multi-byte sequences at the end are held in the buffer
/// until more data arrives.
pub fn push(&mut self, data: &[u8]) -> Option<String> {
self.buffer.extend_from_slice(data);
self.extract_complete_utf8()
}
/// Extract complete UTF-8 characters from the buffer.
fn extract_complete_utf8(&mut self) -> Option<String> {
if self.buffer.is_empty() {
return None;
}
// Find the last valid UTF-8 boundary
let valid_len = self.find_valid_utf8_boundary();
if valid_len == 0 {
return None;
}
// Extract the valid portion
let valid_bytes: Vec<u8> = self.buffer.drain(..valid_len).collect();
// This should always succeed since we found the valid boundary
match String::from_utf8(valid_bytes) {
Ok(s) => Some(s),
Err(_) => None, // Should not happen
}
}
/// Find the position up to which the buffer contains valid UTF-8.
/// Returns the number of bytes that form complete characters.
fn find_valid_utf8_boundary(&self) -> usize {
let len = self.buffer.len();
// Try the full buffer first
if std::str::from_utf8(&self.buffer).is_ok() {
return len;
}
// Check trailing bytes that might be incomplete multi-byte sequences
// UTF-8 encoding:
// - 1 byte: 0xxxxxxx
// - 2 bytes: 110xxxxx 10xxxxxx
// - 3 bytes: 1110xxxx 10xxxxxx 10xxxxxx
// - 4 bytes: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
// Check last 1-3 bytes for incomplete sequences
for trailing in 1..=3.min(len) {
let boundary = len - trailing;
if std::str::from_utf8(&self.buffer[..boundary]).is_ok() {
// Check if the remaining bytes look like an incomplete sequence
let remaining = &self.buffer[boundary..];
if Self::is_incomplete_utf8_start(remaining) {
return boundary;
}
}
}
// Fallback: return what we can parse
for i in (0..len).rev() {
if std::str::from_utf8(&self.buffer[..i]).is_ok() {
return i;
}
}
0
}
/// Check if bytes look like the start of an incomplete UTF-8 sequence.
fn is_incomplete_utf8_start(bytes: &[u8]) -> bool {
if bytes.is_empty() {
return false;
}
let first = bytes[0];
// Check for multi-byte sequence start
if first & 0b11100000 == 0b11000000 {
// 2-byte sequence, need 2 bytes
return bytes.len() < 2;
} else if first & 0b11110000 == 0b11100000 {
// 3-byte sequence, need 3 bytes
return bytes.len() < 3;
} else if first & 0b11111000 == 0b11110000 {
// 4-byte sequence, need 4 bytes
return bytes.len() < 4;
}
false
}
/// Flush any remaining bytes, replacing invalid sequences with replacement character.
pub fn flush(&mut self) -> String {
if self.buffer.is_empty() {
return String::new();
}
let bytes = std::mem::take(&mut self.buffer);
String::from_utf8_lossy(&bytes).into_owned()
}
/// Check if the buffer is empty.
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
/// Get the number of pending bytes.
pub fn pending_bytes(&self) -> usize {
self.buffer.len()
}
}
impl Default for Utf8StreamBuffer {
fn default() -> Self {
Self::new()
}
}
/// Word boundary buffer for clean output.
pub struct WordBuffer {
/// Buffer.
buffer: String,
/// Minimum words before flush.
min_words: usize,
}
impl WordBuffer {
/// Create a new word buffer.
pub fn new(min_words: usize) -> Self {
Self {
buffer: String::new(),
min_words,
}
}
/// Add content.
pub fn push(&mut self, text: &str) {
self.buffer.push_str(text);
}
/// Try to flush complete words.
pub fn flush_words(&mut self) -> Option<String> {
let word_count = self.buffer.split_whitespace().count();
if word_count >= self.min_words {
// Find last word boundary
if let Some(pos) = self.buffer.rfind(|c: char| c.is_whitespace()) {
let result = self.buffer[..=pos].to_string();
self.buffer = self.buffer[pos + 1..].to_string();
return Some(result);
}
}
None
}
/// Force flush all.
pub fn flush(&mut self) -> String {
std::mem::take(&mut self.buffer)
}
}
/// Sentence buffer for sentence-aligned output.
pub struct SentenceBuffer {
/// Buffer.
buffer: String,
}
impl SentenceBuffer {
/// Create a new sentence buffer.
pub fn new() -> Self {
Self {
buffer: String::new(),
}
}
/// Add content.
pub fn push(&mut self, text: &str) {
self.buffer.push_str(text);
}
/// Try to flush complete sentences.
pub fn flush_sentences(&mut self) -> Option<String> {
// Find sentence ending
let endings = [". ", "! ", "? ", ".\n", "!\n", "?\n"];
let mut last_end = 0;
for ending in &endings {
if let Some(pos) = self.buffer.rfind(ending) {
let end = pos + ending.len();
if end > last_end {
last_end = end;
}
}
}
if last_end > 0 {
let result = self.buffer[..last_end].to_string();
self.buffer = self.buffer[last_end..].to_string();
Some(result)
} else {
None
}
}
/// Force flush all.
pub fn flush(&mut self) -> String {
std::mem::take(&mut self.buffer)
}
}
impl Default for SentenceBuffer {
fn default() -> Self {
Self::new()
}
}
/// Async stream wrapper.
pub struct EventStream {
receiver: mpsc::Receiver<StreamEvent>,
}
impl EventStream {
/// Create a new event stream.
pub fn new(receiver: mpsc::Receiver<StreamEvent>) -> Self {
Self { receiver }
}
/// Create a channel pair.
pub fn channel(buffer: usize) -> (mpsc::Sender<StreamEvent>, Self) {
let (tx, rx) = mpsc::channel(buffer);
(tx, Self::new(rx))
}
}
impl Stream for EventStream {
type Item = StreamEvent;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.receiver).poll_recv(cx)
}
}
/// Stream consumer that collects all content.
pub struct StreamCollector {
processor: StreamProcessor,
}
impl StreamCollector {
/// Create a new collector.
pub fn new() -> Self {
Self {
processor: StreamProcessor::new(),
}
}
/// Process an event.
pub fn process(&mut self, event: StreamEvent) {
self.processor.process(event);
}
/// Check if complete.
pub fn is_complete(&self) -> bool {
self.processor.is_complete()
}
/// Get result.
pub fn result(&self) -> CollectorResult {
CollectorResult {
text: self.processor.content.text.clone(),
tool_calls: self.processor.content.tool_calls.clone(),
tokens: self.processor.content.tokens.clone(),
stats: self.processor.stats(),
}
}
/// Take final result.
pub fn take_result(self) -> CollectorResult {
CollectorResult {
text: self.processor.content.text.clone(),
tool_calls: self.processor.content.tool_calls.clone(),
tokens: self.processor.content.tokens.clone(),
stats: self.processor.stats(),
}
}
}
impl Default for StreamCollector {
fn default() -> Self {
Self::new()
}
}
/// Collector result.
#[derive(Debug, Clone, Serialize)]
pub struct CollectorResult {
/// Collected text.
pub text: String,
/// Tool calls.
pub tool_calls: Vec<StreamToolCall>,
/// Token counts.
pub tokens: TokenCounts,
/// Stats.
pub stats: StreamStats,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stream_processor() {
let mut processor = StreamProcessor::new();
processor.process(StreamEvent::Start);
assert_eq!(processor.state(), StreamState::StreamingText);
processor.process(StreamEvent::TextDelta {
delta: "Hello ".to_string(),
});
processor.process(StreamEvent::TextDelta {
delta: "world!".to_string(),
});
assert_eq!(processor.content().text, "Hello world!");
processor.process(StreamEvent::Complete);
assert!(processor.is_complete());
}
#[test]
fn test_tool_call_streaming() {
let mut processor = StreamProcessor::new();
processor.process(StreamEvent::ToolCallStart {
id: "tc1".to_string(),
name: "read_file".to_string(),
});
processor.process(StreamEvent::ToolCallDelta {
id: "tc1".to_string(),
arguments: r#"{"path":"#.to_string(),
});
processor.process(StreamEvent::ToolCallDelta {
id: "tc1".to_string(),
arguments: r#""/test"}"#.to_string(),
});
processor.process(StreamEvent::ToolCallComplete {
id: "tc1".to_string(),
});
let content = processor.content();
assert_eq!(content.tool_calls.len(), 1);
assert!(content.tool_calls[0].complete);
assert_eq!(content.tool_calls[0].arguments, r#"{"path":"/test"}"#);
}
#[test]
fn test_stream_buffer() {
let mut buffer = StreamBuffer::new(Duration::from_millis(10));
buffer.push("Hello ");
buffer.push("world!");
let flushed = buffer.flush();
assert_eq!(flushed, "Hello world!");
assert!(buffer.is_empty());
}
#[test]
fn test_word_buffer() {
let mut buffer = WordBuffer::new(3);
buffer.push("Hello world this is ");
buffer.push("a test");
if let Some(words) = buffer.flush_words() {
assert!(words.split_whitespace().count() >= 3);
}
}
#[test]
fn test_sentence_buffer() {
let mut buffer = SentenceBuffer::new();
buffer.push("Hello world. This is a test. More");
if let Some(sentences) = buffer.flush_sentences() {
assert!(sentences.contains("Hello world."));
assert!(sentences.contains("This is a test."));
}
let remaining = buffer.flush();
assert_eq!(remaining, "More");
}
#[test]
fn test_stream_collector() {
let mut collector = StreamCollector::new();
collector.process(StreamEvent::Start);
collector.process(StreamEvent::TextDelta {
delta: "Test".to_string(),
});
collector.process(StreamEvent::TokenUsage {
prompt: 10,
completion: 5,
});
collector.process(StreamEvent::Complete);
let result = collector.result();
assert_eq!(result.text, "Test");
assert_eq!(result.tokens.total(), 15);
}
}