Skip to content

Commit 8283fb9

Browse files
committed
Refactor CopilotClient.create_session() to have parameters
1 parent f9144f1 commit 8283fb9

64 files changed

Lines changed: 572 additions & 764 deletions

File tree

Some content is hidden

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

docs/auth/byok.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Azure AI Foundry (formerly Azure OpenAI) is a common BYOK deployment target for
2323
```python
2424
import asyncio
2525
import os
26-
from copilot import CopilotClient
26+
from copilot import CopilotClient, PermissionHandler
2727

2828
FOUNDRY_MODEL_URL = "https://your-resource.openai.azure.com/openai/v1/"
2929
# Set FOUNDRY_API_KEY environment variable
@@ -32,14 +32,11 @@ async def main():
3232
client = CopilotClient()
3333
await client.start()
3434

35-
session = await client.create_session({
36-
"model": "gpt-5.2-codex", # Your deployment name
37-
"provider": {
38-
"type": "openai",
39-
"base_url": FOUNDRY_MODEL_URL,
40-
"wire_api": "responses", # Use "completions" for older models
41-
"api_key": os.environ["FOUNDRY_API_KEY"],
42-
},
35+
session = await client.create_session(PermissionHandler.approve_all, "gpt-5.2-codex", provider={
36+
"type": "openai",
37+
"base_url": FOUNDRY_MODEL_URL,
38+
"wire_api": "responses", # Use "completions" for older models
39+
"api_key": os.environ["FOUNDRY_API_KEY"],
4340
})
4441

4542
done = asyncio.Event()

docs/getting-started.md

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ Create `main.py`:
129129

130130
```python
131131
import asyncio
132-
from copilot import CopilotClient
132+
from copilot import CopilotClient, PermissionHandler
133133

134134
async def main():
135135
client = CopilotClient()
136136
await client.start()
137137

138-
session = await client.create_session({"model": "gpt-4.1"})
138+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1")
139139
response = await session.send_and_wait({"prompt": "What is 2 + 2?"})
140140

141141
print(response.data.content)
@@ -274,17 +274,14 @@ Update `main.py`:
274274
```python
275275
import asyncio
276276
import sys
277-
from copilot import CopilotClient
277+
from copilot import CopilotClient, PermissionHandler
278278
from copilot.generated.session_events import SessionEventType
279279

280280
async def main():
281281
client = CopilotClient()
282282
await client.start()
283283

284-
session = await client.create_session({
285-
"model": "gpt-4.1",
286-
"streaming": True,
287-
})
284+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1", streaming=True)
288285

289286
# Listen for response chunks
290287
def handle_event(event):
@@ -565,7 +562,7 @@ Update `main.py`:
565562
import asyncio
566563
import random
567564
import sys
568-
from copilot import CopilotClient
565+
from copilot import CopilotClient, PermissionHandler
569566
from copilot.tools import define_tool
570567
from copilot.generated.session_events import SessionEventType
571568
from pydantic import BaseModel, Field
@@ -588,11 +585,7 @@ async def main():
588585
client = CopilotClient()
589586
await client.start()
590587

591-
session = await client.create_session({
592-
"model": "gpt-4.1",
593-
"streaming": True,
594-
"tools": [get_weather],
595-
})
588+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1", streaming=True, tools=[get_weather])
596589

597590
def handle_event(event):
598591
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
@@ -837,7 +830,7 @@ Create `weather_assistant.py`:
837830
import asyncio
838831
import random
839832
import sys
840-
from copilot import CopilotClient
833+
from copilot import CopilotClient, PermissionHandler
841834
from copilot.tools import define_tool
842835
from copilot.generated.session_events import SessionEventType
843836
from pydantic import BaseModel, Field
@@ -857,11 +850,7 @@ async def main():
857850
client = CopilotClient()
858851
await client.start()
859852

860-
session = await client.create_session({
861-
"model": "gpt-4.1",
862-
"streaming": True,
863-
"tools": [get_weather],
864-
})
853+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1", streaming=True, tools=[get_weather])
865854

866855
def handle_event(event):
867856
if event.type == SessionEventType.ASSISTANT_MESSAGE_DELTA:
@@ -1218,7 +1207,7 @@ client = CopilotClient({
12181207
await client.start()
12191208

12201209
# Use the client normally
1221-
session = await client.create_session({"on_permission_request": PermissionHandler.approve_all})
1210+
session = await client.create_session(PermissionHandler.approve_all)
12221211
# ...
12231212
```
12241213

