Skip to main content

ChatMessage

Struct ChatMessage 

Source
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: String

Conversation 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

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> ChatMessage

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ChatMessage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ChatMessage

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for ChatMessage

Source§

fn eq(&self, other: &ChatMessage) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for ChatMessage

Source§

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.

Source§

impl StructuralPartialEq for ChatMessage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more