Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions cookbook/python/recipe/managing_local_files.py
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":
Comment on lines +17 to +21
Copy link

Copilot AI Jan 27, 2026

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 than event["type"] (dictionary access). The SessionEvent class is a dataclass with a type attribute (see python/copilot/generated/session_events.py:808). This is inconsistent with the corrected property access on lines 18, 20, and 22 where event.data.content, event.data.tool_name, and event.data.tool_call_id are correctly used.

Suggested change
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":
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":

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The session.send() method is async and must be awaited. Without await, this will return a coroutine object that is never executed, and the message will not be sent to the Copilot session.

Copilot uses AI. Check for mistakes.

session.wait_for_idle()
session.wait()
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method session.wait() does not exist in the CopilotSession API. Based on the session.py source code (lines 120-179), you should use send_and_wait() instead of separate send() and wait() calls, or use the event-based approach with a session.idle event handler. The old synchronous API had wait_for_idle(), but in the async API this functionality is provided by send_and_wait().

Copilot uses AI. Check for mistakes.

session.destroy()
client.stop()
session.destroy()
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The session.destroy() method is async and must be awaited. Without await, the session cleanup will not complete properly, potentially leaving resources uncleaned.

Suggested change
session.destroy()
await session.destroy()

Copilot uses AI. Check for mistakes.
client.stop()
Copy link

Copilot AI Jan 27, 2026

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.

Suggested change
client.stop()
await client.stop()

Copilot uses AI. Check for mistakes.

asyncio.run(main())
Loading