Skip to content

Commit f5ed23e

Browse files
committed
Fix Go code examples in docs, scenarios, and lint warning
Update all Go code examples in docs/, test/scenarios/, and one E2E test to use per-event type assertions instead of the old flat event.Data.FieldName pattern. Fix staticcheck SA5011 lint warning in tools_test.go (nil check must precede type assertion).
1 parent 1e2a143 commit f5ed23e

File tree

39 files changed

+256
-166
lines changed

39 files changed

+256
-166
lines changed

docs/auth/byok.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func main() {
130130
panic(err)
131131
}
132132

133-
fmt.Println(*response.Data.Content)
133+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
134+
fmt.Println(d.Content)
135+
}
134136
}
135137
```
136138

docs/features/custom-agents.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -506,17 +506,17 @@ func main() {
506506
})
507507

508508
session.On(func(event copilot.SessionEvent) {
509-
switch event.Type {
510-
case "subagent.started":
511-
fmt.Printf("▶ Sub-agent started: %s\n", *event.Data.AgentDisplayName)
512-
fmt.Printf(" Description: %s\n", *event.Data.AgentDescription)
513-
fmt.Printf(" Tool call ID: %s\n", *event.Data.ToolCallID)
514-
case "subagent.completed":
515-
fmt.Printf("✅ Sub-agent completed: %s\n", *event.Data.AgentDisplayName)
516-
case "subagent.failed":
517-
fmt.Printf("❌ Sub-agent failed: %s%v\n", *event.Data.AgentDisplayName, event.Data.Error)
518-
case "subagent.selected":
519-
fmt.Printf("🎯 Agent selected: %s\n", *event.Data.AgentDisplayName)
509+
switch d := event.Data.(type) {
510+
case *copilot.SubagentStartedData:
511+
fmt.Printf("▶ Sub-agent started: %s\n", d.AgentDisplayName)
512+
fmt.Printf(" Description: %s\n", d.AgentDescription)
513+
fmt.Printf(" Tool call ID: %s\n", d.ToolCallID)
514+
case *copilot.SubagentCompletedData:
515+
fmt.Printf("✅ Sub-agent completed: %s\n", d.AgentDisplayName)
516+
case *copilot.SubagentFailedData:
517+
fmt.Printf("❌ Sub-agent failed: %s%v\n", d.AgentDisplayName, d.Error)
518+
case *copilot.SubagentSelectedData:
519+
fmt.Printf("🎯 Agent selected: %s\n", d.AgentDisplayName)
520520
}
521521
})
522522

@@ -530,17 +530,17 @@ func main() {
530530

531531
```go
532532
session.On(func(event copilot.SessionEvent) {
533-
switch event.Type {
534-
case "subagent.started":
535-
fmt.Printf("▶ Sub-agent started: %s\n", *event.Data.AgentDisplayName)
536-
fmt.Printf(" Description: %s\n", *event.Data.AgentDescription)
537-
fmt.Printf(" Tool call ID: %s\n", *event.Data.ToolCallID)
538-
case "subagent.completed":
539-
fmt.Printf("✅ Sub-agent completed: %s\n", *event.Data.AgentDisplayName)
540-
case "subagent.failed":
541-
fmt.Printf("❌ Sub-agent failed: %s%v\n", *event.Data.AgentDisplayName, event.Data.Error)
542-
case "subagent.selected":
543-
fmt.Printf("🎯 Agent selected: %s\n", *event.Data.AgentDisplayName)
533+
switch d := event.Data.(type) {
534+
case *copilot.SubagentStartedData:
535+
fmt.Printf("▶ Sub-agent started: %s\n", d.AgentDisplayName)
536+
fmt.Printf(" Description: %s\n", d.AgentDescription)
537+
fmt.Printf(" Tool call ID: %s\n", d.ToolCallID)
538+
case *copilot.SubagentCompletedData:
539+
fmt.Printf("✅ Sub-agent completed: %s\n", d.AgentDisplayName)
540+
case *copilot.SubagentFailedData:
541+
fmt.Printf("❌ Sub-agent failed: %s%v\n", d.AgentDisplayName, d.Error)
542+
case *copilot.SubagentSelectedData:
543+
fmt.Printf("🎯 Agent selected: %s\n", d.AgentDisplayName)
544544
}
545545
})
546546

docs/features/streaming-events.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ func main() {
137137
})
138138

