Skip to content

fix: update SDK to be consistent with the other SDKs #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 67 additions & 4 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (o Opts) toArgs() []string {
return append(args, "--quiet="+fmt.Sprint(o.Quiet))
}

// Version will return the output of `gptscript --version`
func Version(ctx context.Context) (string, error) {
out, err := exec.CommandContext(ctx, getCommand(), "--version").CombinedOutput()
return string(out), err
}

// ListTools will list all the available tools.
func ListTools(ctx context.Context) (string, error) {
out, err := exec.CommandContext(ctx, getCommand(), "--list-tools").CombinedOutput()
Expand All @@ -50,8 +56,36 @@ func ListModels(ctx context.Context) ([]string, error) {
func ExecTool(ctx context.Context, opts Opts, tools ...fmt.Stringer) (string, error) {
c := exec.CommandContext(ctx, getCommand(), append(opts.toArgs(), "-")...)
c.Stdin = strings.NewReader(concatTools(tools))
out, err := c.CombinedOutput()
return string(out), err

stdout, err := c.StdoutPipe()
if err != nil {
return "", fmt.Errorf("failed to get stdout pipe: %w", err)
}

stderr, err := c.StderrPipe()
if err != nil {
return "", fmt.Errorf("failed to get stderr pipe: %w", err)
}

if err = c.Start(); err != nil {
return "", fmt.Errorf("failed to start command: %w", err)
}

stdErr, err := io.ReadAll(stderr)
if err != nil {
return "", fmt.Errorf("failed to read stderr: %w", err)
}

stdOut, err := io.ReadAll(stdout)
if err != nil {
return "", fmt.Errorf("failed to read stdout: %w", err)
}

if err = c.Wait(); err != nil {
return "", fmt.Errorf("failed to wait for command, stderr: %s: %w", stdErr, err)
}

return string(stdOut), err
}

// StreamExecTool will execute a tool. The tool must be a fmt.Stringer, and the string should be a valid gptscript file.
Expand Down Expand Up @@ -115,8 +149,37 @@ func ExecFile(ctx context.Context, toolPath, input string, opts Opts) (string, e
args = append(args, input)
}

out, err := exec.CommandContext(ctx, getCommand(), args...).CombinedOutput()
return string(out), err
c := exec.CommandContext(ctx, getCommand(), args...)

stdout, err := c.StdoutPipe()
if err != nil {
return "", fmt.Errorf("failed to get stdout pipe: %w", err)
}

stderr, err := c.StderrPipe()
if err != nil {
return "", fmt.Errorf("failed to get stderr pipe: %w", err)
}

if err = c.Start(); err != nil {
return "", fmt.Errorf("failed to start command: %w", err)
}

stdErr, err := io.ReadAll(stderr)
if err != nil {
return "", fmt.Errorf("failed to read stderr: %w", err)
}

stdOut, err := io.ReadAll(stdout)
if err != nil {
return "", fmt.Errorf("failed to read stdout: %w", err)
}

if err = c.Wait(); err != nil {
return "", fmt.Errorf("failed to wait for command, stderr: %s: %w", stdErr, err)
}

return string(stdOut), err
}

// StreamExecFile will execute the file at the given path with the given input.
Expand Down
15 changes: 13 additions & 2 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func TestVersion(t *testing.T) {
out, err := Version(context.Background())
if err != nil {
t.Errorf("Error getting version: %v", err)
}

if !strings.HasPrefix(out, "gptscript version") {
t.Errorf("Unexpected output: %s", out)
}
}

func TestListTools(t *testing.T) {
tools, err := ListTools(context.Background())
if err != nil {
Expand Down Expand Up @@ -101,7 +112,7 @@ func TestExecWithToolList(t *testing.T) {
tools := []fmt.Stringer{
&Tool{
Tools: []string{"echo"},
Instructions: "echo hello times",
Instructions: "echo hello there",
},
&Tool{
Name: "echo",
Expand All @@ -119,7 +130,7 @@ func TestExecWithToolList(t *testing.T) {
t.Errorf("Error executing tool: %v", err)
}

if !strings.Contains(out, "hello times") {
if !strings.Contains(out, "hello there") {
t.Errorf("Unexpected output: %s", out)
}
}
Expand Down
Loading