@@ -16,6 +16,7 @@ import (
16
16
"strings"
17
17
"sync"
18
18
19
+ "github.com/google/shlex"
19
20
"github.com/gptscript-ai/gptscript/pkg/counter"
20
21
"github.com/gptscript-ai/gptscript/pkg/env"
21
22
"github.com/gptscript-ai/gptscript/pkg/types"
@@ -119,7 +120,7 @@ func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCate
119
120
var extraEnv = []string {
120
121
strings .TrimSpace ("GPTSCRIPT_CONTEXT=" + strings .Join (instructions , "\n " )),
121
122
}
122
- cmd , stop , err := e .newCommand (ctx .Ctx , extraEnv , tool , input )
123
+ cmd , stop , err := e .newCommand (ctx .Ctx , extraEnv , tool , input , true )
123
124
if err != nil {
124
125
if toolCategory == NoCategory {
125
126
return fmt .Sprintf ("ERROR: got (%v) while parsing command" , err ), nil
@@ -242,7 +243,7 @@ func appendInputAsEnv(env []string, input string) []string {
242
243
return env
243
244
}
244
245
245
- func (e * Engine ) newCommand (ctx context.Context , extraEnv []string , tool types.Tool , input string ) (* exec.Cmd , func (), error ) {
246
+ func (e * Engine ) newCommand (ctx context.Context , extraEnv []string , tool types.Tool , input string , useShell bool ) (* exec.Cmd , func (), error ) {
246
247
envvars := append (e .Env [:], extraEnv ... )
247
248
envvars = appendInputAsEnv (envvars , input )
248
249
if log .IsDebug () {
@@ -252,9 +253,20 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
252
253
interpreter , rest , _ := strings .Cut (tool .Instructions , "\n " )
253
254
interpreter = strings .TrimSpace (interpreter )[2 :]
254
255
255
- args := strings .Fields (interpreter )
256
+ var (
257
+ args []string
258
+ err error
259
+ )
260
+ if useShell {
261
+ args = strings .Fields (interpreter )
262
+ } else {
263
+ args , err = shlex .Split (interpreter )
264
+ if err != nil {
265
+ return nil , nil , err
266
+ }
267
+ }
256
268
257
- envvars , err : = e .getRuntimeEnv (ctx , tool , args , envvars )
269
+ envvars , err = e .getRuntimeEnv (ctx , tool , args , envvars )
258
270
if err != nil {
259
271
return nil , nil , err
260
272
}
@@ -297,6 +309,9 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
297
309
if strings .HasPrefix (s , "!" ) {
298
310
return envMap [s [1 :]]
299
311
}
312
+ if ! useShell {
313
+ return envMap [s ]
314
+ }
300
315
if runtime .GOOS == "windows" {
301
316
return "%" + s + "%"
302
317
}
@@ -306,9 +321,14 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
306
321
307
322
if runtime .GOOS == "windows" {
308
323
args [0 ] = strings .ReplaceAll (args [0 ], "/" , "\\ " )
309
- args = append ([]string {"cmd.exe" , "/c" }, strings .Join (args , " " ))
310
- } else {
311
- args = append ([]string {"/bin/sh" , "-c" }, strings .Join (args , " " ))
324
+ }
325
+
326
+ if useShell {
327
+ if runtime .GOOS == "windows" {
328
+ args = append ([]string {"cmd.exe" , "/c" }, strings .Join (args , " " ))
329
+ } else {
330
+ args = append ([]string {"/bin/sh" , "-c" }, strings .Join (args , " " ))
331
+ }
312
332
}
313
333
314
334
cmd := exec .CommandContext (ctx , args [0 ], args [1 :]... )
0 commit comments