Skip to content

Commit c5d5320

Browse files
committed
CalculatorTools and CurrentTimeTool refactored
1 parent e296ef6 commit c5d5320

5 files changed

Lines changed: 24 additions & 47 deletions

File tree

zsmith/src/main/java/airhacks/zsmith/tools/boundary/Tools.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
public enum Tools implements Tool {
2020

21-
CALCULATOR(new CalculatorTool()),
22-
CURRENT_TIME(new CurrentTimeTool()),
21+
CALCULATOR(CalculatorTool.create()),
22+
CURRENT_TIME(CurrentTimeTool.create()),
2323
READ_CLIPBOARD(new ReadClipboardTool()),
2424
WRITE_CLIPBOARD(new WriteClipboardTool()),
2525
READ_ANY_FILE(new ReadAnyFileTool()),

zsmith/src/main/java/airhacks/zsmith/tools/control/CalculatorTool.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@
22

33
import org.json.JSONObject;
44

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 {
166

177
enum Field { operation, a, b }
188

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);
2719
}
2820

29-
@Override
30-
public String execute(JSONObject input) {
21+
private static String run(JSONObject input) {
3122
var operation = input.getString(Field.operation.name());
3223
var a = input.getDouble(Field.a.name());
3324
var b = input.getDouble(Field.b.name());

zsmith/src/main/java/airhacks/zsmith/tools/control/CurrentTimeTool.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,15 @@
33
import java.time.LocalDateTime;
44
import java.time.format.DateTimeFormatter;
55

6-
import org.json.JSONObject;
6+
public interface CurrentTimeTool {
77

8-
public class CurrentTimeTool implements Tool {
8+
DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
99

10-
@Override
11-
public String toolName() {
12-
return "current_time";
13-
}
14-
15-
@Override
16-
public String description() {
17-
return "Returns the current date and time";
18-
}
19-
20-
@Override
21-
public String inputSchema() {
22-
return Tool.emptySchema();
23-
}
24-
25-
@Override
26-
public String execute(JSONObject input) {
27-
var now = LocalDateTime.now();
28-
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
29-
return now.format(formatter);
10+
static Tool create() {
11+
return Tool.of(
12+
"current_time",
13+
"Returns the current date and time",
14+
Tool.emptySchema(),
15+
input -> LocalDateTime.now().format(FORMAT));
3016
}
3117
}

zsmith/src/test/java/LinkCheckerToolRegistrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void main() {
1111

1212
// can be registered alongside other tools
1313
var multi = new Agent().withSystemPrompt("You are a helpful assistant.")
14-
.withTool(new CalculatorTool())
14+
.withTool(CalculatorTool.create())
1515
.withTool(new LinkCheckerTool());
1616
assert multi.tools().containsKey("check_link") : "agent should contain 'check_link' tool";
1717
assert multi.tools().containsKey("calculator") : "agent should contain 'calculator' tool";

zsmith/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2026.06.13.01
1+
2026.06.13.02

0 commit comments

Comments
 (0)