grokkit/cmd/chat.go

100 lines
3.1 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"
"gmgauthier.com/grokkit/internal/grok"
2026-02-28 18:41:20 +00:00
)
2026-02-28 18:28:27 +00:00
var chatCmd = &cobra.Command{
Use: "chat",
Short: "Interactive chat with Grok (use --agent for tool-enabled mode)",
Long: `Start a persistent conversation with Grok.
Normal mode: coherent, reasoning-focused chat (uses full model).
Agent mode (--agent): Grok can call tools with safety previews (uses fast model).`,
Run: runChat,
}
func init() {
chatCmd.Flags().Bool("agent", false, "Enable agent mode with tool calling (uses fast non-reasoning model)")
chatCmd.Flags().String("model", "", "Override model")
rootCmd.AddCommand(chatCmd)
}
func runChat(cmd *cobra.Command, args []string) {
agentMode, _ := cmd.Flags().GetBool("agent")
modelFlag, _ := cmd.Flags().GetString("model")
// Model switching logic
var model string
if modelFlag != "" {
model = modelFlag
} else if agentMode {
model = config.GetModel("chat-agent", "") // we'll add this default next
} else {
model = config.GetModel("chat", "")
}
client := grok.NewClient()
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.", model),
}
if agentMode {
systemPrompt["content"] = `You are Grok in Agent Mode.
You have access to tools: edit, scaffold, testgen, lint, commit, etc.
Always use tools when the user wants you to change code, generate tests, lint or commit.
Be concise and action-oriented. After every tool call, wait for the result.`
}
history := loadChatHistory()
if history == nil || len(history) == 0 {
history = []map[string]string{systemPrompt}
} else if history[0]["role"] != "system" {
history = append([]map[string]string{systemPrompt}, history...)
} else {
history[0] = systemPrompt // refresh system prompt
}
color.Cyan("┌──────────────────────────────────────────────────────────────┐")
color.Cyan("│ Grokkit Chat %s — Model: %s │", map[bool]string{true: "(Agent Mode)", false: ""}[agentMode], model)
color.Cyan("│ Type /quit to exit │")
color.Cyan("└──────────────────────────────────────────────────────────────┘\n")
scanner := bufio.NewScanner(os.Stdin)
for {
color.Yellow("You > ")
if !scanner.Scan() {
break
}
input := strings.TrimSpace(scanner.Text())
if input == "" {
continue
}
if input == "/quit" || input == "/q" || input == "exit" {
color.Cyan("\nGoodbye 👋\n")
break
}
history = append(history, map[string]string{"role": "user", "content": input})
2026-02-28 19:56:23 +00:00
color.Green("Grok > ")
reply := client.Stream(history, model)
history = append(history, map[string]string{"role": "assistant", "content": reply})
_ = saveChatHistory(history)
fmt.Println()
}
}