Skip to content

Commit 4730cd1

Browse files
Add minimal chat samples for all SDK languages
- Add samples/chat.ts for Node.js/TypeScript - Add samples/chat.py for Python - Add samples/chat.go for Go - Add samples/Chat.cs for .NET - Update each SDK README with instructions to run the sample Each sample is a minimal (~30-50 lines) interactive chat loop that: - Prompts user for input - Sends to Copilot and waits for idle - Streams response deltas to console Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3c5368a commit 4730cd1

File tree

11 files changed

+212
-0
lines changed

11 files changed

+212
-0
lines changed

dotnet/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ SDK for programmatic control of GitHub Copilot CLI.
1010
dotnet add package GitHub.Copilot.SDK
1111
```
1212

13+
## Run the Sample
14+
15+
Try the interactive chat sample (from the repo root):
16+
17+
```bash
18+
cd dotnet/samples
19+
dotnet run
20+
```
21+
1322
## Quick Start
1423

1524
```csharp

dotnet/samples/Chat.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using GitHub.Copilot.SDK;
2+
3+
await using var client = new CopilotClient();
4+
await using var session = await client.CreateSessionAsync();
5+
6+
using var _ = session.On(evt =>
7+
{
8+
if (evt is AssistantMessageEvent msg)
9+
Console.WriteLine($"\nAssistant: {msg.Data?.Content}");
10+
else if (evt is AssistantMessageDeltaEvent delta)
11+
Console.Write(delta.Data?.DeltaContent);
12+
});
13+
14+
Console.WriteLine("Chat with Copilot (Ctrl+C to exit)\n");
15+
16+
while (true)
17+
{
18+
Console.Write("You: ");
19+
var input = Console.ReadLine()?.Trim();
20+
if (string.IsNullOrEmpty(input)) continue;
21+
22+
Console.Write("Assistant: ");
23+
await session.SendAndWaitAsync(new MessageOptions { Prompt = input });
24+
Console.WriteLine();
25+
}

dotnet/samples/Chat.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="..\src\GitHub.Copilot.SDK.csproj" />
10+
</ItemGroup>
11+
</Project>

go/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ A Go SDK for programmatic access to the GitHub Copilot CLI.
1010
go get github.com/github/copilot-sdk/go
1111
```
1212

13+
## Run the Sample
14+
15+
Try the interactive chat sample (from the repo root):
16+
17+
```bash
18+
cd go/samples
19+
go run chat.go
20+
```
21+
1322
## Quick Start
1423

1524
```go

go/samples/chat.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"fmt"
7+
"os"
8+
"strings"
9+
10+
"github.com/github/copilot-sdk/go"
11+
)
12+
13+
func main() {
14+
ctx := context.Background()
15+
client := copilot.NewClient(nil)
16+
if err := client.Start(ctx); err != nil {
17+
panic(err)
18+
}
19+
defer client.Stop()
20+
21+
session, err := client.CreateSession(ctx, nil)
22+
if err != nil {
23+
panic(err)
24+
}
25+
defer session.Destroy()
26+
27+
session.On(func(event copilot.SessionEvent) {
28+
if event.Type == copilot.AssistantMessage && event.Data.Content != nil {
29+
fmt.Printf("\nAssistant: %s\n", *event.Data.Content)
30+
} else if event.Type == copilot.AssistantMessageDelta && event.Data.DeltaContent != nil {
31+
fmt.Print(*event.Data.DeltaContent)
32+
}
33+
})
34+
35+
fmt.Println("Chat with Copilot (Ctrl+C to exit)\n")
36+
scanner := bufio.NewScanner(os.Stdin)
37+
38+
for {
39+
fmt.Print("You: ")
40+
if !scanner.Scan() {
41+
break
42+
}
43+
input := strings.TrimSpace(scanner.Text())
44+
if input == "" {
45+
continue
46+
}
47+
48+
fmt.Print("Assistant: ")
49+
session.SendAndWait(ctx, copilot.MessageOptions{Prompt: input})
50+
fmt.Println()
51+
}
52+
}

go/samples/go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/github/copilot-sdk/go/samples
2+
3+
go 1.24
4+
5+
require github.com/github/copilot-sdk/go v0.0.0
6+
7+
require github.com/google/jsonschema-go v0.4.2 // indirect
8+
9+
replace github.com/github/copilot-sdk/go => ../

nodejs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ TypeScript SDK for programmatic control of GitHub Copilot CLI via JSON-RPC.
1010
npm install @github/copilot-sdk
1111
```
1212

13+
## Run the Sample
14+
15+
Try the interactive chat sample (from the repo root):
16+
17+
```bash
18+
cd nodejs/samples
19+
npm install
20+
npm start
21+
```
22+
1323
## Quick Start
1424

1525
```typescript

nodejs/samples/chat.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as readline from "node:readline";
2+
import { CopilotClient, type SessionEvent } from "@github/copilot-sdk";
3+
4+
async function main() {
5+
const client = new CopilotClient();
6+
const session = await client.createSession();
7+
8+
session.on((event: SessionEvent) => {
9+
if (event.type === "assistant.message") {
10+
console.log(`\nAssistant: ${event.data?.content}`);
11+
} else if (event.type === "assistant.message_delta") {
12+
process.stdout.write(event.data?.delta ?? "");
13+
}
14+
});
15+
16+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
17+
const prompt = (q: string) => new Promise<string>((r) => rl.question(q, r));
18+
19+
console.log("Chat with Copilot (Ctrl+C to exit)\n");
20+
21+
while (true) {
22+
const input = await prompt("You: ");
23+
if (!input.trim()) continue;
24+
25+
process.stdout.write("Assistant: ");
26+
await session.sendAndWait({ prompt: input });
27+
console.log();
28+
}
29+
}
30+
31+
main().catch(console.error);

nodejs/samples/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "copilot-sdk-sample",
3+
"type": "module",
4+
"scripts": {
5+
"start": "npx tsx chat.ts"
6+
},
7+
"dependencies": {
8+
"@github/copilot-sdk": "file:.."
9+
},
10+
"devDependencies": {
11+
"tsx": "^4.20.6",
12+
"@types/node": "^22.0.0"
13+
}
14+
}

python/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ pip install -e ".[dev]"
1212
uv pip install -e ".[dev]"
1313
```
1414

15+
## Run the Sample
16+
17+
Try the interactive chat sample (from the repo root):
18+
19+
```bash
20+
cd python/samples
21+
python chat.py
22+
```
23+
1524
## Quick Start
1625

1726
```python

0 commit comments

Comments
 (0)