@@ -12,6 +12,7 @@ import (
1212 "net/http"
1313 "os"
1414 "path/filepath"
15+ "reflect"
1516 "strconv"
1617 "strings"
1718
@@ -29,20 +30,24 @@ import (
2930)
3031
3132type AppConfig struct {
32- Name string
33- Model string
34- ChooseAIModel bool
35- ChoosePlugin bool
36- ChooseWorkflow bool
37- Temperature string
38- Role string
39- Raw bool
40- Version bool
41- WorkflowPath string
33+ Name string
34+ Model string
35+ ChooseAIModel bool
36+ ChoosePlugin bool
37+ ChooseWorkflow bool
38+ Temperature string
39+ Role string
40+ Raw bool
41+ Version bool
42+ WorkflowPath string
43+ IteratorPrompt bool
44+ CurrentIterationValue interface {}
4245}
4346
4447type Tasks struct {
45- Tasks []Task `yaml:"tasks"`
48+ IterationValuesIn string `yaml:"iterator_script"`
49+ IterationValues []interface {}
50+ Tasks []Task `yaml:"tasks"`
4651}
4752
4853type Task struct {
@@ -58,7 +63,7 @@ type Task struct {
5863
5964const (
6065 configFileName = "config.yaml"
61- version = "0.3.1 "
66+ version = "0.4.0 "
6267)
6368
6469var (
@@ -196,6 +201,7 @@ func initializeFlags() {
196201 flags .BoolVarP (& appCfg .Version , "version" , "v" , false , "Print the version" )
197202 flags .StringVarP (& appCfg .WorkflowPath , "workflow" , "w" , "" , "The path to a workflow file" )
198203 flags .BoolVarP (& appCfg .ChooseWorkflow , "choose-workflow" , "W" , false , "Choose a workflow to run" )
204+ flags .BoolVarP (& appCfg .IteratorPrompt , "iterator" , "i" , false , "String array of prompts ['prompt1', 'prompt2']" )
199205 flags .SortFlags = false
200206}
201207
@@ -391,6 +397,7 @@ func runExpr(input string, expression string) (string, error) {
391397 "ReadFile" : readfile ,
392398 "Extism" : callExtismPlugin ,
393399 "Resend" : resend ,
400+ "iterValue" : appCfg .CurrentIterationValue ,
394401 }
395402
396403 program , err := expr .Compile (expression , expr .Env (env ))
@@ -464,32 +471,57 @@ func handleTasks(prompt string) error {
464471 return err
465472 }
466473
467- if len (tasks .Tasks ) > 0 {
468- if prompt != "" {
469- tasks .Tasks [0 ].Prompt = prompt + " " + tasks .Tasks [0 ].Prompt
474+ if tasks .IterationValuesIn == "" {
475+ tasks .IterationValues = []interface {}{nil }
476+ } else {
477+ env := map [string ]interface {}{
478+ "input" : prompt ,
479+ "Get" : httpGet ,
480+ "AppendFile" : appendFile ,
481+ "ReadFile" : readfile ,
482+ "Extism" : callExtismPlugin ,
483+ "Resend" : resend ,
470484 }
471- }
472485
473- ctx , cancel := context .WithCancel (context .Background ())
474- var res string
475- action := func () {
476- res , err = generateResponseForTasks (tasks )
486+ program , err := expr .Compile (tasks .IterationValuesIn , expr .Env (env ), expr .AsKind (reflect .Slice ))
477487 if err != nil {
478- cancel ()
479- fmt .Println (err )
480- os .Exit (1 )
488+ return err
481489 }
482- cancel ()
483- }
484490
485- go action ()
486- _ = spinner .New ().Title ("Generating..." ).TitleStyle (lipgloss .NewStyle ().Faint (true )).Context (ctx ).Run ()
491+ output , err := expr .Run (program , env )
492+ if err != nil {
493+ return err
494+ }
487495
488- if err != nil {
489- return err
496+ tasks .IterationValues = output .([]interface {})
490497 }
491498
492- fmt .Print (res )
499+ for i := range tasks .IterationValues {
500+ appCfg .CurrentIterationValue = tasks .IterationValues [i ]
501+
502+ if len (tasks .Tasks ) > 0 {
503+ if prompt != "" {
504+ tasks .Tasks [0 ].Prompt = prompt + " " + tasks .Tasks [0 ].Prompt
505+ }
506+ }
507+
508+ var res string
509+ action := func (tasks Tasks ) {
510+ res , err = generateResponseForTasks (tasks )
511+ if err != nil {
512+ fmt .Println (err )
513+ os .Exit (1 )
514+ }
515+ }
516+
517+ _ = spinner .New ().
518+ Title ("Generating..." ).
519+ TitleStyle (lipgloss .NewStyle ().Faint (true )).
520+ Action (func () { action (tasks ) }).
521+ Run ()
522+
523+ fmt .Print (res )
524+ }
493525 return nil
494526}
495527
@@ -571,24 +603,52 @@ func runCommand(cmd *cobra.Command, args []string) error {
571603 return err
572604 }
573605 }
574-
575- prompt := generatePrompt (args , appCfg .Raw )
576-
577- ctx , cancel := context .WithCancel (context .Background ())
578606 var res string
579- action := func () {
607+ action := func (prompt string ) {
580608 res , err = pluginCfg .generateResponse (prompt , appCfg .Raw )
581- cancel ()
582609 }
583610
584- go action ()
585- _ = spinner .New ().Title ("Generating..." ).TitleStyle (lipgloss .NewStyle ().Faint (true )).Context (ctx ).Run ()
611+ var prompt string
612+ if appCfg .IteratorPrompt {
613+ if len (args ) > 0 {
614+ prompt = args [0 ]
615+ } else {
616+ huh .NewInput ().
617+ Title ("Enter the prompts in brackets separated by commas:" ).
618+ Value (& prompt ).
619+ Placeholder ("[prompt1, prompt2]" ).
620+ WithTheme (huh .ThemeCharm ()).
621+ Run ()
622+ }
623+ var prompts []string
624+ ps := strings .Trim (prompt , "[]" )
625+ prompts = strings .Split (ps , "," )
626+ for _ , p := range prompts {
627+ _ = spinner .New ().
628+ Title ("Generating..." ).
629+ TitleStyle (lipgloss .NewStyle ().Faint (true )).
630+ Action (func () { action (p ) }).
631+ Run ()
632+
633+ fmt .Println (res )
634+ }
635+ } else {
636+ prompt = generatePrompt (args , appCfg .Raw )
637+
638+ _ = spinner .New ().
639+ Title ("Generating..." ).
640+ TitleStyle (lipgloss .NewStyle ().Faint (true )).
641+ Action (func () { action (prompt ) }).
642+ Run ()
643+ }
586644
587645 if err != nil {
588646 return err
589647 }
590648
591- fmt .Print (res )
649+ if ! appCfg .IteratorPrompt {
650+ fmt .Print (res )
651+ }
592652 return nil
593653}
594654
0 commit comments