-
Notifications
You must be signed in to change notification settings - Fork 773
Refactor file management script to use asyncio and correct event #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,42 +1,46 @@ | ||||||
| #!/usr/bin/env python3 | ||||||
|
|
||||||
| import asyncio | ||||||
| from copilot import CopilotClient | ||||||
| import os | ||||||
|
|
||||||
| # Create and start client | ||||||
| client = CopilotClient() | ||||||
| client.start() | ||||||
| async def main(): | ||||||
| # Create and start client | ||||||
| client = CopilotClient() | ||||||
| await client.start() | ||||||
|
|
||||||
| # Create session | ||||||
| session = client.create_session(model="gpt-5") | ||||||
| # Create session | ||||||
| session = await client.create_session({"model": "gpt-4.1"}) | ||||||
|
|
||||||
| # Event handler | ||||||
| def handle_event(event): | ||||||
| if event["type"] == "assistant.message": | ||||||
| print(f"\nCopilot: {event['data']['content']}") | ||||||
| elif event["type"] == "tool.execution_start": | ||||||
| print(f" → Running: {event['data']['toolName']}") | ||||||
| elif event["type"] == "tool.execution_complete": | ||||||
| print(f" ✓ Completed: {event['data']['toolCallId']}") | ||||||
| # Event handler | ||||||
| def handle_event(event): | ||||||
| if event["type"] == "assistant.message": | ||||||
| print(f"\nCopilot: {event.data.content}") | ||||||
| elif event["type"] == "tool.execution_start": | ||||||
| print(f" → Running: {event.data.tool_name}") | ||||||
| elif event["type"] == "tool.execution_complete": | ||||||
| print(f" ✓ Completed: {event.data.tool_call_id}") | ||||||
|
|
||||||
| session.on(handle_event) | ||||||
| session.on(handle_event) | ||||||
|
|
||||||
| # Ask Copilot to organize files | ||||||
| # Change this to your target folder | ||||||
| target_folder = os.path.expanduser("~/Downloads") | ||||||
| # Ask Copilot to organize files | ||||||
| # Change this to your target folder | ||||||
| target_folder = os.path.expanduser("~/Downloads") | ||||||
|
|
||||||
| session.send(prompt=f""" | ||||||
| Analyze the files in "{target_folder}" and organize them into subfolders. | ||||||
| session.send({"prompt":f""" | ||||||
| Analyze the files in "{target_folder}" and organize them into subfolders. | ||||||
|
|
||||||
| 1. First, list all files and their metadata | ||||||
| 2. Preview grouping by file extension | ||||||
| 3. Create appropriate subfolders (e.g., "images", "documents", "videos") | ||||||
| 4. Move each file to its appropriate subfolder | ||||||
| 1. First, list all files and their metadata | ||||||
| 2. Preview grouping by file extension | ||||||
| 3. Create appropriate subfolders (e.g., "images", "documents", "videos") | ||||||
| 4. Move each file to its appropriate subfolder | ||||||
|
|
||||||
| Please confirm before moving any files. | ||||||
| """) | ||||||
| Please confirm before moving any files. | ||||||
| """}) | ||||||
|
Comment on lines
+30
to
+39
|
||||||
|
|
||||||
| session.wait_for_idle() | ||||||
| session.wait() | ||||||
|
||||||
|
|
||||||
| session.destroy() | ||||||
| client.stop() | ||||||
| session.destroy() | ||||||
|
||||||
| session.destroy() | |
| await session.destroy() |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The client.stop() method is async and must be awaited. Without await, the client shutdown will not complete properly, potentially leaving the CLI process running or connections open.
| client.stop() | |
| await client.stop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event type should be accessed as
event.type(attribute access) rather thanevent["type"](dictionary access). The SessionEvent class is a dataclass with atypeattribute (see python/copilot/generated/session_events.py:808). This is inconsistent with the corrected property access on lines 18, 20, and 22 whereevent.data.content,event.data.tool_name, andevent.data.tool_call_idare correctly used.