velocia 0.3.5

velocia – production-ready AI agent framework using ADK-Rust, A2A protocol, and AWS DynamoDB
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use serde_json::Value;

use crate::error::Result;

/// Type-erased tool handler: takes JSON args, returns JSON result.
pub type ToolFn = Arc<
    dyn Fn(Value) -> Pin<Box<dyn Future<Output = Result<Value>> + Send>>
        + Send
        + Sync,
>;

/// A loaded, ready-to-use tool.
pub struct DynTool {
    pub name: String,
    pub description: String,
    pub handler: ToolFn,
}

/// Manual `Clone` impl: `Arc<dyn Fn…>` is always `Clone`.
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()
    }
}