pub struct ToolBuilder {
name: String,
description: String,
schema: Value,
}
impl ToolBuilder {
pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
Self {
name: name.into(),
description: description.into(),
schema: serde_json::json!({}),
}
}
pub fn schema(mut self, schema: Value) -> Self {
self.schema = schema;
self
}
pub fn param(mut self, name: &str, type_str: &str) -> Self {
if !self.schema.is_object() {
self.schema = serde_json::json!({});
}
let obj = self
.schema
.as_object_mut()
.expect("BUG: schema should be an object after initialization");
obj.insert(name.to_string(), Value::String(type_str.to_string()));
self
}
pub fn build<F, Fut>(self, handler: F) -> Tool
where
F: Fn(Value) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Value>> + Send + 'static,
{
Tool::new(self.name, self.description, self.schema, handler)
}
}