-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathToolsTest.java
More file actions
221 lines (181 loc) · 8.91 KB
/
ToolsTest.java
File metadata and controls
221 lines (181 loc) · 8.91 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
/*---------------------------------------------------------------------------------------------
* 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.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.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().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))).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))).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))).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();
}
}
}