aither_agent/stream.rs
1//! Streaming agent execution.
2//!
3//! Provides a `Stream` implementation for observing agent execution
4//! in real-time with true streaming of text chunks.
5
6use std::pin::Pin;
7use std::task::{Context, Poll};
8
9use crate::error::AgentError;
10use crate::event::AgentEvent;
11use futures_core::Stream;
12
13/// Stream of agent events during execution.
14///
15/// This stream yields `AgentEvent`s as they happen, including
16/// individual text chunks for real-time display.
17///
18/// # Example
19///
20/// ```rust,ignore
21/// use futures::StreamExt;
22///
23/// let mut stream = agent.run("Do something");
24/// while let Some(event) = stream.next().await {
25/// match event? {
26/// AgentEvent::Text(t) => print!("{}", t),
27/// AgentEvent::Complete { .. } => break,
28/// _ => {}
29/// }
30/// }
31/// ```
32pub struct AgentStream<S> {
33 inner: Pin<Box<S>>,
34}
35
36impl<S> AgentStream<S>
37where
38 S: Stream<Item = Result<AgentEvent, AgentError>>,
39{
40 /// Creates a new agent stream wrapping an inner stream.
41 pub fn new(inner: S) -> Self {
42 Self {
43 inner: Box::pin(inner),
44 }
45 }
46}
47
48impl<S> Stream for AgentStream<S>
49where
50 S: Stream<Item = Result<AgentEvent, AgentError>>,
51{
52 type Item = Result<AgentEvent, AgentError>;
53
54 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
55 self.inner.as_mut().poll_next(cx)
56 }
57}
58
59impl<S> std::fmt::Debug for AgentStream<S> {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 f.debug_struct("AgentStream").finish()
62 }
63}