-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathask_user.rs
More file actions
206 lines (187 loc) · 7.59 KB
/
Copy pathask_user.rs
File metadata and controls
206 lines (187 loc) · 7.59 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
use std::sync::Arc;
use async_trait::async_trait;
use github_copilot_sdk::handler::{
PermissionHandler, PermissionResult, UserInputHandler, UserInputResponse,
};
use github_copilot_sdk::{RequestId, SessionConfig, SessionId};
use tokio::sync::mpsc;
use super::support::{
DEFAULT_TEST_TOKEN, assistant_message_content, recv_with_timeout, with_e2e_context,
};
#[tokio::test]
async fn should_invoke_user_input_handler_when_model_uses_ask_user_tool() {
with_e2e_context(
"ask_user",
"should_invoke_user_input_handler_when_model_uses_ask_user_tool",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let (request_tx, mut request_rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let handler = Arc::new(RecordingUserInputHandler {
request_tx,
answer: UserInputAnswer::FirstChoiceOrFreeform("freeform answer"),
});
let session = client
.create_session(
SessionConfig::default()
.with_github_token(DEFAULT_TEST_TOKEN)
.with_user_input_handler(handler.clone() as Arc<dyn UserInputHandler>)
.with_permission_handler(handler as Arc<dyn PermissionHandler>),
)
.await
.expect("create session");
session
.send_and_wait(
"Ask me to choose between 'Option A' and 'Option B' using the ask_user tool. \
Wait for my response before continuing.",
)
.await
.expect("send");
let request = recv_with_timeout(&mut request_rx, "user input request").await;
assert_eq!(request.session_id, *session.id());
assert!(!request.question.is_empty());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_receive_choices_in_user_input_request() {
with_e2e_context(
"ask_user",
"should_receive_choices_in_user_input_request",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let (request_tx, mut request_rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let handler = Arc::new(RecordingUserInputHandler {
request_tx,
answer: UserInputAnswer::FirstChoiceOrFreeform("default"),
});
let session = client
.create_session(
SessionConfig::default()
.with_github_token(DEFAULT_TEST_TOKEN)
.with_user_input_handler(handler.clone() as Arc<dyn UserInputHandler>)
.with_permission_handler(handler as Arc<dyn PermissionHandler>),
)
.await
.expect("create session");
session
.send_and_wait(
"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.",
)
.await
.expect("send");
let request = recv_with_timeout(&mut request_rx, "user input request").await;
let choices = request.choices.expect("choices");
assert!(choices.iter().any(|choice| choice == "Red"));
assert!(choices.iter().any(|choice| choice == "Blue"));
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn should_handle_freeform_user_input_response() {
with_e2e_context(
"ask_user",
"should_handle_freeform_user_input_response",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let freeform_answer =
"This is my custom freeform answer that was not in the choices";
let (request_tx, mut request_rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let handler = Arc::new(RecordingUserInputHandler {
request_tx,
answer: UserInputAnswer::Freeform(freeform_answer),
});
let session = client
.create_session(
SessionConfig::default()
.with_github_token(DEFAULT_TEST_TOKEN)
.with_user_input_handler(handler.clone() as Arc<dyn UserInputHandler>)
.with_permission_handler(handler as Arc<dyn PermissionHandler>),
)
.await
.expect("create session");
let answer = session
.send_and_wait(
"Ask me a question using ask_user and then include my answer in your response. \
The question should be 'What is your favorite color?'",
)
.await
.expect("send")
.expect("assistant message");
let request = recv_with_timeout(&mut request_rx, "user input request").await;
assert!(!request.question.is_empty());
assert!(assistant_message_content(&answer).contains(freeform_answer));
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[derive(Debug)]
struct RecordedUserInputRequest {
session_id: SessionId,
question: String,
choices: Option<Vec<String>>,
}
struct RecordingUserInputHandler {
request_tx: mpsc::UnboundedSender<RecordedUserInputRequest>,
answer: UserInputAnswer,
}
enum UserInputAnswer {
FirstChoiceOrFreeform(&'static str),
Freeform(&'static str),
}
#[async_trait]
impl UserInputHandler for RecordingUserInputHandler {
async fn handle(
&self,
session_id: SessionId,
question: String,
choices: Option<Vec<String>>,
allow_freeform: Option<bool>,
) -> Option<UserInputResponse> {
let _ = self.request_tx.send(RecordedUserInputRequest {
session_id,
question,
choices: choices.clone(),
});
let (answer, was_freeform) = match (&self.answer, choices.as_ref().and_then(|c| c.first()))
{
(UserInputAnswer::FirstChoiceOrFreeform(_), Some(choice)) => (choice.clone(), false),
(UserInputAnswer::FirstChoiceOrFreeform(fallback), None) => {
((*fallback).to_string(), allow_freeform.unwrap_or(true))
}
(UserInputAnswer::Freeform(answer), _) => ((*answer).to_string(), true),
};
Some(UserInputResponse {
answer,
was_freeform,
})
}
}
#[async_trait]
impl PermissionHandler for RecordingUserInputHandler {
async fn handle(
&self,
_session_id: SessionId,
_request_id: RequestId,
_data: github_copilot_sdk::PermissionRequestData,
) -> PermissionResult {
PermissionResult::approve_once()
}
}