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
//! Stream state management for Telex.
//!
//! Provides the `use_stream` hook for handling async streams (e.g., LLM token streaming).
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::Arc;
use std::thread;
/// Represents the state of a streaming operation.
#[derive(Clone, Debug)]
pub enum StreamState {
/// Stream not started yet.
Idle,
/// Stream is active and receiving data.
Streaming,
/// Stream completed successfully.
Done,
/// Stream encountered an error.
Error(String),
}
/// Handle for stream state that can be stored and cloned.
pub struct StreamHandle<T> {
inner: Rc<RefCell<StreamInner<T>>>,
}
struct StreamInner<T> {
/// Accumulated values from the stream.
accumulated: T,
/// Current state of the stream.
state: StreamState,
/// Whether the stream has been started.
started: bool,
/// Receiver for stream items.
receiver: Option<Receiver<StreamItem<T>>>,
/// Wake flag to notify the event loop when tokens arrive.
wake_flag: Option<Arc<AtomicBool>>,
}
/// An item received from the stream.
enum StreamItem<T> {
/// A value from the stream.
Value(T),
/// Stream completed.
Done,
/// Stream errored.
Error(String),
}
impl<T> Clone for StreamHandle<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Self {
inner: Rc::clone(&self.inner),
}
}
}
impl<T: Clone + Default + 'static> StreamHandle<T> {
/// Create a new stream handle with default accumulated value.
pub fn new() -> Self {
Self {
inner: Rc::new(RefCell::new(StreamInner {
accumulated: T::default(),
state: StreamState::Idle,
started: false,
receiver: None,
wake_flag: None,
})),
}
}
/// Create a new stream handle with an event-loop wake flag.
pub fn with_wake_flag(wake_flag: Arc<AtomicBool>) -> Self {
Self {
inner: Rc::new(RefCell::new(StreamInner {
accumulated: T::default(),
state: StreamState::Idle,
started: false,
receiver: None,
wake_flag: Some(wake_flag),
})),
}
}
/// Create a new stream handle with a specific initial value.
pub fn with_initial(initial: T) -> Self {
Self {
inner: Rc::new(RefCell::new(StreamInner {
accumulated: initial,
state: StreamState::Idle,
started: false,
receiver: None,
wake_flag: None,
})),
}
}
/// Get the current accumulated value.
pub fn get(&self) -> T {
self.inner.borrow().accumulated.clone()
}
/// Check if the stream is currently loading/streaming.
pub fn is_loading(&self) -> bool {
matches!(
self.inner.borrow().state,
StreamState::Idle | StreamState::Streaming
)
}
/// Check if the stream is actively receiving data.
pub fn is_streaming(&self) -> bool {
matches!(self.inner.borrow().state, StreamState::Streaming)
}
/// Check if the stream has completed.
pub fn is_done(&self) -> bool {
matches!(self.inner.borrow().state, StreamState::Done)
}
/// Check if the stream encountered an error.
pub fn is_error(&self) -> bool {
matches!(self.inner.borrow().state, StreamState::Error(_))
}
/// Get the error message if there was an error.
pub fn error(&self) -> Option<String> {
match &self.inner.borrow().state {
StreamState::Error(e) => Some(e.clone()),
_ => None,
}
}
/// Get the current stream state.
pub fn state(&self) -> StreamState {
self.inner.borrow().state.clone()
}
}
impl<T: Clone + Send + 'static> StreamHandle<T> {
/// Start the stream if not already started.
///
/// The `stream_fn` should be a function that returns an iterator.
/// Each item from the iterator will be sent through the channel.
pub fn start<F, I>(&self, stream_fn: F)
where
F: FnOnce() -> I + Send + 'static,
I: Iterator<Item = T> + Send + 'static,
{
let mut inner = self.inner.borrow_mut();
if inner.started {
return;
}
inner.started = true;
inner.state = StreamState::Streaming;
// Create channel for stream items
let (tx, rx): (Sender<StreamItem<T>>, Receiver<StreamItem<T>>) = mpsc::channel();
inner.receiver = Some(rx);
let wake_flag = inner.wake_flag.clone();
// Spawn thread to run the stream
thread::spawn(move || {
let iter = stream_fn();
for item in iter {
if tx.send(StreamItem::Value(item)).is_err() {
// Receiver dropped, stop streaming
return;
}
if let Some(ref flag) = wake_flag {
flag.store(true, Ordering::Release);
}
}
let _ = tx.send(StreamItem::Done);
if let Some(ref flag) = wake_flag {
flag.store(true, Ordering::Release);
}
});
}
/// Start the stream with error handling.
pub fn start_with_result<F, I>(&self, stream_fn: F)
where
F: FnOnce() -> Result<I, String> + Send + 'static,
I: Iterator<Item = T> + Send + 'static,
{
let mut inner = self.inner.borrow_mut();
if inner.started {
return;
}
inner.started = true;
inner.state = StreamState::Streaming;
let (tx, rx): (Sender<StreamItem<T>>, Receiver<StreamItem<T>>) = mpsc::channel();
inner.receiver = Some(rx);
let wake_flag = inner.wake_flag.clone();
thread::spawn(move || match stream_fn() {
Ok(iter) => {
for item in iter {
if tx.send(StreamItem::Value(item)).is_err() {
return;
}
if let Some(ref flag) = wake_flag {
flag.store(true, Ordering::Release);
}
}
let _ = tx.send(StreamItem::Done);
if let Some(ref flag) = wake_flag {
flag.store(true, Ordering::Release);
}
}
Err(e) => {
let _ = tx.send(StreamItem::Error(e));
if let Some(ref flag) = wake_flag {
flag.store(true, Ordering::Release);
}
}
});
}
/// Poll for new items and update accumulated value.
/// Returns true if there were updates.
pub fn poll(&self, accumulate: impl Fn(&mut T, T)) -> bool {
let mut inner = self.inner.borrow_mut();
let mut updated = false;
// Take receiver temporarily to avoid borrow conflicts
if let Some(receiver) = inner.receiver.take() {
// Drain all available items
let mut new_state = None;
loop {
match receiver.try_recv() {
Ok(StreamItem::Value(item)) => {
accumulate(&mut inner.accumulated, item);
updated = true;
}
Ok(StreamItem::Done) => {
new_state = Some(StreamState::Done);
break;
}
Ok(StreamItem::Error(e)) => {
new_state = Some(StreamState::Error(e));
break;
}
Err(mpsc::TryRecvError::Empty) => {
break;
}
Err(mpsc::TryRecvError::Disconnected) => {
if !matches!(inner.state, StreamState::Done | StreamState::Error(_)) {
new_state = Some(StreamState::Error(
"Stream disconnected unexpectedly".to_string(),
));
}
break;
}
}
}
// Put receiver back (unless stream is done)
if new_state.is_none() || matches!(new_state, Some(StreamState::Streaming)) {
inner.receiver = Some(receiver);
}
if let Some(state) = new_state {
inner.state = state;
}
}
updated
}
/// Reset the stream to allow restarting.
pub fn reset(&self)
where
T: Default,
{
let mut inner = self.inner.borrow_mut();
inner.accumulated = T::default();
inner.state = StreamState::Idle;
inner.started = false;
inner.receiver = None;
// wake_flag is preserved across resets
}
/// Reset the stream with a specific initial value.
pub fn reset_with(&self, initial: T) {
let mut inner = self.inner.borrow_mut();
inner.accumulated = initial;
inner.state = StreamState::Idle;
inner.started = false;
inner.receiver = None;
// wake_flag is preserved across resets
}
}
impl<T: Clone + Default + 'static> Default for StreamHandle<T> {
fn default() -> Self {
Self::new()
}
}
/// Convenience handle specifically for text streaming.
/// Automatically accumulates string tokens.
pub type TextStreamHandle = StreamHandle<String>;
impl TextStreamHandle {
/// Poll and accumulate text by concatenation.
pub fn poll_text(&self) -> bool {
self.poll(|acc, item| acc.push_str(&item))
}
}