feature/recipe_implementation #5
@ -1,7 +1,7 @@
|
||||
package recipe
|
||||
|
||||
import (
|
||||
_ "bufio"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -29,14 +29,23 @@ func (r *Runner) Run() error {
|
||||
for _, step := range r.Recipe.Steps {
|
||||
fmt.Printf("Step %d/%d: %s\n", step.Number, len(r.Recipe.Steps), step.Title)
|
||||
|
||||
// Only special-case the Apply/Patch step (this is the only place the CLI needs to touch disk)
|
||||
if strings.Contains(strings.ToLower(step.Title), "apply") || strings.Contains(strings.ToLower(step.Title), "patch") {
|
||||
titleLower := strings.ToLower(step.Title)
|
||||
|
||||
// Only two special cases the CLI ever needs to handle
|
||||
switch {
|
||||
case strings.Contains(titleLower, "discover") || strings.Contains(titleLower, "find"):
|
||||
files := r.discoverFiles()
|
||||
result := strings.Join(files, "\n")
|
||||
previousResults = append(previousResults, "Discovered files:\n"+result)
|
||||
fmt.Println(result)
|
||||
|
||||
case strings.Contains(titleLower, "apply") || strings.Contains(titleLower, "patch"):
|
||||
r.handleApplyStep(previousResults)
|
||||
continue
|
||||
}
|
||||
|
||||
// Everything else is pure LLM — the recipe defines exactly what to do
|
||||
prompt := fmt.Sprintf(`Recipe Overview:
|
||||
default:
|
||||
// Everything else = pure LLM (works for any language)
|
||||
prompt := fmt.Sprintf(`Recipe Overview:
|
||||
%s
|
||||
|
||||
Previous step results (for context):
|
||||
@ -47,29 +56,52 @@ Objective: %s
|
||||
Instructions: %s
|
||||
Expected output format: %s
|
||||
|
||||
Execute this step now.`,
|
||||
r.Recipe.Overview,
|
||||
strings.Join(previousResults, "\n\n---\n\n"),
|
||||
step.Objective,
|
||||
step.Instructions,
|
||||
step.Expected)
|
||||
Execute this step now. Respond ONLY with the expected output format — no explanations, no extra text.`,
|
||||
r.Recipe.Overview,
|
||||
strings.Join(previousResults, "\n\n---\n\n"),
|
||||
step.Objective,
|
||||
step.Instructions,
|
||||
step.Expected)
|
||||
|
||||
messages := []map[string]string{
|
||||
{"role": "system", "content": "You are Grok, built by xAI. You are a precise, expert programmer and refactoring assistant. Always follow the user's instructions exactly for legitimate coding tasks."},
|
||||
{"role": "user", "content": prompt},
|
||||
messages := []map[string]string{
|
||||
{"role": "system", "content": "You are Grok, built by xAI. Precise expert programmer and refactoring assistant."},
|
||||
{"role": "user", "content": prompt},
|
||||
}
|
||||
|
||||
response := r.Client.Stream(messages, r.Model)
|
||||
fmt.Println()
|
||||
|
||||
previousResults = append(previousResults, fmt.Sprintf("Step %d result:\n%s", step.Number, response))
|
||||
}
|
||||
|
||||
response := r.Client.Stream(messages, r.Model)
|
||||
fmt.Println()
|
||||
|
||||
previousResults = append(previousResults, fmt.Sprintf("Step %d result:\n%s", step.Number, response))
|
||||
}
|
||||
|
||||
fmt.Println("\n✅ Recipe complete.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleApplyStep is the ONLY place we touch the filesystem (exactly like edit/scaffold)
|
||||
// discoverFiles does a real filesystem scan — generic enough for any Go project
|
||||
func (r *Runner) discoverFiles() []string {
|
||||
var files []string
|
||||
root := "internal" // matches the recipe default; we can make it parametric later
|
||||
|
||||
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") {
|
||||
return nil
|
||||
}
|
||||
b, _ := os.ReadFile(path)
|
||||
if strings.Contains(string(b), "if err != nil") {
|
||||
files = append(files, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if len(files) == 0 {
|
||||
files = append(files, "No files found matching the criteria.")
|
||||
}
|
||||
return files
|
||||
}
|
||||
|
||||
// handleApplyStep stays exactly as you had it (dry-run patch + confirmation)
|
||||
func (r *Runner) handleApplyStep(previousResults []string) {
|
||||
if len(previousResults) == 0 {
|
||||
fmt.Println(" ⚠️ No previous results to apply — skipping.")
|
||||
@ -84,7 +116,6 @@ func (r *Runner) handleApplyStep(previousResults []string) {
|
||||
return
|
||||
}
|
||||
|
||||
// Dry-run by default (we'll wire parameters later)
|
||||
fmt.Println(" 📄 Dry-run mode: creating patch file...")
|
||||
patchPath := filepath.Join(".", "recipe-refactor.patch")
|
||||
if err := createUnifiedPatch(blocks, patchPath); err != nil {
|
||||
@ -95,8 +126,7 @@ func (r *Runner) handleApplyStep(previousResults []string) {
|
||||
fmt.Println(" Review it, then run with dry_run=false to apply.")
|
||||
}
|
||||
|
||||
// Simple regex for the format the recipe asks Grok to return
|
||||
var blockRe = regexp.MustCompile(`(?s)//\s*(.+?\.go)\n` + "```" + `go\n(.*?)\n` + "```")
|
||||
var blockRe = regexp.MustCompile(`(?s)^//\s*(.+?\.go)\n```go\n(.*?)\n````)
|
||||
|
||||
func extractCodeBlocks(text string) map[string]string {
|
||||
blocks := make(map[string]string)
|
||||
@ -114,27 +144,13 @@ func createUnifiedPatch(blocks map[string]string, patchPath string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func(f *os.File) {
|
||||
err := f.Close()
|
||||
if err != nil {
|
||||
_, err := fmt.Fprintf(f, "+%s\n", err.Error())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}(f)
|
||||
defer f.Close()
|
||||
|
||||
for path, content := range blocks {
|
||||
_, err := fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", path, path, strings.Count(content, "\n")+1)
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
_, err := fmt.Fprintf(f, "+%s\n", line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(f, "+%s\n", line)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user