Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn definition(&self) -> ToolDefinition;
    fn execute(
        &self,
        ctx: &ExecutionContext,
        input: Value,
    ) -> Pin<Box<dyn Future<Output = Result<ToolOutput, Error>> + Send + '_>>;
}
Expand description

Trait for tools that agents can invoke.

Uses Pin<Box<dyn Future>> return type for dyn-compatibility, allowing tools to be stored as Arc<dyn Tool>.

§Example

Implementing a simple synchronous tool that echoes its input:

use std::future::Future;
use std::pin::Pin;
use heartbit_core::{Tool, ToolOutput};
use heartbit_core::llm::types::ToolDefinition;

struct EchoTool;

impl Tool for EchoTool {
    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "echo".into(),
            description: "Echo back the input string.".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": { "text": { "type": "string" } },
                "required": ["text"]
            }),
        }
    }

    fn execute(
        &self,
        _ctx: &heartbit_core::ExecutionContext,
        input: serde_json::Value,
    ) -> Pin<Box<dyn Future<Output = Result<ToolOutput, heartbit_core::Error>> + Send + '_>> {
        Box::pin(async move {
            let text = input.get("text").and_then(|v| v.as_str()).unwrap_or("");
            Ok(ToolOutput::success(text.to_string()))
        })
    }
}

Required Methods§

Source

fn definition(&self) -> ToolDefinition

Source

fn execute( &self, ctx: &ExecutionContext, input: Value, ) -> Pin<Box<dyn Future<Output = Result<ToolOutput, Error>> + Send + '_>>

Implementors§