pub struct MessageStream<'a, P: EventProducer<StateT>, StateT: AgentState = JsonValue> { /* private fields */ }Expand description
Helper for streaming a text message piece by piece.
This struct manages the lifecycle of a streaming text message, automatically generating message IDs and emitting the appropriate events.
§Example
ⓘ
let msg = MessageStream::start(&producer).await?;
msg.content("Hello, ").await?;
msg.content("world!").await?;
let message_id = msg.end().await?;Implementations§
Source§impl<'a, P: EventProducer<StateT>, StateT: AgentState> MessageStream<'a, P, StateT>
impl<'a, P: EventProducer<StateT>, StateT: AgentState> MessageStream<'a, P, StateT>
Sourcepub async fn start(producer: &'a P) -> Result<Self, ServerError>
pub async fn start(producer: &'a P) -> Result<Self, ServerError>
Start a new message stream.
Emits a TextMessageStart event with a randomly generated message ID.
Sourcepub async fn start_with_id(
producer: &'a P,
message_id: MessageId,
) -> Result<Self, ServerError>
pub async fn start_with_id( producer: &'a P, message_id: MessageId, ) -> Result<Self, ServerError>
Start a new message stream with a specific message ID.
Sourcepub async fn content(&self, delta: impl Into<String>) -> Result<(), ServerError>
pub async fn content(&self, delta: impl Into<String>) -> Result<(), ServerError>
Append content to the message.
Emits a TextMessageContent event with the given delta.
Empty deltas are silently ignored.
Sourcepub async fn end(self) -> Result<MessageId, ServerError>
pub async fn end(self) -> Result<MessageId, ServerError>
End the message stream.
Emits a TextMessageEnd event and returns the message ID.
Consumes the stream to prevent further content being added.
Sourcepub fn message_id(&self) -> &MessageId
pub fn message_id(&self) -> &MessageId
Get the message ID for this stream.
Auto Trait Implementations§
impl<'a, P, StateT> Freeze for MessageStream<'a, P, StateT>
impl<'a, P, StateT> RefUnwindSafe for MessageStream<'a, P, StateT>where
P: RefUnwindSafe,
StateT: RefUnwindSafe,
impl<'a, P, StateT> Send for MessageStream<'a, P, StateT>
impl<'a, P, StateT> Sync for MessageStream<'a, P, StateT>
impl<'a, P, StateT> Unpin for MessageStream<'a, P, StateT>where
StateT: Unpin,
impl<'a, P, StateT> UnsafeUnpin for MessageStream<'a, P, StateT>
impl<'a, P, StateT> UnwindSafe for MessageStream<'a, P, StateT>where
P: RefUnwindSafe,
StateT: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more