@@ -18,59 +18,56 @@ You need to handle various error conditions like connection failures, timeouts,
1818package main
1919
2020import (
21+ " context"
2122 " fmt"
2223 " log"
23- " github.com/github/copilot-sdk/go"
24+ copilot " github.com/github/copilot-sdk/go"
2425)
2526
2627func 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
6559import (
60+ " context"
6661 " errors"
62+ " fmt"
6763 " os/exec"
64+ copilot " github.com/github/copilot-sdk/go"
6865)
6966
70- func startClient () error {
71- client := copilot.NewClient ()
67+ func startClient(ctx context.Context ) error {
68+ client := copilot.NewClient(nil )
7269
73- if err := client.Start (); err ! = nil {
70+ if err := client.Start(ctx ); err ! = nil {
7471 var execErr * exec.Error
7572 if errors.As(err, & execErr) {
7673 return fmt.Errorf(" Copilot CLI not found. Please install it first: %w" , err)
@@ -90,48 +87,41 @@ func startClient() error {
9087` ` ` go
9188import (
9289 " context"
90+ " errors"
91+ " fmt"
9392 " time"
93+ copilot " github.com/github/copilot-sdk/go"
9494)
9595
9696func sendWithTimeout(session * copilot.Session) error {
9797 ctx, cancel := context.WithTimeout(context.Background (), 30* time.Second)
9898 defer cancel ()
9999
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
100+ result, err := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: " Complex question..." })
101+ if err ! = nil {
102+ if errors.Is(err, context.DeadlineExceeded) {
103+ return fmt.Errorf(" request timed out" )
106104 }
107- })
108-
109- if err := session.Send(copilot.MessageOptions{Prompt: " Complex question..." }); err ! = nil {
110105 return err
111106 }
112107
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" )
108+ if result ! = nil && result.Data.Content ! = nil {
109+ fmt.Println(* result.Data.Content)
121110 }
111+ return nil
122112}
123113` ` `
124114
125115# # Aborting a request
126116
127117` ` ` go
128- func abortAfterDelay(session * copilot.Session) {
129- // Start a request
130- session.Send(copilot.MessageOptions{Prompt: " Write a very long story..." })
118+ func abortAfterDelay(ctx context.Context, session * copilot.Session) {
119+ // Start a request (non-blocking send)
120+ session.Send(ctx, copilot.MessageOptions{Prompt: " Write a very long story..." })
131121
132122 // Abort it after some condition
133123 time.AfterFunc(5* time.Second, func () {
134- if err := session.Abort (); err ! = nil {
124+ if err := session.Abort(ctx ); err ! = nil {
135125 log.Printf(" Failed to abort: %v" , err)
136126 }
137127 fmt.Println(" Request aborted" )
@@ -143,13 +133,18 @@ func abortAfterDelay(session *copilot.Session) {
143133
144134` ` ` go
145135import (
136+ " context"
137+ " fmt"
138+ " log"
146139 " os"
147140 " os/signal"
148141 " syscall"
142+ copilot " github.com/github/copilot-sdk/go"
149143)
150144
151145func main () {
152- client := copilot.NewClient ()
146+ ctx := context.Background ()
147+ client := copilot.NewClient(nil)
153148
154149 // Set up signal handling
155150 sigChan := make(chan os.Signal, 1)
@@ -158,15 +153,11 @@ func main() {
158153 go func () {
159154 < -sigChan
160155 fmt.Println(" \nShutting down..." )
161-
162- if err := client.Stop (); err ! = nil {
163- log.Printf(" Cleanup errors: %v" , err)
164- }
165-
156+ client.Stop ()
166157 os.Exit(0)
167158 }()
168159
169- if err := client.Start (); err ! = nil {
160+ if err := client.Start(ctx ); err ! = nil {
170161 log.Fatal(err)
171162 }
172163
@@ -178,14 +169,15 @@ func main() {
178169
179170` ` ` go
180171func doWork () error {
181- client := copilot.NewClient ()
172+ ctx := context.Background ()
173+ client := copilot.NewClient(nil)
182174
183- if err := client.Start (); err ! = nil {
175+ if err := client.Start(ctx ); err ! = nil {
184176 return fmt.Errorf(" failed to start: %w" , err)
185177 }
186178 defer client.Stop ()
187179
188- session, err := client.CreateSession(copilot.SessionConfig{Model: " gpt-5" })
180+ session, err := client.CreateSession(ctx, & copilot.SessionConfig{Model: " gpt-5" })
189181 if err ! = nil {
190182 return fmt.Errorf(" failed to create session: %w" , err)
191183 }
0 commit comments