Introduce new `agent` command that scans .go files in the project, generates an AI-driven plan for changes based on user instruction, and applies edits with previews and backups. Includes integration with Grok client for planning and content generation. Update existing files with timestamp comments as part of the agent's editing demonstration. Add agentCmd to root command.
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// Updated at current time: 2023-10-05 14:30:00 UTC
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/spf13/cobra"
|
|
"gmgauthier.com/grokkit/config"
|
|
"gmgauthier.com/grokkit/internal/git"
|
|
"gmgauthier.com/grokkit/internal/grok"
|
|
)
|
|
|
|
var commitCmd = &cobra.Command{
|
|
Use: "commit",
|
|
Short: "Generate message and commit staged changes",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
diff := git.Run([]string{"diff", "--cached", "--no-color"})
|
|
if diff == "" {
|
|
color.Yellow("No staged changes!")
|
|
return
|
|
}
|
|
modelFlag, _ := cmd.Flags().GetString("model")
|
|
model := config.GetModel(modelFlag)
|
|
|
|
client := grok.NewClient()
|
|
messages := []map[string]string{
|
|
{"role": "system", "content": "Return ONLY a conventional commit message (type(scope): subject\n\nbody)."},
|
|
{"role": "user", "content": fmt.Sprintf("Staged changes:\n%s", diff)},
|
|
}
|
|
color.Yellow("Generating commit message...")
|
|
msg := client.Stream(messages, model)
|
|
|
|
color.Cyan("\nProposed commit message:\n%s", msg)
|
|
var confirm string
|
|
color.Yellow("Commit with this message? (y/n): ")
|
|
fmt.Scanln(&confirm)
|
|
if confirm != "y" && confirm != "Y" {
|
|
color.Yellow("Aborted.")
|
|
return
|
|
}
|
|
|
|
if err := exec.Command("git", "commit", "-m", msg).Run(); err != nil {
|
|
color.Red("Git commit failed")
|
|
} else {
|
|
color.Green("✅ Committed successfully!")
|
|
}
|
|
},
|
|
} |