Skip to main content

ToolFunction

Trait ToolFunction 

Source
pub trait ToolFunction:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn parameters(&self) -> Option<Value>;
    fn call<'life0, 'async_trait>(
        &'life0 self,
        args: Value,
    ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn call_with_context<'life0, 'async_trait>(
        &'life0 self,
        args: Value,
        ctx: ToolContext,
    ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn requires_confirmation(&self) -> bool { ... }
    fn confirmation_message(&self) -> Option<&str> { ... }
}
Expand description

A regular tool — called once, returns a result.

§Examples

ⓘ
use async_trait::async_trait;
use gemini_adk_rs::tool::ToolFunction;
use gemini_adk_rs::error::ToolError;

struct MyTool;

#[async_trait]
impl ToolFunction for MyTool {
    fn name(&self) -> &str { "my_tool" }
    fn description(&self) -> &str { "Does something useful" }
    fn parameters(&self) -> Option<serde_json::Value> { None }
    async fn call(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        Ok(serde_json::json!({"status": "ok"}))
    }
}

Required Methods§

Source

fn name(&self) -> &str

The unique name of this tool.

Source

fn description(&self) -> &str

Human-readable description of what this tool does.

Source

fn parameters(&self) -> Option<Value>

JSON Schema for the tool’s input parameters, or None if parameterless.

Source

fn call<'life0, 'async_trait>( &'life0 self, args: Value, ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the tool with the given arguments and return the result.

Provided Methods§

Source

fn call_with_context<'life0, 'async_trait>( &'life0 self, args: Value, ctx: ToolContext, ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Execute the tool knowing the session it runs in: its State, the call’s id, and a cancellation token (see ToolContext).

The runtime always calls this. The default ignores the context and runs call, so a tool that needs no context implements only call. A tool that does overrides this, and implements call as self.call_with_context(args, ToolContext::detached()). A wrapper must forward it, or the context is lost.

Source

fn requires_confirmation(&self) -> bool

Whether this tool must be confirmed before it runs. Defaults to false.

Tools built with T::confirm(..) (a PolicyTool with a confirm policy) return true, prompting the ToolDispatcher to consult its configured ConfirmationProvider before executing.

Source

fn confirmation_message(&self) -> Option<&str>

Optional hint shown when confirmation is requested.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: ToolFunction + ?Sized> ToolFunction for Arc<T>

An Arc<dyn ToolFunction> (or Arc<SomeTool>) is itself a tool, so a setter that takes impl ToolFunction accepts a shared handle as readily as a fresh value.

Source§

fn name(&self) -> &str

Source§

fn description(&self) -> &str

Source§

fn parameters(&self) -> Option<Value>

Source§

fn call<'life0, 'async_trait>( &'life0 self, args: Value, ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn call_with_context<'life0, 'async_trait>( &'life0 self, args: Value, ctx: ToolContext, ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn requires_confirmation(&self) -> bool

Source§

fn confirmation_message(&self) -> Option<&str>

Implementors§