From ed614ae0dbc51ef0f782579ddbbab0e9cf449bd5 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sat, 28 Feb 2026 22:40:31 +0000 Subject: [PATCH] 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 --- cmd/agent.go | 10 +++------- internal/grok/client.go | 9 ++++----- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/cmd/agent.go b/cmd/agent.go index 9d5ef5e..aa9b97f 100644 --- a/cmd/agent.go +++ b/cmd/agent.go @@ -26,7 +26,6 @@ var agentCmd = &cobra.Command{ color.Yellow("🔍 Agent mode activated. Scanning project...") - // Discover all .go files var files []string filepath.Walk(".", func(path string, info os.FileInfo, err error) error { 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)) - // Get high-level plan 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": "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 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) if err != nil { @@ -77,18 +75,16 @@ var agentCmd = &cobra.Command{ backupPath := file + ".bak" _ = os.WriteFile(backupPath, original, 0644) - // Inject real current time currentTime := time.Now().Format("2006-01-02 15:04:05 MST") 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)}, } - 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) - // Show clean diff preview color.Cyan("Proposed changes for %s:", filepath.Base(file)) fmt.Println("--- a/" + filepath.Base(file)) fmt.Println("+++ b/" + filepath.Base(file)) diff --git a/internal/grok/client.go b/internal/grok/client.go index e3ae6ec..1776e04 100644 --- a/internal/grok/client.go +++ b/internal/grok/client.go @@ -1,5 +1,3 @@ -// Updated at: 2023-10-05 12:00:00 - package grok import ( @@ -36,7 +34,7 @@ func (c *Client) Stream(messages []map[string]string, model string) string { 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 { 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 func CleanCodeResponse(text string) string { - text = strings.ReplaceAll(text, "", "") + text = strings.ReplaceAll(text, "```go", "") + text = strings.ReplaceAll(text, "```", "") text = strings.TrimSpace(text) return text -} \ No newline at end of file +}