Skip to content

Commit

Permalink
feat: detect not found errors in workspace API
Browse files Browse the repository at this point in the history
This will allow for proper error handling in the workspace API.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Oct 23, 2024
1 parent cd749df commit 36eec32
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gptscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestMain(m *testing.M) {
panic(fmt.Sprintf("error creating gptscript: %s", err))
}

g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY")})
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY"), WorkspaceTool: "/Users/thedadams/code/workspace-provider"})
if err != nil {
gFirst.Close()
panic(fmt.Sprintf("error creating gptscript: %s", err))
Expand Down
35 changes: 32 additions & 3 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ package gptscript

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
)

type NotFoundInWorkspaceError struct {
id string
name string
}

func (e *NotFoundInWorkspaceError) Error() string {
return fmt.Sprintf("not found: %s/%s", e.id, e.name)
}

func newNotFoundInWorkspaceError(id, name string) *NotFoundInWorkspaceError {
return &NotFoundInWorkspaceError{id: id, name: name}
}

func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fromWorkspaces ...string) (string, error) {
out, err := g.runBasicCommand(ctx, "workspaces/create", map[string]any{
"providerType": providerType,
Expand Down Expand Up @@ -75,7 +91,13 @@ func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, opts ...ListFilesI
return nil, err
}

return strings.Split(strings.TrimSpace(out), "\n"), nil
out = strings.TrimSpace(out)
if len(out) == 0 {
return nil, nil
}

var files []string
return files, json.Unmarshal([]byte(out), &files)
}

type RemoveAllOptions struct {
Expand Down Expand Up @@ -126,7 +148,7 @@ func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, filePath string, c

_, err := g.runBasicCommand(ctx, "workspaces/write-file", map[string]any{
"id": opt.WorkspaceID,
"contents": contents,
"contents": base64.StdEncoding.EncodeToString(contents),
"filePath": filePath,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
Expand Down Expand Up @@ -158,6 +180,10 @@ func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, filePath string,
"env": g.globalOpts.Env,
})

if err != nil && strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
return newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
}

return err
}

Expand All @@ -184,8 +210,11 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, op
"env": g.globalOpts.Env,
})
if err != nil {
if strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
return nil, newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
}
return nil, err
}

return []byte(out), nil
return base64.StdEncoding.DecodeString(out)
}
13 changes: 13 additions & 0 deletions workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gptscript
import (
"bytes"
"context"
"errors"
"os"
"testing"
)
Expand Down Expand Up @@ -46,6 +47,12 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
t.Errorf("Unexpected content: %s", content)
}

// Ensure we get the error we expect when trying to read a non-existent file
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
t.Errorf("Unexpected error reading non-existent file: %v", err)
}

err = g.DeleteFileInWorkspace(context.Background(), "test.txt", DeleteFileInWorkspaceOptions{WorkspaceID: id})
if err != nil {
t.Errorf("Error deleting file: %v", err)
Expand Down Expand Up @@ -169,6 +176,12 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
t.Errorf("Unexpected content: %s", content)
}

// Ensure we get the error we expect when trying to read a non-existent file
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
t.Errorf("Unexpected error reading non-existent file: %v", err)
}

err = g.DeleteFileInWorkspace(context.Background(), "test.txt", DeleteFileInWorkspaceOptions{WorkspaceID: id})
if err != nil {
t.Errorf("Error deleting file: %v", err)
Expand Down

0 comments on commit 36eec32

Please sign in to comment.