This repository was archived by the owner on Mar 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathToolsTest.java
More file actions
363 lines (299 loc) · 15.6 KB
/
ToolsTest.java
File metadata and controls
363 lines (299 loc) · 15.6 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import com.github.copilot.sdk.events.AssistantMessageEvent;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.PermissionHandler;
import com.github.copilot.sdk.json.PermissionRequest;
import com.github.copilot.sdk.json.PermissionRequestResult;
import com.github.copilot.sdk.json.SessionConfig;
import com.github.copilot.sdk.json.ToolDefinition;
/**
* Tests for custom tools functionality.
*
* <p>
* These tests use the shared CapiProxy infrastructure for deterministic API
* response replay. Snapshots are stored in test/snapshots/tools/.
* </p>
*/
public class ToolsTest {
private static E2ETestContext ctx;
@BeforeAll
static void setup() throws Exception {
ctx = E2ETestContext.create();
}
@AfterAll
static void teardown() throws Exception {
if (ctx != null) {
ctx.close();
}
}
/**
* Verifies that built-in tools are invoked correctly.
*
* @see Snapshot: tools/invokes_built_in_tools
*/
@Test
void testInvokesBuiltInTools(TestInfo testInfo) throws Exception {
ctx.configureForTest("tools", "invokes_built_in_tools");
// Write a test file
Path readmeFile = ctx.getWorkDir().resolve("README.md");
Files.writeString(readmeFile, "# ELIZA, the only chatbot you'll ever need");
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
AssistantMessageEvent response = session
.sendAndWait(
new MessageOptions().setPrompt("What's the first line of README.md in this directory?"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("ELIZA"),
"Response should contain ELIZA: " + response.getData().content());
session.close();
}
}
/**
* Verifies that custom tools are invoked correctly.
*
* @see Snapshot: tools/invokes_custom_tool
*/
@Test
void testInvokesCustomTool(TestInfo testInfo) throws Exception {
ctx.configureForTest("tools", "invokes_custom_tool");
// Define a simple encrypt_string tool
var parameters = new HashMap<String, Object>();
var properties = new HashMap<String, Object>();
var inputProp = new HashMap<String, Object>();
inputProp.put("type", "string");
inputProp.put("description", "String to encrypt");
properties.put("input", inputProp);
parameters.put("type", "object");
parameters.put("properties", properties);
parameters.put("required", List.of("input"));
ToolDefinition encryptTool = ToolDefinition.create("encrypt_string", "Encrypts a string", parameters,
(invocation) -> {
Map<String, Object> args = invocation.getArguments();
String input = (String) args.get("input");
return CompletableFuture.completedFuture(input.toUpperCase());
});
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setTools(List.of(encryptTool))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Use encrypt_string to encrypt this string: Hello"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("HELLO"),
"Response should contain HELLO: " + response.getData().content());
session.close();
}
}
/**
* Verifies that tool calling errors are handled gracefully.
*
* @see Snapshot: tools/handles_tool_calling_errors
*/
@Test
void testHandlesToolCallingErrors(TestInfo testInfo) throws Exception {
ctx.configureForTest("tools", "handles_tool_calling_errors");
// Define a tool that throws an error
var parameters = new HashMap<String, Object>();
parameters.put("type", "object");
parameters.put("properties", new HashMap<>());
ToolDefinition errorTool = ToolDefinition.create("get_user_location", "Gets the user's location", parameters,
(invocation) -> {
CompletableFuture<Object> future = new CompletableFuture<>();
future.completeExceptionally(new RuntimeException("Melbourne"));
return future;
});
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setTools(List.of(errorTool))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions()
.setPrompt("What is my location? If you can't find out, just say 'unknown'."))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
// The error message should NOT be exposed to the assistant
String content = response.getData().content().toLowerCase();
assertFalse(content.contains("melbourne"), "Error details should not be exposed in response: " + content);
assertTrue(content.contains("unknown") || content.contains("unable") || content.contains("cannot"),
"Response should indicate inability to get location: " + content);
session.close();
}
}
/**
* Verifies that tools can receive and return complex types.
*
* @see Snapshot: tools/can_receive_and_return_complex_types
*/
@Test
void testCanReceiveAndReturnComplexTypes(TestInfo testInfo) throws Exception {
ctx.configureForTest("tools", "can_receive_and_return_complex_types");
// Define a db_query tool with complex parameter and return types
var querySchema = new HashMap<String, Object>();
var queryProps = new HashMap<String, Object>();
queryProps.put("table", Map.of("type", "string"));
queryProps.put("ids", Map.of("type", "array", "items", Map.of("type", "integer")));
queryProps.put("sortAscending", Map.of("type", "boolean"));
querySchema.put("type", "object");
querySchema.put("properties", queryProps);
querySchema.put("required", List.of("table", "ids", "sortAscending"));
var parameters = new HashMap<String, Object>();
var properties = new HashMap<String, Object>();
properties.put("query", querySchema);
parameters.put("type", "object");
parameters.put("properties", properties);
parameters.put("required", List.of("query"));
ToolDefinition dbQueryTool = ToolDefinition.create("db_query", "Performs a database query", parameters,
(invocation) -> {
Map<String, Object> args = invocation.getArguments();
@SuppressWarnings("unchecked")
Map<String, Object> query = (Map<String, Object>) args.get("query");
assertEquals("cities", query.get("table"));
// Return complex data structure
List<Map<String, Object>> results = List.of(
Map.of("countryId", 19, "cityName", "Passos", "population", 135460),
Map.of("countryId", 12, "cityName", "San Lorenzo", "population", 204356));
return CompletableFuture.completedFuture(results);
});
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setTools(List.of(dbQueryTool))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt(
"Perform a DB query for the 'cities' table using IDs 12 and 19, sorting ascending. "
+ "Reply only with lines of the form: [cityname] [population]"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
String content = response.getData().content();
assertTrue(content.contains("Passos"), "Response should contain Passos: " + content);
assertTrue(content.contains("San Lorenzo"), "Response should contain San Lorenzo: " + content);
session.close();
}
}
/**
* Verifies that a custom tool is invoked with the permission handler being
* called and can inspect the permission request kind.
*
* @see Snapshot: tools/invokes_custom_tool_with_permission_handler
*/
@Test
void testInvokesCustomToolWithPermissionHandler(TestInfo testInfo) throws Exception {
ctx.configureForTest("tools", "invokes_custom_tool_with_permission_handler");
var permissionRequests = new ArrayList<PermissionRequest>();
var parameters = new HashMap<String, Object>();
parameters.put("type", "object");
var props = new HashMap<String, Object>();
props.put("input", Map.of("type", "string"));
parameters.put("properties", props);
parameters.put("required", List.of("input"));
ToolDefinition encryptTool = ToolDefinition.create("encrypt_string", "Encrypts a string", parameters,
(invocation) -> {
Map<String, Object> args = invocation.getArguments();
String input = (String) args.get("input");
return CompletableFuture.completedFuture(input.toUpperCase());
});
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(
new SessionConfig().setTools(List.of(encryptTool)).setOnPermissionRequest((request, invocation) -> {
permissionRequests.add(request);
return PermissionHandler.APPROVE_ALL.handle(request, invocation);
})).get();
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Use encrypt_string to encrypt this string: Hello"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("HELLO"),
"Response should contain HELLO: " + response.getData().content());
// Should have received a custom-tool permission request
boolean hasCustomToolRequest = permissionRequests.stream()
.anyMatch(req -> "custom-tool".equals(req.getKind()));
assertTrue(hasCustomToolRequest, "Should have received a custom-tool permission request");
session.close();
}
}
/**
* Verifies that a custom tool is denied when the permission handler denies it.
*
* @see Snapshot: tools/denies_custom_tool_when_permission_denied
*/
@Test
void testDeniesCustomToolWhenPermissionDenied(TestInfo testInfo) throws Exception {
ctx.configureForTest("tools", "denies_custom_tool_when_permission_denied");
final boolean[] toolHandlerCalled = {false};
var parameters = new HashMap<String, Object>();
parameters.put("type", "object");
var props = new HashMap<String, Object>();
props.put("input", Map.of("type", "string"));
parameters.put("properties", props);
parameters.put("required", List.of("input"));
ToolDefinition encryptTool = ToolDefinition.create("encrypt_string", "Encrypts a string", parameters,
(invocation) -> {
toolHandlerCalled[0] = true;
Map<String, Object> args = invocation.getArguments();
String input = (String) args.get("input");
return CompletableFuture.completedFuture(input.toUpperCase());
});
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client
.createSession(
new SessionConfig().setTools(List.of(encryptTool))
.setOnPermissionRequest((request, invocation) -> CompletableFuture.completedFuture(
new PermissionRequestResult().setKind("denied-interactively-by-user"))))
.get();
session.sendAndWait(new MessageOptions().setPrompt("Use encrypt_string to encrypt this string: Hello"))
.get(60, TimeUnit.SECONDS);
// The tool handler should NOT have been called since permission was denied
assertFalse(toolHandlerCalled[0], "Tool handler should not be called when permission is denied");
session.close();
}
}
/**
* Verifies that a custom tool can override a built-in CLI tool with the same
* name when {@code overridesBuiltInTool} is set to {@code true}.
*
* @see Snapshot: tools/overrides_built_in_tool_with_custom_tool
*/
@Test
void testOverridesBuiltInToolWithCustomTool() throws Exception {
ctx.configureForTest("tools", "overrides_built_in_tool_with_custom_tool");
var parameters = new HashMap<String, Object>();
var properties = new HashMap<String, Object>();
properties.put("query", Map.of("type", "string", "description", "Search query"));
parameters.put("type", "object");
parameters.put("properties", properties);
parameters.put("required", List.of("query"));
ToolDefinition customGrep = ToolDefinition.createOverride("grep", "A custom grep implementation", parameters,
(invocation) -> {
Map<String, Object> args = invocation.getArguments();
String query = (String) args.get("query");
return CompletableFuture.completedFuture("CUSTOM_GREP_RESULT: " + query);
});
try (CopilotClient client = ctx.createClient()) {
CopilotSession session = client.createSession(new SessionConfig().setTools(List.of(customGrep))
.setOnPermissionRequest(PermissionHandler.APPROVE_ALL)).get();
AssistantMessageEvent response = session
.sendAndWait(new MessageOptions().setPrompt("Use grep to search for the word 'hello'"))
.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertTrue(response.getData().content().contains("CUSTOM_GREP_RESULT"),
"Response should contain CUSTOM_GREP_RESULT: " + response.getData().content());
session.close();
}
}
}