mirror of
https://github.com/github/awesome-copilot.git
synced 2026-02-20 10:25:13 +00:00
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:
@@ -38,10 +38,15 @@ python pr_visualization.py --repo github/copilot-sdk
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import asyncio
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
from copilot import CopilotClient
|
||||
import re
|
||||
from copilot import (
|
||||
CopilotClient, SessionConfig, MessageOptions,
|
||||
SessionEvent, SessionEventType,
|
||||
)
|
||||
|
||||
# ============================================================================
|
||||
# Git & GitHub Detection
|
||||
@@ -69,7 +74,6 @@ def get_github_remote():
|
||||
remote_url = result.stdout.strip()
|
||||
|
||||
# Handle SSH: git@github.com:owner/repo.git
|
||||
import re
|
||||
ssh_match = re.search(r"git@github\.com:(.+/.+?)(?:\.git)?$", remote_url)
|
||||
if ssh_match:
|
||||
return ssh_match.group(1)
|
||||
@@ -98,7 +102,7 @@ def prompt_for_repo():
|
||||
# Main Application
|
||||
# ============================================================================
|
||||
|
||||
def main():
|
||||
async def main():
|
||||
print("🔍 PR Age Chart Generator\n")
|
||||
|
||||
# Determine the repository
|
||||
@@ -126,11 +130,11 @@ def main():
|
||||
|
||||
owner, repo_name = repo.split("/", 1)
|
||||
|
||||
# Create Copilot client - no custom tools needed!
|
||||
client = CopilotClient(log_level="error")
|
||||
client.start()
|
||||
# Create Copilot client
|
||||
client = CopilotClient()
|
||||
await client.start()
|
||||
|
||||
session = client.create_session(
|
||||
session = await client.create_session(SessionConfig(
|
||||
model="gpt-5",
|
||||
system_message={
|
||||
"content": f"""
|
||||
@@ -147,30 +151,34 @@ The current working directory is: {os.getcwd()}
|
||||
</instructions>
|
||||
"""
|
||||
}
|
||||
)
|
||||
))
|
||||
|
||||
done = asyncio.Event()
|
||||
|
||||
# Set up event handling
|
||||
def handle_event(event):
|
||||
if event["type"] == "assistant.message":
|
||||
print(f"\n🤖 {event['data']['content']}\n")
|
||||
elif event["type"] == "tool.execution_start":
|
||||
print(f" ⚙️ {event['data']['toolName']}")
|
||||
def handle_event(event: SessionEvent):
|
||||
if event.type == SessionEventType.ASSISTANT_MESSAGE:
|
||||
print(f"\n🤖 {event.data.content}\n")
|
||||
elif event.type == SessionEventType.TOOL_EXECUTION_START:
|
||||
print(f" ⚙️ {event.data.tool_name}")
|
||||
elif event.type.value == "session.idle":
|
||||
done.set()
|
||||
|
||||
session.on(handle_event)
|
||||
|
||||
# Initial prompt - let Copilot figure out the details
|
||||
print("\n📊 Starting analysis...\n")
|
||||
|
||||
session.send(prompt=f"""
|
||||
await session.send(MessageOptions(prompt=f"""
|
||||
Fetch the open pull requests for {owner}/{repo_name} from the last week.
|
||||
Calculate the age of each PR in days.
|
||||
Then generate a bar chart image showing the distribution of PR ages
|
||||
(group them into sensible buckets like <1 day, 1-3 days, etc.).
|
||||
Save the chart as "pr-age-chart.png" in the current directory.
|
||||
Finally, summarize the PR health - average age, oldest PR, and how many might be considered stale.
|
||||
""")
|
||||
"""))
|
||||
|
||||
session.wait_for_idle()
|
||||
await done.wait()
|
||||
|
||||
# Interactive loop
|
||||
print("\n💡 Ask follow-up questions or type \"exit\" to quit.\n")
|
||||
@@ -189,14 +197,15 @@ The current working directory is: {os.getcwd()}
|
||||
break
|
||||
|
||||
if user_input:
|
||||
session.send(prompt=user_input)
|
||||
session.wait_for_idle()
|
||||
done.clear()
|
||||
await session.send(MessageOptions(prompt=user_input))
|
||||
await done.wait()
|
||||
|
||||
session.destroy()
|
||||
client.stop()
|
||||
await session.destroy()
|
||||
await client.stop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
## How it works
|
||||
|
||||
Reference in New Issue
Block a user