use serde::{Deserialize, Serialize};
use crate::ir::content::{ContentPart, ToolResultContent};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Role {
User,
Assistant,
System,
Tool,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: Vec<ContentPart>,
}
impl Message {
#[must_use]
pub fn new(role: Role, content: Vec<ContentPart>) -> Self {
Self { role, content }
}
pub fn user(text: impl Into<String>) -> Self {
Self {
role: Role::User,
content: vec![ContentPart::text(text)],
}
}
pub fn assistant(text: impl Into<String>) -> Self {
Self {
role: Role::Assistant,
content: vec![ContentPart::text(text)],
}
}
pub fn system(text: impl Into<String>) -> Self {
Self {
role: Role::System,
content: vec![ContentPart::text(text)],
}
}
pub fn tool_result(
tool_use_id: impl Into<String>,
name: impl Into<String>,
output: impl Into<String>,
) -> Self {
Self {
role: Role::Tool,
content: vec![ContentPart::ToolResult {
tool_use_id: tool_use_id.into(),
name: name.into(),
content: ToolResultContent::Text(output.into()),
is_error: false,
cache_control: None,
provider_echoes: Vec::new(),
}],
}
}
pub fn tool_result_json(
tool_use_id: impl Into<String>,
name: impl Into<String>,
output: serde_json::Value,
) -> Self {
Self {
role: Role::Tool,
content: vec![ContentPart::ToolResult {
tool_use_id: tool_use_id.into(),
name: name.into(),
content: ToolResultContent::Json(output),
is_error: false,
cache_control: None,
provider_echoes: Vec::new(),
}],
}
}
pub fn tool_error(
tool_use_id: impl Into<String>,
name: impl Into<String>,
output: impl Into<String>,
) -> Self {
Self {
role: Role::Tool,
content: vec![ContentPart::ToolResult {
tool_use_id: tool_use_id.into(),
name: name.into(),
content: ToolResultContent::Text(output.into()),
is_error: true,
cache_control: None,
provider_echoes: Vec::new(),
}],
}
}
}