Skip to content

Commit 9fe6fd0

Browse files
committed
fix: update SDK to be consistent with the other SDKs
The Go SDK handled stdout and stderr differently from the other SDKs. This change bring the Go SDK into sync. Signed-off-by: Donnie Adams <[email protected]>
1 parent 4faf337 commit 9fe6fd0

File tree

2 files changed

+80
-6
lines changed

2 files changed

+80
-6
lines changed

exec.go

+67-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func (o Opts) toArgs() []string {
3131
return append(args, "--quiet="+fmt.Sprint(o.Quiet))
3232
}
3333

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+
3440
// ListTools will list all the available tools.
3541
func ListTools(ctx context.Context) (string, error) {
3642
out, err := exec.CommandContext(ctx, getCommand(), "--list-tools").CombinedOutput()
@@ -50,8 +56,36 @@ func ListModels(ctx context.Context) ([]string, error) {
5056
func ExecTool(ctx context.Context, opts Opts, tools ...fmt.Stringer) (string, error) {
5157
c := exec.CommandContext(ctx, getCommand(), append(opts.toArgs(), "-")...)
5258
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
5589
}
5690

5791
// 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
115149
args = append(args, input)
116150
}
117151

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
120183
}
121184

122185
// StreamExecFile will execute the file at the given path with the given input.

exec_test.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ func TestMain(m *testing.M) {
1818
os.Exit(m.Run())
1919
}
2020

21+
func TestVersion(t *testing.T) {
22+
out, err := Version(context.Background())
23+
if err != nil {
24+
t.Errorf("Error getting version: %v", err)
25+
}
26+
27+
if !strings.HasPrefix(out, "gptscript version") {
28+
t.Errorf("Unexpected output: %s", out)
29+
}
30+
}
31+
2132
func TestListTools(t *testing.T) {
2233
tools, err := ListTools(context.Background())
2334
if err != nil {
@@ -101,7 +112,7 @@ func TestExecWithToolList(t *testing.T) {
101112
tools := []fmt.Stringer{
102113
&Tool{
103114
Tools: []string{"echo"},
104-
Instructions: "echo hello times",
115+
Instructions: "echo hello there",
105116
},
106117
&Tool{
107118
Name: "echo",
@@ -119,7 +130,7 @@ func TestExecWithToolList(t *testing.T) {
119130
t.Errorf("Error executing tool: %v", err)
120131
}
121132

122-
if !strings.Contains(out, "hello times") {
133+
if !strings.Contains(out, "hello there") {
123134
t.Errorf("Unexpected output: %s", out)
124135
}
125136
}

0 commit comments

Comments
 (0)