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 = ();
23 /// type Output = ();
24 ///
25 /// async fn definition(&self, _prompt: String) -> rig::completion::ToolDefinition {
26 /// unreachable!("MyTool should never be used");
27 /// }
28 ///
29 /// async fn call(&self, _args: Self::Args) -> Result<Self::Output, Self::Error> {
30 /// unreachable!("MyTool should never be used");
31 /// }
32 /// }
33 ///
34 /// let tool = ToolWrapper::new(MyTool);
35 /// ```
36 pub fn new(tool: T) -> Self {
37 Self(Box::new(tool))
38 }
39
40 pub(crate) fn tool(self) -> Box<T> {
41 self.0
42 }
43}
44
45pub(crate) struct NoTool;
46
47impl Tool for NoTool {
48 const NAME: &'static str = "";
49
50 type Error = Error;
51 type Args = ();
52 type Output = ();
53
54 async fn definition(&self, _prompt: String) -> rig::completion::ToolDefinition {
55 unreachable!("NoTool should never be used");
56 }
57
58 async fn call(&self, _args: Self::Args) -> Result<Self::Output, Self::Error> {
59 unreachable!("NoTool should never be used");
60 }
61}