Skip to content

Commit aa8acb0

Browse files
committed
docs: update go sdk examples
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent 3fc2cef commit aa8acb0

15 files changed

+120
-154
lines changed

cookbook/copilot-sdk/go.sum

Lines changed: 0 additions & 6 deletions
This file was deleted.

cookbook/copilot-sdk/go/accessibility-report.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ func main() {
7575
}
7676
defer client.Stop()
7777
78-
streaming := true
7978
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
8079
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
8180
Model: "claude-opus-4.6",
82-
Streaming: &streaming,
83-
McpServers: map[string]interface{}{
84-
"playwright": map[string]interface{}{
81+
Streaming: true,
82+
MCPServers: map[string]copilot.MCPServerConfig{
83+
"playwright": {
8584
"type": "local",
8685
"command": "npx",
8786
"args": []string{"@playwright/mcp@latest"},
@@ -92,26 +91,22 @@ func main() {
9291
if err != nil {
9392
log.Fatal(err)
9493
}
95-
defer session.Destroy()
94+
defer session.Disconnect()
9695
9796
// Set up streaming event handling
9897
done := make(chan struct{}, 1)
9998
10099
session.On(func(event copilot.SessionEvent) {
101-
switch event.Type {
102-
case "assistant.message.delta":
103-
if event.Data.DeltaContent != nil {
104-
fmt.Print(*event.Data.DeltaContent)
105-
}
106-
case "session.idle":
100+
switch d := event.Data.(type) {
101+
case *copilot.AssistantMessageDeltaData:
102+
fmt.Print(d.DeltaContent)
103+
case *copilot.SessionIdleData:
107104
select {
108105
case done <- struct{}{}:
109106
default:
110107
}
111-
case "session.error":
112-
if event.Data.Message != nil {
113-
fmt.Printf("\nError: %s\n", *event.Data.Message)
114-
}
108+
case *copilot.SessionErrorData:
109+
fmt.Printf("\nError: %s\n", d.Message)
115110
select {
116111
case done <- struct{}{}:
117112
default:
@@ -202,7 +197,7 @@ func main() {
202197
## How it works
203198
204199
1. **Playwright MCP server**: Configures a local MCP server running `@playwright/mcp` to provide browser automation tools
205-
2. **Streaming output**: Uses `Streaming: &streaming` and `assistant.message.delta` events for real-time token-by-token output
200+
2. **Streaming output**: Uses `Streaming: true` and `AssistantMessageDeltaData` events for real-time token-by-token output
206201
3. **Accessibility snapshot**: Playwright's `browser_snapshot` tool captures the full accessibility tree of the page
207202
4. **Structured report**: The prompt engineers a consistent WCAG-aligned report format with emoji severity indicators
208203
5. **Test generation**: Optionally detects the project language and generates Playwright accessibility tests
@@ -216,8 +211,8 @@ The recipe configures a local MCP server that runs alongside the session:
216211
```go
217212
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
218213
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
219-
McpServers: map[string]interface{}{
220-
"playwright": map[string]interface{}{
214+
MCPServers: map[string]copilot.MCPServerConfig{
215+
"playwright": {
221216
"type": "local",
222217
"command": "npx",
223218
"args": []string{"@playwright/mcp@latest"},
@@ -235,12 +230,10 @@ Unlike `SendAndWait`, this recipe uses streaming for real-time output:
235230
236231
```go
237232
session.On(func(event copilot.SessionEvent) {
238-
switch event.Type {
239-
case "assistant.message.delta":
240-
if event.Data.DeltaContent != nil {
241-
fmt.Print(*event.Data.DeltaContent)
242-
}
243-
case "session.idle":
233+
switch d := event.Data.(type) {
234+
case *copilot.AssistantMessageDeltaData:
235+
fmt.Print(d.DeltaContent)
236+
case *copilot.SessionIdleData:
244237
done <- struct{}{}
245238
}
246239
})

cookbook/copilot-sdk/go/error-handling.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@ func main() {
3535
3636
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
3737
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
38-
Model: "gpt-5",
38+
Model: "gpt-5.4",
3939
})
4040
if err != nil {
4141
log.Fatalf("Failed to create session: %v", err)
4242
}
43-
defer session.Destroy()
43+
defer session.Disconnect()
4444
4545
result, err := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
4646
if err != nil {
4747
log.Printf("Failed to send message: %v", err)
4848
return
4949
}
5050
51-
if result != nil && result.Data.Content != nil {
52-
fmt.Println(*result.Data.Content)
51+
if result != nil {
52+
if d, ok := result.Data.(*copilot.AssistantMessageData); ok {
53+
fmt.Println(d.Content)
54+
}
5355
}
5456
}
5557
```
@@ -180,12 +182,12 @@ func doWork() error {
180182
181183
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
182184
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
183-
Model: "gpt-5",
185+
Model: "gpt-5.4",
184186
})
185187
if err != nil {
186188
return fmt.Errorf("failed to create session: %w", err)
187189
}
188-
defer session.Destroy()
190+
defer session.Disconnect()
189191
190192
// ... do work ...
191193

cookbook/copilot-sdk/go/managing-local-files.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,22 @@ func main() {
3939
// Create session
4040
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
4141
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
42-
Model: "gpt-5",
42+
Model: "gpt-5.4",
4343
})
4444
if err != nil {
4545
log.Fatal(err)
4646
}
47-
defer session.Destroy()
47+
defer session.Disconnect()
4848
4949
// Event handler
5050
session.On(func(event copilot.SessionEvent) {
51-
switch event.Type {
52-
case "assistant.message":
53-
if event.Data.Content != nil {
54-
fmt.Printf("\nCopilot: %s\n", *event.Data.Content)
55-
}
56-
case "tool.execution_start":
57-
if event.Data.ToolName != nil {
58-
fmt.Printf(" → Running: %s\n", *event.Data.ToolName)
59-
}
60-
case "tool.execution_complete":
61-
if event.Data.ToolName != nil {
62-
fmt.Printf(" ✓ Completed: %s\n", *event.Data.ToolName)
63-
}
51+
switch d := event.Data.(type) {
52+
case *copilot.AssistantMessageData:
53+
fmt.Printf("\nCopilot: %s\n", d.Content)
54+
case *copilot.ToolExecutionStartData:
55+
fmt.Printf(" → Running: %s\n", d.ToolName)
56+
case *copilot.ToolExecutionCompleteData:
57+
fmt.Printf(" ✓ Completed (success=%v)\n", d.Success)
6458
}
6559
})
6660

cookbook/copilot-sdk/go/multiple-sessions.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,30 @@ func main() {
3636
// Create multiple independent sessions
3737
session1, err := client.CreateSession(ctx, &copilot.SessionConfig{
3838
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
39-
Model: "gpt-5",
39+
Model: "gpt-5.4",
4040
})
4141
if err != nil {
4242
log.Fatal(err)
4343
}
44-
defer session1.Destroy()
44+
defer session1.Disconnect()
4545
4646
session2, err := client.CreateSession(ctx, &copilot.SessionConfig{
4747
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
48-
Model: "gpt-5",
48+
Model: "gpt-5.4",
4949
})
5050
if err != nil {
5151
log.Fatal(err)
5252
}
53-
defer session2.Destroy()
53+
defer session2.Disconnect()
5454
5555
session3, err := client.CreateSession(ctx, &copilot.SessionConfig{
5656
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
57-
Model: "claude-sonnet-4.5",
57+
Model: "claude-sonnet-4.6",
5858
})
5959
if err != nil {
6060
log.Fatal(err)
6161
}
62-
defer session3.Destroy()
62+
defer session3.Disconnect()
6363
6464
// Each session maintains its own conversation history
6565
session1.Send(ctx, copilot.MessageOptions{Prompt: "You are helping with a Python project"})
@@ -81,7 +81,7 @@ Use custom IDs for easier tracking:
8181
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
8282
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
8383
SessionID: "user-123-chat",
84-
Model: "gpt-5",
84+
Model: "gpt-5.4",
8585
})
8686
if err != nil {
8787
log.Fatal(err)
@@ -93,7 +93,7 @@ fmt.Println(session.SessionID) // "user-123-chat"
9393
## Listing sessions
9494
9595
```go
96-
sessions, err := client.ListSessions(ctx)
96+
sessions, err := client.ListSessions(ctx, nil)
9797
if err != nil {
9898
log.Fatal(err)
9999
}

cookbook/copilot-sdk/go/persisting-sessions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ func main() {
3434
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{
3535
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
3636
SessionID: "user-123-conversation",
37-
Model: "gpt-5",
37+
Model: "gpt-5.4",
3838
})
3939
4040
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Let's discuss TypeScript generics"})
4141
4242
// Session ID is preserved
4343
fmt.Println(session.SessionID)
4444
45-
// Destroy session but keep data on disk
46-
session.Destroy()
45+
// Disconnect session but keep data on disk
46+
session.Disconnect()
4747
}
4848
```
4949
@@ -61,13 +61,13 @@ session, _ := client.ResumeSession(ctx, "user-123-conversation", &copilot.Resume
6161
// Previous context is restored
6262
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "What were we discussing?"})
6363

64-
session.Destroy()
64+
session.Disconnect()
6565
```
6666

6767
### Listing available sessions
6868

6969
```go
70-
sessions, _ := client.ListSessions(ctx)
70+
sessions, _ := client.ListSessions(ctx, nil)
7171
for _, s := range sessions {
7272
fmt.Println("Session:", s.SessionID)
7373
}
@@ -85,8 +85,8 @@ client.DeleteSession(ctx, "user-123-conversation")
8585
```go
8686
messages, _ := session.GetMessages(ctx)
8787
for _, msg := range messages {
88-
if msg.Data.Content != nil {
89-
fmt.Printf("[%s] %s\n", msg.Type, *msg.Data.Content)
88+
if d, ok := msg.Data.(*copilot.AssistantMessageData); ok {
89+
fmt.Printf("[assistant.message] %s\n", d.Content)
9090
}
9191
}
9292
```

cookbook/copilot-sdk/go/pr-visualization.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func main() {
139139
cwd, _ := os.Getwd()
140140
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
141141
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
142-
Model: "gpt-5",
142+
Model: "gpt-5.4",
143143
SystemMessage: &copilot.SystemMessageConfig{
144144
Content: fmt.Sprintf(`
145145
<context>
@@ -159,19 +159,15 @@ The current working directory is: %s
159159
if err != nil {
160160
log.Fatal(err)
161161
}
162-
defer session.Destroy()
162+
defer session.Disconnect()
163163

164164
// Set up event handling
165165
session.On(func(event copilot.SessionEvent) {
166-
switch event.Type {
167-
case "assistant.message":
168-
if event.Data.Content != nil {
169-
fmt.Printf("\n🤖 %s\n\n", *event.Data.Content)
170-
}
171-
case "tool.execution_start":
172-
if event.Data.ToolName != nil {
173-
fmt.Printf(" ⚙️ %s\n", *event.Data.ToolName)
174-
}
166+
switch d := event.Data.(type) {
167+
case *copilot.AssistantMessageData:
168+
fmt.Printf("\n🤖 %s\n\n", d.Content)
169+
case *copilot.ToolExecutionStartData:
170+
fmt.Printf(" ⚙️ %s\n", d.ToolName)
175171
}
176172
})
177173

cookbook/copilot-sdk/go/ralph-loop.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func ralphLoop(ctx context.Context, promptFile string, maxIterations int) error
7171
// Fresh session each iteration — context isolation is the point
7272
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
7373
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
74-
Model: "gpt-5.1-codex-mini",
74+
Model: "gpt-5.3-codex",
7575
})
7676
if err != nil {
7777
return err
@@ -80,7 +80,7 @@ func ralphLoop(ctx context.Context, promptFile string, maxIterations int) error
8080
_, err = session.SendAndWait(ctx, copilot.MessageOptions{
8181
Prompt: string(prompt),
8282
})
83-
session.Destroy()
83+
session.Disconnect()
8484
if err != nil {
8585
return err
8686
}
@@ -146,27 +146,27 @@ func ralphLoop(ctx context.Context, mode string, maxIterations int) error {
146146
fmt.Printf("\n=== Iteration %d/%d ===\n", i, maxIterations)
147147
148148
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
149-
Model: "gpt-5.1-codex-mini",
149+
Model: "gpt-5.3-codex",
150150
WorkingDirectory: cwd,
151-
OnPermissionRequest: func(_ copilot.PermissionRequest, _ map[string]string) copilot.PermissionRequestResult {
152-
return copilot.PermissionRequestResult{Kind: "approved"}
151+
OnPermissionRequest: func(_ copilot.PermissionRequest, _ copilot.PermissionInvocation) (copilot.PermissionRequestResult, error) {
152+
return copilot.PermissionRequestResult{Kind: "approved"}, nil
153153
},
154154
})
155155
if err != nil {
156156
return err
157157
}
158158
159159
// Log tool usage for visibility
160-
session.On(func(event copilot.Event) {
161-
if toolExecution, ok := event.(copilot.ToolExecutionStartEvent); ok {
162-
fmt.Printf(" ⚙ %s\n", toolExecution.Data.ToolName)
160+
session.On(func(event copilot.SessionEvent) {
161+
if d, ok := event.Data.(*copilot.ToolExecutionStartData); ok {
162+
fmt.Printf(" ⚙ %s\n", d.ToolName)
163163
}
164164
})
165165
166166
_, err = session.SendAndWait(ctx, copilot.MessageOptions{
167167
Prompt: string(prompt),
168168
})
169-
session.Destroy()
169+
session.Disconnect()
170170
if err != nil {
171171
return err
172172
}

0 commit comments

Comments
 (0)