use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::error::Result;
use crate::tool::retry::ToolRetryPolicy;
use crate::tool::types::ToolOutput;
pub trait Tool: Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn parameters_schema(&self) -> serde_json::Value;
fn execute(&self, input: &serde_json::Value)
-> impl Future<Output = Result<ToolOutput>> + Send;
fn retry_policy(&self) -> Option<ToolRetryPolicy> {
None
}
}
pub trait ErasedTool: Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn parameters_schema(&self) -> serde_json::Value;
fn execute_erased<'a>(
&'a self,
input: &'a serde_json::Value,
) -> Pin<Box<dyn Future<Output = Result<ToolOutput>> + Send + 'a>>;
fn retry_policy(&self) -> Option<ToolRetryPolicy>;
}
impl<T: Tool> ErasedTool for T {
fn name(&self) -> &str {
Tool::name(self)
}
fn description(&self) -> &str {
Tool::description(self)
}
fn parameters_schema(&self) -> serde_json::Value {
Tool::parameters_schema(self)
}
fn execute_erased<'a>(
&'a self,
input: &'a serde_json::Value,
) -> Pin<Box<dyn Future<Output = Result<ToolOutput>> + Send + 'a>> {
Box::pin(Tool::execute(self, input))
}
fn retry_policy(&self) -> Option<ToolRetryPolicy> {
Tool::retry_policy(self)
}
}
pub type SharedTool = Arc<dyn ErasedTool>;