refactor(agent): refine prompts and code cleaning logic
- Remove unnecessary comments in agent.go for cleaner code - Update system prompt to enforce exact header comment format - Adjust progress logging format - Fix CleanCodeResponse to properly strip markdown fences - Update StreamSilent comment to include agent usage
This commit is contained in:
parent
8e0d06d8a1
commit
ed614ae0db
10
cmd/agent.go
10
cmd/agent.go
@ -26,7 +26,6 @@ var agentCmd = &cobra.Command{
|
|||||||
|
|
||||||
color.Yellow("🔍 Agent mode activated. Scanning project...")
|
color.Yellow("🔍 Agent mode activated. Scanning project...")
|
||||||
|
|
||||||
// Discover all .go files
|
|
||||||
var files []string
|
var files []string
|
||||||
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil || info.IsDir() {
|
if err != nil || info.IsDir() {
|
||||||
@ -45,7 +44,6 @@ var agentCmd = &cobra.Command{
|
|||||||
|
|
||||||
color.Yellow("📄 Found %d files. Asking Grok for a plan...", len(files))
|
color.Yellow("📄 Found %d files. Asking Grok for a plan...", len(files))
|
||||||
|
|
||||||
// Get high-level plan
|
|
||||||
planMessages := []map[string]string{
|
planMessages := []map[string]string{
|
||||||
{"role": "system", "content": "You are an expert software engineer. Given an instruction and list of files, return a clear plan: which files to change and a brief description of what to do in each. List files one per line."},
|
{"role": "system", "content": "You are an expert software engineer. Given an instruction and list of files, return a clear plan: which files to change and a brief description of what to do in each. List files one per line."},
|
||||||
{"role": "user", "content": fmt.Sprintf("Instruction: %s\n\nFiles:\n%s", instruction, strings.Join(files, "\n"))},
|
{"role": "user", "content": fmt.Sprintf("Instruction: %s\n\nFiles:\n%s", instruction, strings.Join(files, "\n"))},
|
||||||
@ -66,7 +64,7 @@ var agentCmd = &cobra.Command{
|
|||||||
|
|
||||||
applyAll := false
|
applyAll := false
|
||||||
for i, file := range files {
|
for i, file := range files {
|
||||||
color.Yellow("[%d/%d] → Processing: %s", i+1, len(files), file)
|
color.Yellow("[%d/%d] → %s", i+1, len(files), file)
|
||||||
|
|
||||||
original, err := os.ReadFile(file)
|
original, err := os.ReadFile(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -77,18 +75,16 @@ var agentCmd = &cobra.Command{
|
|||||||
backupPath := file + ".bak"
|
backupPath := file + ".bak"
|
||||||
_ = os.WriteFile(backupPath, original, 0644)
|
_ = os.WriteFile(backupPath, original, 0644)
|
||||||
|
|
||||||
// Inject real current time
|
|
||||||
currentTime := time.Now().Format("2006-01-02 15:04:05 MST")
|
currentTime := time.Now().Format("2006-01-02 15:04:05 MST")
|
||||||
|
|
||||||
messages := []map[string]string{
|
messages := []map[string]string{
|
||||||
{"role": "system", "content": fmt.Sprintf("You are an expert programmer. Return ONLY the complete updated file content. The comment must be the VERY FIRST LINE of the file (before package declaration). Current time is %s.", currentTime)},
|
{"role": "system", "content": fmt.Sprintf("You are an expert programmer. The VERY FIRST LINE of the returned file MUST be a header comment in this exact format: '// Last modified: %s'. Then return the complete updated file content. No explanations, no markdown, no diffs, no extra text.", currentTime)},
|
||||||
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(file), original, instruction)},
|
{"role": "user", "content": fmt.Sprintf("File: %s\n\nOriginal content:\n%s\n\nTask: %s", filepath.Base(file), original, instruction)},
|
||||||
}
|
}
|
||||||
|
|
||||||
raw := client.StreamSilent(messages, "grok-4-1-fast-non-reasoning") // faster model for edits
|
raw := client.StreamSilent(messages, "grok-4-1-fast-non-reasoning")
|
||||||
newContent := grok.CleanCodeResponse(raw)
|
newContent := grok.CleanCodeResponse(raw)
|
||||||
|
|
||||||
// Show clean diff preview
|
|
||||||
color.Cyan("Proposed changes for %s:", filepath.Base(file))
|
color.Cyan("Proposed changes for %s:", filepath.Base(file))
|
||||||
fmt.Println("--- a/" + filepath.Base(file))
|
fmt.Println("--- a/" + filepath.Base(file))
|
||||||
fmt.Println("+++ b/" + filepath.Base(file))
|
fmt.Println("+++ b/" + filepath.Base(file))
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
// Updated at: 2023-10-05 12:00:00
|
|
||||||
|
|
||||||
package grok
|
package grok
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -36,7 +34,7 @@ func (c *Client) Stream(messages []map[string]string, model string) string {
|
|||||||
return c.streamInternal(messages, model, true)
|
return c.streamInternal(messages, model, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamSilent returns the full text without printing (used by TUI)
|
// StreamSilent returns the full text without printing (used by TUI and agent)
|
||||||
func (c *Client) StreamSilent(messages []map[string]string, model string) string {
|
func (c *Client) StreamSilent(messages []map[string]string, model string) string {
|
||||||
return c.streamInternal(messages, model, false)
|
return c.streamInternal(messages, model, false)
|
||||||
}
|
}
|
||||||
@ -94,7 +92,8 @@ func (c *Client) streamInternal(messages []map[string]string, model string, prin
|
|||||||
|
|
||||||
// CleanCodeResponse removes markdown fences and returns pure code content
|
// CleanCodeResponse removes markdown fences and returns pure code content
|
||||||
func CleanCodeResponse(text string) string {
|
func CleanCodeResponse(text string) string {
|
||||||
text = strings.ReplaceAll(text, "", "")
|
text = strings.ReplaceAll(text, "```go", "")
|
||||||
|
text = strings.ReplaceAll(text, "```", "")
|
||||||
text = strings.TrimSpace(text)
|
text = strings.TrimSpace(text)
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user