pub struct ChatMessage {
pub role: String,
pub content: Option<String>,
pub tool_calls: Option<Vec<ToolCall>>,
pub tool_call_id: Option<String>,
pub name: Option<String>,
pub cache_control: Option<CacheControl>,
}Expand description
A single message in the chat conversation.
Why: the OpenAI/OpenRouter /chat/completions schema distinguishes speaker
identity by role and carries the text payload in content (or null for
tool-call-only assistant turns).
What: role is one of "system"/"user"/"assistant"/"tool"; content
is the text or None; tool_calls carries outbound calls on assistant
turns; tool_call_id/name are populated for tool messages;
cache_control is a prompt-cache breakpoint applied at serialisation time.
Test: serialises_all_roles, cache_control_serialises_as_block.
Fields§
§role: StringConversation role.
content: Option<String>Text content; None on assistant turns that only emit tool calls.
tool_calls: Option<Vec<ToolCall>>Tool calls emitted by the assistant.
tool_call_id: Option<String>For tool messages: the id of the tool call this responds to.
name: Option<String>For tool messages: the name of the function that was called.
cache_control: Option<CacheControl>Prompt-cache breakpoint. Request-only, so dropped on deserialise via
#[serde(skip)]; see Self::serialize for its wire effect.
Implementations§
Source§impl ChatMessage
impl ChatMessage
Sourcepub fn system(content: impl Into<String>) -> ChatMessage
pub fn system(content: impl Into<String>) -> ChatMessage
Construct a system message.
Why: convenience constructor avoiding role.into() boilerplate.
What: role = "system", provided content, all optionals None.
Test: constructors_set_role.
Sourcepub fn user(content: impl Into<String>) -> ChatMessage
pub fn user(content: impl Into<String>) -> ChatMessage
Construct a user message.
Why: convenience constructor for the most common message type.
What: role = "user", provided content.
Test: constructors_set_role.
Sourcepub fn assistant(content: impl Into<String>) -> ChatMessage
pub fn assistant(content: impl Into<String>) -> ChatMessage
Construct an assistant message with text content.
Why: convenience constructor for building history from prior responses.
What: role = "assistant", provided content.
Test: constructors_set_role.
Sourcepub fn tool_result(
tool_call_id: impl Into<String>,
name: impl Into<String>,
content: impl Into<String>,
) -> ChatMessage
pub fn tool_result( tool_call_id: impl Into<String>, name: impl Into<String>, content: impl Into<String>, ) -> ChatMessage
Construct a tool result message.
Why: after executing a tool call, the result is fed back in a tool
message referencing the original tool_call_id.
What: role = "tool" with the call id, function name, and result content.
Test: tool_result_fields.
Trait Implementations§
Source§impl Clone for ChatMessage
impl Clone for ChatMessage
Source§fn clone(&self) -> ChatMessage
fn clone(&self) -> ChatMessage
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ChatMessage
impl Debug for ChatMessage
Source§impl<'de> Deserialize<'de> for ChatMessage
impl<'de> Deserialize<'de> for ChatMessage
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<ChatMessage, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<ChatMessage, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for ChatMessage
impl PartialEq for ChatMessage
Source§impl Serialize for ChatMessage
impl Serialize for ChatMessage
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serialise, converting content into a one-element cache-annotated block
array when cache_control is set, and a bare string otherwise.
Why: the caching passthrough only honours the breakpoint when content
is shaped as [{"type":"text","text":...,"cache_control":{...}}] — a
plain string is never inspected. Hand-writing this keeps content: Option<String> for every caller while branching only the wire encoding.
What: content serialises as the block array when both content and
cache_control are Some, a bare string when only content is Some,
and is omitted when content is None. Other fields serialise as their
Option::is_none-skipped selves; cache_control never appears as its
own key.
Test: cache_control_serialises_as_block, plain_content_when_no_cache_control.
impl StructuralPartialEq for ChatMessage
Auto Trait Implementations§
impl Freeze for ChatMessage
impl RefUnwindSafe for ChatMessage
impl Send for ChatMessage
impl Sync for ChatMessage
impl Unpin for ChatMessage
impl UnsafeUnpin for ChatMessage
impl UnwindSafe for ChatMessage
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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