()
| 15 | const reset = "\033[0m" |
| 16 | |
| 17 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…