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: UuidUnique identifier for this message.
conversation_id: UuidID of the conversation this message belongs to.
role: MessageRoleThe role of the message sender.
content: StringThe 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
impl Message
Sourcepub fn builder() -> MessageBuilder<((), (), (), (), (), (), (), (), ())>
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
impl Message
Sourcepub fn new(
conversation_id: Uuid,
role: MessageRole,
content: impl Into<String>,
) -> Self
pub fn new( conversation_id: Uuid, role: MessageRole, content: impl Into<String>, ) -> Self
Creates a new message with the specified role and content.
Sourcepub fn system(conversation_id: Uuid, content: impl Into<String>) -> Self
pub fn system(conversation_id: Uuid, content: impl Into<String>) -> Self
Creates a new system message.
Sourcepub fn user(conversation_id: Uuid, content: impl Into<String>) -> Self
pub fn user(conversation_id: Uuid, content: impl Into<String>) -> Self
Creates a new user message.
Sourcepub fn assistant(conversation_id: Uuid, content: impl Into<String>) -> Self
pub fn assistant(conversation_id: Uuid, content: impl Into<String>) -> Self
Creates a new assistant message.
Sourcepub fn tool(
conversation_id: Uuid,
content: impl Into<String>,
tool_call_id: String,
function_name: String,
) -> Result<Self>
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 tocontent- The result/output from the tool executiontool_call_id- The ID of the tool call this responds tofunction_name- The name of the function that was called
§Errors
Returns an error if the tool_call_id is empty.
Sourcepub fn with_metadata(self, key: impl Into<String>, value: Value) -> Self
pub fn with_metadata(self, key: impl Into<String>, value: Value) -> Self
Adds a metadata key-value pair to this message.
Sourcepub fn with_metadata_typed<T: Serialize>(
self,
key: impl Into<String>,
value: T,
) -> Result<Self>
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.