pub struct Message {
pub role: Role,
pub parts: Vec<MessagePart>,
}Expand description
A message in the conversation.
Messages are the fundamental unit of communication between users,
agents, and tools. Each message has a Role and a list of
MessageParts. The parts is a Vec<MessagePart> because
a single assistant response can contain both text and tool-call
parts.
§Construction
Use the user and assistant
convenience constructors for simple text messages, or new
for messages with multiple parts.
§Example
use loopctl::message::{Message, Role};
let msg = Message::user("Hello, agent!");
assert_eq!(msg.role, Role::User);
assert_eq!(msg.parts.len(), 1);Fields§
§role: RoleWho sent this message.
Determines whether the message came from the User
or the Assistant. Tool-result messages are
typically sent with Role::User per convention.
parts: Vec<MessagePart>The parts in this message.
A message can contain multiple parts of different types — for
example, an assistant response might include a Text
part followed by a ToolCall block.
An empty vector is valid and represents a message with no content.
Implementations§
Source§impl Message
impl Message
Sourcepub fn user(text: impl Into<String>) -> Self
pub fn user(text: impl Into<String>) -> Self
Create a user message from plain text.
Convenience constructor that wraps the text in a single
MessagePart::Text part with Role::User.
§Arguments
text— The message text. Accepts any type that implementsInto<String>(e.g.&str,String).
§Returns
A Message with role Role::User and one text part.
§Example
use loopctl::message::{Message, Role};
let msg = Message::user("What is the weather today?");
assert_eq!(msg.role, Role::User);Sourcepub fn assistant(text: impl Into<String>) -> Self
pub fn assistant(text: impl Into<String>) -> Self
Create an assistant message from plain text.
Convenience constructor that wraps the text in a single
MessagePart::Text part with Role::Assistant.
§Arguments
text— The message text. Accepts any type that implementsInto<String>(e.g.&str,String).
§Returns
A Message with role Role::Assistant and one text part.
§Example
use loopctl::message::{Message, Role};
let msg = Message::assistant("The weather is sunny.");
assert_eq!(msg.role, Role::Assistant);Sourcepub const fn new(role: Role, parts: Vec<MessagePart>) -> Self
pub const fn new(role: Role, parts: Vec<MessagePart>) -> Self
Create a message with the given role and parts.
Use this constructor when you need multiple parts (e.g. text + tool-call in the same message).
§Arguments
role— TheRoleof the message sender.parts— A vector ofMessageParts.
§Returns
A Message with the specified role and content.
§Example
use loopctl::message::{Message, MessagePart, Role};
let msg = Message::new(Role::Assistant, vec![
MessagePart::text("Let me look that up."),
MessagePart::tool_call("t1", "search", serde_json::json!({"q": "weather"})),
]);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
impl<'de> Deserialize<'de> for Message
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 Display for Message
Formats a Message for display.
impl Display for Message
Formats a Message for display.
Formats each MessagePart in the message on its own line.
Text parts render as-is; tool-call parts render as
[Tool: {name} with input: {input}]; tool-result parts render
as [Tool Result: {content}]; image parts render as
[Image: {media_type}].
For structured serialization, use serde_json::to_string instead.
§Example
use loopctl::message::{Message};
let msg = Message::user("Hello");
println!("{msg}"); // prints "Hello"