|
2 | 2 |
|
3 | 3 | import org.json.JSONObject; |
4 | 4 |
|
5 | | -public class CalculatorTool implements Tool { |
6 | | - |
7 | | - @Override |
8 | | - public String toolName() { |
9 | | - return "calculator"; |
10 | | - } |
11 | | - |
12 | | - @Override |
13 | | - public String description() { |
14 | | - return "Performs basic arithmetic operations: add, subtract, multiply, divide"; |
15 | | - } |
| 5 | +public interface CalculatorTool { |
16 | 6 |
|
17 | 7 | enum Field { operation, a, b } |
18 | 8 |
|
19 | | - @Override |
20 | | - public String inputSchema() { |
21 | | - return Tool.schema( |
22 | | - Prop.stringEnum(Field.operation, "The arithmetic operation to perform", |
23 | | - "add", "subtract", "multiply", "divide"), |
24 | | - Prop.number(Field.a, "First operand"), |
25 | | - Prop.number(Field.b, "Second operand") |
26 | | - ); |
| 9 | + static Tool create() { |
| 10 | + return Tool.of( |
| 11 | + "calculator", |
| 12 | + "Performs basic arithmetic operations: add, subtract, multiply, divide", |
| 13 | + Tool.schema( |
| 14 | + Tool.Prop.stringEnum(Field.operation, "The arithmetic operation to perform", |
| 15 | + "add", "subtract", "multiply", "divide"), |
| 16 | + Tool.Prop.number(Field.a, "First operand"), |
| 17 | + Tool.Prop.number(Field.b, "Second operand")), |
| 18 | + CalculatorTool::run); |
27 | 19 | } |
28 | 20 |
|
29 | | - @Override |
30 | | - public String execute(JSONObject input) { |
| 21 | + private static String run(JSONObject input) { |
31 | 22 | var operation = input.getString(Field.operation.name()); |
32 | 23 | var a = input.getDouble(Field.a.name()); |
33 | 24 | var b = input.getDouble(Field.b.name()); |
|
0 commit comments