use super::core::finish;
use super::{AgentError, AgentResult};
use crate::message::ToolCall;
use futures_util::Stream;
use std::pin::Pin;
use std::task::{Context, Poll};
#[derive(Debug)]
#[non_exhaustive]
pub enum AgentEvent<D> {
TextDelta(String),
ReasoningDelta(String),
ToolCall(ToolCall),
ToolResult {
call_id: String,
tool: String,
content: String,
is_error: bool,
},
Finished(AgentResult<D>),
}
#[must_use = "streams do nothing unless polled"]
pub struct AgentStream<D> {
inner: Pin<Box<dyn Stream<Item = Result<AgentEvent<D>, AgentError>> + Send>>,
}
impl<D> AgentStream<D> {
pub(super) fn new(
inner: impl Stream<Item = Result<AgentEvent<D>, AgentError>> + Send + 'static,
) -> Self {
Self {
inner: Box::pin(inner),
}
}
pub async fn finish(self) -> Result<AgentResult<D>, AgentError> {
finish(self).await
}
}
impl<D> Stream for AgentStream<D> {
type Item = Result<AgentEvent<D>, AgentError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.inner.as_mut().poll_next(cx)
}
}