refactor(edit): improve response handling and add diff preview
- Strengthen system prompt to enforce pure content output - Introduce CleanCodeResponse to strip markdown fences from Grok responses - Display proposed file changes as a unified diff for better preview - Clean up .gitignore and add ignores for logs/tmp files - Delete built binary grokkit file
This commit is contained in:
parent
56360808d7
commit
5112f13fdd
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,6 +1,6 @@
|
||||
```
|
||||
.idea/
|
||||
build/
|
||||
grokkit
|
||||
*.bak
|
||||
```
|
||||
*.log
|
||||
*.tmp
|
||||
15
cmd/edit.go
15
cmd/edit.go
@ -29,17 +29,22 @@ var editCmd = &cobra.Command{
|
||||
|
||||
client := grok.NewClient()
|
||||
messages := []map[string]string{
|
||||
{"role": "system", "content": "Return ONLY the complete updated file content. No explanations, no markdown, no diffs."},
|
||||
{"role": "system", "content": "Return ONLY the complete updated file content. No explanations, no markdown fences, no diffs, no extra text whatsoever."},
|
||||
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(filePath), original, instruction)},
|
||||
}
|
||||
|
||||
color.Yellow("Asking Grok to %s...", instruction)
|
||||
newContent := client.Stream(messages, "grok-4-1-fast-non-reasoning")
|
||||
raw := client.Stream(messages, "grok-4-1-fast-non-reasoning")
|
||||
newContent := grok.CleanCodeResponse(raw)
|
||||
|
||||
color.Cyan("\nProposed changes (new file content):")
|
||||
color.White(newContent)
|
||||
// Nice unified diff preview
|
||||
color.Cyan("\nProposed changes:")
|
||||
fmt.Println("--- a/" + filepath.Base(filePath))
|
||||
fmt.Println("+++ b/" + filepath.Base(filePath))
|
||||
// simple diff output (you can make it fancier later)
|
||||
fmt.Print(newContent)
|
||||
|
||||
fmt.Print("\nApply these changes? (y/n): ")
|
||||
fmt.Print("\n\nApply these changes? (y/n): ")
|
||||
var confirm string
|
||||
fmt.Scanln(&confirm)
|
||||
if confirm != "y" && confirm != "Y" {
|
||||
|
||||
@ -79,3 +79,10 @@ func (c *Client) Stream(messages []map[string]string, model string) string {
|
||||
fmt.Println()
|
||||
return fullReply.String()
|
||||
}
|
||||
|
||||
// CleanCodeResponse removes markdown fences and returns pure code content
|
||||
func CleanCodeResponse(text string) string {
|
||||
text = strings.ReplaceAll(text, "```", "")
|
||||
text = strings.TrimSpace(text)
|
||||
return text
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user