pub enum MessagePart {
Text {
text: String,
},
Image {
source: ImageSource,
},
ToolCall {
id: String,
name: String,
input: Value,
},
ToolResult {
call_id: String,
output: ToolContent,
is_error: Option<bool>,
},
}Expand description
A part of message content within a Message.
Messages can contain multiple types of content interleaved in a single response: plain text, base64-encoded images, tool-call invocations by the assistant, and tool results. Each variant corresponds to a possible part type with tagged JSON serialization.
§Serialization
Uses #[serde(tag = "type")] with renamed variants to produce
JSON objects like {"type": "text", "text": "..."} matching the
loopctl message schema.
§Construction
Use the constructors text,
tool_call, and
tool_result rather than building
variants directly.
§Example
use loopctl::message::{MessagePart};
let text_part = MessagePart::text("Hello");
let tool_part = MessagePart::tool_call("id1", "search", serde_json::json!({"q": "test"}));
let result_part = MessagePart::tool_result("id1", "found 3 results", false);Variants§
Text
Plain text content.
Contains a UTF-8 text string generated by the model or provided by the user.
Fields
Image
A base64-encoded image part.
Images can be included in user messages for multimodal models.
The ImageSource contains the MIME type and base64 data.
Serialized as {"type":"image","source":{...}}.
Note: image parts are only valid in Role::User messages;
the API rejects them in assistant messages.
Fields
source: ImageSourceThe image data and metadata.
Contains the MIME type, encoding type, and base64-encoded
image bytes. See ImageSource for details.
ToolCall
A tool-call invocation by the assistant.
When the model decides to call a tool, it emits a part with
the tool’s ID, name, and JSON input. The framework then
executes the tool and sends the result back as a
ToolResult block.
The correlation between a ToolCall and its ToolResult is
done via the id field, which must be
unique within a single conversation turn.
Fields
id: StringUnique identifier for this tool invocation.
Assigned by the API. Used to correlate the tool-call part
with the corresponding ToolResult
part in the next message.
ToolResult
The result of a tool-call invocation.
Sent back to the model with Role::User (per API convention)
to provide the output of a tool execution. Contains the tool-call
ID for correlation, the output content, and an optional error flag.
After the agent executes a tool, it wraps the output in this variant and appends it to the conversation history so the model can reason about the output.
Fields
call_id: StringThe ID of the tool-call part this result corresponds to.
Must match the id from the original
ToolCall block.
output: ToolContentThe output returned by the tool.
Can be a simple string or a multipart response with
multiple parts. See ToolContent.
Implementations§
Source§impl MessagePart
impl MessagePart
Sourcepub fn text(text: impl Into<String>) -> Self
pub fn text(text: impl Into<String>) -> Self
Create a text part.
Constructor for the Text variant.
Accepts any type that implements Into<String>.
§Arguments
text— The text content.
§Returns
A MessagePart::Text variant.
§Example
use loopctl::message::{MessagePart};
let part = MessagePart::text("Hello, world!");
assert!(part.is_text());Sourcepub fn tool_call(
id: impl Into<String>,
name: impl Into<String>,
input: Value,
) -> Self
pub fn tool_call( id: impl Into<String>, name: impl Into<String>, input: Value, ) -> Self
Create a tool-call part.
Used when constructing an assistant message that invokes a tool.
The id must be unique within the conversation to allow
correlation with the corresponding tool result.
§Arguments
id— Unique identifier for this tool invocation.name— The name of the tool to invoke.input— The JSON input parameters for the tool.
§Returns
A MessagePart::ToolCall variant.
§Example
use loopctl::message::{MessagePart};
let part = MessagePart::tool_call(
"tool_abc",
"read_file",
serde_json::json!({"path": "/tmp/test.txt"}),
);
assert!(part.is_tool_call());Sourcepub fn tool_result(
call_id: impl Into<String>,
output: impl Into<ToolContent>,
is_error: bool,
) -> Self
pub fn tool_result( call_id: impl Into<String>, output: impl Into<ToolContent>, is_error: bool, ) -> Self
Create a tool-result part.
Used when sending the output of a tool execution back to the
model. The call_id must match the id from the original
ToolCall block.
§Arguments
call_id— The ID of the tool invocation this output is for.output— The tool output (string or multipart).is_error— Whether the tool invocation failed.
§Returns
A MessagePart::ToolResult variant.
§Example
use loopctl::message::{MessagePart};
let part = MessagePart::tool_result(
"tool_abc",
"file contents here",
false,
);
assert!(part.is_tool_result());Sourcepub const fn is_tool_call(&self) -> bool
pub const fn is_tool_call(&self) -> bool
Returns true if this is a ToolCall block.
Detects tool invocations in an assistant message to decide whether to enter the tool-execution loop.
§Example
use loopctl::message::{MessagePart};
use serde_json::Value;
let part = MessagePart::tool_call("id", "tool", Value::Null);
assert!(part.is_tool_call());Sourcepub const fn is_tool_result(&self) -> bool
pub const fn is_tool_result(&self) -> bool
Returns true if this is a ToolResult block.
Identifies tool results in a conversation history.
§Example
use loopctl::message::{MessagePart};
let part = MessagePart::tool_result("id", "output", false);
assert!(part.is_tool_result());Sourcepub fn as_text(&self) -> Option<&str>
pub fn as_text(&self) -> Option<&str>
Get the text content if this is a Text block.
Returns Some(&str) for text parts, None for all other
variants. Extracts the text from a known-text part.
§Returns
Some(text) if this is a text part, None otherwise.
§Example
use loopctl::message::{MessagePart};
use serde_json::Value;
let part = MessagePart::text("hello");
assert_eq!(part.as_text(), Some("hello"));
let tool = MessagePart::tool_call("id", "tool", Value::Null);
assert_eq!(tool.as_text(), None);Trait Implementations§
Source§impl Clone for MessagePart
impl Clone for MessagePart
Source§fn clone(&self) -> MessagePart
fn clone(&self) -> MessagePart
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more