Message

Struct Message 

Source
pub struct Message {
    pub id: Uuid,
    pub conversation_id: Uuid,
    pub role: MessageRole,
    pub content: String,
    pub metadata: HashMap<String, Value>,
    pub timestamp: DateTime<Utc>,
    pub tool_calls: SmallVec<[ToolCall; 2]>,
    pub tool_call_id: Option<String>,
    pub name: Option<String>,
}
Expand description

A single message in a conversation.

Messages have a role (system, user, assistant, or tool), content, and optional metadata. Tool calls are validated to only appear on assistant messages.

Fields§

§id: Uuid

Unique identifier for this message.

§conversation_id: Uuid

ID of the conversation this message belongs to.

§role: MessageRole

The role of the message sender.

§content: String

The text content of the message.

§metadata: HashMap<String, Value>

Application-specific metadata.

§timestamp: DateTime<Utc>

When this message was created.

§tool_calls: SmallVec<[ToolCall; 2]>

Tool calls requested by this message (assistant messages only, uses SmallVec to avoid allocations for ≤2 calls).

§tool_call_id: Option<String>

Tool call ID this message responds to (required for tool messages).

§name: Option<String>

Function name (required for tool messages).

Implementations§

Source§

impl Message

Source

pub fn builder() -> MessageBuilder<((), (), (), (), (), (), (), (), ())>

Create a builder for building Message. On the builder, call .id(...)(optional), .conversation_id(...), .role(...), .content(...), .metadata(...)(optional), .timestamp(...)(optional), .tool_calls(...)(optional), .tool_call_id(...)(optional), .name(...)(optional) to set the values of the fields. Finally, call .build() to create the instance of Message.

Source§

impl Message

Source

pub fn new( conversation_id: Uuid, role: MessageRole, content: impl Into<String>, ) -> Self

Creates a new message with the specified role and content.

Source

pub fn system(conversation_id: Uuid, content: impl Into<String>) -> Self

Creates a new system message.

Source

pub fn user(conversation_id: Uuid, content: impl Into<String>) -> Self

Creates a new user message.

Source

pub fn assistant(conversation_id: Uuid, content: impl Into<String>) -> Self

Creates a new assistant message.

Source

pub fn tool( conversation_id: Uuid, content: impl Into<String>, tool_call_id: String, function_name: String, ) -> Result<Self>

Creates a new tool result message.

§Arguments
  • conversation_id - The conversation this message belongs to
  • content - The result/output from the tool execution
  • tool_call_id - The ID of the tool call this responds to
  • function_name - The name of the function that was called
§Errors

Returns an error if the tool_call_id is empty.

Source

pub fn with_metadata(self, key: impl Into<String>, value: Value) -> Self

Adds a metadata key-value pair to this message.

Source

pub fn with_metadata_typed<T: Serialize>( self, key: impl Into<String>, value: T, ) -> Result<Self>

Adds a metadata key-value pair to this message with automatic serialization.

This is a convenience method that accepts any serializable type.

Source

pub fn with_tool_calls( self, tool_calls: impl Into<SmallVec<[ToolCall; 2]>>, ) -> Result<Self>

Sets the tool calls for this message.

§Errors

Returns an error if this message is not an assistant message.

Source

pub fn add_tool_call(&mut self, tool_call: ToolCall) -> Result<()>

Adds a single tool call to this message.

§Errors

Returns an error if this message is not an assistant message.

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

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

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

impl<'de> Deserialize<'de> for Message

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 Serialize for Message

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,