use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::Role;
pub trait ContextMessage: Send + Sync + Clone + Serialize + DeserializeOwned {
fn role(&self) -> Role;
fn preserve_reasoning(&self) -> bool;
fn without_reasoning(self) -> Self;
fn with_role(self, role: Role) -> Self;
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct MockMsg {
role: Role,
content: String,
}
impl ContextMessage for MockMsg {
fn role(&self) -> Role {
self.role
}
fn preserve_reasoning(&self) -> bool {
false
}
fn without_reasoning(self) -> Self {
self
}
fn with_role(mut self, role: Role) -> Self {
self.role = role;
self
}
}
#[test]
fn mock_msg_role() {
let msg = MockMsg {
role: Role::User,
content: "hello".into(),
};
assert_eq!(msg.role(), Role::User);
}
#[test]
fn mock_msg_preserve_reasoning() {
let msg = MockMsg {
role: Role::Assistant,
content: "world".into(),
};
assert!(!msg.preserve_reasoning());
}
#[test]
fn mock_msg_serde() {
let msg = MockMsg {
role: Role::Assistant,
content: "world".into(),
};
let json = serde_json::to_string(&msg).unwrap();
let back: MockMsg = serde_json::from_str(&json).unwrap();
assert_eq!(back.content, "world");
}
}