-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathchat.py
More file actions
53 lines (42 loc) · 1.36 KB
/
chat.py
File metadata and controls
53 lines (42 loc) · 1.36 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
import asyncio
from copilot import CopilotClient
from copilot.generated.session_events import (
AssistantMessageData,
AssistantReasoningData,
ToolExecutionStartData,
)
from copilot.session import PermissionHandler
BLUE = "\033[34m"
RESET = "\033[0m"
async def main():
client = CopilotClient()
await client.start()
session = await client.create_session(on_permission_request=PermissionHandler.approve_all)
def on_event(event):
output = None
match event.data:
case AssistantReasoningData() as data:
output = f"[reasoning: {data.content}]"
case ToolExecutionStartData() as data:
output = f"[tool: {data.tool_name}]"
if output:
print(f"{BLUE}{output}{RESET}")
session.on(on_event)
print("Chat with Copilot (Ctrl+C to exit)\n")
while True:
user_input = input("You: ").strip()
if not user_input:
continue
print()
reply = await session.send_and_wait(user_input)
assistant_output = None
if reply:
match reply.data:
case AssistantMessageData() as data:
assistant_output = data.content
print(f"\nAssistant: {assistant_output}\n")
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nBye!")