swiftide_core/chat_completion/
chat_message.rsuse super::tools::{ToolCall, ToolOutput};
#[derive(Clone, strum_macros::EnumIs, PartialEq, Debug)]
pub enum ChatMessage {
System(String),
User(String),
Assistant(Option<String>, Option<Vec<ToolCall>>),
ToolOutput(ToolCall, ToolOutput),
Summary(String),
}
impl std::fmt::Display for ChatMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChatMessage::System(s) => write!(f, "System: \"{s}\""),
ChatMessage::User(s) => write!(f, "User: \"{s}\""),
ChatMessage::Assistant(message, tool_calls) => write!(
f,
"Assistant: \"{}\", tools: {}",
message.as_deref().unwrap_or("None"),
tool_calls.as_deref().map_or("None".to_string(), |tc| {
tc.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(", ")
})
),
ChatMessage::ToolOutput(tc, to) => write!(f, "ToolOutput: \"{tc}\": \"{to}\""),
ChatMessage::Summary(s) => write!(f, "Summary: \"{s}\""),
}
}
}
impl ChatMessage {
pub fn new_system(message: impl Into<String>) -> Self {
ChatMessage::System(message.into())
}
pub fn new_user(message: impl Into<String>) -> Self {
ChatMessage::User(message.into())
}
pub fn new_assistant(
message: Option<impl Into<String>>,
tool_calls: Option<Vec<ToolCall>>,
) -> Self {
ChatMessage::Assistant(message.map(Into::into), tool_calls)
}
pub fn new_tool_output(tool_call: impl Into<ToolCall>, output: impl Into<ToolOutput>) -> Self {
ChatMessage::ToolOutput(tool_call.into(), output.into())
}
pub fn new_summary(message: impl Into<String>) -> Self {
ChatMessage::Summary(message.into())
}
}