forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathask_user_e2e_test.go
More file actions
176 lines (146 loc) · 4.79 KB
/
Copy pathask_user_e2e_test.go
File metadata and controls
176 lines (146 loc) · 4.79 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
package e2e
import (
"sync"
"testing"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/internal/e2e/testharness"
)
func TestAskUserE2E(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
t.Run("should invoke user input handler when model uses ask_user tool", func(t *testing.T) {
ctx.ConfigureForTest(t)
var userInputRequests []copilot.UserInputRequest
var mu sync.Mutex
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
OnUserInputRequest: func(request copilot.UserInputRequest, invocation copilot.UserInputInvocation) (copilot.UserInputResponse, error) {
mu.Lock()
userInputRequests = append(userInputRequests, request)
mu.Unlock()
if invocation.SessionID == "" {
t.Error("Expected non-empty session ID in invocation")
}
// Return the first choice if available, otherwise a freeform answer
answer := "freeform answer"
wasFreeform := true
if len(request.Choices) > 0 {
answer = request.Choices[0]
wasFreeform = false
}
return copilot.UserInputResponse{
Answer: answer,
WasFreeform: wasFreeform,
}, nil
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.SendAndWait(t.Context(), copilot.MessageOptions{
Prompt: "Ask me to choose between 'Option A' and 'Option B' using the ask_user tool. Wait for my response before continuing.",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
mu.Lock()
defer mu.Unlock()
if len(userInputRequests) == 0 {
t.Error("Expected at least one user input request")
}
hasQuestion := false
for _, req := range userInputRequests {
if req.Question != "" {
hasQuestion = true
break
}
}
if !hasQuestion {
t.Error("Expected at least one request with a question")
}
})
t.Run("should receive choices in user input request", func(t *testing.T) {
ctx.ConfigureForTest(t)
var userInputRequests []copilot.UserInputRequest
var mu sync.Mutex
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
OnUserInputRequest: func(request copilot.UserInputRequest, invocation copilot.UserInputInvocation) (copilot.UserInputResponse, error) {
mu.Lock()
userInputRequests = append(userInputRequests, request)
mu.Unlock()
// Pick the first choice
answer := "default"
if len(request.Choices) > 0 {
answer = request.Choices[0]
}
return copilot.UserInputResponse{
Answer: answer,
WasFreeform: false,
}, nil
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
_, err = session.SendAndWait(t.Context(), copilot.MessageOptions{
Prompt: "Use the ask_user tool to ask me to pick between exactly two options: 'Red' and 'Blue'. These should be provided as choices. Wait for my answer.",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
mu.Lock()
defer mu.Unlock()
if len(userInputRequests) == 0 {
t.Error("Expected at least one user input request")
}
hasChoices := false
for _, req := range userInputRequests {
if len(req.Choices) > 0 {
hasChoices = true
break
}
}
if !hasChoices {
t.Error("Expected at least one request with choices")
}
})
t.Run("should handle freeform user input response", func(t *testing.T) {
ctx.ConfigureForTest(t)
var userInputRequests []copilot.UserInputRequest
var mu sync.Mutex
freeformAnswer := "This is my custom freeform answer that was not in the choices"
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
OnUserInputRequest: func(request copilot.UserInputRequest, invocation copilot.UserInputInvocation) (copilot.UserInputResponse, error) {
mu.Lock()
userInputRequests = append(userInputRequests, request)
mu.Unlock()
// Return a freeform answer (not from choices)
return copilot.UserInputResponse{
Answer: freeformAnswer,
WasFreeform: true,
}, nil
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
response, err := session.SendAndWait(t.Context(), copilot.MessageOptions{
Prompt: "Ask me a question using ask_user and then include my answer in your response. The question should be 'What is your favorite color?'",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
mu.Lock()
defer mu.Unlock()
if len(userInputRequests) == 0 {
t.Error("Expected at least one user input request")
}
// The model's response should be defined
if response == nil {
t.Error("Expected non-nil response")
}
})
}