use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Role {
System,
User,
Assistant,
Tool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageOrigin {
User,
Harness,
StopHook,
HookNote,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolCall {
pub id: String,
pub name: String,
pub arguments: Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub provider_data: Option<Value>,
}
impl ToolCall {
pub fn new(id: impl Into<String>, name: impl Into<String>, arguments: Value) -> Self {
Self {
id: id.into(),
name: name.into(),
arguments,
provider_data: None,
}
}
pub fn with_provider_data(mut self, data: Value) -> Self {
self.provider_data = Some(data);
self
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "source", rename_all = "snake_case")]
pub enum Image {
Base64 { media_type: String, data: String },
Url { url: String },
}
impl Image {
pub fn base64(media_type: impl Into<String>, data: impl Into<String>) -> Self {
Self::Base64 {
media_type: media_type.into(),
data: data.into(),
}
}
pub fn url(url: impl Into<String>) -> Self {
Self::Url { url: url.into() }
}
pub fn from_url_or_data(url: &str) -> Self {
url.strip_prefix("data:")
.and_then(|rest| rest.split_once(";base64,"))
.filter(|(media_type, data)| !media_type.is_empty() && !data.is_empty())
.map_or_else(
|| Self::url(url),
|(media_type, data)| Self::base64(media_type, data),
)
}
pub fn to_url(&self) -> String {
match self {
Self::Base64 { media_type, data } => format!("data:{media_type};base64,{data}"),
Self::Url { url } => url.clone(),
}
}
}
impl std::fmt::Debug for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Base64 { media_type, data } => f
.debug_struct("Base64")
.field("media_type", media_type)
.field("bytes_b64", &data.len())
.finish(),
Self::Url { url } => f.debug_struct("Url").field("url", url).finish(),
}
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Audio {
pub format: String,
pub data: String,
}
impl Audio {
pub fn new(format: impl Into<String>, data: impl Into<String>) -> Self {
Self {
format: format.into(),
data: data.into(),
}
}
pub fn media_type(&self) -> Option<&'static str> {
Some(match self.format.as_str() {
"ogg" | "opus" => "audio/ogg",
"mp3" => "audio/mp3",
"wav" => "audio/wav",
"flac" => "audio/flac",
"aac" => "audio/aac",
_ => return None,
})
}
}
impl std::fmt::Debug for Audio {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Audio")
.field("format", &self.format)
.field("bytes_b64", &self.data.len())
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
pub content: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tool_calls: Vec<ToolCall>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub images: Vec<Image>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub audio: Vec<Audio>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub is_error: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub origin: Option<MessageOrigin>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reasoning: Option<String>,
}
impl Message {
pub fn system(content: impl Into<String>) -> Self {
Self::plain(Role::System, content)
}
pub fn user(content: impl Into<String>) -> Self {
Self::from_origin(MessageOrigin::User, content)
}
pub fn from_origin(origin: MessageOrigin, content: impl Into<String>) -> Self {
let mut message = Self::plain(Role::User, content);
message.origin = Some(origin);
message
}
pub fn hook_note(content: impl Into<String>) -> Self {
let mut message = Self::plain(Role::System, content);
message.origin = Some(MessageOrigin::HookNote);
message
}
pub fn is_hook_note(&self) -> bool {
self.origin == Some(MessageOrigin::HookNote)
}
pub fn is_injected(&self) -> bool {
matches!(
self.origin,
Some(MessageOrigin::Harness | MessageOrigin::StopHook)
)
}
pub fn assistant(content: impl Into<String>, tool_calls: Vec<ToolCall>) -> Self {
Self {
role: Role::Assistant,
content: content.into(),
tool_calls,
tool_call_id: None,
images: Vec::new(),
audio: Vec::new(),
is_error: false,
origin: None,
reasoning: None,
}
}
pub fn tool_result(call_id: impl Into<String>, content: impl Into<String>) -> Self {
Self {
role: Role::Tool,
content: content.into(),
tool_calls: Vec::new(),
tool_call_id: Some(call_id.into()),
images: Vec::new(),
audio: Vec::new(),
is_error: false,
origin: None,
reasoning: None,
}
}
pub fn with_images(mut self, images: impl IntoIterator<Item = Image>) -> Self {
self.images.extend(images);
self
}
pub fn with_audio(mut self, audio: impl IntoIterator<Item = Audio>) -> Self {
self.audio.extend(audio);
self
}
pub fn with_reasoning(mut self, reasoning: Option<String>) -> Self {
self.reasoning = reasoning;
self
}
pub fn with_error(mut self, is_error: bool) -> Self {
self.is_error = is_error;
self
}
fn plain(role: Role, content: impl Into<String>) -> Self {
Self {
role,
content: content.into(),
tool_calls: Vec::new(),
tool_call_id: None,
images: Vec::new(),
audio: Vec::new(),
is_error: false,
origin: None,
reasoning: None,
}
}
}
impl From<&str> for Message {
fn from(text: &str) -> Self {
Self::user(text)
}
}
impl From<&String> for Message {
fn from(text: &String) -> Self {
Self::user(text.as_str())
}
}
impl From<String> for Message {
fn from(text: String) -> Self {
Self::user(text)
}
}