2026-07-12 11:28:39 +07:00
|
|
|
//! Chat message types shared across the DTO layer: `Role` and `ChatMessage`
|
|
|
|
|
//! with convenience constructors.
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// The conversation participant who authored a message.
|
2026-07-11 13:16:10 +07:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
|
pub enum Role {
|
|
|
|
|
#[serde(rename = "user")]
|
|
|
|
|
User,
|
|
|
|
|
#[serde(rename = "assistant")]
|
|
|
|
|
Assistant,
|
|
|
|
|
#[serde(rename = "system")]
|
|
|
|
|
System,
|
|
|
|
|
#[serde(rename = "tool")]
|
|
|
|
|
Tool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Role {
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// A single message in a conversation, compatible with the OpenAI/Anthropic
|
|
|
|
|
/// chat-completion API structures.
|
2026-07-11 13:16:10 +07:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct ChatMessage {
|
|
|
|
|
pub role: Role,
|
|
|
|
|
pub content: Option<String>,
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
pub tool_calls: Option<Vec<super::tool::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>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ChatMessage {
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Build a user-role message with the given text content.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn user(content: impl Into<String>) -> Self {
|
|
|
|
|
ChatMessage {
|
|
|
|
|
role: Role::User,
|
|
|
|
|
content: Some(content.into()),
|
|
|
|
|
tool_calls: None,
|
|
|
|
|
tool_call_id: None,
|
|
|
|
|
name: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Build an assistant-role message with an optional text response.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn assistant(content: Option<String>) -> Self {
|
|
|
|
|
ChatMessage {
|
|
|
|
|
role: Role::Assistant,
|
|
|
|
|
content,
|
|
|
|
|
tool_calls: None,
|
|
|
|
|
tool_call_id: None,
|
|
|
|
|
name: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Build a system-role message with the given instruction text.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn system(content: impl Into<String>) -> Self {
|
|
|
|
|
ChatMessage {
|
|
|
|
|
role: Role::System,
|
|
|
|
|
content: Some(content.into()),
|
|
|
|
|
tool_calls: None,
|
|
|
|
|
tool_call_id: None,
|
|
|
|
|
name: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Build a tool-role result message referencing a prior tool call.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn tool_result(tool_call_id: String, content: String) -> Self {
|
|
|
|
|
ChatMessage {
|
|
|
|
|
role: Role::Tool,
|
|
|
|
|
content: Some(content),
|
|
|
|
|
tool_calls: None,
|
|
|
|
|
tool_call_id: Some(tool_call_id),
|
|
|
|
|
name: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|