-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcommands.rs
More file actions
291 lines (267 loc) · 10.8 KB
/
Copy pathcommands.rs
File metadata and controls
291 lines (267 loc) · 10.8 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
use std::sync::Arc;
use async_trait::async_trait;
use github_copilot_sdk::rpc::{
CommandsInvokeRequest, CommandsListRequest, CommandsRespondToQueuedCommandRequest,
EnqueueCommandParams, ExecuteCommandParams, RegisterEventInterestParams,
ReleaseEventInterestParams, SlashCommandInvocationResult, SlashCommandKind,
};
use github_copilot_sdk::session_events::{CommandQueuedData, SessionEventType};
use github_copilot_sdk::{CommandContext, CommandDefinition, CommandHandler, RequestId};
use serde_json::json;
use tokio::sync::mpsc;
use super::support::{recv_with_timeout, wait_for_event, with_e2e_context};
#[tokio::test]
async fn session_commands_list_returns_builtins_and_respects_client_command_filter() {
with_e2e_context(
"commands",
"session_with_commands_creates_successfully",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config().with_commands(vec![
CommandDefinition::new("rust-e2e-command", Arc::new(NoopCommandHandler))
.with_description("Rust E2E command"),
]))
.await
.expect("create session");
let all = session
.rpc()
.commands()
.list()
.await
.expect("list commands");
assert_command(&all.commands, "model", SlashCommandKind::Builtin);
assert_command(&all.commands, "compact", SlashCommandKind::Builtin);
assert_command(&all.commands, "context", SlashCommandKind::Builtin);
assert_command(&all.commands, "rust-e2e-command", SlashCommandKind::Client);
let no_builtins = session
.rpc()
.commands()
.list_with_params(CommandsListRequest {
include_builtins: Some(false),
include_client_commands: Some(true),
include_skills: Some(false),
})
.await
.expect("list without builtins");
assert!(
!no_builtins
.commands
.iter()
.any(|command| command.kind == SlashCommandKind::Builtin)
);
assert_command(
&no_builtins.commands,
"rust-e2e-command",
SlashCommandKind::Client,
);
let client_only_disabled = session
.rpc()
.commands()
.list_with_params(CommandsListRequest {
include_builtins: Some(false),
include_client_commands: Some(false),
include_skills: Some(false),
})
.await
.expect("list with all dynamic sources disabled");
assert!(client_only_disabled.commands.is_empty());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn session_commands_invoke_known_builtin_returns_expected_result() {
with_e2e_context(
"commands",
"session_with_no_commands_creates_successfully",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let result = session
.rpc()
.commands()
.invoke(CommandsInvokeRequest {
name: "context".to_string(),
input: None,
})
.await
.expect("invoke context");
match result {
SlashCommandInvocationResult::Text(text) => {
assert!(!text.text.trim().is_empty());
}
SlashCommandInvocationResult::SelectSubcommand(select) => {
assert!(!select.options.is_empty());
}
SlashCommandInvocationResult::AgentPrompt(prompt) => {
assert!(!prompt.prompt.trim().is_empty());
}
SlashCommandInvocationResult::Completed(_) => {}
}
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn session_commands_execute_runs_registered_command_handler() {
with_e2e_context(
"commands",
"session_with_commands_creates_successfully",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let (tx, mut rx) = mpsc::unbounded_channel();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config().with_commands(vec![
CommandDefinition::new(
"rust-execute",
Arc::new(RecordingCommandHandler { tx }),
)
.with_description("Records command invocations"),
]))
.await
.expect("create session");
let result = session
.rpc()
.commands()
.execute(ExecuteCommandParams {
command_name: "rust-execute".to_string(),
args: "alpha beta".to_string(),
})
.await
.expect("execute command");
assert!(result.error.is_none());
let context = recv_with_timeout(&mut rx, "command context").await;
assert_eq!(context.session_id, session.id().clone());
assert_eq!(context.command_name, "rust-execute");
assert_eq!(context.command, "/rust-execute alpha beta");
assert_eq!(context.args, "alpha beta");
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn session_commands_enqueue_and_respond_to_queued_command() {
with_e2e_context(
"commands",
"session_with_no_commands_creates_successfully",
|ctx| {
Box::pin(async move {
ctx.set_default_copilot_user();
let client = ctx.start_client().await;
let session = client
.create_session(ctx.approve_all_session_config())
.await
.expect("create session");
let interest = session
.rpc()
.event_log()
.register_interest(RegisterEventInterestParams {
event_type: "command.queued".to_string(),
})
.await
.expect("register command interest")
.handle;
let queued_event = wait_for_event(session.subscribe(), "command queued", |event| {
event.parsed_type() == SessionEventType::CommandQueued
});
let result = session
.rpc()
.commands()
.enqueue(EnqueueCommandParams {
command: "/help".to_string(),
})
.await
.expect("enqueue command");
assert!(result.queued);
let queued = queued_event
.await
.typed_data::<CommandQueuedData>()
.expect("command queued data");
assert_eq!(queued.command, "/help");
let response = session
.rpc()
.commands()
.respond_to_queued_command(CommandsRespondToQueuedCommandRequest {
request_id: queued.request_id,
result: json!({
"handled": true,
"stopProcessingQueue": true
}),
})
.await
.expect("respond to queued command");
assert!(response.success);
let missing = session
.rpc()
.commands()
.respond_to_queued_command(CommandsRespondToQueuedCommandRequest {
request_id: RequestId::from("missing-command-request"),
result: json!({
"handled": false,
"stopProcessingQueue": false
}),
})
.await
.expect("respond to missing queued command");
assert!(!missing.success);
session
.rpc()
.event_log()
.release_interest(ReleaseEventInterestParams { handle: interest })
.await
.expect("release command interest");
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
struct NoopCommandHandler;
#[async_trait]
impl CommandHandler for NoopCommandHandler {
async fn on_command(&self, _ctx: CommandContext) -> Result<(), github_copilot_sdk::Error> {
Ok(())
}
}
struct RecordingCommandHandler {
tx: mpsc::UnboundedSender<CommandContext>,
}
#[async_trait]
impl CommandHandler for RecordingCommandHandler {
async fn on_command(&self, ctx: CommandContext) -> Result<(), github_copilot_sdk::Error> {
self.tx.send(ctx).expect("record command context");
Ok(())
}
}
fn assert_command(
commands: &[github_copilot_sdk::rpc::SlashCommandInfo],
name: &str,
kind: SlashCommandKind,
) {
let command = commands
.iter()
.find(|command| command.name == name)
.unwrap_or_else(|| panic!("missing command {name}; actual commands: {commands:?}"));
assert_eq!(command.kind, kind);
assert!(!command.description.trim().is_empty());
}