Fix Python cookbook recipes to use correct async SDK API

All 5 Python recipes and their markdown docs used a synchronous,
kwargs-based API that doesn't match the real github-copilot-sdk:

- client.start() -> await client.start() (all methods are async)
- create_session(model=...) -> create_session(SessionConfig(model=...))
- session.send(prompt=...) -> session.send(MessageOptions(prompt=...))
- session.wait_for_idle() -> session.send_and_wait() (wait_for_idle doesn't exist)
- event['type']/event['data']['content'] -> event.type/event.data.content
- All code wrapped in async def main() + asyncio.run(main())

Verified all imports resolve against github-copilot-sdk.
This commit is contained in:
Anthony Shaw
2026-02-11 05:56:48 -08:00
parent 4555fee5d2
commit c65e8ab0b5
10 changed files with 304 additions and 284 deletions

View File

@@ -1,31 +1,40 @@
#!/usr/bin/env python3
from copilot import CopilotClient
import asyncio
import os
from copilot import (
CopilotClient, SessionConfig, MessageOptions,
SessionEvent, SessionEventType,
)
# 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(SessionConfig(model="gpt-5"))
# 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']}")
done = asyncio.Event()
session.on(handle_event)
# Event handler
def handle_event(event: SessionEvent):
if event.type == SessionEventType.ASSISTANT_MESSAGE:
print(f"\nCopilot: {event.data.content}")
elif event.type == SessionEventType.TOOL_EXECUTION_START:
print(f" → Running: {event.data.tool_name}")
elif event.type == SessionEventType.TOOL_EXECUTION_COMPLETE:
print(f" ✓ Completed: {event.data.tool_call_id}")
elif event.type.value == "session.idle":
done.set()
# Ask Copilot to organize files
# Change this to your target folder
target_folder = os.path.expanduser("~/Downloads")
session.on(handle_event)
session.send(prompt=f"""
# Ask Copilot to organize files
# Change this to your target folder
target_folder = os.path.expanduser("~/Downloads")
await session.send(MessageOptions(prompt=f"""
Analyze the files in "{target_folder}" and organize them into subfolders.
1. First, list all files and their metadata
@@ -34,9 +43,12 @@ Analyze the files in "{target_folder}" and organize them into subfolders.
4. Move each file to its appropriate subfolder
Please confirm before moving any files.
""")
"""))
session.wait_for_idle()
await done.wait()
session.destroy()
client.stop()
await session.destroy()
await client.stop()
if __name__ == "__main__":
asyncio.run(main())