Skip to content

Commit ee9df36

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 ee9df36

File tree

13 files changed

+251
-0
lines changed

13 files changed

+251
-0
lines changed

dotnet/GitHub.Copilot.SDK.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<Folder Name="/test/">
1111
<Project Path="test/GitHub.Copilot.SDK.Test.csproj" />
1212
</Folder>
13+
<Folder Name="/samples/">
14+
<Project Path="samples/Chat.csproj" />
15+
</Folder>
1316
</Solution>

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
Console.ForegroundColor = ConsoleColor.Blue;
9+
var output = evt switch
10+
{
11+
AssistantReasoningEvent reasoning => $"[reasoning: {reasoning.Data.Content}]",
12+
ToolExecutionStartEvent toolStart => $"[tool: {toolStart.Data.ToolName} {toolStart.Data.Arguments}]",
13+
_ => null
14+
};
15+
if (output != null) Console.WriteLine(output);
16+
Console.ResetColor();
17+
});
18+
19+
Console.WriteLine("Chat with Copilot (Ctrl+C to exit)\n");
20+
21+
while (true)
22+
{
23+
Console.Write("You: ");
24+
var input = Console.ReadLine()?.Trim();
25+
if (string.IsNullOrEmpty(input)) continue;
26+
Console.WriteLine();
27+
28+
var reply = await session.SendAndWaitAsync(new MessageOptions { Prompt = input });
29+
Console.WriteLine($"\nAssistant: {reply?.Data.Content}\n");
30+
}

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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"os"
9+
"strings"
10+
11+
"github.com/github/copilot-sdk/go"
12+
)
13+
14+
const blue = "\033[34m"
15+
const reset = "\033[0m"
16+
17+
func main() {
18+
ctx := context.Background()
19+
client := copilot.NewClient(nil)
20+
if err := client.Start(ctx); err != nil {
21+
panic(err)
22+
}
23+
defer client.Stop()
24+
25+
session, err := client.CreateSession(ctx, nil)
26+
if err != nil {
27+
panic(err)
28+
}
29+
defer session.Destroy()
30+
31+
session.On(func(event copilot.SessionEvent) {
32+
var output string
33+
switch event.Type {
34+
case copilot.AssistantReasoning:
35+
if event.Data.Content != nil {
36+
output = fmt.Sprintf("[reasoning: %s]", *event.Data.Content)
37+
}
38+
case copilot.ToolExecutionStart:
39+
if event.Data.ToolName != nil {
40+
args, _ := json.Marshal(event.Data.Arguments)
41+
output = fmt.Sprintf("[tool: %s %s]", *event.Data.ToolName, args)
42+
}
43+
}
44+
if output != "" {
45+
fmt.Printf("%s%s%s\n", blue, output, reset)
46+
}
47+
})
48+
49+
fmt.Println("Chat with Copilot (Ctrl+C to exit)\n")
50+
scanner := bufio.NewScanner(os.Stdin)
51+
52+
for {
53+
fmt.Print("You: ")
54+
if !scanner.Scan() {
55+
break
56+
}
57+
input := strings.TrimSpace(scanner.Text())
58+
if input == "" {
59+
continue
60+
}
61+
fmt.Println()
62+
63+
reply, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: input})
64+
content := ""
65+
if reply != nil && reply.Data.Content != nil {
66+
content = *reply.Data.Content
67+
}
68+
fmt.Printf("\nAssistant: %s\n\n", content)
69+
}
70+
}

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 => ../

go/samples/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
2+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
3+
github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8=
4+
github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
let output: string | null = null;
10+
if (event.type === "assistant.reasoning") {
11+
output = `[reasoning: ${event.data.content}]`;
12+
} else if (event.type === "tool.execution_start") {
13+
output = `[tool: ${event.data.toolName} ${JSON.stringify(event.data.arguments)}]`;
14+
}
15+
if (output) console.log(`\x1b[34m${output}\x1b[0m`);
16+
});
17+
18+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
19+
const prompt = (q: string) => new Promise<string>((r) => rl.question(q, r));
20+
21+
console.log("Chat with Copilot (Ctrl+C to exit)\n");
22+
23+
while (true) {
24+
const input = await prompt("You: ");
25+
if (!input.trim()) continue;
26+
console.log();
27+
28+
const reply = await session.sendAndWait({ prompt: input });
29+
console.log(`\nAssistant: ${reply?.data.content}\n`);
30+
}
31+
}
32+
33+
main().catch(console.error);

0 commit comments

Comments
 (0)