2026-03-06 18:35:58 +00:00
|
|
|
package recipe
|
|
|
|
|
|
2026-03-06 20:32:04 +00:00
|
|
|
import (
|
2026-03-06 22:40:29 +00:00
|
|
|
"encoding/json"
|
2026-03-06 20:32:04 +00:00
|
|
|
"fmt"
|
2026-03-06 21:01:01 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-03-06 20:32:04 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gmgauthier.com/grokkit/internal/grok"
|
|
|
|
|
)
|
2026-03-06 18:35:58 +00:00
|
|
|
|
|
|
|
|
type Runner struct {
|
|
|
|
|
Recipe *Recipe
|
2026-03-06 20:32:04 +00:00
|
|
|
Client *grok.Client
|
|
|
|
|
Model string
|
2026-03-06 18:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 20:32:04 +00:00
|
|
|
func NewRunner(r *Recipe, client *grok.Client, model string) *Runner {
|
|
|
|
|
return &Runner{Recipe: r, Client: client, Model: model}
|
2026-03-06 18:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) Run() error {
|
2026-03-06 19:00:16 +00:00
|
|
|
fmt.Printf("🍳 Starting recipe: %s v%s\n\n", r.Recipe.Name, r.Recipe.Version)
|
|
|
|
|
|
2026-03-06 20:32:04 +00:00
|
|
|
var previousResults []string
|
|
|
|
|
|
2026-03-06 18:35:58 +00:00
|
|
|
for _, step := range r.Recipe.Steps {
|
|
|
|
|
fmt.Printf("Step %d/%d: %s\n", step.Number, len(r.Recipe.Steps), step.Title)
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 21:57:35 +00:00
|
|
|
titleLower := strings.ToLower(step.Title)
|
|
|
|
|
|
|
|
|
|
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"):
|
2026-03-06 21:01:01 +00:00
|
|
|
r.handleApplyStep(previousResults)
|
|
|
|
|
continue
|
|
|
|
|
|
2026-03-06 21:57:35 +00:00
|
|
|
default:
|
|
|
|
|
prompt := fmt.Sprintf(`Recipe Overview:
|
2026-03-06 20:32:04 +00:00
|
|
|
%s
|
|
|
|
|
|
|
|
|
|
Previous step results (for context):
|
|
|
|
|
%s
|
|
|
|
|
|
|
|
|
|
=== CURRENT STEP ===
|
|
|
|
|
Objective: %s
|
|
|
|
|
Instructions: %s
|
|
|
|
|
Expected output format: %s
|
|
|
|
|
|
2026-03-06 21:57:35 +00:00
|
|
|
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)
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 21:57:35 +00:00
|
|
|
messages := []map[string]string{
|
|
|
|
|
{"role": "system", "content": "You are Grok, built by xAI. Precise expert programmer and refactoring assistant."},
|
|
|
|
|
{"role": "user", "content": prompt},
|
|
|
|
|
}
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 21:57:35 +00:00
|
|
|
response := r.Client.Stream(messages, r.Model)
|
|
|
|
|
fmt.Println()
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 21:57:35 +00:00
|
|
|
previousResults = append(previousResults, fmt.Sprintf("Step %d result:\n%s", step.Number, response))
|
|
|
|
|
}
|
2026-03-06 20:32:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 21:01:01 +00:00
|
|
|
fmt.Println("\n✅ Recipe complete.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 22:48:15 +00:00
|
|
|
// discoverFiles — temporary .go hard-code (we'll generalize with extensions next)
|
2026-03-06 21:57:35 +00:00
|
|
|
func (r *Runner) discoverFiles() []string {
|
|
|
|
|
var files []string
|
2026-03-06 22:07:22 +00:00
|
|
|
root := "internal"
|
2026-03-06 21:57:35 +00:00
|
|
|
|
|
|
|
|
_ = 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
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:48:15 +00:00
|
|
|
type FileChange struct {
|
|
|
|
|
File string `json:"file"`
|
|
|
|
|
Content string `json:"content"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-06 21:01:01 +00:00
|
|
|
func (r *Runner) handleApplyStep(previousResults []string) {
|
|
|
|
|
if len(previousResults) == 0 {
|
|
|
|
|
fmt.Println(" ⚠️ No previous results to apply — skipping.")
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 21:01:01 +00:00
|
|
|
lastResult := previousResults[len(previousResults)-1]
|
2026-03-06 20:32:04 +00:00
|
|
|
|
2026-03-06 22:40:29 +00:00
|
|
|
// Find the JSON array in the response
|
|
|
|
|
start := strings.Index(lastResult, "[")
|
|
|
|
|
end := strings.LastIndex(lastResult, "]") + 1
|
|
|
|
|
if start == -1 || end == 0 {
|
|
|
|
|
fmt.Println(" ⚠️ No JSON found in previous step — skipping.")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jsonStr := lastResult[start:end]
|
|
|
|
|
|
|
|
|
|
var changes []FileChange
|
|
|
|
|
if err := json.Unmarshal([]byte(jsonStr), &changes); err != nil {
|
|
|
|
|
fmt.Printf(" ⚠️ Could not parse JSON: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(changes) == 0 {
|
|
|
|
|
fmt.Println(" ⚠️ No files to apply — skipping.")
|
2026-03-06 21:01:01 +00:00
|
|
|
return
|
2026-03-06 18:35:58 +00:00
|
|
|
}
|
2026-03-06 19:00:16 +00:00
|
|
|
|
2026-03-06 21:36:31 +00:00
|
|
|
fmt.Println(" 📄 Dry-run mode: creating patch file...")
|
|
|
|
|
patchPath := filepath.Join(".", "recipe-refactor.patch")
|
2026-03-06 22:40:29 +00:00
|
|
|
if err := createUnifiedPatch(changes, patchPath); err != nil {
|
2026-03-06 21:36:31 +00:00
|
|
|
fmt.Printf(" ❌ Failed to create patch: %v\n", err)
|
2026-03-06 21:01:01 +00:00
|
|
|
return
|
|
|
|
|
}
|
2026-03-06 21:36:31 +00:00
|
|
|
fmt.Printf(" ✅ Patch created: %s\n", patchPath)
|
|
|
|
|
fmt.Println(" Review it, then run with dry_run=false to apply.")
|
2026-03-06 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-06 22:48:15 +00:00
|
|
|
func createUnifiedPatch(changes []FileChange, patchPath string) error {
|
2026-03-06 21:01:01 +00:00
|
|
|
f, err := os.Create(patchPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-06 22:48:15 +00:00
|
|
|
defer func(f *os.File) {
|
|
|
|
|
err := f.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}(f)
|
2026-03-06 21:01:01 +00:00
|
|
|
|
2026-03-06 22:40:29 +00:00
|
|
|
for _, ch := range changes {
|
2026-03-06 22:48:15 +00:00
|
|
|
_, err := fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", ch.File, ch.File, strings.Count(ch.Content, "\n")+1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-06 22:40:29 +00:00
|
|
|
for _, line := range strings.Split(ch.Content, "\n") {
|
2026-03-06 22:48:15 +00:00
|
|
|
_, err := fmt.Fprintf(f, "+%s\n", line)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-06 21:01:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-06 18:35:58 +00:00
|
|
|
return nil
|
2026-03-06 22:40:29 +00:00
|
|
|
}
|