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 (
2026-02-28 23:03:53 +00:00
"bufio"
2026-02-28 20:17:12 +00:00
"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"
2026-02-28 20:52:03 +00:00
"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
2026-02-28 20:17:12 +00:00
var chatCmd = & cobra . Command {
Use : "chat" ,
2026-02-28 23:03:53 +00:00
Short : "Simple interactive CLI chat with Grok (full history + streaming)" ,
2026-02-28 20:17:12 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2026-02-28 20:52:03 +00:00
modelFlag , _ := cmd . Flags ( ) . GetString ( "model" )
model := config . GetModel ( modelFlag )
2026-02-28 23:03:53 +00:00
client := grok . NewClient ( )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
// Strong system prompt to lock in correct model identity
systemPrompt := map [ string ] string {
"role" : "system" ,
"content" : fmt . Sprintf ( "You are Grok 4, the latest and most powerful model from xAI (2026). You are currently running as `%s`. Be helpful, truthful, and a little irreverent. Never claim to be an older model." , model ) ,
}
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
history := [ ] map [ string ] string { systemPrompt }
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
color . Cyan ( "┌──────────────────────────────────────────────────────────────┐" )
color . Cyan ( "│ Grokkit Chat — Model: %s │" , model )
color . Cyan ( "│ Type /quit or Ctrl+C to exit │" )
color . Cyan ( "└──────────────────────────────────────────────────────────────┘\n" )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
scanner := bufio . NewScanner ( os . Stdin )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
for {
color . Yellow ( "You > " )
if ! scanner . Scan ( ) {
break
}
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
input := strings . TrimSpace ( scanner . Text ( ) )
2026-02-28 20:17:12 +00:00
if input == "" {
2026-02-28 23:03:53 +00:00
continue
2026-02-28 19:56:23 +00:00
}
2026-02-28 23:03:53 +00:00
if input == "/quit" || input == "/q" || input == "exit" {
color . Cyan ( "\nGoodbye 👋\n" )
break
2026-02-28 19:56:23 +00:00
}
2026-02-28 23:03:53 +00:00
history = append ( history , map [ string ] string { "role" : "user" , "content" : input } )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
color . Green ( "Grok > " )
reply := client . Stream ( history , model )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
history = append ( history , map [ string ] string { "role" : "assistant" , "content" : reply } )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
fmt . Println ( )
2026-02-28 21:53:35 +00:00
}
2026-02-28 23:03:53 +00:00
} ,
2026-02-28 22:59:16 +00:00
}