package recipe import ( "encoding/json" "fmt" "os" "path/filepath" "strings" "gmgauthier.com/grokkit/internal/grok" ) type Runner struct { Recipe *Recipe Client *grok.Client Model string } func NewRunner(r *Recipe, client *grok.Client, model string) *Runner { return &Runner{Recipe: r, Client: client, Model: model} } func (r *Runner) Run() error { fmt.Printf("šŸ³ Starting recipe: %s v%s\n\n", r.Recipe.Name, r.Recipe.Version) var previousResults []string for _, step := range r.Recipe.Steps { fmt.Printf("Step %d/%d: %s\n", step.Number, len(r.Recipe.Steps), step.Title) 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, "refactor"): r.refactorFiles(previousResults) // <-- new one-file-at-a-time handler continue case strings.Contains(titleLower, "apply") || strings.Contains(titleLower, "patch"): r.handleApplyStep(previousResults) continue default: // fallback for any other step prompt := fmt.Sprintf(`Recipe Overview: %s Previous step results (for context): %s === CURRENT STEP === Objective: %s Instructions: %s Expected output format: %s 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. 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)) } } fmt.Println("\nāœ… Recipe complete.") return nil } // discoverFiles — real filesystem scan (we'll generalize with extensions next) func (r *Runner) discoverFiles() []string { var files []string root := "internal" _ = 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 } // refactorFiles — one file at a time (small JSON, no truncation) func (r *Runner) refactorFiles(previousResults []string) { discoveredLine := previousResults[len(previousResults)-1] lines := strings.Split(discoveredLine, "\n") for _, line := range lines { filePath := strings.TrimSpace(line) if filePath == "" || strings.HasPrefix(filePath, "Discovered") || filePath == "No files found matching the criteria." { continue } fmt.Printf(" Refactoring %s...\n", filePath) content, err := os.ReadFile(filePath) if err != nil { fmt.Printf(" āŒ Could not read %s\n", filePath) continue } prompt := fmt.Sprintf(`Refactor the following file to use Result[T] instead of naked errors. Follow existing style and preserve all comments. Return ONLY this exact JSON (no extra text, no markdown): { "file": "%s", "content": "the complete refactored file here" } Original file: %s`, filePath, string(content)) 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() // store the JSON response for the apply step previousResults = append(previousResults, response) } } type FileChange struct { File string `json:"file"` Content string `json:"content"` } func (r *Runner) handleApplyStep(previousResults []string) { if len(previousResults) == 0 { fmt.Println(" āš ļø No previous results to apply — skipping.") return } // collect all JSON objects from the refactor step(s) var allChanges []FileChange for _, res := range previousResults { start := strings.Index(res, "{") end := strings.LastIndex(res, "}") + 1 if start == -1 { continue } jsonStr := res[start:end] var ch FileChange if err := json.Unmarshal([]byte(jsonStr), &ch); err == nil { allChanges = append(allChanges, ch) } } if len(allChanges) == 0 { fmt.Println(" āš ļø No valid file changes found — skipping.") return } fmt.Println(" šŸ“„ Dry-run mode: creating patch file...") patchPath := filepath.Join(".", "recipe-refactor.patch") if err := createUnifiedPatch(allChanges, patchPath); err != nil { fmt.Printf(" āŒ Failed to create patch: %v\n", err) return } fmt.Printf(" āœ… Patch created: %s\n", patchPath) fmt.Println(" Review it, then run with dry_run=false to apply.") } func createUnifiedPatch(changes []FileChange, patchPath string) error { f, err := os.Create(patchPath) if err != nil { return err } defer func(f *os.File) { err := f.Close() if err != nil { return } }(f) for _, ch := range changes { _, 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 } for _, line := range strings.Split(ch.Content, "\n") { _, err := fmt.Fprintf(f, "+%s\n", line) if err != nil { return err } } } return nil }