-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (58 loc) · 2.28 KB
/
Copy pathmain.py
File metadata and controls
83 lines (58 loc) · 2.28 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
import asyncio
import os
from copilot import CopilotClient
hook_log: list[str] = []
async def auto_approve_permission(request, invocation):
return {"kind": "approved"}
async def on_session_start(input_data, invocation):
hook_log.append("onSessionStart")
async def on_session_end(input_data, invocation):
hook_log.append("onSessionEnd")
async def on_pre_tool_use(input_data, invocation):
tool_name = input_data.get("toolName", "unknown")
hook_log.append(f"onPreToolUse:{tool_name}")
return {"permissionDecision": "allow"}
async def on_post_tool_use(input_data, invocation):
tool_name = input_data.get("toolName", "unknown")
hook_log.append(f"onPostToolUse:{tool_name}")
async def on_user_prompt_submitted(input_data, invocation):
hook_log.append("onUserPromptSubmitted")
return input_data
async def on_error_occurred(input_data, invocation):
error = input_data.get("error", "unknown")
hook_log.append(f"onErrorOccurred:{error}")
async def main():
opts = {"github_token": os.environ.get("GITHUB_TOKEN")}
if os.environ.get("COPILOT_CLI_PATH"):
opts["cli_path"] = os.environ["COPILOT_CLI_PATH"]
client = CopilotClient(opts)
try:
session = await client.create_session(
{
"model": "claude-haiku-4.5",
"on_permission_request": auto_approve_permission,
"hooks": {
"on_session_start": on_session_start,
"on_session_end": on_session_end,
"on_pre_tool_use": on_pre_tool_use,
"on_post_tool_use": on_post_tool_use,
"on_user_prompt_submitted": on_user_prompt_submitted,
"on_error_occurred": on_error_occurred,
},
}
)
response = await session.send_and_wait(
{
"prompt": "List the files in the current directory using the glob tool with pattern '*.md'.",
}
)
if response:
print(response.data.content)
await session.destroy()
print("\n--- Hook execution log ---")
for entry in hook_log:
print(f" {entry}")
print(f"\nTotal hooks fired: {len(hook_log)}")
finally:
await client.stop()
asyncio.run(main())