use std::fmt;
use serde::{Deserialize, Serialize};
use crate::message::Part;
#[derive(
Clone,
Debug,
Serialize,
Deserialize,
PartialEq,
Eq,
strum::Display,
strum::EnumString,
schemars::JsonSchema,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Role {
System,
User,
Assistant,
Tool,
}
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema)]
pub struct Message {
pub role: Role,
pub contents: Vec<Part>,
#[serde(skip_serializing_if = "Option::is_none")]
pub thinking: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<Part>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
}
impl Message {
pub fn new(role: Role) -> Self {
Self {
role,
contents: Vec::new(),
id: None,
thinking: None,
tool_calls: None,
signature: None,
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
pub fn with_thinking(mut self, thinking: impl Into<String>) -> Self {
self.thinking = Some(thinking.into());
self
}
pub fn with_signatured_thinking(
mut self,
thinking: impl Into<String>,
signature: impl Into<String>,
) -> Self {
self.thinking = Some(thinking.into());
self.signature = Some(signature.into());
self
}
pub fn with_contents(mut self, contents: impl IntoIterator<Item = impl Into<Part>>) -> Self {
self.contents = contents.into_iter().map(|v| v.into()).collect();
self
}
pub fn with_tool_calls(
mut self,
tool_calls: impl IntoIterator<Item = impl Into<Part>>,
) -> Self {
self.tool_calls = Some(tool_calls.into_iter().map(|v| v.into()).collect());
self
}
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).map_err(|_| fmt::Error)?;
write!(f, "Message {}", s)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum FinishReason {
Stop {},
Length {},
ToolCall {},
Refusal { reason: String },
}
impl Default for FinishReason {
fn default() -> Self {
Self::Stop {}
}
}
impl fmt::Display for FinishReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).map_err(|_| fmt::Error)?;
write!(f, "FinishReason {}", s)
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
pub struct TokenUsage {
pub input_tokens: u64,
pub output_tokens: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_creation_input_tokens: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_read_input_tokens: Option<u64>,
}
#[derive(Clone, Debug, Serialize, Deserialize, schemars::JsonSchema)]
pub struct MessageOutput {
pub message: Message,
pub finish_reason: FinishReason,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<TokenUsage>,
}
impl fmt::Display for MessageOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).map_err(|_| fmt::Error)?;
write!(f, "MessageOutput {}", s)
}
}