pub enum Message {
System(SystemMessage),
Assistant(AssistantMessage),
User(UserMessage),
Result(ResultMessage),
StreamEvent(StreamEvent),
}Expand description
Top-level message enum — the primary type consumers work with.
Every message emitted by the SDK during a session is one of these variants.
Consumers typically match on this enum to route messages to their
appropriate handlers.
§Example
match message {
Message::System(sys) => println!("session: {}", sys.session_id),
Message::Assistant(asst) => {
if let Some(text) = Message::Assistant(asst).assistant_text() {
println!("{}", text);
}
}
Message::Result(r) if r.is_error => eprintln!("error turn"),
Message::StreamEvent(ev) => { /* rich streaming data */ }
_ => {}
}Variants§
System(SystemMessage)
Synthesized initialization message, emitted once per session.
Assistant(AssistantMessage)
An assistant response containing one or more content blocks.
User(UserMessage)
A user message (present in session history / resume replay).
Result(ResultMessage)
Terminal message for a completed prompt turn.
StreamEvent(StreamEvent)
Rich streaming event for data that does not map to a content block (tool call lifecycle, plan updates, usage deltas, etc.).
Implementations§
Source§impl Message
impl Message
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Returns the session ID carried by this message, if present.
All current variants carry a session_id. This method returns
Some(&str) for every variant; the Option wrapper exists for
forward-compatibility with future variants that may omit it.
Sourcepub fn is_error_result(&self) -> bool
pub fn is_error_result(&self) -> bool
Returns true if this is a ResultMessage with is_error = true.
Sourcepub fn is_stream_event(&self) -> bool
pub fn is_stream_event(&self) -> bool
Returns true if this is a StreamEvent.
Sourcepub fn assistant_text(&self) -> Option<String>
pub fn assistant_text(&self) -> Option<String>
Extracts concatenated text from all ContentBlock::Text blocks in
an AssistantMessage. Returns None for any other variant, and
None when the assistant message contains no text blocks.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
impl UnwindSafe for Message
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more