@@ -31,6 +31,12 @@ func (o Opts) toArgs() []string {
31
31
return append (args , "--quiet=" + fmt .Sprint (o .Quiet ))
32
32
}
33
33
34
+ // Version will return the output of `gptscript --version`
35
+ func Version (ctx context.Context ) (string , error ) {
36
+ out , err := exec .CommandContext (ctx , getCommand (), "--version" ).CombinedOutput ()
37
+ return string (out ), err
38
+ }
39
+
34
40
// ListTools will list all the available tools.
35
41
func ListTools (ctx context.Context ) (string , error ) {
36
42
out , err := exec .CommandContext (ctx , getCommand (), "--list-tools" ).CombinedOutput ()
@@ -50,8 +56,36 @@ func ListModels(ctx context.Context) ([]string, error) {
50
56
func ExecTool (ctx context.Context , opts Opts , tools ... fmt.Stringer ) (string , error ) {
51
57
c := exec .CommandContext (ctx , getCommand (), append (opts .toArgs (), "-" )... )
52
58
c .Stdin = strings .NewReader (concatTools (tools ))
53
- out , err := c .CombinedOutput ()
54
- return string (out ), err
59
+
60
+ stdout , err := c .StdoutPipe ()
61
+ if err != nil {
62
+ return "" , fmt .Errorf ("failed to get stdout pipe: %w" , err )
63
+ }
64
+
65
+ stderr , err := c .StderrPipe ()
66
+ if err != nil {
67
+ return "" , fmt .Errorf ("failed to get stderr pipe: %w" , err )
68
+ }
69
+
70
+ if err = c .Start (); err != nil {
71
+ return "" , fmt .Errorf ("failed to start command: %w" , err )
72
+ }
73
+
74
+ stdErr , err := io .ReadAll (stderr )
75
+ if err != nil {
76
+ return "" , fmt .Errorf ("failed to read stderr: %w" , err )
77
+ }
78
+
79
+ stdOut , err := io .ReadAll (stdout )
80
+ if err != nil {
81
+ return "" , fmt .Errorf ("failed to read stdout: %w" , err )
82
+ }
83
+
84
+ if err = c .Wait (); err != nil {
85
+ return "" , fmt .Errorf ("failed to wait for command, stderr: %s: %w" , stdErr , err )
86
+ }
87
+
88
+ return string (stdOut ), err
55
89
}
56
90
57
91
// StreamExecTool will execute a tool. The tool must be a fmt.Stringer, and the string should be a valid gptscript file.
@@ -115,8 +149,37 @@ func ExecFile(ctx context.Context, toolPath, input string, opts Opts) (string, e
115
149
args = append (args , input )
116
150
}
117
151
118
- out , err := exec .CommandContext (ctx , getCommand (), args ... ).CombinedOutput ()
119
- return string (out ), err
152
+ c := exec .CommandContext (ctx , getCommand (), args ... )
153
+
154
+ stdout , err := c .StdoutPipe ()
155
+ if err != nil {
156
+ return "" , fmt .Errorf ("failed to get stdout pipe: %w" , err )
157
+ }
158
+
159
+ stderr , err := c .StderrPipe ()
160
+ if err != nil {
161
+ return "" , fmt .Errorf ("failed to get stderr pipe: %w" , err )
162
+ }
163
+
164
+ if err = c .Start (); err != nil {
165
+ return "" , fmt .Errorf ("failed to start command: %w" , err )
166
+ }
167
+
168
+ stdErr , err := io .ReadAll (stderr )
169
+ if err != nil {
170
+ return "" , fmt .Errorf ("failed to read stderr: %w" , err )
171
+ }
172
+
173
+ stdOut , err := io .ReadAll (stdout )
174
+ if err != nil {
175
+ return "" , fmt .Errorf ("failed to read stdout: %w" , err )
176
+ }
177
+
178
+ if err = c .Wait (); err != nil {
179
+ return "" , fmt .Errorf ("failed to wait for command, stderr: %s: %w" , stdErr , err )
180
+ }
181
+
182
+ return string (stdOut ), err
120
183
}
121
184
122
185
// StreamExecFile will execute the file at the given path with the given input.
0 commit comments