139139
session.On(func(event copilot.SessionEvent) {
140-
if event.Type == "assistant.message_delta" {
141-
fmt.Print(*event.Data.DeltaContent)
140+
if d, ok := event.Data.(*copilot.AssistantMessageDeltaData); ok {
141+
fmt.Print(d.DeltaContent)
142142
}
143143
})
144144
_ = session
@@ -148,8 +148,8 @@ func main() {
148148

149149
```go
150150
session.On(func(event copilot.SessionEvent) {
151-
if event.Type == "assistant.message_delta" {
152-
fmt.Print(*event.Data.DeltaContent)
151+
if d, ok := event.Data.(*copilot.AssistantMessageDeltaData); ok {
152+
fmt.Print(d.DeltaContent)
153153
}
154154
})
155155
```

docs/getting-started.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ func main() {
211211
log.Fatal(err)
212212
}
213213

214-
fmt.Println(*response.Data.Content)
214+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
215+
fmt.Println(d.Content)
216+
}
215217
os.Exit(0)
216218
}
217219
```
@@ -406,10 +408,11 @@ func main() {
406408

407409
// Listen for response chunks
408410
session.On(func(event copilot.SessionEvent) {
409-
if event.Type == "assistant.message_delta" {
410-
fmt.Print(*event.Data.DeltaContent)
411-
}
412-
if event.Type == "session.idle" {
411+
switch d := event.Data.(type) {
412+
case *copilot.AssistantMessageDeltaData:
413+
fmt.Print(d.DeltaContent)
414+
case *copilot.SessionIdleData:
415+
_ = d
413416
fmt.Println()
414417
}
415418
})
@@ -604,10 +607,12 @@ func main() {
604607

605608
// Filter by event type in your handler
606609
session.On(func(event copilot.SessionEvent) {
607-
if event.Type == "session.idle" {
610+
switch d := event.Data.(type) {
611+
case *copilot.SessionIdleData:
612+
_ = d
608613
fmt.Println("Session is idle")
609-
} else if event.Type == "assistant.message" {
610-
fmt.Println("Message:", *event.Data.Content)
614+
case *copilot.AssistantMessageData:
615+
fmt.Println("Message:", d.Content)
611616
}
612617
})
613618

@@ -625,10 +630,12 @@ unsubscribe := session.On(func(event copilot.SessionEvent) {
625630

626631
// Filter by event type in your handler
627632
session.On(func(event copilot.SessionEvent) {
628-
if event.Type == "session.idle" {
633+
switch d := event.Data.(type) {
634+
case *copilot.SessionIdleData:
635+
_ = d
629636
fmt.Println("Session is idle")
630-
} else if event.Type == "assistant.message" {
631-
fmt.Println("Message:", *event.Data.Content)
637+
case *copilot.AssistantMessageData:
638+
fmt.Println("Message:", d.Content)
632639
}
633640
})
634641

@@ -897,10 +904,11 @@ func main() {
897904
}
898905

899906
session.On(func(event copilot.SessionEvent) {
900-
if event.Type == "assistant.message_delta" {
901-
fmt.Print(*event.Data.DeltaContent)
902-
}
903-
if event.Type == "session.idle" {
907+
switch d := event.Data.(type) {
908+
case *copilot.AssistantMessageDeltaData:
909+
fmt.Print(d.DeltaContent)
910+
case *copilot.SessionIdleData:
911+
_ = d
904912
fmt.Println()
905913
}
906914
})
@@ -1251,12 +1259,11 @@ func main() {
12511259
}
12521260

12531261
session.On(func(event copilot.SessionEvent) {
1254-
if event.Type == "assistant.message_delta" {
1255-
if event.Data.DeltaContent != nil {
1256-
fmt.Print(*event.Data.DeltaContent)
1257-
}
1258-
}
1259-
if event.Type == "session.idle" {
1262+
switch d := event.Data.(type) {
1263+
case *copilot.AssistantMessageDeltaData:
1264+
fmt.Print(d.DeltaContent)
1265+
case *copilot.SessionIdleData:
1266+
_ = d
12601267
fmt.Println()
12611268
}
12621269
})

docs/setup/bundled-cli.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func main() {
130130

131131
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-4.1"})
132132
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
133-
fmt.Println(*response.Data.Content)
133+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
134+
fmt.Println(d.Content)
135+
}
134136
}
135137
```
136138
<!-- /docs-validate: hidden -->
@@ -146,7 +148,9 @@ defer client.Stop()
146148

147149
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-4.1"})
148150
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
149-
fmt.Println(*response.Data.Content)
151+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
152+
fmt.Println(d.Content)
153+
}
150154
```
151155

152156
</details>

docs/setup/local-cli.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ func main() {
9191

9292
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-4.1"})
9393
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
94-
fmt.Println(*response.Data.Content)
94+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
95+
fmt.Println(d.Content)
96+
}
9597
}
9698
```
9799
<!-- /docs-validate: hidden -->
@@ -105,7 +107,9 @@ defer client.Stop()
105107