docs/guides/session-persistence.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ await session.sendAndWait({ prompt: "Analyze my codebase" });
4646
### Python
4747

4848
```python
49-
from copilot import CopilotClient
49+
from copilot import CopilotClient, PermissionHandler
5050

5151
client = CopilotClient()
5252
await client.start()
5353

5454
# Create a session with a meaningful ID
55-
session = await client.create_session({
56-
"session_id": "user-123-task-456",
57-
"model": "gpt-5.2-codex",
58-
})
55+
session = await client.create_session(PermissionHandler.approve_all, "gpt-5.2-codex", session_id="user-123-task-456")
5956

6057
# Do some work...
6158
await session.send_and_wait({"prompt": "Analyze my codebase"})

docs/guides/setup/azure-managed-identity.md

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import asyncio
4242
import os
4343

4444
from azure.identity import DefaultAzureCredential
45-
from copilot import CopilotClient, ProviderConfig, SessionConfig
45+
from copilot import CopilotClient, PermissionHandler
4646

4747
COGNITIVE_SERVICES_SCOPE = "https://cognitiveservices.azure.com/.default"
4848

@@ -58,15 +58,14 @@ async def main():
5858
await client.start()
5959

6060
session = await client.create_session(
61-
SessionConfig(
62-
model="gpt-4.1",
63-
provider=ProviderConfig(
64-
type="openai",
65-
base_url=f"{foundry_url.rstrip('/')}/openai/v1/",
66-
bearer_token=token, # Short-lived bearer token
67-
wire_api="responses",
68-
),
69-
)
61+
PermissionHandler.approve_all,
62+
"gpt-4.1",
63+
provider={
64+
"type": "openai",
65+
"base_url": f"{foundry_url.rstrip('/')}/openai/v1/",
66+
"bearer_token": token, # Short-lived bearer token
67+
"wire_api": "responses",
68+
},
7069
)
7170

7271
response = await session.send_and_wait({"prompt": "Hello from Managed Identity!"})
@@ -84,7 +83,7 @@ Bearer tokens expire (typically after ~1 hour). For servers or long-running agen
8483

8584
```python
8685
from azure.identity import DefaultAzureCredential
87-
from copilot import CopilotClient, ProviderConfig, SessionConfig
86+
from copilot import CopilotClient, PermissionHandler
8887

8988
COGNITIVE_SERVICES_SCOPE = "https://cognitiveservices.azure.com/.default"
9089

@@ -98,24 +97,21 @@ class ManagedIdentityCopilotAgent:
9897
self.credential = DefaultAzureCredential()
9998
self.client = CopilotClient()
10099

101-
def _get_session_config(self) -> SessionConfig:
102-
"""Build a SessionConfig with a fresh bearer token."""
100+
def _get_provider_config(self) -> dict:
101+
"""Build a provider config dict with a fresh bearer token."""
103102
token = self.credential.get_token(COGNITIVE_SERVICES_SCOPE).token
104-
return SessionConfig(
105-
model=self.model,
106-
provider=ProviderConfig(
107-
type="openai",
108-
base_url=f"{self.foundry_url}/openai/v1/",
109-
bearer_token=token,
110-
wire_api="responses",
111-
),
112-
)
103+
return {
104+
"type": "openai",
105+
"base_url": f"{self.foundry_url}/openai/v1/",
106+
"bearer_token": token,
107+
"wire_api": "responses",
108+
}
113109

114110
async def chat(self, prompt: str) -> str:
115111
"""Send a prompt and return the response text."""
116112
# Fresh token for each session
117-
config = self._get_session_config()
118-
session = await self.client.create_session(config)
113+
provider = self._get_provider_config()
114+
session = await self.client.create_session(PermissionHandler.approve_all, self.model, provider=provider)
119115

120116
response = await session.send_and_wait({"prompt": prompt})
121117
await session.destroy()

docs/guides/setup/backend-services.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,14 @@ res.json({ content: response?.data.content });
111111
<summary><strong>Python</strong></summary>
112112

