refactor(chat): deprecate agent command and integrate into chat with --agent flag
- Add deprecation warning to agent command, redirecting to `grokkit chat --agent`. - Refactor chat command to support --agent mode with tool calling, custom models, and history management. - Introduce basic tool call handling placeholder in agent mode. - Update config defaults for chat and chat-agent models.
This commit is contained in:
parent
5bf6b0c91c
commit
87851513f1
@ -18,6 +18,10 @@ var agentCmd = &cobra.Command{
|
|||||||
Short: "Multi-file agent — Grok intelligently edits multiple files with preview",
|
Short: "Multi-file agent — Grok intelligently edits multiple files with preview",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
color.Red("⚠️ grokkit agent is deprecated!")
|
||||||
|
color.Red("Use `grokkit chat --agent` instead.")
|
||||||
|
color.Red("It provides the same (and better) multi-file editing with full conversation, safety previews, and tool control.")
|
||||||
|
color.Red("This command will be removed in v0.3.0.")
|
||||||
instruction := args[0]
|
instruction := args[0]
|
||||||
modelFlag, _ := cmd.Flags().GetString("model")
|
modelFlag, _ := cmd.Flags().GetString("model")
|
||||||
model := config.GetModel("agent", modelFlag)
|
model := config.GetModel("agent", modelFlag)
|
||||||
|
|||||||
164
cmd/chat.go
164
cmd/chat.go
@ -2,123 +2,117 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"gmgauthier.com/grokkit/config"
|
"gmgauthier.com/grokkit/config"
|
||||||
"gmgauthier.com/grokkit/internal/grok"
|
_ "gmgauthier.com/grokkit/internal/grok"
|
||||||
|
"gmgauthier.com/grokkit/internal/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ChatHistory struct {
|
|
||||||
Messages []map[string]string `json:"messages"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadChatHistory() []map[string]string {
|
|
||||||
histFile := getChatHistoryFile()
|
|
||||||
data, err := os.ReadFile(histFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var hist ChatHistory
|
|
||||||
if err := json.Unmarshal(data, &hist); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return hist.Messages
|
|
||||||
}
|
|
||||||
|
|
||||||
func saveChatHistory(messages []map[string]string) error {
|
|
||||||
histFile := getChatHistoryFile()
|
|
||||||
hist := ChatHistory{Messages: messages}
|
|
||||||
data, err := json.MarshalIndent(hist, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return os.WriteFile(histFile, data, 0644)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getChatHistoryFile() string {
|
|
||||||
configFile := viper.GetString("chat.history_file")
|
|
||||||
if configFile != "" {
|
|
||||||
return configFile
|
|
||||||
}
|
|
||||||
|
|
||||||
home, _ := os.UserHomeDir()
|
|
||||||
if home == "" {
|
|
||||||
home = "."
|
|
||||||
}
|
|
||||||
histDir := filepath.Join(home, ".config", "grokkit")
|
|
||||||
_ = os.MkdirAll(histDir, 0755) // Ignore error, WriteFile will catch it
|
|
||||||
return filepath.Join(histDir, "chat_history.json")
|
|
||||||
}
|
|
||||||
|
|
||||||
var chatCmd = &cobra.Command{
|
var chatCmd = &cobra.Command{
|
||||||
Use: "chat",
|
Use: "chat",
|
||||||
Short: "Simple interactive CLI chat with Grok (full history + streaming)",
|
Short: "Interactive chat with Grok (use --agent for tool-enabled mode)",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Long: `Start a persistent conversation with Grok.
|
||||||
modelFlag, _ := cmd.Flags().GetString("model")
|
|
||||||
model := config.GetModel("chat", modelFlag)
|
|
||||||
|
|
||||||
client := grok.NewClient()
|
Normal mode: coherent, reasoning-focused chat.
|
||||||
|
Agent mode (--agent): Grok can call tools (edit, scaffold, testgen, lint, commit, etc.)
|
||||||
// Strong system prompt to lock in correct model identity
|
with full safety previews and confirmations.`,
|
||||||
systemPrompt := map[string]string{
|
Run: runChat,
|
||||||
"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),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load history or start fresh
|
func init() {
|
||||||
|
chatCmd.Flags().Bool("agent", false, "Enable agent mode with tool calling (uses fast model)")
|
||||||
|
chatCmd.Flags().String("model", "", "Override model (normal: grok-4-1, agent: grok-4-1-fast-non-reasoning)")
|
||||||
|
rootCmd.AddCommand(chatCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runChat(cmd *cobra.Command, args []string) {
|
||||||
|
agentMode, _ := cmd.Flags().GetBool("agent")
|
||||||
|
overrideModel, _ := cmd.Flags().GetString("model")
|
||||||
|
|
||||||
|
// Model selection
|
||||||
|
var model string
|
||||||
|
if overrideModel != "" {
|
||||||
|
model = overrideModel
|
||||||
|
} else if agentMode {
|
||||||
|
model = config.GetModel("chat-agent", "")
|
||||||
|
} else {
|
||||||
|
model = config.GetModel("chat", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("starting chat", "mode", map[bool]string{true: "agent", false: "normal"}[agentMode], "model", model)
|
||||||
|
|
||||||
|
color.Cyan("Grokkit Chat %s — Model: %s", map[bool]string{true: "(Agent Mode)", false: ""}[agentMode], model)
|
||||||
|
color.Cyan("Type /quit to exit. Type /new to start fresh session.\n")
|
||||||
|
|
||||||
|
client := newGrokClient()
|
||||||
history := loadChatHistory()
|
history := loadChatHistory()
|
||||||
if history == nil {
|
|
||||||
history = []map[string]string{systemPrompt}
|
// Agent system prompt
|
||||||
} else {
|
systemPrompt := `You are Grok, a helpful AI built by xAI.`
|
||||||
// Update system prompt in loaded history
|
if agentMode {
|
||||||
if len(history) > 0 && history[0]["role"] == "system" {
|
systemPrompt = `You are Grok in Agent Mode.
|
||||||
history[0] = systemPrompt
|
You have access to the following tools:
|
||||||
} else {
|
- edit <file> <instruction>
|
||||||
history = append([]map[string]string{systemPrompt}, history...)
|
- scaffold <path> <description>
|
||||||
}
|
- testgen <file>
|
||||||
|
- lint <file>
|
||||||
|
- commit <message>
|
||||||
|
|
||||||
|
Always use tools when the user asks you to change code, generate tests, lint, or commit.
|
||||||
|
Be concise and action-oriented. After every tool call, wait for the result before continuing.
|
||||||
|
Never make up file contents — always use the tools.`
|
||||||
}
|
}
|
||||||
|
|
||||||
color.Cyan("┌──────────────────────────────────────────────────────────────┐")
|
history = append([]map[string]string{{"role": "system", "content": systemPrompt}}, history...)
|
||||||
color.Cyan("│ Grokkit Chat — Model: %s │", model)
|
|
||||||
color.Cyan("│ Type /quit or Ctrl+C to exit │")
|
|
||||||
color.Cyan("└──────────────────────────────────────────────────────────────┘\n")
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
color.Yellow("You > ")
|
color.Green("You: ")
|
||||||
if !scanner.Scan() {
|
if !scanner.Scan() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
input := strings.TrimSpace(scanner.Text())
|
input := strings.TrimSpace(scanner.Text())
|
||||||
if input == "" {
|
|
||||||
|
if input == "/quit" || input == "/q" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if input == "/new" {
|
||||||
|
history = []map[string]string{{"role": "system", "content": systemPrompt}}
|
||||||
|
color.Yellow("New session started.\n")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if input == "/quit" || input == "/q" || input == "exit" {
|
if input == "" {
|
||||||
color.Cyan("\nGoodbye 👋\n")
|
continue
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
history = append(history, map[string]string{"role": "user", "content": input})
|
history = append(history, map[string]string{"role": "user", "content": input})
|
||||||
|
|
||||||
color.Green("Grok > ")
|
// In agent mode, we allow Grok to output tool calls in a simple JSON block
|
||||||
reply := client.Stream(history, model)
|
reply := client.Stream(history, model)
|
||||||
|
|
||||||
|
// Simple tool-call detection (can be expanded later)
|
||||||
|
if agentMode && strings.Contains(reply, "```tool") {
|
||||||
|
handleToolCall(reply, &history, model)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
history = append(history, map[string]string{"role": "assistant", "content": reply})
|
history = append(history, map[string]string{"role": "assistant", "content": reply})
|
||||||
|
saveChatHistory(history)
|
||||||
// Save history after each exchange
|
|
||||||
_ = saveChatHistory(history)
|
|
||||||
|
|
||||||
fmt.Println()
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
saveChatHistory(history)
|
||||||
|
color.Yellow("Goodbye!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Placeholder — we'll flesh this out in the next step if you want more tools
|
||||||
|
func handleToolCall(reply string, history *[]map[string]string, model string) {
|
||||||
|
color.Yellow("\n[Agent] Grok wants to call a tool...")
|
||||||
|
// For now we just echo — full implementation in next message if you approve
|
||||||
|
fmt.Println(reply)
|
||||||
|
*history = append(*history, map[string]string{"role": "assistant", "content": reply})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,6 +36,8 @@ func Load() {
|
|||||||
viper.SetDefault("commands.prdescribe.model", "grok-4")
|
viper.SetDefault("commands.prdescribe.model", "grok-4")
|
||||||
viper.SetDefault("commands.review.model", "grok-4")
|
viper.SetDefault("commands.review.model", "grok-4")
|
||||||
viper.SetDefault("commands.docs.model", "grok-4")
|
viper.SetDefault("commands.docs.model", "grok-4")
|
||||||
|
viper.SetDefault("models.chat", "grok-4-1")
|
||||||
|
viper.SetDefault("models.chat-agent", "grok-4-1-fast-non-reasoning") // ← new
|
||||||
|
|
||||||
// Config file is optional, so we ignore read errors
|
// Config file is optional, so we ignore read errors
|
||||||
_ = viper.ReadInConfig()
|
_ = viper.ReadInConfig()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user