use serde_json::Value;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Role {
User,
Assistant,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Block {
Text(String),
ToolUse {
id: String,
name: String,
input: Value,
},
ToolResult {
id: String,
content: String,
is_error: bool,
},
Opaque(Value),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Message {
pub role: Role,
pub blocks: Vec<Block>,
}
impl Message {
pub fn user(text: impl Into<String>) -> Message {
Message {
role: Role::User,
blocks: vec![Block::Text(text.into())],
}
}
pub fn assistant(text: impl Into<String>) -> Message {
Message {
role: Role::Assistant,
blocks: vec![Block::Text(text.into())],
}
}
pub fn text(&self) -> String {
let mut out = String::new();
for b in &self.blocks {
if let Block::Text(t) = b {
if !out.is_empty() && !t.is_empty() {
out.push('\n');
}
out.push_str(t);
}
}
out
}
pub fn tool_uses(&self) -> Vec<(&str, &str, &Value)> {
self.blocks
.iter()
.filter_map(|b| match b {
Block::ToolUse { id, name, input } => Some((id.as_str(), name.as_str(), input)),
_ => None,
})
.collect()
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ToolSpec {
pub name: String,
pub description: String,
pub schema: Value,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Effort {
Low,
Medium,
High,
}
impl Effort {
pub const ALL: [Effort; 3] = [Effort::Low, Effort::Medium, Effort::High];
pub fn id(self) -> &'static str {
match self {
Effort::Low => "low",
Effort::Medium => "medium",
Effort::High => "high",
}
}
pub fn from_id(s: &str) -> Option<Effort> {
Effort::ALL.into_iter().find(|e| e.id() == s)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Request<'a> {
pub model: &'a str,
pub system: &'a str,
pub messages: &'a [Message],
pub tools: &'a [ToolSpec],
pub max_tokens: u32,
pub json: Option<&'a Value>,
pub effort: Option<Effort>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Usage {
pub input: u64,
pub output: u64,
pub cache_read: u64,
pub cache_write: u64,
}
impl Usage {
pub fn add(&mut self, o: &Usage) {
self.input += o.input;
self.output += o.output;
self.cache_read += o.cache_read;
self.cache_write += o.cache_write;
}
pub fn total_input(&self) -> u64 {
self.input + self.cache_read + self.cache_write
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Stop {
EndTurn,
ToolUse,
MaxTokens,
Refusal,
Other(String),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Reply {
pub blocks: Vec<Block>,
pub stop: Stop,
pub usage: Usage,
pub model: String,
}
impl Reply {
pub fn message(&self) -> Message {
Message {
role: Role::Assistant,
blocks: self.blocks.clone(),
}
}
pub fn text(&self) -> String {
self.message().text()
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Event<'a> {
Text(&'a str),
Thinking,
Tool(&'a str),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_and_tool_uses() {
let m = Message {
role: Role::Assistant,
blocks: vec![
Block::Opaque(serde_json::json!({"type": "thinking"})),
Block::Text("first".into()),
Block::ToolUse {
id: "t1".into(),
name: "find_all".into(),
input: serde_json::json!({"pattern": "x"}),
},
Block::Text("second".into()),
],
};
assert_eq!(m.text(), "first\nsecond");
let calls = m.tool_uses();
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].1, "find_all");
assert_eq!(Effort::from_id("high"), Some(Effort::High));
}
}