Skip to content

Commit 36eec32

Browse files
committed
feat: detect not found errors in workspace API
This will allow for proper error handling in the workspace API. Signed-off-by: Donnie Adams <[email protected]>
1 parent cd749df commit 36eec32

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

gptscript_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestMain(m *testing.M) {
3030
panic(fmt.Sprintf("error creating gptscript: %s", err))
3131
}
3232

33-
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY")})
33+
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY"), WorkspaceTool: "/Users/thedadams/code/workspace-provider"})
3434
if err != nil {
3535
gFirst.Close()
3636
panic(fmt.Sprintf("error creating gptscript: %s", err))

workspace.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@ package gptscript
22

33
import (
44
"context"
5+
"encoding/base64"
6+
"encoding/json"
7+
"fmt"
58
"os"
69
"strings"
710
)
811

12+
type NotFoundInWorkspaceError struct {
13+
id string
14+
name string
15+
}
16+
17+
func (e *NotFoundInWorkspaceError) Error() string {
18+
return fmt.Sprintf("not found: %s/%s", e.id, e.name)
19+
}
20+
21+
func newNotFoundInWorkspaceError(id, name string) *NotFoundInWorkspaceError {
22+
return &NotFoundInWorkspaceError{id: id, name: name}
23+
}
24+
925
func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fromWorkspaces ...string) (string, error) {
1026
out, err := g.runBasicCommand(ctx, "workspaces/create", map[string]any{
1127
"providerType": providerType,
@@ -75,7 +91,13 @@ func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, opts ...ListFilesI
7591
return nil, err
7692
}
7793

78-
return strings.Split(strings.TrimSpace(out), "\n"), nil
94+
out = strings.TrimSpace(out)
95+
if len(out) == 0 {
96+
return nil, nil
97+
}
98+
99+
var files []string
100+
return files, json.Unmarshal([]byte(out), &files)
79101
}
80102

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

127149
_, err := g.runBasicCommand(ctx, "workspaces/write-file", map[string]any{
128150
"id": opt.WorkspaceID,
129-
"contents": contents,
151+
"contents": base64.StdEncoding.EncodeToString(contents),
130152
"filePath": filePath,
131153
"workspaceTool": g.globalOpts.WorkspaceTool,
132154
"env": g.globalOpts.Env,
@@ -158,6 +180,10 @@ func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, filePath string,
158180
"env": g.globalOpts.Env,
159181
})
160182

183+
if err != nil && strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
184+
return newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
185+
}
186+
161187
return err
162188
}
163189

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

190-
return []byte(out), nil
219+
return base64.StdEncoding.DecodeString(out)
191220
}

workspace_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gptscript
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"os"
78
"testing"
89
)
@@ -46,6 +47,12 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
4647
t.Errorf("Unexpected content: %s", content)
4748
}
4849

50+
// Ensure we get the error we expect when trying to read a non-existent file
51+
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
52+
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
53+
t.Errorf("Unexpected error reading non-existent file: %v", err)
54+
}
55+
4956
err = g.DeleteFileInWorkspace(context.Background(), "test.txt", DeleteFileInWorkspaceOptions{WorkspaceID: id})
5057
if err != nil {
5158
t.Errorf("Error deleting file: %v", err)
@@ -169,6 +176,12 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
169176
t.Errorf("Unexpected content: %s", content)
170177
}
171178

179+
// Ensure we get the error we expect when trying to read a non-existent file
180+
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
181+
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
182+
t.Errorf("Unexpected error reading non-existent file: %v", err)
183+
}
184+
172185
err = g.DeleteFileInWorkspace(context.Background(), "test.txt", DeleteFileInWorkspaceOptions{WorkspaceID: id})
173186
if err != nil {
174187
t.Errorf("Error deleting file: %v", err)

0 commit comments

Comments
 (0)