use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ToolDefinition {
pub name: String,
pub description: String,
pub input_schema: ToolParameters,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ToolParameters {
#[serde(rename = "type")]
pub param_type: String,
pub properties: HashMap<String, ParameterProperty>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub required: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParameterProperty {
#[serde(rename = "type")]
pub param_type: String,
pub description: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub enum_values: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub maximum: Option<f64>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum ToolChoice {
#[serde(rename = "auto")]
Auto,
#[serde(rename = "tool")]
Tool {
name: String,
},
#[serde(rename = "any")]
Any,
#[serde(rename = "none")]
None,
}
impl ToolChoice {
pub fn auto() -> Self {
Self::Auto
}
pub fn specific(name: impl Into<String>) -> Self {
Self::Tool { name: name.into() }
}
pub fn any() -> Self {
Self::Any
}
pub fn none() -> Self {
Self::None
}
}
pub fn evaluate_expression(expression: &str) -> Result<f64, String> {
expression.parse::<f64>().map_err(|_| {
format!("Failed to evaluate expression: '{}'. Only simple numeric values are supported in this implementation.", expression)
})
}
pub fn calculator_tool() -> ToolDefinition {
ToolDefinition {
name: "calculator".to_string(),
description: "Evaluates mathematical expressions".to_string(),
input_schema: ToolParameters {
param_type: "object".to_string(),
properties: {
let mut map = HashMap::new();
map.insert(
"expression".to_string(),
ParameterProperty {
param_type: "string".to_string(),
description: "The mathematical expression to evaluate".to_string(),
enum_values: None,
minimum: None,
maximum: None,
},
);
map
},
required: vec!["expression".to_string()],
},
}
}