Skip to content

Commit 7feee42

Browse files
authored
Merge pull request #632 from github/copilot-sdk-cookbook
Migrating SDK cookbook over
2 parents 44a43b2 + 18cde40 commit 7feee42

55 files changed

Lines changed: 5440 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 🤖 Awesome GitHub Copilot Customizations
1+
# 🤖 Awesome GitHub Copilot
22
[![Powered by Awesome Copilot](https://img.shields.io/badge/Powered_by-Awesome_Copilot-blue?logo=githubcopilot)](https://aka.ms/awesome-github-copilot) [![GitHub contributors from allcontributors.org](https://img.shields.io/github/all-contributors/github/awesome-copilot?color=ee8449)](#contributors-)
33

44

@@ -13,6 +13,7 @@ This repository provides a comprehensive toolkit for enhancing GitHub Copilot wi
1313
- **👉 [Awesome Instructions](docs/README.instructions.md)** - Comprehensive coding standards and best practices that apply to specific file patterns or entire projects
1414
- **👉 [Awesome Skills](docs/README.skills.md)** - Self-contained folders with instructions and bundled resources that enhance AI capabilities for specialized tasks
1515
- **👉 [Awesome Collections](docs/README.collections.md)** - Curated collections of related prompts, instructions, agents, and skills organized around specific themes and workflows
16+
- **👉 [Awesome Cookbook Recipes](cookbook/README.md)** - Practical, copy-paste-ready code snippets and real-world examples for working with GitHub Copilot tools and features
1617

1718
## 🌟 Featured Collections
1819

cookbook/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# GitHub Copilot Cookbook
2+
3+
A collection of practical recipes and examples for working with GitHub Copilot tools and features. Each recipe provides focused, copy-paste-ready code snippets and real-world examples to help you accomplish common tasks.
4+
5+
## What's in the Cookbook
6+
7+
The cookbook is organized by tool or product, with recipes collected by language where applicable:
8+
9+
### GitHub Copilot SDK
10+
11+
Ready-to-use recipes for building with the GitHub Copilot SDK across multiple languages.
12+
13+
- **[Copilot SDK Cookbook](copilot-sdk/)** - Recipes for .NET, Go, Node.js, and Python
14+
- Error handling, session management, file operations, and more
15+
- Runnable examples for each language
16+
- Best practices and complete implementation guides
17+
18+
## Getting Started
19+
20+
1. Browse the tool or product folder that matches what you want to build
21+
2. Find the recipe that solves your use case
22+
3. Copy the code snippet or check the `recipe/` subfolder for complete, runnable examples
23+
4. Refer to the language-specific documentation for setup and execution instructions
24+
25+
## Planned Expansions
26+
27+
The cookbook is designed to grow alongside the GitHub Copilot ecosystem. Future additions may include recipes for:
28+
29+
- Additional Copilot tools and integrations
30+
- Advanced patterns and workflows
31+
- Integration with external services and APIs
32+
- Language-specific optimizations and best practices
33+
34+
## Contributing
35+
36+
Have a recipe to share? We'd love to include it! See [CONTRIBUTING.md](../CONTRIBUTING.md) for guidelines on submitting new recipes.
37+
38+
## Resources
39+
40+
### Official Documentation
41+
42+
- [GitHub Copilot Documentation](https://docs.github.com/copilot)
43+
- [GitHub Copilot SDK](https://github.com/github/copilot-sdk)
44+
45+
### External Cookbooks
46+
47+
- [Microsoft Copilot Adventures](https://github.com/microsoft/CopilotAdventures) - Interactive adventures and tutorials for learning GitHub Copilot
48+
- [GitHub Copilot Chat Cookbook](https://docs.github.com/en/copilot/tutorials/copilot-chat-cookbook) - Official cookbook with Copilot Chat examples and techniques
49+
50+
### Other
51+
52+
- [Main Repository](../)

cookbook/copilot-sdk/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# GitHub Copilot SDK Cookbook
2+
3+
This cookbook collects small, focused recipes showing how to accomplish common tasks with the GitHub Copilot SDK across languages. Each recipe is intentionally short and practical, with copy‑pasteable snippets and pointers to fuller examples and tests.
4+
5+
## Recipes by Language
6+
7+
### .NET (C#)
8+
9+
- [Error Handling](dotnet/error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
10+
- [Multiple Sessions](dotnet/multiple-sessions.md): Manage multiple independent conversations simultaneously.
11+
- [Managing Local Files](dotnet/managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
12+
- [PR Visualization](dotnet/pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
13+
- [Persisting Sessions](dotnet/persisting-sessions.md): Save and resume sessions across restarts.
14+
15+
### Node.js / TypeScript
16+
17+
- [Error Handling](nodejs/error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
18+
- [Multiple Sessions](nodejs/multiple-sessions.md): Manage multiple independent conversations simultaneously.
19+
- [Managing Local Files](nodejs/managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
20+
- [PR Visualization](nodejs/pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
21+
- [Persisting Sessions](nodejs/persisting-sessions.md): Save and resume sessions across restarts.
22+
23+
### Python
24+
25+
- [Error Handling](python/error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
26+
- [Multiple Sessions](python/multiple-sessions.md): Manage multiple independent conversations simultaneously.
27+
- [Managing Local Files](python/managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
28+
- [PR Visualization](python/pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
29+
- [Persisting Sessions](python/persisting-sessions.md): Save and resume sessions across restarts.
30+
31+
### Go
32+
33+
- [Error Handling](go/error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
34+
- [Multiple Sessions](go/multiple-sessions.md): Manage multiple independent conversations simultaneously.
35+
- [Managing Local Files](go/managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
36+
- [PR Visualization](go/pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
37+
- [Persisting Sessions](go/persisting-sessions.md): Save and resume sessions across restarts.
38+
39+
## How to Use
40+
41+
- Browse your language section above and open the recipe links
42+
- Each recipe includes runnable examples in a `recipe/` subfolder with language-specific tooling
43+
- See existing examples and tests for working references:
44+
- Node.js examples: `nodejs/examples/basic-example.ts`
45+
- E2E tests: `go/e2e`, `python/e2e`, `nodejs/test/e2e`, `dotnet/test/Harness`
46+
47+
## Running Examples
48+
49+
### .NET
50+
51+
```bash
52+
cd dotnet/cookbook/recipe
53+
dotnet run <filename>.cs
54+
```
55+
56+
### Node.js
57+
58+
```bash
59+
cd nodejs/cookbook/recipe
60+
npm install
61+
npx tsx <filename>.ts
62+
```
63+
64+
### Python
65+
66+
```bash
67+
cd python/cookbook/recipe
68+
pip install -r requirements.txt
69+
python <filename>.py
70+
```
71+
72+
### Go
73+
74+
```bash
75+
cd go/cookbook/recipe
76+
go run <filename>.go
77+
```
78+
79+
## Contributing
80+
81+
- Propose or add a new recipe by creating a markdown file in your language's `cookbook/` folder and a runnable example in `recipe/`
82+
- Follow repository guidance in [CONTRIBUTING.md](../../CONTRIBUTING.md)
83+
84+
## Status
85+
86+
Cookbook structure is complete with 4 recipes across all 4 supported languages. Each recipe includes both markdown documentation and runnable examples.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# GitHub Copilot SDK Cookbook — .NET (C#)
2+
3+
This folder hosts short, practical recipes for using the GitHub Copilot SDK with .NET. Each recipe is concise, copy‑pasteable, and points to fuller examples and tests.
4+
5+
## Recipes
6+
7+
- [Error Handling](error-handling.md): Handle errors gracefully including connection failures, timeouts, and cleanup.
8+
- [Multiple Sessions](multiple-sessions.md): Manage multiple independent conversations simultaneously.
9+
- [Managing Local Files](managing-local-files.md): Organize files by metadata using AI-powered grouping strategies.
10+
- [PR Visualization](pr-visualization.md): Generate interactive PR age charts using GitHub MCP Server.
11+
- [Persisting Sessions](persisting-sessions.md): Save and resume sessions across restarts.
12+
13+
## Contributing
14+
15+
Add a new recipe by creating a markdown file in this folder and linking it above. Follow repository guidance in [CONTRIBUTING.md](../../../CONTRIBUTING.md).
16+
17+
## Status
18+
19+
These recipes are now complete and ready to use; the cookbook will continue to evolve as new scenarios are added.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Error Handling Patterns
2+
3+
Handle errors gracefully in your Copilot SDK applications.
4+
5+
> **Runnable example:** [recipe/error-handling.cs](recipe/error-handling.cs)
6+
>
7+
> ```bash
8+
> dotnet run recipe/error-handling.cs
9+
> ```
10+
11+
## Example scenario
12+
13+
You need to handle various error conditions like connection failures, timeouts, and invalid responses.
14+
15+
## Basic try-catch
16+
17+
```csharp
18+
using GitHub.Copilot.SDK;
19+
20+
var client = new CopilotClient();
21+
22+
try
23+
{
24+
await client.StartAsync();
25+
var session = await client.CreateSessionAsync(new SessionConfig
26+
{
27+
Model = "gpt-5"
28+
});
29+
30+
var done = new TaskCompletionSource<string>();
31+
session.On(evt =>
32+
{
33+
if (evt is AssistantMessageEvent msg)
34+
{
35+
done.SetResult(msg.Data.Content);
36+
}
37+
});
38+
39+
await session.SendAsync(new MessageOptions { Prompt = "Hello!" });
40+
var response = await done.Task;
41+
Console.WriteLine(response);
42+
43+
await session.DisposeAsync();
44+
}
45+
catch (Exception ex)
46+
{
47+
Console.WriteLine($"Error: {ex.Message}");
48+
}
49+
finally
50+
{
51+
await client.StopAsync();
52+
}
53+
```
54+
55+
## Handling specific error types
56+
57+
```csharp
58+
try
59+
{
60+
await client.StartAsync();
61+
}
62+
catch (FileNotFoundException)
63+
{
64+
Console.WriteLine("Copilot CLI not found. Please install it first.");
65+
}
66+
catch (HttpRequestException ex) when (ex.Message.Contains("connection"))
67+
{
68+
Console.WriteLine("Could not connect to Copilot CLI server.");
69+
}
70+
catch (Exception ex)
71+
{
72+
Console.WriteLine($"Unexpected error: {ex.Message}");
73+
}
74+
```
75+
76+
## Timeout handling
77+
78+
```csharp
79+
var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });
80+
81+
try
82+
{
83+
var done = new TaskCompletionSource<string>();
84+
session.On(evt =>
85+
{
86+
if (evt is AssistantMessageEvent msg)
87+
{
88+
done.SetResult(msg.Data.Content);
89+
}
90+
});
91+
92+
await session.SendAsync(new MessageOptions { Prompt = "Complex question..." });
93+
94+
// Wait with timeout (30 seconds)
95+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
96+
var response = await done.Task.WaitAsync(cts.Token);
97+
98+
Console.WriteLine(response);
99+
}
100+
catch (OperationCanceledException)
101+
{
102+
Console.WriteLine("Request timed out");
103+
}
104+
```
105+
106+
## Aborting a request
107+
108+
```csharp
109+
var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });
110+
111+
// Start a request
112+
await session.SendAsync(new MessageOptions { Prompt = "Write a very long story..." });
113+
114+
// Abort it after some condition
115+
await Task.Delay(5000);
116+
await session.AbortAsync();
117+
Console.WriteLine("Request aborted");
118+
```
119+
120+
## Graceful shutdown
121+
122+
```csharp
123+
Console.CancelKeyPress += async (sender, e) =>
124+
{
125+
e.Cancel = true;
126+
Console.WriteLine("Shutting down...");
127+
128+
var errors = await client.StopAsync();
129+
if (errors.Count > 0)
130+
{
131+
Console.WriteLine($"Cleanup errors: {string.Join(", ", errors)}");
132+
}
133+
134+
Environment.Exit(0);
135+
};
136+
```
137+
138+
## Using await using for automatic disposal
139+
140+
```csharp
141+
await using var client = new CopilotClient();
142+
await client.StartAsync();
143+
144+
var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });
145+
146+
// ... do work ...
147+
148+
// client.StopAsync() is automatically called when exiting scope
149+
```
150+
151+
## Best practices
152+
153+
1. **Always clean up**: Use try-finally or `await using` to ensure `StopAsync()` is called
154+
2. **Handle connection errors**: The CLI might not be installed or running
155+
3. **Set appropriate timeouts**: Use `CancellationToken` for long-running requests
156+
4. **Log errors**: Capture error details for debugging

0 commit comments

Comments
 (0)