Skip to content

Commit 685df87

Browse files
qmuntalCopilot
andauthored
[go] Accept and propagate context (#340)
* accept and propagate context * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 56431ab commit 685df87

18 files changed

+300
-315
lines changed

.github/workflows/copilot-setup-steps.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Set up Go
5353
uses: actions/setup-go@v6
5454
with:
55-
go-version: "1.23"
55+
go-version: "1.24"
5656

5757
# Setup .NET (for .NET SDK)
5858
- name: Set up .NET

.github/workflows/go-sdk-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
id: setup-copilot
4444
- uses: actions/setup-go@v6
4545
with:
46-
go-version: "1.23"
46+
go-version: "1.24"
4747

4848
- name: Run go fmt
4949
if: runner.os == 'Linux'

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ This is a multi-language SDK repository. Install the tools for the SDK(s) you pl
3838
1. Install dependencies: `cd python && uv pip install -e ".[dev]"`
3939

4040
### Go SDK
41-
1. Install [Go 1.23+](https://go.dev/doc/install)
41+
1. Install [Go 1.24+](https://go.dev/doc/install)
4242
1. Install [golangci-lint](https://golangci-lint.run/welcome/install/#local-installation)
4343
1. Install dependencies: `cd go && go mod download`
4444

go/README.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ func main() {
2929
})
3030

3131
// Start the client
32-
if err := client.Start(); err != nil {
32+
if err := client.Start(context.Background()); err != nil {
3333
log.Fatal(err)
3434
}
3535
defer client.Stop()
3636

3737
// Create a session
38-
session, err := client.CreateSession(&copilot.SessionConfig{
38+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
3939
Model: "gpt-5",
4040
})
4141
if err != nil {
@@ -57,7 +57,7 @@ func main() {
5757
})
5858

5959
// Send a message
60-
_, err = session.Send(copilot.MessageOptions{
60+
_, err = session.Send(context.Background(), copilot.MessageOptions{
6161
Prompt: "What is 2+2?",
6262
})
6363
if err != nil {
@@ -74,16 +74,16 @@ func main() {
7474
### Client
7575

7676
- `NewClient(options *ClientOptions) *Client` - Create a new client
77-
- `Start() error` - Start the CLI server
78-
- `Stop() []error` - Stop the CLI server (returns array of errors, empty if all succeeded)
77+
- `Start(ctx context.Context) error` - Start the CLI server
78+
- `Stop() error` - Stop the CLI server
7979
- `ForceStop()` - Forcefully stop without graceful cleanup
80-
- `CreateSession(config *SessionConfig) (*Session, error)` - Create a new session
81-
- `ResumeSession(sessionID string) (*Session, error)` - Resume an existing session
82-
- `ResumeSessionWithOptions(sessionID string, config *ResumeSessionConfig) (*Session, error)` - Resume with additional configuration
83-
- `ListSessions() ([]SessionMetadata, error)` - List all sessions known to the server
84-
- `DeleteSession(sessionID string) error` - Delete a session permanently
85-
- `GetState() ConnectionState` - Get connection state
86-
- `Ping(message string) (*PingResponse, error)` - Ping the server
80+
- `CreateSession(ctx context.Context, config *SessionConfig) (*Session, error)` - Create a new session
81+
- `ResumeSession(ctx context.Context, sessionID string) (*Session, error)` - Resume an existing session
82+
- `ResumeSessionWithOptions(ctx context.Context, sessionID string, config *ResumeSessionConfig) (*Session, error)` - Resume with additional configuration
83+
- `ListSessions(ctx context.Context) ([]SessionMetadata, error)` - List all sessions known to the server
84+
- `DeleteSession(ctx context.Context, sessionID string) error` - Delete a session permanently
85+
- `State() ConnectionState` - Get connection state
86+
- `Ping(ctx context.Context, message string) (*PingResponse, error)` - Ping the server
8787

8888
**ClientOptions:**
8989

@@ -121,10 +121,10 @@ func main() {
121121

122122
### Session
123123

124-
- `Send(options MessageOptions) (string, error)` - Send a message
124+
- `Send(ctx context.Context, options MessageOptions) (string, error)` - Send a message
125125
- `On(handler SessionEventHandler) func()` - Subscribe to events (returns unsubscribe function)
126-
- `Abort() error` - Abort the currently processing message
127-
- `GetMessages() ([]SessionEvent, error)` - Get message history
126+
- `Abort(ctx context.Context) error` - Abort the currently processing message
127+
- `GetMessages(ctx context.Context) ([]SessionEvent, error)` - Get message history
128128
- `Destroy() error` - Destroy the session
129129

130130
### Helper Functions
@@ -136,7 +136,7 @@ func main() {
136136
The SDK supports image attachments via the `Attachments` field in `MessageOptions`. You can attach images by providing their file path:
137137

138138
```go
139-
_, err = session.Send(copilot.MessageOptions{
139+
_, err = session.Send(context.Background(), copilot.MessageOptions{
140140
Prompt: "What's in this image?",
141141
Attachments: []copilot.Attachment{
142142
{
@@ -150,7 +150,7 @@ _, err = session.Send(copilot.MessageOptions{
150150
Supported image formats include JPG, PNG, GIF, and other common image types. The agent's `view` tool can also read images directly from the filesystem, so you can also ask questions like:
151151

152152
```go
153-
_, err = session.Send(copilot.MessageOptions{
153+
_, err = session.Send(context.Background(), copilot.MessageOptions{
154154
Prompt: "What does the most recent jpg in this directory portray?",
155155
})
156156
```
@@ -178,7 +178,7 @@ lookupIssue := copilot.DefineTool("lookup_issue", "Fetch issue details from our
178178
return issue.Summary, nil
179179
})
180180

181-
session, _ := client.CreateSession(&copilot.SessionConfig{
181+
session, _ := client.CreateSession(context.Background(), &copilot.SessionConfig{
182182
Model: "gpt-5",
183183
Tools: []copilot.Tool{lookupIssue},
184184
})
@@ -216,7 +216,7 @@ lookupIssue := copilot.Tool{
216216
},
217217
}
218218

219-
session, _ := client.CreateSession(&copilot.SessionConfig{
219+
session, _ := client.CreateSession(context.Background(), &copilot.SessionConfig{
220220
Model: "gpt-5",
221221
Tools: []copilot.Tool{lookupIssue},
222222
})
@@ -241,12 +241,12 @@ import (
241241
func main() {
242242
client := copilot.NewClient(nil)
243243

244-
if err := client.Start(); err != nil {
244+
if err := client.Start(context.Background()); err != nil {
245245
log.Fatal(err)
246246
}
247247
defer client.Stop()
248248

249-
session, err := client.CreateSession(&copilot.SessionConfig{
249+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
250250
Model: "gpt-5",
251251
Streaming: true,
252252
})
@@ -286,7 +286,7 @@ func main() {
286286
}
287287
})
288288

289-
_, err = session.Send(copilot.MessageOptions{
289+
_, err = session.Send(context.Background(), copilot.MessageOptions{
290290
Prompt: "Tell me a short story",
291291
})
292292
if err != nil {
@@ -312,7 +312,7 @@ By default, sessions use **infinite sessions** which automatically manage contex
312312

313313
```go
314314
// Default: infinite sessions enabled with default thresholds
315-
session, _ := client.CreateSession(&copilot.SessionConfig{
315+
session, _ := client.CreateSession(context.Background(), &copilot.SessionConfig{
316316
Model: "gpt-5",
317317
})
318318

@@ -321,7 +321,7 @@ fmt.Println(session.WorkspacePath())
321321
// => ~/.copilot/session-state/{sessionId}/
322322

323323
// Custom thresholds
324-
session, _ := client.CreateSession(&copilot.SessionConfig{
324+
session, _ := client.CreateSession(context.Background(), &copilot.SessionConfig{
325325
Model: "gpt-5",
326326
InfiniteSessions: &copilot.InfiniteSessionConfig{
327327
Enabled: copilot.Bool(true),
@@ -331,7 +331,7 @@ session, _ := client.CreateSession(&copilot.SessionConfig{
331331
})
332332

333333
// Disable infinite sessions
334-
session, _ := client.CreateSession(&copilot.SessionConfig{
334+
session, _ := client.CreateSession(context.Background(), &copilot.SessionConfig{
335335
Model: "gpt-5",
336336
InfiniteSessions: &copilot.InfiniteSessionConfig{
337337
Enabled: copilot.Bool(false),
@@ -360,7 +360,7 @@ The SDK supports custom OpenAI-compatible API providers (BYOK - Bring Your Own K
360360
**Example with Ollama:**
361361

362362
```go
363-
session, err := client.CreateSession(&copilot.SessionConfig{
363+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
364364
Model: "deepseek-coder-v2:16b", // Required when using custom provider
365365
Provider: &copilot.ProviderConfig{
366366
Type: "openai",
@@ -373,7 +373,7 @@ session, err := client.CreateSession(&copilot.SessionConfig{
373373
**Example with custom OpenAI-compatible API:**
374374

375375
```go
376-
session, err := client.CreateSession(&copilot.SessionConfig{
376+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
377377
Model: "gpt-4",
378378
Provider: &copilot.ProviderConfig{
379379
Type: "openai",
@@ -386,7 +386,7 @@ session, err := client.CreateSession(&copilot.SessionConfig{
386386
**Example with Azure OpenAI:**
387387

388388
```go
389-
session, err := client.CreateSession(&copilot.SessionConfig{
389+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
390390
Model: "gpt-4",
391391
Provider: &copilot.ProviderConfig{
392392
Type: "azure", // Must be "azure" for Azure endpoints, NOT "openai"
@@ -408,7 +408,7 @@ session, err := client.CreateSession(&copilot.SessionConfig{
408408
Enable the agent to ask questions to the user using the `ask_user` tool by providing an `OnUserInputRequest` handler:
409409

410410
```go
411-
session, err := client.CreateSession(&copilot.SessionConfig{
411+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
412412
Model: "gpt-5",
413413
OnUserInputRequest: func(request copilot.UserInputRequest, invocation copilot.UserInputInvocation) (copilot.UserInputResponse, error) {
414414
// request.Question - The question to ask
@@ -434,7 +434,7 @@ session, err := client.CreateSession(&copilot.SessionConfig{
434434
Hook into session lifecycle events by providing handlers in the `Hooks` configuration:
435435

436436
```go
437-
session, err := client.CreateSession(&copilot.SessionConfig{
437+
session, err := client.CreateSession(context.Background(), &copilot.SessionConfig{
438438
Model: "gpt-5",
439439
Hooks: &copilot.SessionHooks{
440440
// Called before each tool execution

0 commit comments

Comments
 (0)