use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum Role {
#[default]
User,
Assistant,
}
impl Role {
pub fn wire_name(self) -> &'static str {
match self {
Role::User => "user",
Role::Assistant => "assistant",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ToolCall {
#[serde(default)]
pub id: String,
pub name: String,
pub args: Value,
}
impl ToolCall {
pub fn new(name: impl Into<String>, args: Value) -> Self {
Self {
id: String::new(),
name: name.into(),
args,
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = id.into();
self
}
pub fn call_id(&self, index: usize) -> String {
if self.id.is_empty() {
format!("call_{index}_{}", self.name)
} else {
self.id.clone()
}
}
pub fn arg(&self, name: &str) -> Option<&str> {
self.args.get(name).and_then(Value::as_str)
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ToolResult {
pub id: String,
pub name: String,
pub output: String,
#[serde(default)]
pub failed: bool,
}
impl ToolResult {
pub fn new(call: &ToolCall, index: usize, output: impl Into<String>) -> Self {
Self {
id: call.call_id(index),
name: call.name.clone(),
output: output.into(),
failed: false,
}
}
pub fn failure(call: &ToolCall, index: usize, message: impl Into<String>) -> Self {
Self {
failed: true,
..Self::new(call, index, message)
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Message {
pub role: Role,
#[serde(default)]
pub text: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub calls: Vec<ToolCall>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub results: Vec<ToolResult>,
}
impl Message {
pub fn user(text: impl Into<String>) -> Self {
Self {
role: Role::User,
text: text.into(),
..Self::default()
}
}
pub fn assistant(text: impl Into<String>) -> Self {
Self {
role: Role::Assistant,
text: text.into(),
..Self::default()
}
}
pub fn tool_calls(text: impl Into<String>, calls: Vec<ToolCall>) -> Self {
let calls = calls
.into_iter()
.enumerate()
.map(|(index, call)| {
let id = call.call_id(index);
ToolCall { id, ..call }
})
.collect();
Self {
role: Role::Assistant,
text: text.into(),
calls,
results: Vec::new(),
}
}
pub fn tool_results(results: Vec<ToolResult>) -> Self {
Self {
role: Role::User,
text: String::new(),
calls: Vec::new(),
results,
}
}
pub fn is_empty(&self) -> bool {
self.text.trim().is_empty() && self.calls.is_empty() && self.results.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn calls_without_provider_ids_get_stable_synthetic_ones() {
let message = Message::tool_calls(
"looking it up",
vec![
ToolCall::new("search", json!({"q": "rust"})),
ToolCall::new("search", json!({"q": "gats"})),
],
);
assert_ne!(message.calls[0].id, message.calls[1].id);
assert!(message.calls[0].id.contains("search"));
let result = ToolResult::new(&message.calls[0], 0, "hits");
assert_eq!(result.id, message.calls[0].id);
}
#[test]
fn provider_supplied_ids_are_left_alone() {
let message = Message::tool_calls(
"",
vec![ToolCall::new("search", json!({})).with_id("toolu_01ABC")],
);
assert_eq!(message.calls[0].id, "toolu_01ABC");
}
#[test]
fn a_failed_tool_is_marked_as_such() {
let call = ToolCall::new("open_page", json!({"url": "x"}));
assert!(ToolResult::failure(&call, 0, "404").failed);
assert!(!ToolResult::new(&call, 0, "body").failed);
}
#[test]
fn plain_turns_carry_no_tool_baggage() {
let message = Message::user("hello");
assert!(message.calls.is_empty() && message.results.is_empty());
assert!(Message::assistant("").is_empty());
}
}