use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use serde_json::Value;
use crate::error::Result;
pub type ToolFn = Arc<
dyn Fn(Value) -> Pin<Box<dyn Future<Output = Result<Value>> + Send>>
+ Send
+ Sync,
>;
pub struct DynTool {
pub name: String,
pub description: String,
pub handler: ToolFn,
}
impl Clone for DynTool {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
description: self.description.clone(),
handler: Arc::clone(&self.handler),
}
}
}
impl DynTool {
pub fn new(
name: impl Into<String>,
description: impl Into<String>,
handler: impl Fn(Value) -> Pin<Box<dyn Future<Output = Result<Value>> + Send>>
+ Send
+ Sync
+ 'static,
) -> Self {
Self {
name: name.into(),
description: description.into(),
handler: Arc::new(handler),
}
}
}
impl std::fmt::Debug for DynTool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DynTool")
.field("name", &self.name)
.field("description", &self.description)
.finish()
}
}