use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
System,
User,
Assistant,
Tool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmToolCall {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub name: String,
pub arguments: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmMessage {
#[serde(default = "default_role")]
pub role: MessageRole,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<LlmToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub image_paths: Option<Vec<String>>,
}
fn default_role() -> MessageRole {
MessageRole::User
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct ResponseEvidence {
pub usage: Option<serde_json::Value>,
pub provider_model: Option<String>,
pub finish_reason: Option<String>,
pub metadata: HashMap<String, serde_json::Value>,
}
#[derive(Debug, Clone, Default)]
pub struct LlmGatewayResponse<T = ()> {
pub content: Option<String>,
pub object: Option<T>,
pub tool_calls: Vec<LlmToolCall>,
pub thinking: Option<String>,
pub evidence: ResponseEvidence,
}
impl LlmMessage {
pub fn user(content: impl Into<String>) -> Self {
Self {
role: MessageRole::User,
content: Some(content.into()),
tool_calls: None,
image_paths: None,
}
}
pub fn system(content: impl Into<String>) -> Self {
Self {
role: MessageRole::System,
content: Some(content.into()),
tool_calls: None,
image_paths: None,
}
}
pub fn assistant(content: impl Into<String>) -> Self {
Self {
role: MessageRole::Assistant,
content: Some(content.into()),
tool_calls: None,
image_paths: None,
}
}
pub fn with_images(mut self, paths: Vec<String>) -> Self {
self.image_paths = Some(paths);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_message_role_serialization() {
assert_eq!(serde_json::to_string(&MessageRole::System).unwrap(), "\"system\"");
assert_eq!(serde_json::to_string(&MessageRole::User).unwrap(), "\"user\"");
assert_eq!(serde_json::to_string(&MessageRole::Assistant).unwrap(), "\"assistant\"");
assert_eq!(serde_json::to_string(&MessageRole::Tool).unwrap(), "\"tool\"");
}
#[test]
fn test_message_role_deserialization() {
assert_eq!(serde_json::from_str::<MessageRole>("\"system\"").unwrap(), MessageRole::System);
assert_eq!(serde_json::from_str::<MessageRole>("\"user\"").unwrap(), MessageRole::User);
assert_eq!(
serde_json::from_str::<MessageRole>("\"assistant\"").unwrap(),
MessageRole::Assistant
);
assert_eq!(serde_json::from_str::<MessageRole>("\"tool\"").unwrap(), MessageRole::Tool);
}
#[test]
fn test_user_message() {
let msg = LlmMessage::user("Hello");
assert_eq!(msg.role, MessageRole::User);
assert_eq!(msg.content, Some("Hello".to_string()));
assert!(msg.tool_calls.is_none());
assert!(msg.image_paths.is_none());
}
#[test]
fn test_system_message() {
let msg = LlmMessage::system("You are a helpful assistant");
assert_eq!(msg.role, MessageRole::System);
assert_eq!(msg.content, Some("You are a helpful assistant".to_string()));
assert!(msg.tool_calls.is_none());
assert!(msg.image_paths.is_none());
}
#[test]
fn test_assistant_message() {
let msg = LlmMessage::assistant("I can help with that");
assert_eq!(msg.role, MessageRole::Assistant);
assert_eq!(msg.content, Some("I can help with that".to_string()));
assert!(msg.tool_calls.is_none());
assert!(msg.image_paths.is_none());
}
#[test]
fn test_message_with_images() {
let msg = LlmMessage::user("Describe this image")
.with_images(vec!["/path/to/image.jpg".to_string()]);
assert_eq!(msg.role, MessageRole::User);
assert_eq!(msg.content, Some("Describe this image".to_string()));
assert_eq!(msg.image_paths, Some(vec!["/path/to/image.jpg".to_string()]));
}
#[test]
fn test_llm_tool_call_serialization() {
let mut args = HashMap::new();
args.insert("key".to_string(), serde_json::json!("value"));
let tool_call = LlmToolCall {
id: Some("call_123".to_string()),
name: "test_tool".to_string(),
arguments: args,
};
let json = serde_json::to_string(&tool_call).unwrap();
assert!(json.contains("test_tool"));
assert!(json.contains("call_123"));
}
#[test]
fn test_llm_tool_call_without_id() {
let tool_call = LlmToolCall {
id: None,
name: "test_tool".to_string(),
arguments: HashMap::new(),
};
let json = serde_json::to_string(&tool_call).unwrap();
assert!(!json.contains("\"id\""));
assert!(json.contains("test_tool"));
}
#[test]
fn test_llm_message_serialization() {
let msg = LlmMessage::user("test content");
let json = serde_json::to_string(&msg).unwrap();
assert!(json.contains("\"role\":\"user\""));
assert!(json.contains("\"content\":\"test content\""));
}
#[test]
fn test_llm_message_deserialization() {
let json = r#"{"role":"assistant","content":"response"}"#;
let msg: LlmMessage = serde_json::from_str(json).unwrap();
assert_eq!(msg.role, MessageRole::Assistant);
assert_eq!(msg.content, Some("response".to_string()));
}
#[test]
fn test_llm_message_default_role() {
let json = r#"{"content":"test"}"#;
let msg: LlmMessage = serde_json::from_str(json).unwrap();
assert_eq!(msg.role, MessageRole::User);
}
}