Trait ToolDefinition

Source
pub trait ToolDefinition {
    type Input: DeserializeOwned + JsonSchema + Send + Sync + 'static;
    type Output: Serialize + Send + Sync + 'static;

    // Required methods
    fn name(&self) -> String;
    fn description(&self) -> String;

    // Provided method
    fn schema(&self) -> Result<Value> { ... }
}
Expand description

Core trait for defining tools with associated input and output types

This trait provides a more flexible and type-safe way to define tools compared to the original Tool trait. It uses associated types to specify the input and output types for a tool, allowing for better compile-time type checking.

§Type Parameters

  • Input - The type of input that this tool accepts, must implement DeserializeOwned and JsonSchema
  • Output - The type of output that this tool produces, must implement Serialize

§Examples

use language_barrier_core::ToolDefinition;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, JsonSchema)]
struct WeatherRequest {
    location: String,
    units: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
struct WeatherResponse {
    temperature: f64,
    condition: String,
    location: String,
    units: String,
}

struct WeatherTool;

impl ToolDefinition for WeatherTool {
    type Input = WeatherRequest;
    type Output = WeatherResponse;

    fn name(&self) -> String {
        "get_weather".to_string()
    }

    fn description(&self) -> String {
        "Get current weather for a location".to_string()
    }
}

Required Associated Types§

Source

type Input: DeserializeOwned + JsonSchema + Send + Sync + 'static

The input type that this tool accepts

Source

type Output: Serialize + Send + Sync + 'static

The output type that this tool produces

Required Methods§

Source

fn name(&self) -> String

Returns the name of the tool

Source

fn description(&self) -> String

Returns the description of the tool

Provided Methods§

Source

fn schema(&self) -> Result<Value>

Helper to generate the JSON schema for the input type

Implementors§