113113
```python
114-
from copilot import CopilotClient
114+
from copilot import CopilotClient, PermissionHandler
115115

116116
client = CopilotClient({
117117
"cli_url": "localhost:4321",
118118
})
119119
await client.start()
120120

121-
session = await client.create_session({
122-
"session_id": f"user-{user_id}-{int(time.time())}",
123-
"model": "gpt-4.1",
124-
})
121+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1", session_id=f"user-{user_id}-{int(time.time())}")
125122

126123
response = await session.send_and_wait({"prompt": message})
127124
```

docs/guides/setup/bundled-cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ await client.stop();
8585
<summary><strong>Python</strong></summary>
8686

8787
```python
88-
from copilot import CopilotClient
88+
from copilot import CopilotClient, PermissionHandler
8989
from pathlib import Path
9090

9191
client = CopilotClient({
9292
"cli_path": str(Path(__file__).parent / "vendor" / "copilot"),
9393
})
9494
await client.start()
9595

96-
session = await client.create_session({"model": "gpt-4.1"})
96+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1")
9797
response = await session.send_and_wait({"prompt": "Hello!"})
9898
print(response.data.content)
9999

docs/guides/setup/byok.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,15 @@ await client.stop();
9393

9494
```python
9595
import os
96-
from copilot import CopilotClient
96+
from copilot import CopilotClient, PermissionHandler
9797

9898
client = CopilotClient()
9999
await client.start()
100100

101-
session = await client.create_session({
102-
"model": "gpt-4.1",
103-
"provider": {
104-
"type": "openai",
105-
"base_url": "https://api.openai.com/v1",
106-
"api_key": os.environ["OPENAI_API_KEY"],
107-
},
101+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1", provider={
102+
"type": "openai",
103+
"base_url": "https://api.openai.com/v1",
104+
"api_key": os.environ["OPENAI_API_KEY"],
108105
})
109106

110107
response = await session.send_and_wait({"prompt": "Hello!"})

docs/guides/setup/github-oauth.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const response = await session.sendAndWait({ prompt: "Hello!" });
145145
<summary><strong>Python</strong></summary>
146146

147147
```python
148-
from copilot import CopilotClient
148+
from copilot import CopilotClient, PermissionHandler
149149

150150
def create_client_for_user(user_token: str) -> CopilotClient:
151151
return CopilotClient({
@@ -157,10 +157,7 @@ def create_client_for_user(user_token: str) -> CopilotClient:
157157
client = create_client_for_user("gho_user_access_token")
158158
await client.start()
159159

160-
session = await client.create_session({
161-
"session_id": f"user-{user_id}-session",
162-
"model": "gpt-4.1",
163-
})
160+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1", session_id=f"user-{user_id}-session")
164161

165162
response = await session.send_and_wait({"prompt": "Hello!"})
166163
```

docs/guides/setup/local-cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ await client.stop();
5151
<summary><strong>Python</strong></summary>
5252

5353
```python
54-
from copilot import CopilotClient
54+
from copilot import CopilotClient, PermissionHandler
5555

5656
client = CopilotClient()
5757
await client.start()
5858

59-
session = await client.create_session({"model": "gpt-4.1"})
59+
session = await client.create_session(PermissionHandler.approve_all, "gpt-4.1")
6060
response = await session.send_and_wait({"prompt": "Hello!"})
6161
print(response.data.content)
6262

docs/guides/skills.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ async def main():
4848
client = CopilotClient()
4949
await client.start()
5050

51-
session = await client.create_session({
52-
"model": "gpt-4.1",
53-
"skill_directories": [
51+
session = await client.create_session(
52+
lambda req: {"kind": "approved"},
53+
"gpt-4.1",
54+
skill_directories=[
5455
"./skills/code-review",
5556
"./skills/documentation",
5657
],
57-
"on_permission_request": lambda req: {"kind": "approved"},
58-
})
58+
)
5959

6060
# Copilot now has access to skills in those directories
6161
await session.send_and_wait({"prompt": "Review this code for security issues"})
@@ -159,10 +159,13 @@ const session = await client.createSession({
159159
<summary><strong>Python</strong></summary>
160160

161161
```python
162-
session = await client.create_session({
163-
"skill_directories": ["./skills"],
164-
"disabled_skills": ["experimental-feature", "deprecated-tool"],
165-
})
162+
from copilot import PermissionHandler
163+
164+
session = await client.create_session(
165+
PermissionHandler.approve_all,
166+
skill_directories=["./skills"],
167+
disabled_skills=["experimental-feature", "deprecated-tool"],
168+
)
166169
```
167170

168171
</details>

0 commit comments

Comments
 (0)