@@ -14,6 +14,21 @@ type Opts struct {
14
14
DisableCache bool
15
15
CacheDir string
16
16
Quiet bool
17
+ Chdir string
18
+ }
19
+
20
+ func (o Opts ) toArgs () []string {
21
+ var args []string
22
+ if o .DisableCache {
23
+ args = append (args , "--disable-cache" )
24
+ }
25
+ if o .CacheDir != "" {
26
+ args = append (args , "--cache-dir=" + o .CacheDir )
27
+ }
28
+ if o .Chdir != "" {
29
+ args = append (args , "--chdir=" + o .Chdir )
30
+ }
31
+ return append (args , "--quiet=" + fmt .Sprint (o .Quiet ))
17
32
}
18
33
19
34
// ListTools will list all the available tools.
@@ -33,7 +48,7 @@ func ListModels(ctx context.Context) ([]string, error) {
33
48
34
49
// ExecTool will execute a tool. The tool must be a fmt.Stringer, and the string should be a valid gptscript file.
35
50
func ExecTool (ctx context.Context , opts Opts , tools ... fmt.Stringer ) (string , error ) {
36
- c := exec .CommandContext (ctx , getCommand (), append (toArgs (opts ), "-" )... )
51
+ c := exec .CommandContext (ctx , getCommand (), append (opts . toArgs (), "-" )... )
37
52
c .Stdin = strings .NewReader (concatTools (tools ))
38
53
out , err := c .CombinedOutput ()
39
54
return string (out ), err
@@ -43,7 +58,7 @@ func ExecTool(ctx context.Context, opts Opts, tools ...fmt.Stringer) (string, er
43
58
// This returns two io.ReadClosers, one for stdout and one for stderr, and a function to wait for the process to exit.
44
59
// Reading from stdOut and stdErr should be completed before calling the wait function.
45
60
func StreamExecTool (ctx context.Context , opts Opts , tools ... fmt.Stringer ) (io.Reader , io.Reader , func () error ) {
46
- c , stdout , stderr , err := setupForkCommand (ctx , "" , append (toArgs (opts ), "-" ))
61
+ c , stdout , stderr , err := setupForkCommand (ctx , "" , append (opts . toArgs (), "-" ))
47
62
if err != nil {
48
63
return stdout , stderr , func () error { return err }
49
64
}
@@ -68,7 +83,7 @@ func StreamExecToolWithEvents(ctx context.Context, opts Opts, tools ...fmt.Strin
68
83
// Close the parent pipe after starting the child process
69
84
defer eventsWrite .Close ()
70
85
71
- c , stdout , stderr , err := setupForkCommand (ctx , "" , append (toArgs (opts ), "-" ))
86
+ c , stdout , stderr , err := setupForkCommand (ctx , "" , append (opts . toArgs (), "-" ))
72
87
if err != nil {
73
88
_ = eventsRead .Close ()
74
89
return stdout , stderr , new (reader ), func () error { return err }
@@ -95,7 +110,7 @@ func StreamExecToolWithEvents(ctx context.Context, opts Opts, tools ...fmt.Strin
95
110
// The file at the path should be a valid gptscript file.
96
111
// The input should be command line arguments in the form of a string (i.e. "--arg1 value1 --arg2 value2").
97
112
func ExecFile (ctx context.Context , toolPath , input string , opts Opts ) (string , error ) {
98
- args := append (toArgs (opts ), toolPath )
113
+ args := append (opts . toArgs (), toolPath )
99
114
if input != "" {
100
115
args = append (args , input )
101
116
}
@@ -110,7 +125,7 @@ func ExecFile(ctx context.Context, toolPath, input string, opts Opts) (string, e
110
125
// This returns two io.ReadClosers, one for stdout and one for stderr, and a function to wait for the process to exit.
111
126
// Reading from stdOut and stdErr should be completed before calling the wait function.
112
127
func StreamExecFile (ctx context.Context , toolPath , input string , opts Opts ) (io.Reader , io.Reader , func () error ) {
113
- args := append (toArgs (opts ), toolPath )
128
+ args := append (opts . toArgs (), toolPath )
114
129
c , stdout , stderr , err := setupForkCommand (ctx , input , args )
115
130
if err != nil {
116
131
return stdout , stderr , func () error { return err }
@@ -136,7 +151,7 @@ func StreamExecFileWithEvents(ctx context.Context, toolPath, input string, opts
136
151
// Close the parent pipe after starting the child process
137
152
defer eventsWrite .Close ()
138
153
139
- args := append (toArgs (opts ), toolPath )
154
+ args := append (opts . toArgs (), toolPath )
140
155
141
156
c , stdout , stderr , err := setupForkCommand (ctx , input , args )
142
157
if err != nil {
@@ -160,18 +175,6 @@ func StreamExecFileWithEvents(ctx context.Context, toolPath, input string, opts
160
175
return stdout , stderr , eventsRead , wait
161
176
}
162
177
163
- func toArgs (opts Opts ) []string {
164
- var args []string
165
- if opts .DisableCache {
166
- args = append (args , "--disable-cache" )
167
- }
168
- if opts .CacheDir != "" {
169
- args = append (args , "--cache-dir=" + opts .CacheDir )
170
- }
171
- args = append (args , "--quiet=" + fmt .Sprint (opts .Quiet ))
172
- return args
173
- }
174
-
175
178
func concatTools (tools []fmt.Stringer ) string {
176
179
var sb strings.Builder
177
180
for i , tool := range tools {
0 commit comments