use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize, Serializer};
use super::request::{CacheControl, ToolCall};
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct ChatMessage {
pub role: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip)]
pub cache_control: Option<CacheControl>,
}
impl Serialize for ChatMessage {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("ChatMessage", 5)?;
state.serialize_field("role", &self.role)?;
match (&self.content, &self.cache_control) {
(Some(text), Some(cache_control)) => {
let block = [CachedTextBlock {
kind: "text",
text: text.as_str(),
cache_control,
}];
state.serialize_field("content", &block)?;
}
(Some(text), None) => state.serialize_field("content", text)?,
(None, _) => state.skip_field("content")?,
}
match &self.tool_calls {
Some(calls) => state.serialize_field("tool_calls", calls)?,
None => state.skip_field("tool_calls")?,
}
match &self.tool_call_id {
Some(id) => state.serialize_field("tool_call_id", id)?,
None => state.skip_field("tool_call_id")?,
}
match &self.name {
Some(name) => state.serialize_field("name", name)?,
None => state.skip_field("name")?,
}
state.end()
}
}
#[derive(Serialize)]
struct CachedTextBlock<'a> {
#[serde(rename = "type")]
kind: &'static str,
text: &'a str,
cache_control: &'a CacheControl,
}
impl ChatMessage {
pub fn system(content: impl Into<String>) -> Self {
Self {
role: "system".into(),
content: Some(content.into()),
tool_calls: None,
tool_call_id: None,
name: None,
cache_control: None,
}
}
pub fn user(content: impl Into<String>) -> Self {
Self {
role: "user".into(),
content: Some(content.into()),
tool_calls: None,
tool_call_id: None,
name: None,
cache_control: None,
}
}
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: "assistant".into(),
content: Some(content.into()),
tool_calls: None,
tool_call_id: None,
name: None,
cache_control: None,
}
}
pub fn tool_result(
tool_call_id: impl Into<String>,
name: impl Into<String>,
content: impl Into<String>,
) -> Self {
Self {
role: "tool".into(),
content: Some(content.into()),
tool_calls: None,
tool_call_id: Some(tool_call_id.into()),
name: Some(name.into()),
cache_control: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chat_message_constructors() {
assert_eq!(ChatMessage::system("s").role, "system");
assert_eq!(ChatMessage::user("u").role, "user");
assert_eq!(ChatMessage::assistant("a").role, "assistant");
}
#[test]
fn chat_message_tool_role_round_trip() {
let msg = ChatMessage::tool_result("call_abc", "get_weather", r#"{"temp":72}"#);
assert_eq!(msg.role, "tool");
assert_eq!(msg.tool_call_id.as_deref(), Some("call_abc"));
assert_eq!(msg.name.as_deref(), Some("get_weather"));
assert_eq!(msg.content.as_deref(), Some(r#"{"temp":72}"#));
}
#[test]
fn chat_message_serialises_all_roles() {
for (msg, expected_role) in [
(ChatMessage::system("sys"), "system"),
(ChatMessage::user("usr"), "user"),
(ChatMessage::assistant("ast"), "assistant"),
(ChatMessage::tool_result("id", "fn", "result"), "tool"),
] {
let v: serde_json::Value = serde_json::to_value(&msg).expect("serialise");
assert_eq!(
v["role"].as_str(),
Some(expected_role),
"role mismatch for expected={expected_role}"
);
}
}
#[test]
fn chat_message_cache_control_serialises_as_content_block() {
let mut msg = ChatMessage::system("you are helpful");
msg.cache_control = Some(CacheControl::ephemeral());
let v: serde_json::Value = serde_json::to_value(&msg).expect("serialise");
assert_eq!(
v["content"],
serde_json::json!([{
"type": "text",
"text": "you are helpful",
"cache_control": {"type": "ephemeral"}
}])
);
assert_eq!(v["role"], "system");
}
#[test]
fn chat_message_without_cache_control_serialises_plain_string() {
let msg = ChatMessage::system("you are helpful");
assert!(msg.cache_control.is_none());
let v: serde_json::Value = serde_json::to_value(&msg).expect("serialise");
assert_eq!(v["content"], serde_json::json!("you are helpful"));
}
}