| 34 | /// from the request's tool list. See the [Tool] class javadoc for |
| 35 | /// the full pattern. |
| 36 | public final class ToolCall { |
| 37 | private final String id; |
| 38 | private final String name; |
| 39 | private final String argumentsJson; |
| 40 | |
| 41 | public ToolCall(String id, String name, String argumentsJson) { |
| 42 | this.id = id; |
| 43 | this.name = name; |
| 44 | this.argumentsJson = argumentsJson == null ? "{}" : argumentsJson; |
| 45 | } |
| 46 | |
| 47 | public String getId() { |
| 48 | return id; |
| 49 | } |
| 50 | |
| 51 | public String getName() { |
| 52 | return name; |
| 53 | } |
| 54 | |
| 55 | public String getArgumentsJson() { |
| 56 | return argumentsJson; |
| 57 | } |
| 58 | |
| 59 | /// Finds the [Tool] whose name matches this call and invokes its |
| 60 | /// [ToolHandler] with this call's `argumentsJson`. Returns the |
| 61 | /// JSON result the handler produced. Apps typically wrap that in |
| 62 | /// a [ChatMessage#toolResult] and append it to the conversation |
| 63 | /// before the next chat turn. |
| 64 | /// |
| 65 | /// Throws `IllegalArgumentException` when no tool in `tools` |
| 66 | /// has a matching name, or `IllegalStateException` when the |
| 67 | /// matching tool has no handler registered. |
| 68 | public String execute(List<Tool> tools) throws Exception { |
| 69 | Tool match = findTool(tools); |
| 70 | if (match == null) { |
| 71 | throw new IllegalArgumentException( |
| 72 | "No tool registered with name '" + name + "'. " |
| 73 | + "Add it to the request's tool list, or dispatch by hand " |
| 74 | + "via getName() / getArgumentsJson()."); |
| 75 | } |
| 76 | return match.invoke(argumentsJson); |
| 77 | } |
| 78 | |
| 79 | /// Looks up the matching [Tool] without invoking it. Useful when |
| 80 | /// the caller wants to dispatch by hand but still benefit from |
| 81 | /// the name-matching plumbing. |
| 82 | public Tool findTool(List<Tool> tools) { |
| 83 | if (tools == null) { |
| 84 | return null; |
| 85 | } |
| 86 | for (Tool t : tools) { |
| 87 | if (t.getName().equals(name)) { |
| 88 | return t; |
| 89 | } |
| 90 | } |
| 91 | return null; |
| 92 | } |
| 93 | } |
nothing calls this directly
no outgoing calls
no test coverage detected