|
| 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 | +} |
0 commit comments