Skip to content

Commit 05dd60e

Browse files
brettcannonSteveSandersonMSCopilot
authored
[python] Change CopilotClient.__init__ to take config objects (#793)
* Change `CopilotClient.__init__` to take config objects * Improve CLI path verification in CopilotClient to use `shutil.which` * Fix changes made in `main` * Fix shutil.which error message showing None instead of original path The walrus operator reassigned cli_path to None before the f-string was evaluated, losing the original path value in the error message. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Remove dead auto_restart field from SubprocessConfig This field was defined but never read anywhere in client.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Steve Sanderson <SteveSandersonMS@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b100339 commit 05dd60e

File tree

43 files changed

+461
-427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+461
-427
lines changed

python/README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ async with await client.create_session({"model": "gpt-5"}) as session:
7979
### CopilotClient
8080

8181
```python
82-
client = CopilotClient({
83-
"cli_path": "copilot", # Optional: path to CLI executable
84-
"cli_url": None, # Optional: URL of existing server (e.g., "localhost:8080")
85-
"log_level": "info", # Optional: log level (default: "info")
86-
"auto_start": True, # Optional: auto-start server (default: True)
87-
})
82+
from copilot import CopilotClient, SubprocessConfig
83+
84+
# Spawn a local CLI process (default)
85+
client = CopilotClient() # uses bundled CLI, stdio transport
8886
await client.start()
8987

9088
session = await client.create_session({"model": "gpt-5"})
@@ -101,17 +99,39 @@ await session.disconnect()
10199
await client.stop()
102100
```
103101

104-
**CopilotClient Options:**
102+
```python
103+
from copilot import CopilotClient, ExternalServerConfig
105104

106-
- `cli_path` (str): Path to CLI executable (default: "copilot" or `COPILOT_CLI_PATH` env var)
107-
- `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.
108-
- `cwd` (str): Working directory for CLI process
109-
- `port` (int): Server port for TCP mode (default: 0 for random)
105+
# Connect to an existing CLI server
106+
client = CopilotClient(ExternalServerConfig(url="localhost:3000"))
107+
```
108+
109+
**CopilotClient Constructor:**
110+
111+
```python
112+
CopilotClient(
113+
config=None, # SubprocessConfig | ExternalServerConfig | None
114+
*,
115+
auto_start=True, # auto-start server on first use
116+
on_list_models=None, # custom handler for list_models()
117+
)
118+
```
119+
120+
**SubprocessConfig** — spawn a local CLI process:
121+
122+
- `cli_path` (str | None): Path to CLI executable (default: bundled binary)
123+
- `cli_args` (list[str]): Extra arguments for the CLI executable
124+
- `cwd` (str | None): Working directory for CLI process (default: current dir)
110125
- `use_stdio` (bool): Use stdio transport instead of TCP (default: True)
126+
- `port` (int): Server port for TCP mode (default: 0 for random)
111127
- `log_level` (str): Log level (default: "info")
112-
- `auto_start` (bool): Auto-start server on first use (default: True)
113-
- `github_token` (str): GitHub token for authentication. When provided, takes priority over other auth methods.
114-
- `use_logged_in_user` (bool): Whether to use logged-in user for authentication (default: True, but False when `github_token` is provided). Cannot be used with `cli_url`.
128+
- `env` (dict | None): Environment variables for the CLI process
129+
- `github_token` (str | None): GitHub token for authentication. When provided, takes priority over other auth methods.
130+
- `use_logged_in_user` (bool | None): Whether to use logged-in user for authentication (default: True, but False when `github_token` is provided).
131+
132+
**ExternalServerConfig** — connect to an existing CLI server:
133+
134+
- `url` (str): Server URL (e.g., `"localhost:8080"`, `"http://127.0.0.1:9000"`, or just `"8080"`).
115135

116136
**SessionConfig Options (for `create_session`):**
117137

python/copilot/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
AzureProviderOptions,
1212
ConnectionState,
1313
CustomAgentConfig,
14+
ExternalServerConfig,
1415
GetAuthStatusResponse,
1516
GetStatusResponse,
1617
MCPLocalServerConfig,
@@ -33,6 +34,7 @@
3334
SessionListFilter,
3435
SessionMetadata,
3536
StopError,
37+
SubprocessConfig,
3638
Tool,
3739
ToolHandler,
3840
ToolInvocation,
@@ -47,6 +49,7 @@
4749
"CopilotSession",
4850
"ConnectionState",
4951
"CustomAgentConfig",
52+
"ExternalServerConfig",
5053
"GetAuthStatusResponse",
5154
"GetStatusResponse",
5255
"MCPLocalServerConfig",
@@ -69,6 +72,7 @@
6972
"SessionListFilter",
7073
"SessionMetadata",
7174
"StopError",
75+
"SubprocessConfig",
7276
"Tool",
7377
"ToolHandler",
7478
"ToolInvocation",

0 commit comments

Comments
 (0)