-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathchat.go
More file actions
73 lines (64 loc) · 1.58 KB
/
Copy pathchat.go
File metadata and controls
73 lines (64 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/github/copilot-sdk/go"
)
const blue = "\033[34m"
const reset = "\033[0m"
func main() {
ctx := context.Background()
cliPath := filepath.Join("..", "..", "nodejs", "node_modules", "@github", "copilot", "index.js")
client := copilot.NewClient(&copilot.ClientOptions{CLIPath: cliPath})
if err := client.Start(ctx); err != nil {
panic(err)
}
defer client.Stop()
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
CLIPath: cliPath,
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
})
if err != nil {
panic(err)
}
defer session.Destroy()
session.On(func(event copilot.SessionEvent) {
var output string
switch event.Type {
case copilot.AssistantReasoning:
if event.Data.Content != nil {
output = fmt.Sprintf("[reasoning: %s]", *event.Data.Content)
}
case copilot.ToolExecutionStart:
if event.Data.ToolName != nil {
output = fmt.Sprintf("[tool: %s]", *event.Data.ToolName)
}
}
if output != "" {
fmt.Printf("%s%s%s\n", blue, output, reset)
}
})
fmt.Println("Chat with Copilot (Ctrl+C to exit)\n")
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("You: ")
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
continue
}
fmt.Println()
reply, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: input})
content := ""
if reply != nil && reply.Data.Content != nil {
content = *reply.Data.Content
}
fmt.Printf("\nAssistant: %s\n\n", content)
}
}