Overview
The Message struct represents a message in a conversation with the t3.chat API. It supports both text and image content types.
Struct Definition
#[derive(Clone, Debug)]
pub struct Message {
pub id: String,
pub role: Type,
pub content: String,
pub content_type: ContentType,
pub image_url: Option<String>,
pub base64_data: Option<String>,
}
Fields
Unique identifier for the message, automatically generated as a UUID
The role of the message sender (Assistant or User)
The text content of the message. For images, this contains the image URL
The type of content (Text or Image)
Optional URL of the image if the message contains image content
Optional base64-encoded image data for images
Type Enum
Represents the role type in a message.
#[derive(Clone, Debug)]
pub enum Type {
Assistant,
User,
}
Variants
Message sent by the AI assistant
Example:
use t3router::t3::message::{Message, Type};
let user_msg = Message::new(Type::User, "Hello!".to_string());
let assistant_msg = Message::new(Type::Assistant, "Hi there!".to_string());
ContentType Enum
Represents the content type of a message.
#[derive(Clone, Debug)]
pub enum ContentType {
Text,
Image,
}
Variants
Message contains text content
Message contains image content
Example:
use t3router::t3::message::{Message, ContentType};
match message.content_type {
ContentType::Text => println!("Text: {}", message.content),
ContentType::Image => println!("Image URL: {:?}", message.image_url),
}
Constructors
new
Creates a new text Message with a randomly generated ID.
pub fn new(role: Type, content: String) -> Self
The role of the message sender (Assistant or User)
The text content of the message
A new text message instance with auto-generated UUID
Example:
use t3router::t3::message::{Message, Type};
let msg = Message::new(
Type::User,
"What is the weather today?".to_string()
);
println!("Message ID: {}", msg.id);
println!("Content: {}", msg.content);
new_image
Creates a new image Message with a randomly generated ID.
pub fn new_image(role: Type, url: String, base64: Option<String>) -> Self
The role of the message sender (Assistant or User)
The URL of the generated image
Optional base64-encoded image data
A new image message instance with auto-generated UUID
Example:
use t3router::t3::message::{Message, Type};
let image_msg = Message::new_image(
Type::Assistant,
"https://example.com/generated-image.png".to_string(),
Some("iVBORw0KGgo...".to_string())
);
println!("Image URL: {:?}", image_msg.image_url);
println!("Has base64 data: {}", image_msg.base64_data.is_some());
with_id
Creates a new Message with a specific ID.
pub fn with_id(id: String, role: Type, content: String) -> Self
The specific ID for the message (useful for reconstructing conversations)
The role of the message sender (Assistant or User)
The text content of the message
A new message instance with the provided ID
Example:
use t3router::t3::message::{Message, Type};
let msg = Message::with_id(
"custom-id-123".to_string(),
Type::User,
"Reconstructed message".to_string()
);
assert_eq!(msg.id, "custom-id-123");
Usage Examples
Creating a conversation
use t3router::t3::{client::Client, message::{Message, Type}};
let mut client = Client::new(
"cookies".to_string(),
"session_id".to_string()
);
// Create user message
let user_msg = Message::new(
Type::User,
"Tell me a joke".to_string()
);
// Send and get response
let response = client.send("gpt-4", Some(user_msg), None).await?;
// Check response type
match response.content_type {
ContentType::Text => {
println!("Assistant: {}", response.content);
},
ContentType::Image => {
println!("Image generated: {:?}", response.image_url);
}
}
Handling image messages
use t3router::t3::{client::Client, message::{Message, Type, ContentType}};
let response = client.send(
"dall-e-3",
Some(Message::new(Type::User, "Generate a sunset".to_string())),
None
).await?;
if matches!(response.content_type, ContentType::Image) {
if let Some(url) = response.image_url {
println!("Image URL: {}", url);
}
if let Some(base64) = response.base64_data {
println!("Base64 data available: {} bytes", base64.len());
}
}