Repository navigation
Expand file tree
/
Copy pathworkshop.rs
More file actions
382 lines (341 loc) · 13.4 KB
/
Copy pathworkshop.rs
File metadata and controls
382 lines (341 loc) · 13.4 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
use std::io::{self, Write};
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use github_copilot_sdk::handler::{PermissionHandler, PermissionResult};
use github_copilot_sdk::tool::{ToolHandler, schema_for};
use github_copilot_sdk::types::{
PermissionRequestData, PermissionRequestKind, RequestId, SessionId, Tool, ToolInvocation,
};
use github_copilot_sdk::{Client, Error, ToolResult};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
const FEED_URL: &str = "https://feeds.simplecast.com/ioCY0vfY";
struct Item {
title: Option<String>,
published: Option<String>,
description: Option<String>,
link: Option<String>,
duration: Option<String>,
}
#[derive(Debug, Serialize, Clone)]
pub struct EpisodeBrief {
pub episode_number: Option<i32>,
pub title: String,
pub published: String,
pub duration: String,
pub description: String,
pub episode_url: String,
pub source_feed_url: String,
}
#[derive(Deserialize, JsonSchema)]
struct EpisodeParams {
/// Optional GitHub Podcast episode title. Omit for the latest episode.
episode_title: Option<String>,
}
#[derive(Deserialize, JsonSchema)]
struct EmptyParams {}
struct EpisodeTool;
#[async_trait]
impl ToolHandler for EpisodeTool {
async fn call(&self, invocation: ToolInvocation) -> Result<ToolResult, Error> {
let params: EpisodeParams = serde_json::from_value(invocation.arguments)?;
let episode = get_episode(params.episode_title).await?;
Ok(ToolResult::Text(serde_json::to_string(&episode)?))
}
}
struct LatestEpisodesTool;
#[async_trait]
impl ToolHandler for LatestEpisodesTool {
async fn call(&self, _invocation: ToolInvocation) -> Result<ToolResult, Error> {
let episodes = get_latest_episodes().await?;
Ok(ToolResult::Text(serde_json::to_string(&episodes)?))
}
}
pub fn episode_tool() -> Tool {
Tool::new("get_github_podcast_episode")
.with_description("Gets a GitHub Podcast episode from the official RSS feed.")
.with_parameters(schema_for::<EpisodeParams>())
.with_handler(Arc::new(EpisodeTool))
}
pub fn latest_episodes_tool() -> Tool {
Tool::new("get_latest_github_podcast_episodes")
.with_description("Gets the ten newest GitHub Podcast episodes from the official RSS feed.")
.with_parameters(schema_for::<EmptyParams>())
.with_handler(Arc::new(LatestEpisodesTool))
}
/// Keeps the presenter as the approval point. Anything that is not this demo's own
/// local tool is denied, and a tool call must be approved on stdin.
struct PermissionPrompt;
#[async_trait]
impl PermissionHandler for PermissionPrompt {
async fn handle(
&self,
_session_id: SessionId,
_request_id: RequestId,
data: PermissionRequestData,
) -> PermissionResult {
let tool_name = match requested_tool_name(data) {
Ok(tool_name) => tool_name,
Err(feedback) => return PermissionResult::reject(feedback),
};
let answer = read_on_thread(move || {
print!("Approve {tool_name}? [y/N] ");
io::stdout().flush()?;
let mut answer = String::new();
io::stdin().read_line(&mut answer)?;
Ok(answer)
})
.await;
let answer = match answer {
Ok(answer) => answer,
Err(error) => return PermissionResult::reject(format!("Permission prompt failed: {error}")),
};
if answer.trim().eq_ignore_ascii_case("y") {
PermissionResult::approve_once()
} else {
PermissionResult::reject("The user did not approve the episode lookup.".to_owned())
}
}
}
fn requested_tool_name(data: PermissionRequestData) -> Result<String, String> {
let request = match data.extra.get("permissionRequest") {
Some(request) => serde_json::from_value::<PermissionRequestData>(request.clone())
.map_err(|error| format!("Invalid permission request: {error}"))?,
None => data,
};
if request.kind != Some(PermissionRequestKind::CustomTool) {
return Err("This demo only permits its GitHub Podcast episode lookup tools.".to_owned());
}
Ok(request
.extra
.get("toolName")
.and_then(|value| value.as_str())
.unwrap_or("custom tool")
.to_owned())
}
async fn read_on_thread<F>(read: F) -> io::Result<String>
where
F: FnOnce() -> io::Result<String> + Send + 'static,
{
let (sender, receiver) = tokio::sync::oneshot::channel();
std::thread::Builder::new()
.name("podcast-permission-input".to_owned())
.spawn(move || {
// A turn timeout may have already dropped the receiver.
let _ = sender.send(read());
})?;
receiver.await.map_err(io::Error::other)?
}
pub fn permission_prompt() -> Arc<dyn PermissionHandler> {
Arc::new(PermissionPrompt)
}
pub async fn get_latest_episodes() -> Result<Vec<EpisodeBrief>, Error> {
let items = get_items().await?;
Ok(items.iter().take(10).map(to_episode_brief).collect())
}
pub async fn get_episode(episode_title: Option<String>) -> Result<EpisodeBrief, Error> {
let items = get_items().await?;
let item = if let Some(title) = episode_title.filter(|title| !title.trim().is_empty()) {
items.iter().find(|item| item.title.as_deref().is_some_and(|candidate| candidate.eq_ignore_ascii_case(&title)))
} else {
items.first()
};
item.map(to_episode_brief)
.ok_or_else(|| std::io::Error::other("Episode was not found in the GitHub Podcast RSS feed").into())
}
pub fn pick_episode(episodes: &[EpisodeBrief]) -> Result<EpisodeBrief, Box<dyn std::error::Error>> {
if episodes.is_empty() {
return Err(std::io::Error::other("No GitHub Podcast episodes are available to select.").into());
}
println!("\nChoose a GitHub Podcast episode:");
for (index, episode) in episodes.iter().enumerate() {
println!(" {}. {}", index + 1, episode.title);
}
Ok(episodes[read_index("Episode [1]: ", episodes.len(), 0)?].clone())
}
pub async fn select_model(client: &Client, preferred_model: &str) -> Result<Option<String>, Box<dyn std::error::Error>> {
let models = client.list_models().await?;
if models.is_empty() {
return Ok(None);
}
let default_index = models
.iter()
.position(|model| model.id.eq_ignore_ascii_case(preferred_model))
.unwrap_or(0);
println!("Choose a Copilot model:");
for (index, model) in models.iter().enumerate() {
println!(" {}. {}", index + 1, model.id);
}
Ok(Some(models[read_index(&format!("Model [{}]: ", default_index + 1), models.len(), default_index)?].id.clone()))
}
async fn get_items() -> Result<Vec<Item>, Error> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
.build()
.map_err(|error| std::io::Error::other(error.to_string()))?;
get_items_from(&client, FEED_URL).await
}
async fn get_items_from(client: &reqwest::Client, url: &str) -> Result<Vec<Item>, Error> {
let response = client.get(url).send()
.await
.map_err(|error| std::io::Error::other(error.to_string()))?
.error_for_status()
.map_err(|error| std::io::Error::other(error.to_string()))?;
let xml = response
.text()
.await
.map_err(|error| std::io::Error::other(error.to_string()))?;
let document = roxmltree::Document::parse(&xml)
.map_err(|error| std::io::Error::other(error.to_string()))?;
Ok(document
.descendants()
.filter(|node| node.has_tag_name("item"))
.map(|item| Item {
title: child_text(item, "title", None),
published: child_text(item, "pubDate", None),
description: child_text(item, "description", None),
link: child_text(item, "link", None),
duration: child_text(item, "duration", Some("http://www.itunes.com/dtds/podcast-1.0.dtd")),
})
.collect())
}
fn to_episode_brief(item: &Item) -> EpisodeBrief {
EpisodeBrief {
episode_number: episode_number_from_title(item),
title: item.title.clone().unwrap_or_default(),
published: item.published.clone().unwrap_or_default(),
duration: item.duration.clone().unwrap_or_else(|| "Unknown".to_owned()),
description: strip_html(&item.description.clone().unwrap_or_default()),
episode_url: item.link.clone().unwrap_or_default(),
source_feed_url: FEED_URL.to_owned(),
}
}
fn episode_number_from_title(item: &Item) -> Option<i32> {
item.title
.as_ref()?
.split_once(':')?
.0
.parse::<i32>()
.ok()
}
fn child_text(item: roxmltree::Node<'_, '_>, name: &str, namespace: Option<&str>) -> Option<String> {
item.children()
.find(|node| node.is_element() && node.tag_name().name() == name && node.tag_name().namespace() == namespace)
.and_then(|node| node.text())
.map(str::trim)
.filter(|value| !value.is_empty())
.map(str::to_owned)
}
fn strip_html(value: &str) -> String {
let mut result = String::new();
let mut inside_tag = false;
for character in value.chars() {
match character {
'<' => inside_tag = true,
'>' => inside_tag = false,
_ if !inside_tag => result.push(character),
_ => {}
}
}
result.trim().to_owned()
}
fn read_index(prompt: &str, option_count: usize, default_index: usize) -> Result<usize, Box<dyn std::error::Error>> {
loop {
print!("{prompt}");
io::stdout().flush()?;
let mut answer = String::new();
io::stdin().read_line(&mut answer)?;
let answer = answer.trim();
if answer.is_empty() {
return Ok(default_index);
}
if let Ok(selection) = answer.parse::<usize>() {
if (1..=option_count).contains(&selection) {
return Ok(selection - 1);
}
}
println!("Enter a number from 1 to {option_count}.");
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use std::io::Read;
use std::net::TcpListener;
use std::time::Instant;
#[test]
fn accepts_flat_and_nested_custom_tool_requests() {
for name in ["get_github_podcast_episode", "get_latest_github_podcast_episodes"] {
let request = json!({"kind": "custom-tool", "toolName": name});
for payload in [request.clone(), json!({"permissionRequest": request})] {
let data = serde_json::from_value(payload).unwrap();
assert_eq!(requested_tool_name(data).unwrap(), name);
}
}
}
#[test]
fn denies_other_kinds_and_malformed_nested_requests() {
for payload in [
json!({"kind": "shell"}),
json!({"permissionRequest": {"kind": "mcp"}}),
json!({"kind": "custom-tool", "permissionRequest": null}),
json!({"kind": "custom-tool", "permissionRequest": "invalid"}),
json!({"permissionRequest": {}}),
] {
assert!(requested_tool_name(serde_json::from_value(payload).unwrap()).is_err());
}
}
#[tokio::test]
async fn reports_input_errors() {
let result = read_on_thread(|| Err(io::Error::other("input unavailable"))).await;
assert_eq!(result.unwrap_err().to_string(), "input unavailable");
}
#[test]
fn canceled_input_does_not_block_runtime_shutdown() {
let (sender, receiver) = std::sync::mpsc::channel::<String>();
let release = std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(1500));
sender.send("n".to_owned()).unwrap();
});
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
let start = Instant::now();
let result = runtime.block_on(async {
tokio::time::timeout(
Duration::from_millis(20),
read_on_thread(move || receiver.recv().map_err(io::Error::other)),
).await
});
assert!(result.is_err());
drop(runtime);
let elapsed = start.elapsed();
release.join().unwrap();
assert!(elapsed < Duration::from_secs(1), "Shutdown waited for input: {elapsed:?}");
}
#[tokio::test]
async fn bounds_stalled_headers_and_bodies() {
for send_headers in [false, true] {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}/", listener.local_addr().unwrap());
let server = std::thread::spawn(move || {
let (mut stream, _) = listener.accept().unwrap();
stream.set_read_timeout(Some(Duration::from_secs(2))).unwrap();
let mut request = [0; 4096];
stream.read(&mut request).unwrap();
if send_headers {
stream.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\n").unwrap();
stream.flush().unwrap();
}
std::thread::sleep(Duration::from_millis(500));
});
let client = reqwest::Client::builder().no_proxy()
.timeout(Duration::from_millis(100)).build().unwrap();
let start = Instant::now();
let result = get_items_from(&client, &url).await;
assert!(result.is_err());
assert!(start.elapsed() < Duration::from_millis(400));
server.join().unwrap();
}
}
}