use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
System,
#[default]
User,
Assistant,
Tool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Message {
role: Role,
content: String,
#[cfg(feature = "tools")]
#[serde(skip_serializing_if = "Option::is_none")]
tool_calls: Option<Vec<crate::tool::ToolCall>>,
#[cfg(feature = "tools")]
#[serde(skip_serializing_if = "Option::is_none")]
tool_call_id: Option<String>,
}
impl Message {
#[must_use]
pub fn new(role: Role, content: impl Into<String>) -> Self {
Self {
role,
content: content.into(),
#[cfg(feature = "tools")]
tool_calls: None,
#[cfg(feature = "tools")]
tool_call_id: None,
}
}
#[cfg(feature = "tools")]
#[must_use]
pub fn with_tool_calls(mut self, tool_calls: Vec<crate::tool::ToolCall>) -> Self {
self.tool_calls = Some(tool_calls);
self
}
#[cfg(feature = "tools")]
#[must_use]
pub fn with_tool_call_id(mut self, tool_call_id: impl Into<String>) -> Self {
self.tool_call_id = Some(tool_call_id.into());
self
}
#[cfg(feature = "tools")]
#[must_use]
pub fn tool_calls(&self) -> Option<&[crate::tool::ToolCall]> {
self.tool_calls.as_deref()
}
#[cfg(feature = "tools")]
#[must_use]
pub fn tool_call_id(&self) -> Option<&str> {
self.tool_call_id.as_deref()
}
#[must_use]
pub fn role(&self) -> &Role {
&self.role
}
#[must_use]
pub fn content(&self) -> &str {
&self.content
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_creation() {
let msg = Message::new(Role::User, "Hello");
assert_eq!(*msg.role(), Role::User);
assert_eq!(msg.content(), "Hello");
}
}