github-copilot-sdk 0.1.11__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- github_copilot_sdk-0.1.11/.gitignore +164 -0
- github_copilot_sdk-0.1.11/PKG-INFO +252 -0
- github_copilot_sdk-0.1.11/README.md +221 -0
- github_copilot_sdk-0.1.11/copilot/__init__.py +55 -0
- github_copilot_sdk-0.1.11/copilot/client.py +976 -0
- github_copilot_sdk-0.1.11/copilot/generated/__init__.py +0 -0
- github_copilot_sdk-0.1.11/copilot/generated/session_events.py +640 -0
- github_copilot_sdk-0.1.11/copilot/jsonrpc.py +283 -0
- github_copilot_sdk-0.1.11/copilot/sdk_protocol_version.py +19 -0
- github_copilot_sdk-0.1.11/copilot/session.py +315 -0
- github_copilot_sdk-0.1.11/copilot/tools.py +212 -0
- github_copilot_sdk-0.1.11/copilot/types.py +270 -0
- github_copilot_sdk-0.1.11/e2e/__init__.py +1 -0
- github_copilot_sdk-0.1.11/e2e/conftest.py +33 -0
- github_copilot_sdk-0.1.11/e2e/test_client.py +74 -0
- github_copilot_sdk-0.1.11/e2e/test_mcp_and_agents.py +237 -0
- github_copilot_sdk-0.1.11/e2e/test_permissions.py +193 -0
- github_copilot_sdk-0.1.11/e2e/test_session.py +335 -0
- github_copilot_sdk-0.1.11/e2e/test_tools.py +126 -0
- github_copilot_sdk-0.1.11/e2e/test_tools_unit.py +286 -0
- github_copilot_sdk-0.1.11/e2e/testharness/__init__.py +7 -0
- github_copilot_sdk-0.1.11/e2e/testharness/context.py +143 -0
- github_copilot_sdk-0.1.11/e2e/testharness/helper.py +127 -0
- github_copilot_sdk-0.1.11/e2e/testharness/proxy.py +105 -0
- github_copilot_sdk-0.1.11/github_copilot_sdk.egg-info/PKG-INFO +252 -0
- github_copilot_sdk-0.1.11/github_copilot_sdk.egg-info/SOURCES.txt +33 -0
- github_copilot_sdk-0.1.11/github_copilot_sdk.egg-info/dependency_links.txt +1 -0
- github_copilot_sdk-0.1.11/github_copilot_sdk.egg-info/requires.txt +11 -0
- github_copilot_sdk-0.1.11/github_copilot_sdk.egg-info/top_level.txt +1 -0
- github_copilot_sdk-0.1.11/pyproject.toml +89 -0
- github_copilot_sdk-0.1.11/setup.cfg +4 -0
- github_copilot_sdk-0.1.11/setup.py +11 -0
- github_copilot_sdk-0.1.11/test-requirements.txt +5 -0
- github_copilot_sdk-0.1.11/test_client.py +94 -0
- github_copilot_sdk-0.1.11/uv.lock +814 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# poetry
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
102
|
+
#poetry.lock
|
|
103
|
+
|
|
104
|
+
# pdm
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
106
|
+
#pdm.lock
|
|
107
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
108
|
+
# in version control.
|
|
109
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
110
|
+
.pdm.toml
|
|
111
|
+
|
|
112
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
113
|
+
__pypackages__/
|
|
114
|
+
|
|
115
|
+
# Celery stuff
|
|
116
|
+
celerybeat-schedule
|
|
117
|
+
celerybeat.pid
|
|
118
|
+
|
|
119
|
+
# SageMath parsed files
|
|
120
|
+
*.sage.py
|
|
121
|
+
|
|
122
|
+
# Environments
|
|
123
|
+
.env
|
|
124
|
+
.venv
|
|
125
|
+
env/
|
|
126
|
+
venv/
|
|
127
|
+
ENV/
|
|
128
|
+
env.bak/
|
|
129
|
+
venv.bak/
|
|
130
|
+
|
|
131
|
+
# Spyder project settings
|
|
132
|
+
.spyderproject
|
|
133
|
+
.spyproject
|
|
134
|
+
|
|
135
|
+
# Rope project settings
|
|
136
|
+
.ropeproject
|
|
137
|
+
|
|
138
|
+
# mkdocs documentation
|
|
139
|
+
/site
|
|
140
|
+
|
|
141
|
+
# mypy
|
|
142
|
+
.mypy_cache/
|
|
143
|
+
.dmypy.json
|
|
144
|
+
dmypy.json
|
|
145
|
+
|
|
146
|
+
# Pyre type checker
|
|
147
|
+
.pyre/
|
|
148
|
+
|
|
149
|
+
# pytype static type analyzer
|
|
150
|
+
.pytype/
|
|
151
|
+
|
|
152
|
+
# Cython debug symbols
|
|
153
|
+
cython_debug/
|
|
154
|
+
|
|
155
|
+
# PyCharm
|
|
156
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
157
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
158
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
159
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
160
|
+
#.idea/
|
|
161
|
+
|
|
162
|
+
# Ruff and ty cache
|
|
163
|
+
.ruff_cache/
|
|
164
|
+
.ty_cache/
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: github-copilot-sdk
|
|
3
|
+
Version: 0.1.11
|
|
4
|
+
Summary: Python SDK for GitHub Copilot CLI
|
|
5
|
+
Author-email: GitHub <opensource@github.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/github/copilot-sdk
|
|
8
|
+
Project-URL: Repository, https://github.com/github/copilot-sdk
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Requires-Python: >=3.8
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
Requires-Dist: python-dateutil>=2.9.0.post0
|
|
21
|
+
Requires-Dist: pydantic>=2.0
|
|
22
|
+
Requires-Dist: typing-extensions>=4.0.0
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
25
|
+
Requires-Dist: ty>=0.0.2; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
28
|
+
Requires-Dist: typing-extensions>=4.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: httpx>=0.24.0; extra == "dev"
|
|
30
|
+
Dynamic: requires-python
|
|
31
|
+
|
|
32
|
+
# Copilot Python SDK
|
|
33
|
+
|
|
34
|
+
Python SDK for programmatic control of GitHub Copilot CLI via JSON-RPC.
|
|
35
|
+
|
|
36
|
+
> **Note:** This SDK is in technical preview and may change in breaking ways.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install -e .
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import asyncio
|
|
48
|
+
from copilot import CopilotClient
|
|
49
|
+
|
|
50
|
+
async def main():
|
|
51
|
+
# Create and start client
|
|
52
|
+
client = CopilotClient()
|
|
53
|
+
await client.start()
|
|
54
|
+
|
|
55
|
+
# Create a session
|
|
56
|
+
session = await client.create_session({"model": "gpt-5"})
|
|
57
|
+
|
|
58
|
+
# Wait for response using session.idle event
|
|
59
|
+
done = asyncio.Event()
|
|
60
|
+
|
|
61
|
+
def on_event(event):
|
|
62
|
+
if event.type.value == "assistant.message":
|
|
63
|
+
print(event.data.content)
|
|
64
|
+
elif event.type.value == "session.idle":
|
|
65
|
+
done.set()
|
|
66
|
+
|
|
67
|
+
session.on(on_event)
|
|
68
|
+
|
|
69
|
+
# Send a message and wait for completion
|
|
70
|
+
await session.send({"prompt": "What is 2+2?"})
|
|
71
|
+
await done.wait()
|
|
72
|
+
|
|
73
|
+
# Clean up
|
|
74
|
+
await session.destroy()
|
|
75
|
+
await client.stop()
|
|
76
|
+
|
|
77
|
+
asyncio.run(main())
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Features
|
|
81
|
+
|
|
82
|
+
- ✅ Full JSON-RPC protocol support
|
|
83
|
+
- ✅ stdio and TCP transports
|
|
84
|
+
- ✅ Real-time streaming events
|
|
85
|
+
- ✅ Session history with `get_messages()`
|
|
86
|
+
- ✅ Type hints throughout
|
|
87
|
+
- ✅ Async/await native
|
|
88
|
+
|
|
89
|
+
## API Reference
|
|
90
|
+
|
|
91
|
+
### CopilotClient
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
client = CopilotClient({
|
|
95
|
+
"cli_path": "copilot", # Optional: path to CLI executable
|
|
96
|
+
"cli_url": None, # Optional: URL of existing server (e.g., "localhost:8080")
|
|
97
|
+
"log_level": "info", # Optional: log level (default: "info")
|
|
98
|
+
"auto_start": True, # Optional: auto-start server (default: True)
|
|
99
|
+
"auto_restart": True, # Optional: auto-restart on crash (default: True)
|
|
100
|
+
})
|
|
101
|
+
await client.start()
|
|
102
|
+
|
|
103
|
+
session = await client.create_session({"model": "gpt-5"})
|
|
104
|
+
|
|
105
|
+
def on_event(event):
|
|
106
|
+
print(f"Event: {event['type']}")
|
|
107
|
+
|
|
108
|
+
session.on(on_event)
|
|
109
|
+
await session.send({"prompt": "Hello!"})
|
|
110
|
+
|
|
111
|
+
# ... wait for events ...
|
|
112
|
+
|
|
113
|
+
await session.destroy()
|
|
114
|
+
await client.stop()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**CopilotClient Options:**
|
|
118
|
+
|
|
119
|
+
- `cli_path` (str): Path to CLI executable (default: "copilot" or `COPILOT_CLI_PATH` env var)
|
|
120
|
+
- `cli_url` (str): URL of existing CLI server (e.g., `"localhost:8080"`, `"http://127.0.0.1:9000"`, or just `"8080"`). When provided, the client will not spawn a CLI process.
|
|
121
|
+
- `cwd` (str): Working directory for CLI process
|
|
122
|
+
- `port` (int): Server port for TCP mode (default: 0 for random)
|
|
123
|
+
- `use_stdio` (bool): Use stdio transport instead of TCP (default: True)
|
|
124
|
+
- `log_level` (str): Log level (default: "info")
|
|
125
|
+
- `auto_start` (bool): Auto-start server on first use (default: True)
|
|
126
|
+
- `auto_restart` (bool): Auto-restart on crash (default: True)
|
|
127
|
+
|
|
128
|
+
### Tools
|
|
129
|
+
|
|
130
|
+
Define tools with automatic JSON schema generation using the `@define_tool` decorator and Pydantic models:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from pydantic import BaseModel, Field
|
|
134
|
+
from copilot import CopilotClient, define_tool
|
|
135
|
+
|
|
136
|
+
class LookupIssueParams(BaseModel):
|
|
137
|
+
id: str = Field(description="Issue identifier")
|
|
138
|
+
|
|
139
|
+
@define_tool(description="Fetch issue details from our tracker")
|
|
140
|
+
async def lookup_issue(params: LookupIssueParams) -> str:
|
|
141
|
+
issue = await fetch_issue(params.id)
|
|
142
|
+
return issue.summary
|
|
143
|
+
|
|
144
|
+
session = await client.create_session({
|
|
145
|
+
"model": "gpt-5",
|
|
146
|
+
"tools": [lookup_issue],
|
|
147
|
+
})
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
> **Note:** When using `from __future__ import annotations`, define Pydantic models at module level (not inside functions).
|
|
151
|
+
|
|
152
|
+
**Low-level API (without Pydantic):**
|
|
153
|
+
|
|
154
|
+
For users who prefer manual schema definition:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from copilot import CopilotClient, Tool
|
|
158
|
+
|
|
159
|
+
async def lookup_issue(invocation):
|
|
160
|
+
issue_id = invocation["arguments"]["id"]
|
|
161
|
+
issue = await fetch_issue(issue_id)
|
|
162
|
+
return {
|
|
163
|
+
"textResultForLlm": issue.summary,
|
|
164
|
+
"resultType": "success",
|
|
165
|
+
"sessionLog": f"Fetched issue {issue_id}",
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
session = await client.create_session({
|
|
169
|
+
"model": "gpt-5",
|
|
170
|
+
"tools": [
|
|
171
|
+
Tool(
|
|
172
|
+
name="lookup_issue",
|
|
173
|
+
description="Fetch issue details from our tracker",
|
|
174
|
+
parameters={
|
|
175
|
+
"type": "object",
|
|
176
|
+
"properties": {
|
|
177
|
+
"id": {"type": "string", "description": "Issue identifier"},
|
|
178
|
+
},
|
|
179
|
+
"required": ["id"],
|
|
180
|
+
},
|
|
181
|
+
handler=lookup_issue,
|
|
182
|
+
)
|
|
183
|
+
],
|
|
184
|
+
})
|
|
185
|
+
````
|
|
186
|
+
|
|
187
|
+
The SDK automatically handles `tool.call`, executes your handler (sync or async), and responds with the final result when the tool completes.
|
|
188
|
+
|
|
189
|
+
## Streaming
|
|
190
|
+
|
|
191
|
+
Enable streaming to receive assistant response chunks as they're generated:
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
import asyncio
|
|
195
|
+
from copilot import CopilotClient
|
|
196
|
+
|
|
197
|
+
async def main():
|
|
198
|
+
client = CopilotClient()
|
|
199
|
+
await client.start()
|
|
200
|
+
|
|
201
|
+
session = await client.create_session({
|
|
202
|
+
"model": "gpt-5",
|
|
203
|
+
"streaming": True
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
# Use asyncio.Event to wait for completion
|
|
207
|
+
done = asyncio.Event()
|
|
208
|
+
|
|
209
|
+
def on_event(event):
|
|
210
|
+
if event.type.value == "assistant.message_delta":
|
|
211
|
+
# Streaming message chunk - print incrementally
|
|
212
|
+
delta = event.data.delta_content or ""
|
|
213
|
+
print(delta, end="", flush=True)
|
|
214
|
+
elif event.type.value == "assistant.reasoning_delta":
|
|
215
|
+
# Streaming reasoning chunk (if model supports reasoning)
|
|
216
|
+
delta = event.data.delta_content or ""
|
|
217
|
+
print(delta, end="", flush=True)
|
|
218
|
+
elif event.type.value == "assistant.message":
|
|
219
|
+
# Final message - complete content
|
|
220
|
+
print("\n--- Final message ---")
|
|
221
|
+
print(event.data.content)
|
|
222
|
+
elif event.type.value == "assistant.reasoning":
|
|
223
|
+
# Final reasoning content (if model supports reasoning)
|
|
224
|
+
print("--- Reasoning ---")
|
|
225
|
+
print(event.data.content)
|
|
226
|
+
elif event.type.value == "session.idle":
|
|
227
|
+
# Session finished processing
|
|
228
|
+
done.set()
|
|
229
|
+
|
|
230
|
+
session.on(on_event)
|
|
231
|
+
await session.send({"prompt": "Tell me a short story"})
|
|
232
|
+
await done.wait() # Wait for streaming to complete
|
|
233
|
+
|
|
234
|
+
await session.destroy()
|
|
235
|
+
await client.stop()
|
|
236
|
+
|
|
237
|
+
asyncio.run(main())
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
When `streaming=True`:
|
|
241
|
+
|
|
242
|
+
- `assistant.message_delta` events are sent with `delta_content` containing incremental text
|
|
243
|
+
- `assistant.reasoning_delta` events are sent with `delta_content` for reasoning/chain-of-thought (model-dependent)
|
|
244
|
+
- Accumulate `delta_content` values to build the full response progressively
|
|
245
|
+
- The final `assistant.message` and `assistant.reasoning` events contain the complete content
|
|
246
|
+
|
|
247
|
+
Note: `assistant.message` and `assistant.reasoning` (final events) are always sent regardless of streaming setting.
|
|
248
|
+
|
|
249
|
+
## Requirements
|
|
250
|
+
|
|
251
|
+
- Python 3.8+
|
|
252
|
+
- GitHub Copilot CLI installed and accessible
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# Copilot Python SDK
|
|
2
|
+
|
|
3
|
+
Python SDK for programmatic control of GitHub Copilot CLI via JSON-RPC.
|
|
4
|
+
|
|
5
|
+
> **Note:** This SDK is in technical preview and may change in breaking ways.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install -e .
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import asyncio
|
|
17
|
+
from copilot import CopilotClient
|
|
18
|
+
|
|
19
|
+
async def main():
|
|
20
|
+
# Create and start client
|
|
21
|
+
client = CopilotClient()
|
|
22
|
+
await client.start()
|
|
23
|
+
|
|
24
|
+
# Create a session
|
|
25
|
+
session = await client.create_session({"model": "gpt-5"})
|
|
26
|
+
|
|
27
|
+
# Wait for response using session.idle event
|
|
28
|
+
done = asyncio.Event()
|
|
29
|
+
|
|
30
|
+
def on_event(event):
|
|
31
|
+
if event.type.value == "assistant.message":
|
|
32
|
+
print(event.data.content)
|
|
33
|
+
elif event.type.value == "session.idle":
|
|
34
|
+
done.set()
|
|
35
|
+
|
|
36
|
+
session.on(on_event)
|
|
37
|
+
|
|
38
|
+
# Send a message and wait for completion
|
|
39
|
+
await session.send({"prompt": "What is 2+2?"})
|
|
40
|
+
await done.wait()
|
|
41
|
+
|
|
42
|
+
# Clean up
|
|
43
|
+
await session.destroy()
|
|
44
|
+
await client.stop()
|
|
45
|
+
|
|
46
|
+
asyncio.run(main())
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Features
|
|
50
|
+
|
|
51
|
+
- ✅ Full JSON-RPC protocol support
|
|
52
|
+
- ✅ stdio and TCP transports
|
|
53
|
+
- ✅ Real-time streaming events
|
|
54
|
+
- ✅ Session history with `get_messages()`
|
|
55
|
+
- ✅ Type hints throughout
|
|
56
|
+
- ✅ Async/await native
|
|
57
|
+
|
|
58
|
+
## API Reference
|
|
59
|
+
|
|
60
|
+
### CopilotClient
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
client = CopilotClient({
|
|
64
|
+
"cli_path": "copilot", # Optional: path to CLI executable
|
|
65
|
+
"cli_url": None, # Optional: URL of existing server (e.g., "localhost:8080")
|
|
66
|
+
"log_level": "info", # Optional: log level (default: "info")
|
|
67
|
+
"auto_start": True, # Optional: auto-start server (default: True)
|
|
68
|
+
"auto_restart": True, # Optional: auto-restart on crash (default: True)
|
|
69
|
+
})
|
|
70
|
+
await client.start()
|
|
71
|
+
|
|
72
|
+
session = await client.create_session({"model": "gpt-5"})
|
|
73
|
+
|
|
74
|
+
def on_event(event):
|
|
75
|
+
print(f"Event: {event['type']}")
|
|
76
|
+
|
|
77
|
+
session.on(on_event)
|
|
78
|
+
await session.send({"prompt": "Hello!"})
|
|
79
|
+
|
|
80
|
+
# ... wait for events ...
|
|
81
|
+
|
|
82
|
+
await session.destroy()
|
|
83
|
+
await client.stop()
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**CopilotClient Options:**
|
|
87
|
+
|
|
88
|
+
- `cli_path` (str): Path to CLI executable (default: "copilot" or `COPILOT_CLI_PATH` env var)
|
|
89
|
+
- `cli_url` (str): URL of existing CLI server (e.g., `"localhost:8080"`, `"http://127.0.0.1:9000"`, or just `"8080"`). When provided, the client will not spawn a CLI process.
|
|
90
|
+
- `cwd` (str): Working directory for CLI process
|
|
91
|
+
- `port` (int): Server port for TCP mode (default: 0 for random)
|
|
92
|
+
- `use_stdio` (bool): Use stdio transport instead of TCP (default: True)
|
|
93
|
+
- `log_level` (str): Log level (default: "info")
|
|
94
|
+
- `auto_start` (bool): Auto-start server on first use (default: True)
|
|
95
|
+
- `auto_restart` (bool): Auto-restart on crash (default: True)
|
|
96
|
+
|
|
97
|
+
### Tools
|
|
98
|
+
|
|
99
|
+
Define tools with automatic JSON schema generation using the `@define_tool` decorator and Pydantic models:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from pydantic import BaseModel, Field
|
|
103
|
+
from copilot import CopilotClient, define_tool
|
|
104
|
+
|
|
105
|
+
class LookupIssueParams(BaseModel):
|
|
106
|
+
id: str = Field(description="Issue identifier")
|
|
107
|
+
|
|
108
|
+
@define_tool(description="Fetch issue details from our tracker")
|
|
109
|
+
async def lookup_issue(params: LookupIssueParams) -> str:
|
|
110
|
+
issue = await fetch_issue(params.id)
|
|
111
|
+
return issue.summary
|
|
112
|
+
|
|
113
|
+
session = await client.create_session({
|
|
114
|
+
"model": "gpt-5",
|
|
115
|
+
"tools": [lookup_issue],
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
> **Note:** When using `from __future__ import annotations`, define Pydantic models at module level (not inside functions).
|
|
120
|
+
|
|
121
|
+
**Low-level API (without Pydantic):**
|
|
122
|
+
|
|
123
|
+
For users who prefer manual schema definition:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from copilot import CopilotClient, Tool
|
|
127
|
+
|
|
128
|
+
async def lookup_issue(invocation):
|
|
129
|
+
issue_id = invocation["arguments"]["id"]
|
|
130
|
+
issue = await fetch_issue(issue_id)
|
|
131
|
+
return {
|
|
132
|
+
"textResultForLlm": issue.summary,
|
|
133
|
+
"resultType": "success",
|
|
134
|
+
"sessionLog": f"Fetched issue {issue_id}",
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
session = await client.create_session({
|
|
138
|
+
"model": "gpt-5",
|
|
139
|
+
"tools": [
|
|
140
|
+
Tool(
|
|
141
|
+
name="lookup_issue",
|
|
142
|
+
description="Fetch issue details from our tracker",
|
|
143
|
+
parameters={
|
|
144
|
+
"type": "object",
|
|
145
|
+
"properties": {
|
|
146
|
+
"id": {"type": "string", "description": "Issue identifier"},
|
|
147
|
+
},
|
|
148
|
+
"required": ["id"],
|
|
149
|
+
},
|
|
150
|
+
handler=lookup_issue,
|
|
151
|
+
)
|
|
152
|
+
],
|
|
153
|
+
})
|
|
154
|
+
````
|
|
155
|
+
|
|
156
|
+
The SDK automatically handles `tool.call`, executes your handler (sync or async), and responds with the final result when the tool completes.
|
|
157
|
+
|
|
158
|
+
## Streaming
|
|
159
|
+
|
|
160
|
+
Enable streaming to receive assistant response chunks as they're generated:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
import asyncio
|
|
164
|
+
from copilot import CopilotClient
|
|
165
|
+
|
|
166
|
+
async def main():
|
|
167
|
+
client = CopilotClient()
|
|
168
|
+
await client.start()
|
|
169
|
+
|
|
170
|
+
session = await client.create_session({
|
|
171
|
+
"model": "gpt-5",
|
|
172
|
+
"streaming": True
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
# Use asyncio.Event to wait for completion
|
|
176
|
+
done = asyncio.Event()
|
|
177
|
+
|
|
178
|
+
def on_event(event):
|
|
179
|
+
if event.type.value == "assistant.message_delta":
|
|
180
|
+
# Streaming message chunk - print incrementally
|
|
181
|
+
delta = event.data.delta_content or ""
|
|
182
|
+
print(delta, end="", flush=True)
|
|
183
|
+
elif event.type.value == "assistant.reasoning_delta":
|
|
184
|
+
# Streaming reasoning chunk (if model supports reasoning)
|
|
185
|
+
delta = event.data.delta_content or ""
|
|
186
|
+
print(delta, end="", flush=True)
|
|
187
|
+
elif event.type.value == "assistant.message":
|
|
188
|
+
# Final message - complete content
|
|
189
|
+
print("\n--- Final message ---")
|
|
190
|
+
print(event.data.content)
|
|
191
|
+
elif event.type.value == "assistant.reasoning":
|
|
192
|
+
# Final reasoning content (if model supports reasoning)
|
|
193
|
+
print("--- Reasoning ---")
|
|
194
|
+
print(event.data.content)
|
|
195
|
+
elif event.type.value == "session.idle":
|
|
196
|
+
# Session finished processing
|
|
197
|
+
done.set()
|
|
198
|
+
|
|
199
|
+
session.on(on_event)
|
|
200
|
+
await session.send({"prompt": "Tell me a short story"})
|
|
201
|
+
await done.wait() # Wait for streaming to complete
|
|
202
|
+
|
|
203
|
+
await session.destroy()
|
|
204
|
+
await client.stop()
|
|
205
|
+
|
|
206
|
+
asyncio.run(main())
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
When `streaming=True`:
|
|
210
|
+
|
|
211
|
+
- `assistant.message_delta` events are sent with `delta_content` containing incremental text
|
|
212
|
+
- `assistant.reasoning_delta` events are sent with `delta_content` for reasoning/chain-of-thought (model-dependent)
|
|
213
|
+
- Accumulate `delta_content` values to build the full response progressively
|
|
214
|
+
- The final `assistant.message` and `assistant.reasoning` events contain the complete content
|
|
215
|
+
|
|
216
|
+
Note: `assistant.message` and `assistant.reasoning` (final events) are always sent regardless of streaming setting.
|
|
217
|
+
|
|
218
|
+
## Requirements
|
|
219
|
+
|
|
220
|
+
- Python 3.8+
|
|
221
|
+
- GitHub Copilot CLI installed and accessible
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copilot SDK - Python Client for GitHub Copilot CLI
|
|
3
|
+
|
|
4
|
+
JSON-RPC based SDK for programmatic control of GitHub Copilot CLI
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .client import CopilotClient
|
|
8
|
+
from .session import CopilotSession
|
|
9
|
+
from .tools import define_tool
|
|
10
|
+
from .types import (
|
|
11
|
+
AzureProviderOptions,
|
|
12
|
+
ConnectionState,
|
|
13
|
+
CustomAgentConfig,
|
|
14
|
+
MCPLocalServerConfig,
|
|
15
|
+
MCPRemoteServerConfig,
|
|
16
|
+
MCPServerConfig,
|
|
17
|
+
MessageOptions,
|
|
18
|
+
PermissionHandler,
|
|
19
|
+
PermissionRequest,
|
|
20
|
+
PermissionRequestResult,
|
|
21
|
+
ProviderConfig,
|
|
22
|
+
ResumeSessionConfig,
|
|
23
|
+
SessionConfig,
|
|
24
|
+
SessionEvent,
|
|
25
|
+
Tool,
|
|
26
|
+
ToolHandler,
|
|
27
|
+
ToolInvocation,
|
|
28
|
+
ToolResult,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
__version__ = "0.1.0"
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"AzureProviderOptions",
|
|
35
|
+
"CopilotClient",
|
|
36
|
+
"CopilotSession",
|
|
37
|
+
"ConnectionState",
|
|
38
|
+
"CustomAgentConfig",
|
|
39
|
+
"MCPLocalServerConfig",
|
|
40
|
+
"MCPRemoteServerConfig",
|
|
41
|
+
"MCPServerConfig",
|
|
42
|
+
"MessageOptions",
|
|
43
|
+
"PermissionHandler",
|
|
44
|
+
"PermissionRequest",
|
|
45
|
+
"PermissionRequestResult",
|
|
46
|
+
"ProviderConfig",
|
|
47
|
+
"ResumeSessionConfig",
|
|
48
|
+
"SessionConfig",
|
|
49
|
+
"SessionEvent",
|
|
50
|
+
"Tool",
|
|
51
|
+
"ToolHandler",
|
|
52
|
+
"ToolInvocation",
|
|
53
|
+
"ToolResult",
|
|
54
|
+
"define_tool",
|
|
55
|
+
]
|