-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathchat.go
More file actions
71 lines (62 loc) · 1.54 KB
/
chat.go
File metadata and controls
71 lines (62 loc) · 1.54 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
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.Disconnect()
session.On(func(event copilot.SessionEvent) {
var output string
switch d := event.Data.(type) {
case *copilot.AssistantReasoningData:
output = fmt.Sprintf("[reasoning: %s]", d.Content)
case *copilot.ToolExecutionStartData:
output = fmt.Sprintf("[tool: %s]", d.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 {
if d, ok := reply.Data.(*copilot.AssistantMessageData); ok {
content = d.Content
}
}
fmt.Printf("\nAssistant: %s\n\n", content)
}
}