use anyllm_translate::anthropic::{Tool, ToolChoice};
use serde_json::Value;
pub struct ToolBuilder {
name: String,
description: Option<String>,
input_schema: Value,
}
impl ToolBuilder {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
description: None,
input_schema: Value::Object(serde_json::Map::new()),
}
}
pub fn description(mut self, desc: &str) -> Self {
self.description = Some(desc.to_string());
self
}
pub fn input_schema(mut self, schema: Value) -> Self {
self.input_schema = schema;
self
}
pub fn build(self) -> Tool {
Tool {
name: self.name,
description: self.description,
input_schema: self.input_schema,
}
}
}
pub struct ToolChoiceBuilder;
impl ToolChoiceBuilder {
pub fn auto() -> ToolChoice {
ToolChoice::Auto {
disable_parallel_tool_use: None,
}
}
pub fn any() -> ToolChoice {
ToolChoice::Any {
disable_parallel_tool_use: None,
}
}
pub fn none() -> ToolChoice {
ToolChoice::None
}
pub fn specific(name: &str) -> ToolChoice {
ToolChoice::Tool {
name: name.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn tool_builder_minimal() {
let tool = ToolBuilder::new("test_tool").build();
assert_eq!(tool.name, "test_tool");
assert!(tool.description.is_none());
assert!(tool.input_schema.is_object());
}
#[test]
fn tool_builder_full() {
let schema = json!({
"type": "object",
"properties": {
"query": { "type": "string" }
},
"required": ["query"]
});
let tool = ToolBuilder::new("search")
.description("Search the web")
.input_schema(schema.clone())
.build();
assert_eq!(tool.name, "search");
assert_eq!(tool.description.as_deref(), Some("Search the web"));
assert_eq!(tool.input_schema, schema);
}
#[test]
fn tool_choice_auto() {
let choice = ToolChoiceBuilder::auto();
assert_eq!(
choice,
ToolChoice::Auto {
disable_parallel_tool_use: None
}
);
}
#[test]
fn tool_choice_any() {
let choice = ToolChoiceBuilder::any();
assert_eq!(
choice,
ToolChoice::Any {
disable_parallel_tool_use: None
}
);
}
#[test]
fn tool_choice_none() {
let choice = ToolChoiceBuilder::none();
assert_eq!(choice, ToolChoice::None);
}
#[test]
fn tool_choice_specific() {
let choice = ToolChoiceBuilder::specific("get_weather");
assert_eq!(
choice,
ToolChoice::Tool {
name: "get_weather".to_string()
}
);
}
#[test]
fn tool_serializes_correctly() {
let tool = ToolBuilder::new("calc")
.description("Calculator")
.input_schema(json!({"type": "object"}))
.build();
let json = serde_json::to_value(&tool).unwrap();
assert_eq!(json["name"], "calc");
assert_eq!(json["description"], "Calculator");
}
}