|
4 | 4 |
|
5 | 5 | package com.github.copilot.sdk.json; |
6 | 6 |
|
| 7 | +import java.util.Map; |
| 8 | + |
7 | 9 | import com.fasterxml.jackson.annotation.JsonInclude; |
| 10 | +import com.fasterxml.jackson.annotation.JsonSetter; |
| 11 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 12 | +import com.fasterxml.jackson.databind.JsonNode; |
| 13 | +import com.fasterxml.jackson.databind.ObjectMapper; |
8 | 14 |
|
9 | 15 | /** |
10 | 16 | * Represents a tool invocation request from the AI assistant. |
|
20 | 26 | @JsonInclude(JsonInclude.Include.NON_NULL) |
21 | 27 | public final class ToolInvocation { |
22 | 28 |
|
| 29 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 30 | + private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() { |
| 31 | + }; |
| 32 | + |
23 | 33 | private String sessionId; |
24 | 34 | private String toolCallId; |
25 | 35 | private String toolName; |
26 | | - private Object arguments; |
| 36 | + private JsonNode argumentsNode; |
27 | 37 |
|
28 | 38 | /** |
29 | 39 | * Gets the session ID where the tool was invoked. |
@@ -91,26 +101,71 @@ public ToolInvocation setToolName(String toolName) { |
91 | 101 | } |
92 | 102 |
|
93 | 103 | /** |
94 | | - * Gets the arguments passed to the tool. |
| 104 | + * Gets the arguments passed to the tool as a Map. |
| 105 | + * <p> |
| 106 | + * The arguments are provided as a {@code Map<String, Object>} matching the |
| 107 | + * parameter schema defined in the tool's {@link ToolDefinition}. Values can be |
| 108 | + * accessed using standard Map operations. |
| 109 | + * <p> |
| 110 | + * For type-safe access, use {@link #getArgumentsAs(Class)} to deserialize |
| 111 | + * arguments into a record or POJO. |
| 112 | + * |
| 113 | + * @return the arguments as a Map, or null if no arguments |
| 114 | + * @see #getArgumentsAs(Class) |
| 115 | + */ |
| 116 | + public Map<String, Object> getArguments() { |
| 117 | + if (argumentsNode == null) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + return MAPPER.convertValue(argumentsNode, MAP_TYPE); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Deserializes the tool arguments into the specified type. |
95 | 125 | * <p> |
96 | | - * This is typically a {@code Map<String, Object>} matching the parameter schema |
97 | | - * defined in the tool's {@link ToolDefinition}. |
| 126 | + * This method provides type-safe access to tool arguments by converting the |
| 127 | + * JSON arguments into a record, POJO, or other compatible type. |
98 | 128 | * |
99 | | - * @return the arguments object |
| 129 | + * <pre>{@code |
| 130 | + * // Define a record for your tool's arguments |
| 131 | + * record WeatherArgs(String city) { |
| 132 | + * } |
| 133 | + * |
| 134 | + * // In your tool handler |
| 135 | + * WeatherArgs args = invocation.getArgumentsAs(WeatherArgs.class); |
| 136 | + * String city = args.city(); |
| 137 | + * }</pre> |
| 138 | + * |
| 139 | + * @param <T> |
| 140 | + * the type to deserialize to |
| 141 | + * @param type |
| 142 | + * the class of the target type |
| 143 | + * @return the arguments deserialized as the specified type |
| 144 | + * @throws IllegalArgumentException |
| 145 | + * if deserialization fails |
| 146 | + * @since 1.0.0 |
100 | 147 | */ |
101 | | - public Object getArguments() { |
102 | | - return arguments; |
| 148 | + public <T> T getArgumentsAs(Class<T> type) { |
| 149 | + try { |
| 150 | + return MAPPER.treeToValue(argumentsNode, type); |
| 151 | + } catch (Exception e) { |
| 152 | + throw new IllegalArgumentException("Failed to deserialize arguments to " + type.getName(), e); |
| 153 | + } |
103 | 154 | } |
104 | 155 |
|
105 | 156 | /** |
106 | 157 | * Sets the tool arguments. |
| 158 | + * <p> |
| 159 | + * <strong>Note:</strong> This method is intended for internal SDK use and JSON |
| 160 | + * deserialization. Users typically do not need to call this method directly. |
107 | 161 | * |
108 | 162 | * @param arguments |
109 | | - * the arguments object |
| 163 | + * the arguments as a JsonNode |
110 | 164 | * @return this invocation for method chaining |
111 | 165 | */ |
112 | | - public ToolInvocation setArguments(Object arguments) { |
113 | | - this.arguments = arguments; |
| 166 | + @JsonSetter("arguments") |
| 167 | + public ToolInvocation setArguments(JsonNode arguments) { |
| 168 | + this.argumentsNode = arguments; |
114 | 169 | return this; |
115 | 170 | } |
116 | 171 | } |
0 commit comments