@@ -115,16 +115,16 @@ func generate(
115115 return buf .String (), nil
116116}
117117
118- // TODO return correct exit code when -run or -exec fails
119- func runBlock (block * parser.CodeBlock ) error {
118+ // TODO return correct exit code when -run or -exec fails - is this fixed?
119+ func runBlock (block * parser.CodeBlock , tempDir string ) error {
120120 switch block .Lang {
121121 case parser .Go :
122122 code := block .Code
123123 // TODO remove when prompt engineering is there to add "make it runnable"
124124 if ! strings .HasPrefix (block .Code , "package" ) {
125125 code = fmt .Sprintf ("package main\n \n %s" , block .Code )
126126 }
127- path := fmt .Sprintf ("%smain.go" , os . TempDir () )
127+ path := fmt .Sprintf ("%smain.go" , tempDir )
128128 if err := os .WriteFile (path , []byte (code ), 0644 ); err != nil {
129129 return err
130130 }
@@ -136,15 +136,27 @@ func runBlock(block *parser.CodeBlock) error {
136136 return err
137137 }
138138 case parser .HTML :
139- path := fmt .Sprintf ("%sindex.html" , os . TempDir () )
139+ path := fmt .Sprintf ("%sindex.html" , tempDir )
140140 if err := os .WriteFile (path , []byte (block .Code ), 0644 ); err != nil {
141141 return err
142142 }
143143 if err := runCmd ("open" , fmt .Sprintf ("file://%s" , path )); err != nil {
144144 return err
145145 }
146+ case parser .JavaScript :
147+ path := fmt .Sprintf ("%sscript.js" , tempDir ) // TODO can this be parsed from model output? sometimes changes
148+ if err := os .WriteFile (path , []byte (block .Code ), 0644 ); err != nil {
149+ return err
150+ }
151+ // assumed to be part of html, so not executed separately
152+ case parser .CSS :
153+ path := fmt .Sprintf ("%sstyle.css" , tempDir ) // TODO can this be parsed from model output? sometimes changes
154+ if err := os .WriteFile (path , []byte (block .Code ), 0644 ); err != nil {
155+ return err
156+ }
157+ // assumed to be part of html, so not executed separately
146158 case parser .Python :
147- path := fmt .Sprintf ("%smain.py" , os . TempDir () )
159+ path := fmt .Sprintf ("%smain.py" , tempDir )
148160 if err := os .WriteFile (path , []byte (block .Code ), 0644 ); err != nil {
149161 return err
150162 }
@@ -280,8 +292,9 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
280292 codeW , blockCh := parser .NewCode ()
281293 go func () {
282294 defer close (done )
295+ tempDir := os .TempDir ()
283296 for block := range blockCh {
284- _ = runBlock (block )
297+ _ = runBlock (block , tempDir )
285298 }
286299 }()
287300 // no output to the user, we just execute the code
@@ -310,8 +323,9 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
310323 out .Close ()
311324 }
312325 <- done
326+ tempDir := os .TempDir ()
313327 for _ , block := range blocks {
314- if err := runBlock (block ); err != nil {
328+ if err := runBlock (block , tempDir ); err != nil {
315329 return "" , err
316330 }
317331 }
@@ -338,13 +352,14 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
338352 return fmt .Errorf ("failed to open /dev/tty: %w" , err )
339353 }
340354 }
341- if usrMsg == "" && pipeContent == "" {
355+ if usrMsg == "" && pipeContent == "" && ! flagVals . Interactive {
342356 return fmt .Errorf ("what's your command?" )
343357 }
344358 if pipeContent != "" {
345359 usrMsg = fmt .Sprintf (CONTEXT_TEMPLATE , pipeContent , usrMsg )
346360 }
347361
362+ // TODO require -f flag for file input, use it for image too
348363 if _ , err := os .Stat (usrMsg ); err == nil {
349364 f , err := os .Open (usrMsg )
350365 if err != nil {
0 commit comments