Skip to main content

ai_agents_core/
message.rs

1//! Core message types for AI Agents framework
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum Role {
9    System,
10    User,
11    Assistant,
12    Tool,
13    Function,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct ChatMessage {
18    pub role: Role,
19    pub content: String,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub name: Option<String>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub timestamp: Option<DateTime<Utc>>,
24}
25
26impl ChatMessage {
27    pub fn system(content: impl Into<String>) -> Self {
28        Self {
29            role: Role::System,
30            content: content.into(),
31            name: None,
32            timestamp: Some(Utc::now()),
33        }
34    }
35
36    pub fn user(content: impl Into<String>) -> Self {
37        Self {
38            role: Role::User,
39            content: content.into(),
40            name: None,
41            timestamp: Some(Utc::now()),
42        }
43    }
44
45    pub fn assistant(content: impl Into<String>) -> Self {
46        Self {
47            role: Role::Assistant,
48            content: content.into(),
49            name: None,
50            timestamp: Some(Utc::now()),
51        }
52    }
53
54    pub fn tool(name: impl Into<String>, content: impl Into<String>) -> Self {
55        Self {
56            role: Role::Tool,
57            content: content.into(),
58            name: Some(name.into()),
59            timestamp: Some(Utc::now()),
60        }
61    }
62
63    pub fn function(name: impl Into<String>, content: impl Into<String>) -> Self {
64        Self {
65            role: Role::Function,
66            content: content.into(),
67            name: Some(name.into()),
68            timestamp: Some(Utc::now()),
69        }
70    }
71}