pub struct Message {
pub role: MessageRole,
pub content: Vec<ContentBlock>,
}Expand description
A complete message in a conversation.
Messages are the primary unit of communication in the agent system. Each message has a role (who sent it) and content (what it contains). Content is structured as a vector of blocks to support multi-modal communication.
§Structure
role: Who sent the message (MessageRole)content: What the message contains (one or moreContentBlocks)
§Message Patterns
§Simple Text Message
use open_agent::Message;
let msg = Message::user("What's the weather?");§Assistant Response with Tool Call
use open_agent::{Message, ContentBlock, TextBlock, ToolUseBlock};
use serde_json::json;
let msg = Message::assistant(vec![
ContentBlock::Text(TextBlock::new("Let me check that for you.")),
ContentBlock::ToolUse(ToolUseBlock::new(
"call_123",
"get_weather",
json!({"location": "San Francisco"})
))
]);§Tool Result
use open_agent::{Message, ContentBlock, ToolResultBlock};
use serde_json::json;
let msg = Message::user_with_blocks(vec![
ContentBlock::ToolResult(ToolResultBlock::new(
"call_123",
json!({"temp": 72, "conditions": "sunny"})
))
]);Fields§
§role: MessageRoleThe role/sender of this message.
content: Vec<ContentBlock>The content blocks that make up this message.
A message can contain multiple blocks of different types. For example, an assistant message might have both text and tool use blocks.
Implementations§
Source§impl Message
impl Message
Sourcepub fn new(role: MessageRole, content: Vec<ContentBlock>) -> Self
pub fn new(role: MessageRole, content: Vec<ContentBlock>) -> Self
Creates a new message with the specified role and content.
This is the most general constructor. For convenience, use the
role-specific constructors like user(),
assistant(), etc.
§Example
use open_agent::{Message, MessageRole, ContentBlock, TextBlock};
let msg = Message::new(
MessageRole::User,
vec![ContentBlock::Text(TextBlock::new("Hello"))]
);Sourcepub fn user(text: impl Into<String>) -> Self
pub fn user(text: impl Into<String>) -> Self
Creates a user message with simple text content.
This is the most common way to create user messages. For more complex
content with multiple blocks, use user_with_blocks().
§Example
use open_agent::Message;
let msg = Message::user("What is 2+2?");Sourcepub fn assistant(content: Vec<ContentBlock>) -> Self
pub fn assistant(content: Vec<ContentBlock>) -> Self
Creates an assistant message with the specified content blocks.
Assistant messages often contain multiple content blocks (text + tool use). This method takes a vector of blocks for maximum flexibility.
§Example
use open_agent::{Message, ContentBlock, TextBlock};
let msg = Message::assistant(vec![
ContentBlock::Text(TextBlock::new("The answer is 4"))
]);Sourcepub fn system(text: impl Into<String>) -> Self
pub fn system(text: impl Into<String>) -> Self
Creates a system message with simple text content.
System messages establish the agent’s behavior and context. They’re typically sent at the start of a conversation.
§Example
use open_agent::Message;
let msg = Message::system("You are a helpful assistant. Be concise.");Sourcepub fn user_with_blocks(content: Vec<ContentBlock>) -> Self
pub fn user_with_blocks(content: Vec<ContentBlock>) -> Self
Creates a user message with custom content blocks.
Use this when you need to send structured content beyond simple text,
such as tool results. For simple text messages, prefer
user().
§Example
use open_agent::{Message, ContentBlock, ToolResultBlock};
use serde_json::json;
let msg = Message::user_with_blocks(vec![
ContentBlock::ToolResult(ToolResultBlock::new(
"call_123",
json!({"result": "success"})
))
]);