-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtools_test.go
More file actions
449 lines (378 loc) · 14.5 KB
/
Copy pathtools_test.go
File metadata and controls
449 lines (378 loc) · 14.5 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package e2e
import (
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/internal/e2e/testharness"
)
func TestTools(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
t.Run("invokes built-in tools", func(t *testing.T) {
ctx.ConfigureForTest(t)
// Write a test file
err := os.WriteFile(filepath.Join(ctx.WorkDir, "README.md"), []byte("# ELIZA, the only chatbot you'll ever need"), 0644)
if err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "What's the first line of README.md in this directory?"})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if answer.Data.Content == nil || !strings.Contains(*answer.Data.Content, "ELIZA") {
t.Errorf("Expected answer to contain 'ELIZA', got %v", answer.Data.Content)
}
})
t.Run("invokes custom tool", func(t *testing.T) {
ctx.ConfigureForTest(t)
type EncryptParams struct {
Input string `json:"input" jsonschema:"String to encrypt"`
}
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Tools: []copilot.Tool{
copilot.DefineTool("encrypt_string", "Encrypts a string",
func(params EncryptParams, inv copilot.ToolInvocation) (string, error) {
return strings.ToUpper(params.Input), nil
}),
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "Use encrypt_string to encrypt this string: Hello"})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if answer.Data.Content == nil || !strings.Contains(*answer.Data.Content, "HELLO") {
t.Errorf("Expected answer to contain 'HELLO', got %v", answer.Data.Content)
}
})
t.Run("handles tool calling errors", func(t *testing.T) {
ctx.ConfigureForTest(t)
type EmptyParams struct{}
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Tools: []copilot.Tool{
copilot.DefineTool("get_user_location", "Gets the user's location",
func(params EmptyParams, inv copilot.ToolInvocation) (any, error) {
return nil, errors.New("Melbourne")
}),
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{
Prompt: "What is my location? If you can't find out, just say 'unknown'.",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
// Check the underlying traffic
traffic, err := ctx.GetExchanges()
if err != nil {
t.Fatalf("Failed to get exchanges: %v", err)
}
lastConversation := traffic[len(traffic)-1]
// Find tool calls
var toolCalls []testharness.ToolCall
for _, msg := range lastConversation.Request.Messages {
if msg.Role == "assistant" && msg.ToolCalls != nil {
toolCalls = append(toolCalls, msg.ToolCalls...)
}
}
if len(toolCalls) != 1 {
t.Fatalf("Expected 1 tool call, got %d", len(toolCalls))
}
toolCall := toolCalls[0]
if toolCall.Type != "function" {
t.Errorf("Expected tool call type 'function', got '%s'", toolCall.Type)
}
if toolCall.Function.Name != "get_user_location" {
t.Errorf("Expected tool call name 'get_user_location', got '%s'", toolCall.Function.Name)
}
// Find tool results
var toolResults []testharness.Message
for _, msg := range lastConversation.Request.Messages {
if msg.Role == "tool" {
toolResults = append(toolResults, msg)
}
}
if len(toolResults) != 1 {
t.Fatalf("Expected 1 tool result, got %d", len(toolResults))
}
toolResult := toolResults[0]
if toolResult.ToolCallID != toolCall.ID {
t.Errorf("Expected tool result ID '%s', got '%s'", toolCall.ID, toolResult.ToolCallID)
}
// The error message "Melbourne" should NOT be exposed to the LLM
if strings.Contains(toolResult.Content, "Melbourne") {
t.Errorf("Tool result should not contain error details 'Melbourne', got '%s'", toolResult.Content)
}
// The assistant should not see the exception information
if answer.Data.Content != nil && strings.Contains(*answer.Data.Content, "Melbourne") {
t.Errorf("Assistant should not see error details 'Melbourne', got '%s'", *answer.Data.Content)
}
if answer.Data.Content == nil || !strings.Contains(strings.ToLower(*answer.Data.Content), "unknown") {
t.Errorf("Expected answer to contain 'unknown', got %v", answer.Data.Content)
}
})
t.Run("can receive and return complex types", func(t *testing.T) {
ctx.ConfigureForTest(t)
type DbQuery struct {
Table string `json:"table"`
IDs []int `json:"ids"`
SortAscending bool `json:"sortAscending"`
}
type DbQueryParams struct {
Query DbQuery `json:"query"`
}
type City struct {
CountryID int `json:"countryId"`
CityName string `json:"cityName"`
Population int `json:"population"`
}
var receivedInvocation *copilot.ToolInvocation
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Tools: []copilot.Tool{
copilot.DefineTool("db_query", "Performs a database query",
func(params DbQueryParams, inv copilot.ToolInvocation) ([]City, error) {
receivedInvocation = &inv
if params.Query.Table != "cities" {
t.Errorf("Expected table 'cities', got '%s'", params.Query.Table)
}
if len(params.Query.IDs) != 2 || params.Query.IDs[0] != 12 || params.Query.IDs[1] != 19 {
t.Errorf("Expected IDs [12, 19], got %v", params.Query.IDs)
}
if !params.Query.SortAscending {
t.Errorf("Expected sortAscending to be true")
}
return []City{
{CountryID: 19, CityName: "Passos", Population: 135460},
{CountryID: 12, CityName: "San Lorenzo", Population: 204356},
}, nil
}),
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{
Prompt: "Perform a DB query for the 'cities' table using IDs 12 and 19, sorting ascending. " +
"Reply only with lines of the form: [cityname] [population]",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if answer == nil || answer.Data.Content == nil {
t.Fatalf("Expected assistant message with content")
}
responseContent := *answer.Data.Content
if responseContent == "" {
t.Errorf("Expected non-empty response")
}
if !strings.Contains(responseContent, "Passos") {
t.Errorf("Expected response to contain 'Passos', got '%s'", responseContent)
}
if !strings.Contains(responseContent, "San Lorenzo") {
t.Errorf("Expected response to contain 'San Lorenzo', got '%s'", responseContent)
}
// Remove commas for number checking (e.g., "135,460" -> "135460")
responseWithoutCommas := strings.ReplaceAll(responseContent, ",", "")
if !strings.Contains(responseWithoutCommas, "135460") {
t.Errorf("Expected response to contain '135460', got '%s'", responseContent)
}
if !strings.Contains(responseWithoutCommas, "204356") {
t.Errorf("Expected response to contain '204356', got '%s'", responseContent)
}
// We can access the raw invocation if needed
if receivedInvocation == nil {
t.Fatalf("Expected to receive invocation")
}
if receivedInvocation.SessionID != session.SessionID {
t.Errorf("Expected session ID '%s', got '%s'", session.SessionID, receivedInvocation.SessionID)
}
})
t.Run("skipPermission sent in tool definition", func(t *testing.T) {
ctx.ConfigureForTest(t)
type LookupParams struct {
ID string `json:"id" jsonschema:"ID to look up"`
}
safeLookupTool := copilot.DefineTool("safe_lookup", "A safe lookup that skips permission",
func(params LookupParams, inv copilot.ToolInvocation) (string, error) {
return "RESULT: " + params.ID, nil
})
safeLookupTool.SkipPermission = true
didRunPermissionRequest := false
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: func(request copilot.PermissionRequest, invocation copilot.PermissionInvocation) (copilot.PermissionRequestResult, error) {
didRunPermissionRequest = true
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindNoResult}, nil
},
Tools: []copilot.Tool{
safeLookupTool,
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "Use safe_lookup to look up 'test123'"})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if answer.Data.Content == nil || !strings.Contains(*answer.Data.Content, "RESULT: test123") {
t.Errorf("Expected answer to contain 'RESULT: test123', got %v", answer.Data.Content)
}
if didRunPermissionRequest {
t.Errorf("Expected permission handler to NOT be called for skipPermission tool")
}
})
t.Run("overrides built-in tool with custom tool", func(t *testing.T) {
ctx.ConfigureForTest(t)
type GrepParams struct {
Query string `json:"query" jsonschema:"Search query"`
}
grepTool := copilot.DefineTool("grep", "A custom grep implementation that overrides the built-in",
func(params GrepParams, inv copilot.ToolInvocation) (string, error) {
return "CUSTOM_GREP_RESULT: " + params.Query, nil
})
grepTool.OverridesBuiltInTool = true
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Tools: []copilot.Tool{
grepTool,
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "Use grep to search for the word 'hello'"})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if answer.Data.Content == nil || !strings.Contains(*answer.Data.Content, "CUSTOM_GREP_RESULT") {
t.Errorf("Expected answer to contain 'CUSTOM_GREP_RESULT', got %v", answer.Data.Content)
}
})
t.Run("invokes custom tool with permission handler", func(t *testing.T) {
ctx.ConfigureForTest(t)
type EncryptParams struct {
Input string `json:"input" jsonschema:"String to encrypt"`
}
var permissionRequests []copilot.PermissionRequest
var mu sync.Mutex
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
Tools: []copilot.Tool{
copilot.DefineTool("encrypt_string", "Encrypts a string",
func(params EncryptParams, inv copilot.ToolInvocation) (string, error) {
return strings.ToUpper(params.Input), nil
}),
},
OnPermissionRequest: func(request copilot.PermissionRequest, invocation copilot.PermissionInvocation) (copilot.PermissionRequestResult, error) {
mu.Lock()
permissionRequests = append(permissionRequests, request)
mu.Unlock()
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindApproved}, nil
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "Use encrypt_string to encrypt this string: Hello"})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
answer, err := testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if answer.Data.Content == nil || !strings.Contains(*answer.Data.Content, "HELLO") {
t.Errorf("Expected answer to contain 'HELLO', got %v", answer.Data.Content)
}
// Should have received a custom-tool permission request
mu.Lock()
customToolReqs := 0
for _, req := range permissionRequests {
if req.Kind == "custom-tool" {
customToolReqs++
if req.ToolName == nil || *req.ToolName != "encrypt_string" {
t.Errorf("Expected toolName 'encrypt_string', got '%v'", req.ToolName)
}
}
}
mu.Unlock()
if customToolReqs == 0 {
t.Errorf("Expected at least one custom-tool permission request, got none")
}
})
t.Run("denies custom tool when permission denied", func(t *testing.T) {
ctx.ConfigureForTest(t)
type EncryptParams struct {
Input string `json:"input" jsonschema:"String to encrypt"`
}
toolHandlerCalled := false
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
Tools: []copilot.Tool{
copilot.DefineTool("encrypt_string", "Encrypts a string",
func(params EncryptParams, inv copilot.ToolInvocation) (string, error) {
toolHandlerCalled = true
return strings.ToUpper(params.Input), nil
}),
},
OnPermissionRequest: func(request copilot.PermissionRequest, invocation copilot.PermissionInvocation) (copilot.PermissionRequestResult, error) {
return copilot.PermissionRequestResult{Kind: copilot.PermissionRequestResultKindDeniedInteractivelyByUser}, nil
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.Send(t.Context(), copilot.MessageOptions{Prompt: "Use encrypt_string to encrypt this string: Hello"})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
_, err = testharness.GetFinalAssistantMessage(t.Context(), session)
if err != nil {
t.Fatalf("Failed to get assistant message: %v", err)
}
if toolHandlerCalled {
t.Errorf("Tool handler should NOT have been called since permission was denied")
}
})
}