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: OpenRouter’s /chat/completions endpoint uses the standard OpenAI
message schema; role distinguishes speaker identity and content carries
the text payload (or null for tool-call-only turns).
What: role is one of "system", "user", "assistant", "tool";
content is the text or None for assistant turns that only emit tool
calls; tool_calls carries the outbound calls for assistant turns;
tool_call_id and name are populated for tool role messages;
cache_control (#2156) is a prompt-cache breakpoint applied at
serialisation time — see Self::serialize for the wire-shape switch it
drives.
Test: chat_message_serialises_all_roles.
Fields§
§role: StringConversation role: "system", "user", "assistant", or "tool".
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 role messages: the ID of the tool call this message responds to.
name: Option<String>For tool role messages: the name of the function that was called.
cache_control: Option<CacheControl>Prompt-cache breakpoint (#2156). Never read back from the wire (this
type is request-only — see the module doc), so it is dropped on
deserialise via #[serde(skip)] rather than round-tripped.
agent_loop::build_request sets this on the system-role message when
the run is cache-eligible; see Self::serialize for how it changes
content’s wire shape.
Implementations§
Source§impl ChatMessage
impl ChatMessage
Sourcepub fn system(content: impl Into<String>) -> Self
pub fn system(content: impl Into<String>) -> Self
Construct a system message.
Why: Convenience constructor; avoids repeated role.to_string() boilerplate.
What: Returns a ChatMessage with role = "system" and the provided content.
Test: chat_message_constructors.
Sourcepub fn user(content: impl Into<String>) -> Self
pub fn user(content: impl Into<String>) -> Self
Construct a user message.
Why: Convenience constructor for the most common message type.
What: Returns a ChatMessage with role = "user" and the provided content.
Test: chat_message_constructors.
Sourcepub fn assistant(content: impl Into<String>) -> Self
pub fn assistant(content: impl Into<String>) -> Self
Construct an assistant message with text content.
Why: Convenience constructor for building conversation history from prior
assistant responses.
What: Returns a ChatMessage with role = "assistant" and the provided content.
Test: chat_message_constructors.
Sourcepub fn tool_result(
tool_call_id: impl Into<String>,
name: impl Into<String>,
content: impl Into<String>,
) -> Self
pub fn tool_result( tool_call_id: impl Into<String>, name: impl Into<String>, content: impl Into<String>, ) -> Self
Construct a tool result message.
Why: After executing a tool call, the result must be fed back in a tool
role message referencing the original tool_call_id.
What: Returns a ChatMessage with role = "tool", the call ID, function
name, and result content.
Test: chat_message_tool_role_round_trip.
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<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for ChatMessage
impl PartialEq for ChatMessage
Source§fn eq(&self, other: &ChatMessage) -> bool
fn eq(&self, other: &ChatMessage) -> bool
self and other values to be equal, and is used by ==.Source§impl Serialize for ChatMessage
impl Serialize for ChatMessage
Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialise ChatMessage, converting content into a one-element
cache-annotated content-block array when cache_control is set
(#2156), and a bare string (today’s shape) otherwise.
Why: OpenRouter’s Claude cache_control passthrough only honours the
breakpoint when content is shaped as
[{"type":"text","text":...,"cache_control":{"type":"ephemeral"}}] —
a plain string content value is never inspected for a cache marker
(verified against OpenRouter’s prompt-caching docs and block/goose’s
production OpenRouter provider). Hand-writing this impl — rather than
deriving — keeps content: Option<String> the type every other call
site (Transcript, tests) already depends on; only the wire encoding
branches on cache_control.
What: role always serialises. content serialises as the cached
block array when both content and cache_control are Some, as a
bare string when only content is Some, and is omitted entirely
when content is None — matching the previous
skip_serializing_if = "Option::is_none" behaviour byte-for-byte when
cache_control is None. tool_calls, tool_call_id, and name
serialise unchanged; cache_control itself never appears as its own
JSON key.
Test: chat_message_cache_control_serialises_as_content_block,
chat_message_without_cache_control_serialises_plain_string,
chat_message_serialises_all_roles.
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