Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions cookbook/copilot-sdk/go.sum

This file was deleted.

41 changes: 17 additions & 24 deletions cookbook/copilot-sdk/go/accessibility-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ func main() {
}
defer client.Stop()

streaming := true
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "claude-opus-4.6",
Streaming: &streaming,
McpServers: map[string]interface{}{
"playwright": map[string]interface{}{
Streaming: true,
MCPServers: map[string]copilot.MCPServerConfig{
"playwright": {
"type": "local",
"command": "npx",
"args": []string{"@playwright/mcp@latest"},
Comment thread
caarlos0 marked this conversation as resolved.
Expand All @@ -92,26 +91,22 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer session.Destroy()
defer session.Disconnect()

// Set up streaming event handling
done := make(chan struct{}, 1)

session.On(func(event copilot.SessionEvent) {
switch event.Type {
case "assistant.message.delta":
if event.Data.DeltaContent != nil {
fmt.Print(*event.Data.DeltaContent)
}
case "session.idle":
switch d := event.Data.(type) {
case *copilot.AssistantMessageDeltaData:
fmt.Print(d.DeltaContent)
case *copilot.SessionIdleData:
select {
case done <- struct{}{}:
default:
}
case "session.error":
if event.Data.Message != nil {
fmt.Printf("\nError: %s\n", *event.Data.Message)
}
case *copilot.SessionErrorData:
fmt.Printf("\nError: %s\n", d.Message)
select {
case done <- struct{}{}:
default:
Expand Down Expand Up @@ -202,7 +197,7 @@ func main() {
## How it works

1. **Playwright MCP server**: Configures a local MCP server running `@playwright/mcp` to provide browser automation tools
2. **Streaming output**: Uses `Streaming: &streaming` and `assistant.message.delta` events for real-time token-by-token output
2. **Streaming output**: Uses `Streaming: true` and `AssistantMessageDeltaData` events for real-time token-by-token output
3. **Accessibility snapshot**: Playwright's `browser_snapshot` tool captures the full accessibility tree of the page
4. **Structured report**: The prompt engineers a consistent WCAG-aligned report format with emoji severity indicators
5. **Test generation**: Optionally detects the project language and generates Playwright accessibility tests
Expand All @@ -216,8 +211,8 @@ The recipe configures a local MCP server that runs alongside the session:
```go
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
McpServers: map[string]interface{}{
"playwright": map[string]interface{}{
MCPServers: map[string]copilot.MCPServerConfig{
"playwright": {
"type": "local",
"command": "npx",
"args": []string{"@playwright/mcp@latest"},
Expand All @@ -235,12 +230,10 @@ Unlike `SendAndWait`, this recipe uses streaming for real-time output:

```go
session.On(func(event copilot.SessionEvent) {
switch event.Type {
case "assistant.message.delta":
if event.Data.DeltaContent != nil {
fmt.Print(*event.Data.DeltaContent)
}
case "session.idle":
switch d := event.Data.(type) {
case *copilot.AssistantMessageDeltaData:
fmt.Print(d.DeltaContent)
case *copilot.SessionIdleData:
done <- struct{}{}
}
})
Expand Down
14 changes: 8 additions & 6 deletions cookbook/copilot-sdk/go/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,23 @@ func main() {

session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5",
Model: "gpt-5.4",
})
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
defer session.Destroy()
defer session.Disconnect()

result, err := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Hello!"})
if err != nil {
log.Printf("Failed to send message: %v", err)
return
}

if result != nil && result.Data.Content != nil {
fmt.Println(*result.Data.Content)
if result != nil {
if d, ok := result.Data.(*copilot.AssistantMessageData); ok {
fmt.Println(d.Content)
}
}
}
```
Expand Down Expand Up @@ -180,12 +182,12 @@ func doWork() error {

session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5",
Model: "gpt-5.4",
})
if err != nil {
return fmt.Errorf("failed to create session: %w", err)
}
defer session.Destroy()
defer session.Disconnect()

// ... do work ...

Expand Down
24 changes: 9 additions & 15 deletions cookbook/copilot-sdk/go/managing-local-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,22 @@ func main() {
// Create session
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5",
Model: "gpt-5.4",
})
if err != nil {
log.Fatal(err)
}
defer session.Destroy()
defer session.Disconnect()

// Event handler
session.On(func(event copilot.SessionEvent) {
switch event.Type {
case "assistant.message":
if event.Data.Content != nil {
fmt.Printf("\nCopilot: %s\n", *event.Data.Content)
}
case "tool.execution_start":
if event.Data.ToolName != nil {
fmt.Printf(" → Running: %s\n", *event.Data.ToolName)
}
case "tool.execution_complete":
if event.Data.ToolName != nil {
fmt.Printf(" ✓ Completed: %s\n", *event.Data.ToolName)
}
switch d := event.Data.(type) {
case *copilot.AssistantMessageData:
fmt.Printf("\nCopilot: %s\n", d.Content)
case *copilot.ToolExecutionStartData:
fmt.Printf(" → Running: %s\n", d.ToolName)
case *copilot.ToolExecutionCompleteData:
fmt.Printf(" ✓ Completed (success=%v)\n", d.Success)
}
})

Expand Down
16 changes: 8 additions & 8 deletions cookbook/copilot-sdk/go/multiple-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,30 @@ func main() {
// Create multiple independent sessions
session1, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5",
Model: "gpt-5.4",
})
if err != nil {
log.Fatal(err)
}
defer session1.Destroy()
defer session1.Disconnect()

session2, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5",
Model: "gpt-5.4",
})
if err != nil {
log.Fatal(err)
}
defer session2.Destroy()
defer session2.Disconnect()

session3, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "claude-sonnet-4.5",
Model: "claude-sonnet-4.6",
})
if err != nil {
log.Fatal(err)
}
defer session3.Destroy()
defer session3.Disconnect()

// Each session maintains its own conversation history
session1.Send(ctx, copilot.MessageOptions{Prompt: "You are helping with a Python project"})
Expand All @@ -81,7 +81,7 @@ Use custom IDs for easier tracking:
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
SessionID: "user-123-chat",
Model: "gpt-5",
Model: "gpt-5.4",
})
if err != nil {
log.Fatal(err)
Expand All @@ -93,7 +93,7 @@ fmt.Println(session.SessionID) // "user-123-chat"
## Listing sessions

```go
sessions, err := client.ListSessions(ctx)
sessions, err := client.ListSessions(ctx, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 7 additions & 7 deletions cookbook/copilot-sdk/go/persisting-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ func main() {
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
SessionID: "user-123-conversation",
Model: "gpt-5",
Model: "gpt-5.4",
})

session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "Let's discuss TypeScript generics"})

// Session ID is preserved
fmt.Println(session.SessionID)

// Destroy session but keep data on disk
session.Destroy()
// Disconnect session but keep data on disk
session.Disconnect()
}
```

Expand All @@ -61,13 +61,13 @@ session, _ := client.ResumeSession(ctx, "user-123-conversation", &copilot.Resume
// Previous context is restored
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: "What were we discussing?"})

session.Destroy()
session.Disconnect()
```

### Listing available sessions

```go
sessions, _ := client.ListSessions(ctx)
sessions, _ := client.ListSessions(ctx, nil)
for _, s := range sessions {
fmt.Println("Session:", s.SessionID)
}
Expand All @@ -85,8 +85,8 @@ client.DeleteSession(ctx, "user-123-conversation")
```go
messages, _ := session.GetMessages(ctx)
for _, msg := range messages {
if msg.Data.Content != nil {
fmt.Printf("[%s] %s\n", msg.Type, *msg.Data.Content)
if d, ok := msg.Data.(*copilot.AssistantMessageData); ok {
fmt.Printf("[assistant.message] %s\n", d.Content)
}
}
```
Expand Down
18 changes: 7 additions & 11 deletions cookbook/copilot-sdk/go/pr-visualization.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func main() {
cwd, _ := os.Getwd()
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5",
Model: "gpt-5.4",
SystemMessage: &copilot.SystemMessageConfig{
Content: fmt.Sprintf(`
<context>
Expand All @@ -159,19 +159,15 @@ The current working directory is: %s
if err != nil {
log.Fatal(err)
}
defer session.Destroy()
defer session.Disconnect()

// Set up event handling
session.On(func(event copilot.SessionEvent) {
switch event.Type {
case "assistant.message":
if event.Data.Content != nil {
fmt.Printf("\n🤖 %s\n\n", *event.Data.Content)
}
case "tool.execution_start":
if event.Data.ToolName != nil {
fmt.Printf(" ⚙️ %s\n", *event.Data.ToolName)
}
switch d := event.Data.(type) {
case *copilot.AssistantMessageData:
fmt.Printf("\n🤖 %s\n\n", d.Content)
case *copilot.ToolExecutionStartData:
fmt.Printf(" ⚙️ %s\n", d.ToolName)
}
})

Expand Down
18 changes: 9 additions & 9 deletions cookbook/copilot-sdk/go/ralph-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ralphLoop(ctx context.Context, promptFile string, maxIterations int) error
// Fresh session each iteration — context isolation is the point
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
Model: "gpt-5.1-codex-mini",
Model: "gpt-5.3-codex",
})
if err != nil {
return err
Expand All @@ -80,7 +80,7 @@ func ralphLoop(ctx context.Context, promptFile string, maxIterations int) error
_, err = session.SendAndWait(ctx, copilot.MessageOptions{
Prompt: string(prompt),
})
session.Destroy()
session.Disconnect()
if err != nil {
return err
}
Expand Down Expand Up @@ -146,27 +146,27 @@ func ralphLoop(ctx context.Context, mode string, maxIterations int) error {
fmt.Printf("\n=== Iteration %d/%d ===\n", i, maxIterations)

session, err := client.CreateSession(ctx, &copilot.SessionConfig{
Model: "gpt-5.1-codex-mini",
Model: "gpt-5.3-codex",
WorkingDirectory: cwd,
OnPermissionRequest: func(_ copilot.PermissionRequest, _ map[string]string) copilot.PermissionRequestResult {
return copilot.PermissionRequestResult{Kind: "approved"}
OnPermissionRequest: func(_ copilot.PermissionRequest, _ copilot.PermissionInvocation) (copilot.PermissionRequestResult, error) {
return copilot.PermissionRequestResult{Kind: "approved"}, nil
},
})
if err != nil {
return err
}

// Log tool usage for visibility
session.On(func(event copilot.Event) {
if toolExecution, ok := event.(copilot.ToolExecutionStartEvent); ok {
fmt.Printf(" ⚙ %s\n", toolExecution.Data.ToolName)
session.On(func(event copilot.SessionEvent) {
if d, ok := event.Data.(*copilot.ToolExecutionStartData); ok {
fmt.Printf(" ⚙ %s\n", d.ToolName)
}
})

_, err = session.SendAndWait(ctx, copilot.MessageOptions{
Prompt: string(prompt),
})
session.Destroy()
session.Disconnect()
if err != nil {
return err
}
Expand Down
Loading
Loading