use std::sync::Arc;
use adk_core::{Tool, ToolContext};
use async_trait::async_trait;
use serde_json::Value;
use crate::config::ToolDefinition;
use crate::error::Result;
use crate::events::ToolCall;
use crate::runner::ToolHandler;
use super::context::ToolContextFactory;
pub struct ToolBridgeAdapter {
tool: Arc<dyn Tool>,
context_factory: Arc<dyn ToolContextFactory>,
}
impl ToolBridgeAdapter {
pub fn new(tool: Arc<dyn Tool>, context_factory: Arc<dyn ToolContextFactory>) -> Self {
Self { tool, context_factory }
}
pub fn definition(tool: &dyn Tool) -> ToolDefinition {
ToolDefinition {
name: tool.name().to_string(),
description: Some(tool.description().to_string()),
parameters: tool.parameters_schema(),
}
}
}
#[async_trait]
impl ToolHandler for ToolBridgeAdapter {
async fn execute(&self, call: &ToolCall) -> Result<Value> {
let ctx: Arc<dyn ToolContext> = self.context_factory.create_context(&call.call_id);
match self.tool.execute(ctx, call.arguments.clone()).await {
Ok(value) => Ok(value),
Err(e) => Ok(serde_json::json!({ "error": e.to_string() })),
}
}
}