106108
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{Model: "gpt-4.1"})
107109
response, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
108-
fmt.Println(*response.Data.Content)
110+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
111+
fmt.Println(d.Content)
112+
}
109113
```
110114

111115
</details>

go/internal/e2e/session_fs_test.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ func TestSessionFs(t *testing.T) {
4848
t.Fatalf("Failed to send message: %v", err)
4949
}
5050
content := ""
51-
if msg != nil && msg.Data.Content != nil {
52-
content = *msg.Data.Content
51+
if msg != nil {
52+
if d, ok := msg.Data.(*copilot.AssistantMessageData); ok {
53+
content = d.Content
54+
}
5355
}
5456
if !strings.Contains(content, "300") {
5557
t.Fatalf("Expected response to contain 300, got %q", content)
@@ -84,8 +86,10 @@ func TestSessionFs(t *testing.T) {
8486
t.Fatalf("Failed to send first message: %v", err)
8587
}
8688
content := ""
87-
if msg != nil && msg.Data.Content != nil {
88-
content = *msg.Data.Content
89+
if msg != nil {
90+
if d, ok := msg.Data.(*copilot.AssistantMessageData); ok {
91+
content = d.Content
92+
}
8993
}
9094
if !strings.Contains(content, "100") {
9195
t.Fatalf("Expected response to contain 100, got %q", content)
@@ -111,8 +115,10 @@ func TestSessionFs(t *testing.T) {
111115
t.Fatalf("Failed to send second message: %v", err)
112116
}
113117
content2 := ""
114-
if msg2 != nil && msg2.Data.Content != nil {
115-
content2 = *msg2.Data.Content
118+
if msg2 != nil {
119+
if d, ok := msg2.Data.(*copilot.AssistantMessageData); ok {
120+
content2 = d.Content
121+
}
116122
}
117123
if !strings.Contains(content2, "300") {
118124
t.Fatalf("Expected response to contain 300, got %q", content2)
@@ -396,24 +402,20 @@ func providerPath(root string, sessionID string, path string) string {
396402

397403
func findToolCallResult(messages []copilot.SessionEvent, toolName string) string {
398404
for _, message := range messages {
399-
if message.Type == "tool.execution_complete" &&
400-
message.Data.Result != nil &&
401-
message.Data.Result.Content != nil &&
402-
message.Data.ToolCallID != nil &&
403-
findToolName(messages, *message.Data.ToolCallID) == toolName {
404-
return *message.Data.Result.Content
405+
if d, ok := message.Data.(*copilot.ToolExecutionCompleteData); ok &&
406+
d.Result != nil &&
407+
findToolName(messages, d.ToolCallID) == toolName {
408+
return d.Result.Content
405409
}
406410
}
407411
return ""
408412
}
409413

410414
func findToolName(messages []copilot.SessionEvent, toolCallID string) string {
411415
for _, message := range messages {
412-
if message.Type == "tool.execution_start" &&
413-
message.Data.ToolCallID != nil &&
414-
*message.Data.ToolCallID == toolCallID &&
415-
message.Data.ToolName != nil {
416-
return *message.Data.ToolName
416+
if d, ok := message.Data.(*copilot.ToolExecutionStartData); ok &&
417+
d.ToolCallID == toolCallID {
418+
return d.ToolName
417419
}
418420
}
419421
return ""

go/internal/e2e/tools_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,11 @@ func TestTools(t *testing.T) {
232232
t.Fatalf("Failed to get assistant message: %v", err)
233233
}
234234

235+
if answer == nil {
236+
t.Fatalf("Expected assistant message with content")
237+
}
235238
ad, ok := answer.Data.(*copilot.AssistantMessageData)
236-
if answer == nil || !ok {
239+
if !ok {
237240
t.Fatalf("Expected assistant message with content")
238241
}
239242

test/scenarios/auth/byok-anthropic/go/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func main() {
5858
log.Fatal(err)
5959
}
6060

61-
if response != nil && response.Data.Content != nil {
62-
fmt.Println(*response.Data.Content)
63-
}
61+
if response != nil {
62+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
63+
fmt.Println(d.Content)
64+
}
65+
}
6466
}

test/scenarios/auth/byok-azure/go/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ func main() {
6262
log.Fatal(err)
6363
}
6464

65-
if response != nil && response.Data.Content != nil {
66-
fmt.Println(*response.Data.Content)
67-
}
65+
if response != nil {
66+
if d, ok := response.Data.(*copilot.AssistantMessageData); ok {
67+
fmt.Println(d.Content)
68+
}
69+
}
6870
}

0 commit comments

Comments
 (0)