MCPcopy Create free account
hub / github.com/github/copilot-sdk / main

Function main

go/samples/chat.go:17–71  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15const reset = "\033[0m"
16
17func main() {
18 ctx := context.Background()
19 cliPath := filepath.Join("..", "..", "nodejs", "node_modules", "@github", "copilot", "index.js")
20 client := copilot.NewClient(&copilot.ClientOptions{Connection: copilot.StdioConnection{Path: cliPath}})
21 if err := client.Start(ctx); err != nil {
22 panic(err)
23 }
24 defer client.Stop()
25
26 session, err := client.CreateSession(ctx, &copilot.SessionConfig{
27 OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
28 })
29 if err != nil {
30 panic(err)
31 }
32 defer session.Disconnect()
33
34 session.On(func(event copilot.SessionEvent) {
35 var output string
36 switch d := event.Data.(type) {
37 case *copilot.AssistantReasoningData:
38 output = fmt.Sprintf("[reasoning: %s]", d.Content)
39 case *copilot.ToolExecutionStartData:
40 output = fmt.Sprintf("[tool: %s]", d.ToolName)
41 }
42 if output != "" {
43 fmt.Printf("%s%s%s\n", blue, output, reset)
44 }
45 })
46
47 fmt.Println("Chat with Copilot (Ctrl+C to exit)")
48 fmt.Println()
49 scanner := bufio.NewScanner(os.Stdin)
50
51 for {
52 fmt.Print("You: ")
53 if !scanner.Scan() {
54 break
55 }
56 input := strings.TrimSpace(scanner.Text())
57 if input == "" {
58 continue
59 }
60 fmt.Println()
61
62 reply, _ := session.SendAndWait(ctx, copilot.MessageOptions{Prompt: input})
63 content := ""
64 if reply != nil {
65 if d, ok := reply.Data.(*copilot.AssistantMessageData); ok {
66 content = d.Content
67 }
68 }
69 fmt.Printf("\nAssistant: %s\n\n", content)
70 }
71}

Callers

nothing calls this directly

Calls 8

StartMethod · 0.95
StopMethod · 0.95
CreateSessionMethod · 0.95
NewClientMethod · 0.80
DisconnectMethod · 0.80
TextMethod · 0.80
SendAndWaitMethod · 0.80
OnMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…