forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
106 lines (91 loc) · 2.92 KB
/
Copy pathmain.rs
File metadata and controls
106 lines (91 loc) · 2.92 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
//! User-input callback — answer the agent's `ask_user` prompts and log
//! every question.
use std::sync::Arc;
use async_trait::async_trait;
use github_copilot_sdk::handler::{
PermissionHandler, PermissionResult, UserInputHandler, UserInputResponse,
};
use github_copilot_sdk::hooks::{HookContext, PreToolUseInput, PreToolUseOutput, SessionHooks};
use github_copilot_sdk::types::{PermissionRequestData, RequestId, SessionConfig, SessionId};
use github_copilot_sdk::{Client, ClientOptions};
use tokio::sync::Mutex;
struct InputResponder {
log: Arc<Mutex<Vec<String>>>,
}
#[async_trait]
impl PermissionHandler for InputResponder {
async fn handle(
&self,
_session_id: SessionId,
_request_id: RequestId,
_data: PermissionRequestData,
) -> PermissionResult {
PermissionResult::approve_once()
}
}
#[async_trait]
impl UserInputHandler for InputResponder {
async fn handle(
&self,
_session_id: SessionId,
question: String,
_choices: Option<Vec<String>>,
_allow_freeform: Option<bool>,
) -> Option<UserInputResponse> {
self.log
.lock()
.await
.push(format!("question: {question}"));
Some(UserInputResponse {
answer: "Paris".to_string(),
was_freeform: true,
})
}
}
struct AllowAllHooks;
#[async_trait]
impl SessionHooks for AllowAllHooks {
async fn on_pre_tool_use(
&self,
_input: PreToolUseInput,
_ctx: HookContext,
) -> Option<PreToolUseOutput> {
let mut out = PreToolUseOutput::default();
out.permission_decision = Some("allow".to_string());
Some(out)
}
}
#[tokio::main]
async fn main() -> Result<(), github_copilot_sdk::Error> {
let client = Client::start(ClientOptions::default()).await?;
let input_log = Arc::new(Mutex::new(Vec::<String>::new()));
let handler = Arc::new(InputResponder {
log: input_log.clone(),
});
let mut config = SessionConfig::default();
config.model = Some("claude-haiku-4.5".to_string());
let config = config
.with_permission_handler(handler.clone())
.with_user_input_handler(handler)
.with_hooks(Arc::new(AllowAllHooks));
let session = client.create_session(config).await?;
let response = session
.send_and_wait(
"I want to learn about a city. Use the ask_user tool to ask me \
which city I'm interested in. Then tell me about that city.",
)
.await?;
if let Some(event) = response {
if let Some(content) = event.data.get("content").and_then(|c| c.as_str()) {
println!("{content}");
}
}
println!("\n--- User input log ---");
let log = input_log.lock().await;
for entry in log.iter() {
println!(" {entry}");
}
println!("\nTotal user input requests: {}", log.len());
session.disconnect().await?;
Ok(())
}