-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolRunner.java
More file actions
65 lines (54 loc) · 1.74 KB
/
ToolRunner.java
File metadata and controls
65 lines (54 loc) · 1.74 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
/*
* Copyright (c) 2024 Christian Stein
* Licensed under the Universal Permissive License v 1.0 -> https://opensource.org/license/upl
*/
package run.bach;
import java.lang.System.Logger.Level;
import java.util.function.UnaryOperator;
/**
* A runner of tool calls.
*
* <p>Usage example:
*
* <pre>{@code
* ToolRunner.ofSystem().run("java", "--version");
* }</pre>
*/
@FunctionalInterface
public interface ToolRunner {
/**
* {@return an instance of the default tool runner using the given tool finder}
*
* @param finder the finder instance to be used for finding tools by name
*/
static ToolRunner of(ToolFinder finder) {
return new ToolSpace(finder);
}
/** {@return an instance of the default tool runner using the system tool finder} */
static ToolRunner ofSilence() {
return new ToolSpace(ToolFinder.ofSystem(), Level.OFF, ToolSpace.Flag.SILENT);
}
/** {@return an instance of the default tool runner using the system tool finder} */
static ToolRunner ofSystem() {
class SystemRunner {
static final ToolRunner SINGLETON = ToolRunner.of(ToolFinder.ofSystem());
}
return SystemRunner.SINGLETON;
}
ToolRun run(ToolCall call);
default void log(Level level, String message) {
System.out.printf("[%s] %s".formatted(level.name().charAt(0), message));
}
default ToolRun run(Tool tool, String... args) {
return run(ToolCall.of(tool).addAll(args));
}
default ToolRun run(Tool tool, UnaryOperator<ToolCall> args) {
return run(args.apply(ToolCall.of(tool)));
}
default ToolRun run(String tool, String... args) {
return run(ToolCall.of(tool).addAll(args));
}
default ToolRun run(String tool, UnaryOperator<ToolCall> args) {
return run(args.apply(ToolCall.of(tool)));
}
}