-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path07-custom-tools.rs
More file actions
186 lines (172 loc) · 5.77 KB
/
07-custom-tools.rs
File metadata and controls
186 lines (172 loc) · 5.77 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Example 7: Custom Tools
//
// Shows how to define and use custom tools alongside built-in tools.
//
// Run: cargo run --example 07-custom-tools
use async_trait::async_trait;
use open_agent_sdk::types::{self, Tool, ToolError, ToolInputSchema, ToolResult, ToolUseContext};
use open_agent_sdk::{Agent, AgentOptions, SDKMessage};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Arc;
// --- GetWeather tool ---
struct WeatherTool;
#[async_trait]
impl Tool for WeatherTool {
fn name(&self) -> &str {
"GetWeather"
}
fn description(&self) -> &str {
"Get current weather for a city. Returns temperature and conditions."
}
fn input_schema(&self) -> ToolInputSchema {
ToolInputSchema {
schema_type: "object".to_string(),
properties: HashMap::from([(
"city".to_string(),
json!({"type": "string", "description": "City name (e.g., \"Tokyo\", \"London\")"}),
)]),
required: vec!["city".to_string()],
additional_properties: Some(false),
}
}
fn is_read_only(&self, _: &Value) -> bool {
true
}
async fn call(&self, input: Value, _ctx: &ToolUseContext) -> Result<ToolResult, ToolError> {
let city = input
.get("city")
.and_then(|c| c.as_str())
.unwrap_or("Unknown");
let temps: HashMap<&str, i32> = HashMap::from([
("tokyo", 22),
("london", 14),
("beijing", 25),
("new york", 18),
("paris", 16),
]);
let temp = temps
.get(city.to_lowercase().as_str())
.copied()
.unwrap_or(20);
Ok(ToolResult::text(format!(
"Weather in {}: {}°C, partly cloudy",
city, temp
)))
}
}
// --- Calculator tool ---
struct CalculatorTool;
#[async_trait]
impl Tool for CalculatorTool {
fn name(&self) -> &str {
"Calculator"
}
fn description(&self) -> &str {
"Evaluate a simple mathematical expression with two numbers and an operator (+, -, *, /, **)."
}
fn input_schema(&self) -> ToolInputSchema {
ToolInputSchema {
schema_type: "object".to_string(),
properties: HashMap::from([(
"expression".to_string(),
json!({"type": "string", "description": "Math expression (e.g., \"2 ** 10\", \"42 * 17\")"}),
)]),
required: vec!["expression".to_string()],
additional_properties: Some(false),
}
}
fn is_read_only(&self, _: &Value) -> bool {
true
}
async fn call(&self, input: Value, _ctx: &ToolUseContext) -> Result<ToolResult, ToolError> {
let expr = input
.get("expression")
.and_then(|e| e.as_str())
.unwrap_or("0");
let result = eval_simple(expr);
Ok(ToolResult::text(match result {
Ok(val) => format!("{} = {}", expr, val),
Err(e) => format!("Error: {}", e),
}))
}
}
fn eval_simple(expr: &str) -> Result<String, String> {
for op in &["**", "*", "/", "+", "-"] {
if let Some(idx) = expr.find(op) {
if idx == 0 {
continue;
}
let left: f64 = expr[..idx]
.trim()
.parse()
.map_err(|_| "invalid number".to_string())?;
let right: f64 = expr[idx + op.len()..]
.trim()
.parse()
.map_err(|_| "invalid number".to_string())?;
let result = match *op {
"**" => left.powf(right),
"*" => left * right,
"/" => {
if right == 0.0 {
return Err("division by zero".to_string());
}
left / right
}
"+" => left + right,
"-" => left - right,
_ => return Err(format!("unknown operator: {}", op)),
};
return if result == (result as i64) as f64 {
Ok(format!("{}", result as i64))
} else {
Ok(format!("{:.4}", result))
};
}
}
Err(format!("cannot evaluate: {}", expr))
}
#[tokio::main]
async fn main() {
println!("--- Example 7: Custom Tools ---");
let mut agent = Agent::new(AgentOptions {
max_turns: Some(10),
custom_tools: vec![Arc::new(WeatherTool), Arc::new(CalculatorTool)],
..Default::default()
})
.await
.unwrap();
println!("Loaded tools with 2 custom tools (GetWeather, Calculator)\n");
let (mut rx, handle) = agent
.query("What is the weather in Tokyo and London? Also calculate 2 ** 10 * 3. Be brief.")
.await;
while let Some(event) = rx.recv().await {
match event {
SDKMessage::Assistant { message, .. } => {
for block in &message.content {
match block {
types::ContentBlock::ToolUse { name, input, .. } => {
println!("[{}] {}", name, serde_json::to_string(input).unwrap_or_default());
}
types::ContentBlock::Text { text } => {
if !text.trim().is_empty() {
println!("\n{}", text);
}
}
_ => {}
}
}
}
SDKMessage::Result { .. } => {
println!("\n--- Done ---");
}
SDKMessage::Error { message } => {
eprintln!("Error: {}", message);
}
_ => {}
}
}
handle.await.unwrap();
agent.close().await;
}