-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathToolInvocation.java
More file actions
171 lines (156 loc) · 4.94 KB
/
ToolInvocation.java
File metadata and controls
171 lines (156 loc) · 4.94 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk.json;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Represents a tool invocation request from the AI assistant.
* <p>
* When the assistant invokes a tool, this object contains the context including
* the session ID, tool call ID, tool name, and arguments parsed from the
* assistant's request.
*
* @see ToolHandler
* @see ToolDefinition
* @since 1.0.0
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class ToolInvocation {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {
};
private String sessionId;
private String toolCallId;
private String toolName;
private JsonNode argumentsNode;
/**
* Gets the session ID where the tool was invoked.
*
* @return the session ID
*/
public String getSessionId() {
return sessionId;
}
/**
* Sets the session ID.
*
* @param sessionId
* the session ID
* @return this invocation for method chaining
*/
public ToolInvocation setSessionId(String sessionId) {
this.sessionId = sessionId;
return this;
}
/**
* Gets the unique identifier for this tool call.
* <p>
* This ID correlates the tool invocation with its response.
*
* @return the tool call ID
*/
public String getToolCallId() {
return toolCallId;
}
/**
* Sets the tool call ID.
*
* @param toolCallId
* the tool call ID
* @return this invocation for method chaining
*/
public ToolInvocation setToolCallId(String toolCallId) {
this.toolCallId = toolCallId;
return this;
}
/**
* Gets the name of the tool being invoked.
*
* @return the tool name
*/
public String getToolName() {
return toolName;
}
/**
* Sets the tool name.
*
* @param toolName
* the tool name
* @return this invocation for method chaining
*/
public ToolInvocation setToolName(String toolName) {
this.toolName = toolName;
return this;
}
/**
* Gets the arguments passed to the tool as a Map.
* <p>
* The arguments are provided as a {@code Map<String, Object>} matching the
* parameter schema defined in the tool's {@link ToolDefinition}. Values can be
* accessed using standard Map operations.
* <p>
* For type-safe access, use {@link #getArgumentsAs(Class)} to deserialize
* arguments into a record or POJO.
*
* @return the arguments as a Map, or null if no arguments
* @see #getArgumentsAs(Class)
*/
public Map<String, Object> getArguments() {
if (argumentsNode == null) {
return null;
}
return MAPPER.convertValue(argumentsNode, MAP_TYPE);
}
/**
* Deserializes the tool arguments into the specified type.
* <p>
* This method provides type-safe access to tool arguments by converting the
* JSON arguments into a record, POJO, or other compatible type.
*
* <pre>{@code
* // Define a record for your tool's arguments
* record WeatherArgs(String city) {
* }
*
* // In your tool handler
* WeatherArgs args = invocation.getArgumentsAs(WeatherArgs.class);
* String city = args.city();
* }</pre>
*
* @param <T>
* the type to deserialize to
* @param type
* the class of the target type
* @return the arguments deserialized as the specified type
* @throws IllegalArgumentException
* if deserialization fails
* @since 1.0.0
*/
public <T> T getArgumentsAs(Class<T> type) {
try {
return MAPPER.treeToValue(argumentsNode, type);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to deserialize arguments to " + type.getName(), e);
}
}
/**
* Sets the tool arguments.
* <p>
* <strong>Note:</strong> This method is intended for internal SDK use and JSON
* deserialization. Users typically do not need to call this method directly.
*
* @param arguments
* the arguments as a JsonNode
* @return this invocation for method chaining
*/
@JsonSetter("arguments")
public ToolInvocation setArguments(JsonNode arguments) {
this.argumentsNode = arguments;
return this;
}
}