use super::{ToolConfig, ToolName};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tool {
pub name: ToolName,
pub description: Option<String>,
pub schema: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strict: Option<bool>,
pub config: Option<ToolConfig>,
}
impl Tool {
pub fn size(&self) -> usize {
let mut size = match &self.name {
ToolName::WebSearch => 9, ToolName::Custom(name) => name.len(),
};
size += self.description.as_ref().map(|d| d.len()).unwrap_or_default();
size += self
.schema
.as_ref()
.map(|s| serde_json::to_string(s).map(|j| j.len()).unwrap_or_default())
.unwrap_or_default();
size += self
.config
.as_ref()
.map(|c| match c {
ToolConfig::WebSearch(_) => 0,
ToolConfig::Custom(v) => serde_json::to_string(v).map(|j| j.len()).unwrap_or_default(),
})
.unwrap_or_default();
size
}
}
impl Tool {
pub fn new(name: impl Into<ToolName>) -> Self {
Self {
name: name.into(),
description: None,
schema: None,
strict: None,
config: None,
}
}
pub fn new_web_search() -> Self {
Self::new(ToolName::WebSearch)
}
}
impl Tool {
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn with_schema(mut self, parameters: Value) -> Self {
self.schema = Some(parameters);
self
}
pub fn with_strict(mut self, strict: bool) -> Self {
self.strict = Some(strict);
self
}
pub fn with_config(mut self, config: impl Into<ToolConfig>) -> Self {
self.config = Some(config.into());
self
}
}