use std::sync::Arc;
use entelix_core::tools::Tool;
use entelix_core::{AgentContext, ExecutionContext, Result};
use crate::runnable::Runnable;
pub struct ToolToRunnableAdapter {
inner: Arc<dyn Tool>,
}
impl ToolToRunnableAdapter {
pub fn new<T: Tool>(tool: T) -> Self {
Self {
inner: Arc::new(tool),
}
}
pub fn from_arc(tool: Arc<dyn Tool>) -> Self {
Self { inner: tool }
}
pub fn inner(&self) -> &Arc<dyn Tool> {
&self.inner
}
}
impl Clone for ToolToRunnableAdapter {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
#[async_trait::async_trait]
impl Runnable<serde_json::Value, serde_json::Value> for ToolToRunnableAdapter {
async fn invoke(
&self,
input: serde_json::Value,
ctx: &ExecutionContext,
) -> Result<serde_json::Value> {
let agent_ctx = AgentContext::<()>::from(ctx.clone());
self.inner.execute(input, &agent_ctx).await
}
fn name(&self) -> std::borrow::Cow<'_, str> {
std::borrow::Cow::Borrowed(&self.inner.metadata().name)
}
}