Skip to content

Commit 0eadc52

Browse files
committed
Fix Go cookbook recipes to use correct SDK API
All 5 Go recipes and their markdown docs used incorrect API patterns that don't match the real github.com/github/copilot-sdk/go v0.1.23: - copilot.NewClient() -> copilot.NewClient(nil) (*ClientOptions param) - client.Start() -> client.Start(ctx) (context.Context required) - copilot.SessionConfig -> &copilot.SessionConfig (pointer required) - session.On(func(event copilot.Event)) -> session.On(func(event copilot.SessionEvent)) - Type assertions -> event.Type string check + *event.Data.Content deref - session.WaitForIdle() -> session.SendAndWait(ctx, ...) (WaitForIdle doesn't exist) - copilot.SystemMessage -> copilot.SystemMessageConfig All 5 recipes verified to compile against SDK v0.1.23.
1 parent 4555fee commit 0eadc52

File tree

10 files changed

+241
-232
lines changed

10 files changed

+241
-232
lines changed

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

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,55 @@ You need to handle various error conditions like connection failures, timeouts,
1818
package main
1919
2020
import (
21+
"context"
2122
"fmt"
2223
"log"
23-
"github.com/github/copilot-sdk/go"
24+
copilot "github.com/github/copilot-sdk/go"
2425
)
2526
2627
func main() {
27-
client := copilot.NewClient()
28+
ctx := context.Background()
29+
client := copilot.NewClient(nil)
2830
29-
if err := client.Start(); err != nil {
31+
if err := client.Start(ctx); err != nil {
3032
log.Fatalf("Failed to start client: %v", err)
3133
}
32-
defer func() {
33-
if err := client.Stop(); err != nil {
34-
log.Printf("Error stopping client: %v", err)
35-
}
36-
}()
34+
defer client.Stop()
3735
38-
session, err := client.CreateSession(copilot.SessionConfig{
36+
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
3937
Model: "gpt-5",
4038
})
4139
if err != nil {
4240
log.Fatalf("Failed to create session: %v", err)
4341
}
4442
defer session.Destroy()
4543
46-
responseChan := make(chan string, 1)
47-
session.On(func(event copilot.Event) {
48-
if msg, ok := event.(copilot.AssistantMessageEvent); ok {
49-
responseChan <- msg.Data.Content
50-
}
51-
})
52-
53-
if err := session.Send(copilot.MessageOptions{Prompt: "Hello!"}); err != nil {
44+
result, err := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
45+
if err != nil {
5446
log.Printf("Failed to send message: %v", err)
47+
return
5548
}
5649
57-
response := <-responseChan
58-
fmt.Println(response)
50+
if result != nil && result.Data.Content != nil {
51+
fmt.Println(*result.Data.Content)
52+
}
5953
}
6054
```
6155
6256
## Handling specific error types
6357
6458
```go
6559
import (
60+
"context"
6661
"errors"
6762
"os/exec"
63+
copilot "github.com/github/copilot-sdk/go"
6864
)
6965
70-
func startClient() error {
71-
client := copilot.NewClient()
66+
func startClient(ctx context.Context) error {
67+
client := copilot.NewClient(nil)
7268
73-
if err := client.Start(); err != nil {
69+
if err := client.Start(ctx); err != nil {
7470
var execErr *exec.Error
7571
if errors.As(err, &execErr) {
7672
return fmt.Errorf("Copilot CLI not found. Please install it first: %w", err)
@@ -91,47 +87,38 @@ func startClient() error {
9187
import (
9288
"context"
9389
"time"
90+
copilot "github.com/github/copilot-sdk/go"
9491
)
9592
9693
func sendWithTimeout(session *copilot.Session) error {
9794
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
9895
defer cancel()
9996
100-
responseChan := make(chan string, 1)
101-
errChan := make(chan error, 1)
102-
103-
session.On(func(event copilot.Event) {
104-
if msg, ok := event.(copilot.AssistantMessageEvent); ok {
105-
responseChan <- msg.Data.Content
97+
result, err := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Complex question..."})
98+
if err != nil {
99+
if errors.Is(err, context.DeadlineExceeded) {
100+
return fmt.Errorf("request timed out")
106101
}
107-
})
108-
109-
if err := session.Send(copilot.MessageOptions{Prompt: "Complex question..."}); err != nil {
110102
return err
111103
}
112104
113-
select {
114-
case response := <-responseChan:
115-
fmt.Println(response)
116-
return nil
117-
case err := <-errChan:
118-
return err
119-
case <-ctx.Done():
120-
return fmt.Errorf("request timed out")
105+
if result != nil && result.Data.Content != nil {
106+
fmt.Println(*result.Data.Content)
121107
}
108+
return nil
122109
}
123110
```
124111
125112
## Aborting a request
126113
127114
```go
128-
func abortAfterDelay(session *copilot.Session) {
129-
// Start a request
130-
session.Send(copilot.MessageOptions{Prompt: "Write a very long story..."})
115+
func abortAfterDelay(ctx context.Context, session *copilot.Session) {
116+
// Start a request (non-blocking send)
117+
session.Send(ctx, copilot.MessageOptions{Prompt: "Write a very long story..."})
131118
132119
// Abort it after some condition
133120
time.AfterFunc(5*time.Second, func() {
134-
if err := session.Abort(); err != nil {
121+
if err := session.Abort(ctx); err != nil {
135122
log.Printf("Failed to abort: %v", err)
136123
}
137124
fmt.Println("Request aborted")
@@ -143,13 +130,16 @@ func abortAfterDelay(session *copilot.Session) {
143130
144131
```go
145132
import (
133+
"context"
146134
"os"
147135
"os/signal"
148136
"syscall"
137+
copilot "github.com/github/copilot-sdk/go"
149138
)
150139
151140
func main() {
152-
client := copilot.NewClient()
141+
ctx := context.Background()
142+
client := copilot.NewClient(nil)
153143
154144
// Set up signal handling
155145
sigChan := make(chan os.Signal, 1)
@@ -158,15 +148,11 @@ func main() {
158148
go func() {
159149
<-sigChan
160150
fmt.Println("\nShutting down...")
161-
162-
if err := client.Stop(); err != nil {
163-
log.Printf("Cleanup errors: %v", err)
164-
}
165-
151+
client.Stop()
166152
os.Exit(0)
167153
}()
168154
169-
if err := client.Start(); err != nil {
155+
if err := client.Start(ctx); err != nil {
170156
log.Fatal(err)
171157
}
172158
@@ -178,14 +164,15 @@ func main() {
178164
179165
```go
180166
func doWork() error {
181-
client := copilot.NewClient()
167+
ctx := context.Background()
168+
client := copilot.NewClient(nil)
182169
183-
if err := client.Start(); err != nil {
170+
if err := client.Start(ctx); err != nil {
184171
return fmt.Errorf("failed to start: %w", err)
185172
}
186173
defer client.Stop()
187174
188-
session, err := client.CreateSession(copilot.SessionConfig{Model: "gpt-5"})
175+
session, err := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-5"})
189176
if err != nil {
190177
return fmt.Errorf("failed to create session: %w", err)
191178
}

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,26 @@ You have a folder with many files and want to organize them into subfolders base
1818
package main
1919
2020
import (
21+
"context"
2122
"fmt"
2223
"log"
2324
"os"
2425
"path/filepath"
25-
"github.com/github/copilot-sdk/go"
26+
copilot "github.com/github/copilot-sdk/go"
2627
)
2728
2829
func main() {
30+
ctx := context.Background()
31+
2932
// Create and start client
30-
client := copilot.NewClient()
31-
if err := client.Start(); err != nil {
33+
client := copilot.NewClient(nil)
34+
if err := client.Start(ctx); err != nil {
3235
log.Fatal(err)
3336
}
3437
defer client.Stop()
3538
3639
// Create session
37-
session, err := client.CreateSession(copilot.SessionConfig{
40+
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
3841
Model: "gpt-5",
3942
})
4043
if err != nil {
@@ -43,14 +46,20 @@ func main() {
4346
defer session.Destroy()
4447
4548
// Event handler
46-
session.On(func(event copilot.Event) {
47-
switch e := event.(type) {
48-
case copilot.AssistantMessageEvent:
49-
fmt.Printf("\nCopilot: %s\n", e.Data.Content)
50-
case copilot.ToolExecutionStartEvent:
51-
fmt.Printf(" → Running: %s\n", e.Data.ToolName)
52-
case copilot.ToolExecutionCompleteEvent:
53-
fmt.Printf(" ✓ Completed: %s\n", e.Data.ToolName)
49+
session.On(func(event copilot.SessionEvent) {
50+
switch event.Type {
51+
case "assistant.message":
52+
if event.Data.Content != nil {
53+
fmt.Printf("\nCopilot: %s\n", *event.Data.Content)
54+
}
55+
case "tool.execution_start":
56+
if event.Data.ToolName != nil {
57+
fmt.Printf(" → Running: %s\n", *event.Data.ToolName)
58+
}
59+
case "tool.execution_complete":
60+
if event.Data.ToolName != nil {
61+
fmt.Printf(" ✓ Completed: %s\n", *event.Data.ToolName)
62+
}
5463
}
5564
})
5665
@@ -69,11 +78,10 @@ Analyze the files in "%s" and organize them into subfolders.
6978
Please confirm before moving any files.
7079
`, targetFolder)
7180
72-
if err := session.Send(copilot.MessageOptions{Prompt: prompt}); err != nil {
81+
_, err = session.SendAndWait(ctx, copilot.MessageOptions{Prompt: prompt})
82+
if err != nil {
7383
log.Fatal(err)
7484
}
75-
76-
session.WaitForIdle()
7785
}
7886
```
7987
@@ -116,7 +124,7 @@ Analyze files in "%s" and show me how you would organize them
116124
by file type. DO NOT move any files - just show me the plan.
117125
`, targetFolder)
118126
119-
session.Send(copilot.MessageOptions{Prompt: prompt})
127+
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: prompt})
120128
```
121129
122130
## Custom grouping with AI analysis
@@ -134,7 +142,7 @@ Consider:
134142
Propose folder names that are descriptive and useful.
135143
`, targetFolder)
136144
137-
session.Send(copilot.MessageOptions{Prompt: prompt})
145+
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: prompt})
138146
```
139147
140148
## Safety considerations

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,49 @@ You need to run multiple conversations in parallel, each with its own context an
1818
package main
1919
2020
import (
21+
"context"
2122
"fmt"
2223
"log"
23-
"github.com/github/copilot-sdk/go"
24+
copilot "github.com/github/copilot-sdk/go"
2425
)
2526
2627
func main() {
27-
client := copilot.NewClient()
28+
ctx := context.Background()
29+
client := copilot.NewClient(nil)
2830
29-
if err := client.Start(); err != nil {
31+
if err := client.Start(ctx); err != nil {
3032
log.Fatal(err)
3133
}
3234
defer client.Stop()
3335
3436
// Create multiple independent sessions
35-
session1, err := client.CreateSession(copilot.SessionConfig{Model: "gpt-5"})
37+
session1, err := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-5"})
3638
if err != nil {
3739
log.Fatal(err)
3840
}
3941
defer session1.Destroy()
4042
41-
session2, err := client.CreateSession(copilot.SessionConfig{Model: "gpt-5"})
43+
session2, err := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-5"})
4244
if err != nil {
4345
log.Fatal(err)
4446
}
4547
defer session2.Destroy()
4648
47-
session3, err := client.CreateSession(copilot.SessionConfig{Model: "claude-sonnet-4.5"})
49+
session3, err := client.CreateSession(ctx, &copilot.SessionConfig{Model: "claude-sonnet-4.5"})
4850
if err != nil {
4951
log.Fatal(err)
5052
}
5153
defer session3.Destroy()
5254
5355
// Each session maintains its own conversation history
54-
session1.Send(copilot.MessageOptions{Prompt: "You are helping with a Python project"})
55-
session2.Send(copilot.MessageOptions{Prompt: "You are helping with a TypeScript project"})
56-
session3.Send(copilot.MessageOptions{Prompt: "You are helping with a Go project"})
56+
session1.Send(ctx, copilot.MessageOptions{Prompt: "You are helping with a Python project"})
57+
session2.Send(ctx, copilot.MessageOptions{Prompt: "You are helping with a TypeScript project"})
58+
session3.Send(ctx, copilot.MessageOptions{Prompt: "You are helping with a Go project"})
5759
5860
// Follow-up messages stay in their respective contexts
59-
session1.Send(copilot.MessageOptions{Prompt: "How do I create a virtual environment?"})
60-
session2.Send(copilot.MessageOptions{Prompt: "How do I set up tsconfig?"})
61-
session3.Send(copilot.MessageOptions{Prompt: "How do I initialize a module?"})
61+
session1.Send(ctx, copilot.MessageOptions{Prompt: "How do I create a virtual environment?"})
62+
session2.Send(ctx, copilot.MessageOptions{Prompt: "How do I set up tsconfig?"})
63+
session3.Send(ctx, copilot.MessageOptions{Prompt: "How do I initialize a module?"})
6264
}
6365
```
6466
@@ -67,7 +69,7 @@ func main() {
6769
Use custom IDs for easier tracking:
6870
6971
```go
70-
session, err := client.CreateSession(copilot.SessionConfig{
72+
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
7173
SessionID: "user-123-chat",
7274
Model: "gpt-5",
7375
})
@@ -81,7 +83,7 @@ fmt.Println(session.SessionID) // "user-123-chat"
8183
## Listing sessions
8284
8385
```go
84-
sessions, err := client.ListSessions()
86+
sessions, err := client.ListSessions(ctx)
8587
if err != nil {
8688
log.Fatal(err)
8789
}
@@ -95,7 +97,7 @@ for _, sessionInfo := range sessions {
9597
9698
```go
9799
// Delete a specific session
98-
if err := client.DeleteSession("user-123-chat"); err != nil {
100+
if err := client.DeleteSession(ctx, "user-123-chat"); err != nil {
99101
log.Printf("Failed to delete session: %v", err)
100102
}
101103
```

0 commit comments

Comments
 (0)