|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "codes/internal/ui" |
| 9 | + "codes/internal/workflow" |
| 10 | +) |
| 11 | + |
| 12 | +// RunWorkflowList displays all available workflows. |
| 13 | +func RunWorkflowList() { |
| 14 | + workflows, err := workflow.ListWorkflows() |
| 15 | + if err != nil { |
| 16 | + ui.ShowError("Failed to list workflows", err) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + if len(workflows) == 0 { |
| 21 | + fmt.Println("No workflows found.") |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + fmt.Println() |
| 26 | + for _, wf := range workflows { |
| 27 | + tag := "" |
| 28 | + if wf.BuiltIn { |
| 29 | + tag = " (built-in)" |
| 30 | + } |
| 31 | + fmt.Printf(" %s%s\n", wf.Name, tag) |
| 32 | + if wf.Description != "" { |
| 33 | + fmt.Printf(" %s\n", wf.Description) |
| 34 | + } |
| 35 | + fmt.Printf(" Steps: %d\n", len(wf.Steps)) |
| 36 | + fmt.Println() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// RunWorkflowRun executes a workflow by name. |
| 41 | +func RunWorkflowRun(name, dir, model string) { |
| 42 | + wf, err := workflow.GetWorkflow(name) |
| 43 | + if err != nil { |
| 44 | + ui.ShowError("Workflow not found", err) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if dir == "" { |
| 49 | + dir, _ = os.Getwd() |
| 50 | + } |
| 51 | + |
| 52 | + fmt.Printf("Running workflow: %s (%d steps)\n", wf.Name, len(wf.Steps)) |
| 53 | + if wf.Description != "" { |
| 54 | + fmt.Printf(" %s\n", wf.Description) |
| 55 | + } |
| 56 | + fmt.Println() |
| 57 | + |
| 58 | + ctx := context.Background() |
| 59 | + run, err := workflow.RunWorkflow(ctx, wf, dir, model) |
| 60 | + if err != nil { |
| 61 | + ui.ShowError("Workflow execution failed", err) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // Print results |
| 66 | + for i, result := range run.Results { |
| 67 | + status := "✓" |
| 68 | + if result.Error != "" { |
| 69 | + status = "✗" |
| 70 | + } |
| 71 | + fmt.Printf(" %s Step %d: %s\n", status, i+1, result.StepName) |
| 72 | + if result.Error != "" { |
| 73 | + fmt.Printf(" Error: %s\n", result.Error) |
| 74 | + } |
| 75 | + if result.Cost > 0 { |
| 76 | + fmt.Printf(" Cost: $%.4f\n", result.Cost) |
| 77 | + } |
| 78 | + fmt.Println() |
| 79 | + } |
| 80 | + |
| 81 | + if run.Status == "completed" { |
| 82 | + ui.ShowSuccess("Workflow completed successfully.") |
| 83 | + } else { |
| 84 | + ui.ShowError(fmt.Sprintf("Workflow %s", run.Status), nil) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// RunWorkflowCreate creates a new workflow template file. |
| 89 | +func RunWorkflowCreate(name string) { |
| 90 | + // Check if already exists |
| 91 | + if _, err := workflow.GetWorkflow(name); err == nil { |
| 92 | + ui.ShowError(fmt.Sprintf("Workflow %q already exists", name), nil) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + wf := &workflow.Workflow{ |
| 97 | + Name: name, |
| 98 | + Description: "Custom workflow", |
| 99 | + Steps: []workflow.Step{ |
| 100 | + { |
| 101 | + Name: "Step 1", |
| 102 | + Prompt: "Describe what this step should do", |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + if err := workflow.SaveWorkflow(wf); err != nil { |
| 108 | + ui.ShowError("Failed to create workflow", err) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Printf("Created workflow template: %s\n", workflow.WorkflowDir()+"/"+name+".yml") |
| 113 | + fmt.Println("Edit the YAML file to customize steps.") |
| 114 | +} |
| 115 | + |
| 116 | +// RunWorkflowDelete removes a workflow. |
| 117 | +func RunWorkflowDelete(name string) { |
| 118 | + if err := workflow.DeleteWorkflow(name); err != nil { |
| 119 | + ui.ShowError("Failed to delete workflow", err) |
| 120 | + return |
| 121 | + } |
| 122 | + ui.ShowSuccess("Deleted workflow: %s", name) |
| 123 | +} |
0 commit comments