llm_agent/
tool.rs

1use crate::RunState;
2use futures::future::BoxFuture;
3use llm_sdk::{JSONSchema, Part, Tool};
4use serde_json::Value;
5use std::{error::Error, fmt::Debug};
6
7/**
8 * Agent tool that can be used by the agent to perform specific tasks. Any
9 * type that implements the `AgentTool` trait can be used as a tool.
10 */
11pub trait AgentTool<TCtx>: Send + Sync {
12    /// Name of the tool.
13    fn name(&self) -> String;
14    /// A description of the tool to instruct the model how and when to use it.
15    fn description(&self) -> String;
16    /// The JSON schema of the parameters that the tool accepts. The type must
17    /// be "object".
18    fn parameters(&self) -> JSONSchema;
19    /// The function that will be called to execute the tool with given
20    /// parameters and context.
21    ///
22    /// If the tool throws an error, the agent will be interrupted and the error
23    /// will be propagated. To avoid interrupting the agent, the tool must
24    /// return an `AgentToolResult` with `is_error` set to true.
25    fn execute<'a>(
26        &'a self,
27        args: Value,
28        context: &'a TCtx,
29        state: &'a RunState,
30    ) -> BoxFuture<'a, Result<AgentToolResult, Box<dyn Error + Send + Sync>>>;
31}
32
33impl<TCtx> Debug for dyn AgentTool<TCtx> {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.debug_struct("AgentTool")
36            .field("name", &self.name())
37            .field("description", &self.description())
38            .field("parameters", &self.parameters())
39            .field("execute", &"Function")
40            .finish()
41    }
42}
43
44#[derive(Clone)]
45pub struct AgentToolResult {
46    pub content: Vec<Part>,
47    pub is_error: bool,
48}
49
50impl<TCtx> From<&dyn AgentTool<TCtx>> for Tool {
51    fn from(agent_tool: &dyn AgentTool<TCtx>) -> Self {
52        Self {
53            name: agent_tool.name(),
54            description: agent_tool.description(),
55            parameters: agent_tool.parameters(),
56        }
57    }
58}