Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: WasmCompatSend + WasmCompatSync {
    type Args: DeserializeOwned + JsonSchema + WasmCompatSend;
    type Output: Serialize;
    type Error: Error + WasmCompatSend + 'static;

    const NAME: &'static str;

    // Required methods
    fn definition(&self) -> ToolDefinition;
    fn call(
        &self,
        args: Self::Args,
        ctx: &ToolContext,
    ) -> impl Future<Output = Result<Self::Output, Self::Error>> + WasmCompatSend;
}
Expand description

Strongly-typed tool trait. Implement this for your tools.

The blanket impl of ToolDyn handles JSON deserialization/serialization so you work with concrete Rust types.

§Example

use neuron_types::*;
use serde::Deserialize;

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct MyArgs { query: String }

struct MyTool;
impl Tool for MyTool {
    const NAME: &'static str = "my_tool";
    type Args = MyArgs;
    type Output = String;
    type Error = std::io::Error;

    fn definition(&self) -> ToolDefinition { todo!() }
    fn call(&self, args: MyArgs, ctx: &ToolContext)
        -> impl Future<Output = Result<String, std::io::Error>> + Send
    { async { Ok(args.query) } }
}

Required Associated Constants§

Source

const NAME: &'static str

The unique name of this tool.

Required Associated Types§

Source

type Args: DeserializeOwned + JsonSchema + WasmCompatSend

The deserialized input type.

Source

type Output: Serialize

The serializable output type.

Source

type Error: Error + WasmCompatSend + 'static

The tool-specific error type.

Required Methods§

Source

fn definition(&self) -> ToolDefinition

Returns the tool definition (name, description, schema).

Source

fn call( &self, args: Self::Args, ctx: &ToolContext, ) -> impl Future<Output = Result<Self::Output, Self::Error>> + WasmCompatSend

Execute the tool with typed arguments.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§