use std::any::Any;
use std::sync::Arc;
use futures::stream::BoxStream;
use gadk_genai::FunctionDeclaration;
use gadk_model::LlmRequest;
use serde_json::{Map, Value};
use crate::agent::Agent;
use crate::context::{Context, ReadonlyContext};
use crate::error::ToolError;
#[async_trait::async_trait]
pub trait Tool: Send + Sync + 'static {
fn name(&self) -> &str;
fn description(&self) -> &str;
fn is_long_running(&self) -> bool {
false
}
fn declaration(&self) -> Option<FunctionDeclaration> {
None
}
async fn process_request(
&self,
_ctx: &Context,
_req: &mut LlmRequest,
) -> Result<(), ToolError> {
Ok(())
}
fn as_runnable(&self) -> Option<&dyn RunnableTool> {
None
}
fn as_streaming(&self) -> Option<&dyn StreamingTool> {
None
}
fn defers_response(&self) -> bool {
false
}
fn display_result_on_skip_summarization(&self) -> bool {
false
}
fn wrapper_key(&self) -> Option<&str> {
None
}
fn unwrap(&self) -> Option<Arc<dyn Tool>> {
None
}
fn origin_kind(&self) -> ToolOriginKind {
ToolOriginKind::Unknown
}
fn origin_agent(&self) -> Option<Arc<dyn Agent>> {
None
}
fn origin_agents(&self) -> Vec<Arc<dyn Agent>> {
self.origin_agent().into_iter().collect()
}
fn as_any(&self) -> &dyn Any;
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
#[non_exhaustive]
pub enum ToolOriginKind {
#[default]
Unknown,
Local,
Mcp,
TransferAgent,
SubAgent,
Skill,
Toolbox,
}
#[async_trait::async_trait]
pub trait RunnableTool: Send + Sync {
async fn run(&self, ctx: Context, args: Value) -> Result<Map<String, Value>, ToolError>;
}
pub trait StreamingTool: Send + Sync {
fn run_stream(
&self,
ctx: Context,
args: Value,
) -> BoxStream<'static, Result<String, ToolError>>;
}
#[async_trait::async_trait]
pub trait Toolset: Send + Sync {
fn name(&self) -> &str;
async fn tools(&self, ctx: &ReadonlyContext) -> Result<Vec<Arc<dyn Tool>>, ToolError>;
async fn process_request(
&self,
_ctx: &Context,
_req: &mut LlmRequest,
) -> Result<(), ToolError> {
Ok(())
}
fn as_any(&self) -> &dyn Any;
}
pub type ToolPredicate = Arc<dyn Fn(&ReadonlyContext, &dyn Tool) -> bool + Send + Sync>;
pub type ConfirmationProvider = Arc<dyn Fn(&str, &Value) -> bool + Send + Sync>;
pub use crate::tool_util::{pack_tool, tool_from_request};