grokkit/cmd/chat.go

48 lines
1.0 KiB
Go
Raw Normal View History

2026-02-28 18:03:12 +00:00
package cmd
2026-02-28 18:28:27 +00:00
2026-02-28 18:41:20 +00:00
import (
"bufio"
"fmt"
2026-02-28 19:56:23 +00:00
"os"
"strings"
"github.com/fatih/color"
2026-02-28 18:41:20 +00:00
"github.com/spf13/cobra"
"gmgauthier.com/grokkit/config"
2026-02-28 18:41:20 +00:00
"gmgauthier.com/grokkit/internal/grok"
)
2026-02-28 18:28:27 +00:00
var chatCmd = &cobra.Command{
Use: "chat",
Short: "Interactive streaming chat with Grok",
Run: func(cmd *cobra.Command, args []string) {
client := grok.NewClient()
color.Cyan("Grokkit Chat — type /quit or Ctrl+C to exit\n")
2026-02-28 19:56:23 +00:00
history := []map[string]string{}
scanner := bufio.NewScanner(os.Stdin)
2026-02-28 19:56:23 +00:00
modelFlag, _ := cmd.Flags().GetString("model")
model := config.GetModel(modelFlag)
for {
fmt.Print(color.YellowString("You: "))
if !scanner.Scan() {
break
2026-02-28 19:56:23 +00:00
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
continue
2026-02-28 19:56:23 +00:00
}
if input == "/quit" || input == "/q" {
break
2026-02-28 19:56:23 +00:00
}
history = append(history, map[string]string{"role": "user", "content": input})
color.Green("Grok: ")
reply := client.Stream(history, model)
history = append(history, map[string]string{"role": "assistant", "content": reply})
2026-02-28 19:56:23 +00:00
}
2026-02-28 18:28:27 +00:00
},
}