use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
User,
Assistant,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Content {
Text {
text: String,
},
ToolUse {
id: String,
name: String,
input: Value,
},
ToolResult {
tool_use_id: String,
content: Value,
#[serde(default)]
is_error: bool,
},
Thinking {
text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
provider_data: Option<Value>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: Vec<Content>,
}
impl Message {
pub fn user(text: impl Into<String>) -> Self {
Self { role: Role::User, content: vec![Content::Text { text: text.into() }] }
}
pub fn assistant(content: Vec<Content>) -> Self {
Self { role: Role::Assistant, content }
}
pub fn text(&self) -> String {
content_text(&self.content)
}
}
pub fn content_text(content: &[Content]) -> String {
content
.iter()
.filter_map(|c| match c {
Content::Text { text } => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("")
}