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