Skip to main content

rpc_agent/
tools.rs

1use rig::tool::Tool;
2
3use crate::error::Error;
4
5/// A wrapper around a [`Tool`] that implements [`Clone`].
6#[derive(Clone)]
7pub struct ToolWrapper<T: Tool + 'static>(Box<T>);
8
9impl<T: Tool + 'static> ToolWrapper<T> {
10    /// Creates a new [`ToolWrapper`] with the given `struct` that implements the [`Tool`] trait.
11    ///
12    /// example:
13    /// ```rust,ignore
14    /// use rpc_agent::tools::ToolWrapper;
15    /// use rig::tool::Tool;
16    ///
17    /// struct MyTool;
18    ///
19    /// impl Tool for MyTool {
20    ///     const NAME: &'static str = "my_tool";
21    ///     type Error = anyhow::Error;
22    ///     type Args = Input;
23    ///     type Output = u32;
24    ///
25    ///     async fn definition(&self, _prompt: String) -> rig::completion::ToolDefinition {
26    ///         ToolDefinition {
27    ///             name: "get_ticket_price".to_string(),
28    ///             description: "Get the price of a return ticket to the destination city.".to_string(),
29    ///             parameters: serde_json::json!({
30    ///                 "type": "object",
31    ///                 "properties": {
32    ///                     "destination_city": {
33    ///                         "type": "string",
34    ///                         "description": "The destination city"
35    ///                     }
36    ///                 },
37    ///                 "required": ["destination_city"],
38    ///             }),
39    ///         }
40    ///     }
41    ///
42    ///     async fn call(&self, _args: Self::Args) -> Result<Self::Output, Self::Error> {
43    ///         println!("Tools called for {}", &args.destination_city);
44    ///         let result = Self::get_ticket_price(&args.destination_city)?;
45    ///         Ok(result)
46    ///     }
47    /// }
48    ///
49    /// let tool = ToolWrapper::new(MyTool);
50    /// ```
51    pub fn new(tool: T) -> Self {
52        Self(Box::new(tool))
53    }
54
55    pub(crate) fn tool(self) -> Box<T> {
56        self.0
57    }
58}
59
60pub(crate) struct NoTool;
61
62impl Tool for NoTool {
63    const NAME: &'static str = "";
64
65    type Error = Error;
66    type Args = ();
67    type Output = ();
68
69    async fn definition(&self, _prompt: String) -> rig::completion::ToolDefinition {
70        unreachable!("NoTool should never be used");
71    }
72
73    async fn call(&self, _args: Self::Args) -> Result<Self::Output, Self::Error> {
74        unreachable!("NoTool should never be used");
75    }
76}