Skip to main content

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

id
String
Unique identifier for the message, automatically generated as a UUID
role
Type
The role of the message sender (Assistant or User)
content
String
The text content of the message. For images, this contains the image URL
content_type
ContentType
The type of content (Text or Image)
image_url
Option<String>
Optional URL of the image if the message contains image content
base64_data
Option<String>
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

Assistant
Message sent by the AI assistant
User
Message sent by the user
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

Text
Message contains text content
Image
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
role
Type
required
The role of the message sender (Assistant or User)
content
String
required
The text content of the message
return
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
role
Type
required
The role of the message sender (Assistant or User)
url
String
required
The URL of the generated image
base64
Option<String>
Optional base64-encoded image data
return
Message
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
id
String
required
The specific ID for the message (useful for reconstructing conversations)
role
Type
required
The role of the message sender (Assistant or User)
content
String
required
The text content of the message
return
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());
    }
}

Build docs developers (and LLMs) love