Skip to content

Commit 3901872

Browse files
committed
feat: make the workspace ID optional in the workspace API
If the workspace ID is not specified, then it will be read from the GPTSCRIPT_WORKSPACE_ID environment variable. Signed-off-by: Donnie Adams <donnie@acorn.io>
1 parent d72fa05 commit 3901872

File tree

2 files changed

+130
-42
lines changed

2 files changed

+130
-42
lines changed

workspace.go

+102-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gptscript
22

33
import (
44
"context"
5+
"os"
56
"strings"
67
)
78

@@ -19,9 +20,24 @@ func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fr
1920
return strings.TrimSpace(out), nil
2021
}
2122

22-
func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string) error {
23+
type DeleteWorkspaceOptions struct {
24+
WorkspaceID string
25+
}
26+
27+
func (g *GPTScript) DeleteWorkspace(ctx context.Context, opts ...DeleteWorkspaceOptions) error {
28+
var opt DeleteWorkspaceOptions
29+
for _, o := range opts {
30+
if o.WorkspaceID != "" {
31+
opt.WorkspaceID = o.WorkspaceID
32+
}
33+
}
34+
35+
if opt.WorkspaceID == "" {
36+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
37+
}
38+
2339
_, err := g.runBasicCommand(ctx, "workspaces/delete", map[string]any{
24-
"id": workspaceID,
40+
"id": opt.WorkspaceID,
2541
"workspaceTool": g.globalOpts.WorkspaceTool,
2642
"env": g.globalOpts.Env,
2743
})
@@ -30,19 +46,27 @@ func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string) err
3046
}
3147

3248
type ListFilesInWorkspaceOptions struct {
33-
Prefix string
49+
WorkspaceID string
50+
Prefix string
3451
}
3552

36-
func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, workspaceID string, opts ...ListFilesInWorkspaceOptions) ([]string, error) {
53+
func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, opts ...ListFilesInWorkspaceOptions) ([]string, error) {
3754
var opt ListFilesInWorkspaceOptions
3855
for _, o := range opts {
3956
if o.Prefix != "" {
4057
opt.Prefix = o.Prefix
4158
}
59+
if o.WorkspaceID != "" {
60+
opt.WorkspaceID = o.WorkspaceID
61+
}
62+
}
63+
64+
if opt.WorkspaceID == "" {
65+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
4266
}
4367

4468
out, err := g.runBasicCommand(ctx, "workspaces/list", map[string]any{
45-
"id": workspaceID,
69+
"id": opt.WorkspaceID,
4670
"prefix": opt.Prefix,
4771
"workspaceTool": g.globalOpts.WorkspaceTool,
4872
"env": g.globalOpts.Env,
@@ -54,20 +78,54 @@ func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, workspaceID string
5478
return strings.Split(strings.TrimSpace(out), "\n"), nil
5579
}
5680

57-
func (g *GPTScript) RemoveAllWithPrefix(ctx context.Context, workspaceID, prefix string) error {
81+
type RemoveAllOptions struct {
82+
WorkspaceID string
83+
WithPrefix string
84+
}
85+
86+
func (g *GPTScript) RemoveAll(ctx context.Context, opts ...RemoveAllOptions) error {
87+
var opt RemoveAllOptions
88+
for _, o := range opts {
89+
if o.WithPrefix != "" {
90+
opt.WithPrefix = o.WithPrefix
91+
}
92+
if o.WorkspaceID != "" {
93+
opt.WorkspaceID = o.WorkspaceID
94+
}
95+
}
96+
97+
if opt.WorkspaceID == "" {
98+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
99+
}
100+
58101
_, err := g.runBasicCommand(ctx, "workspaces/remove-all-with-prefix", map[string]any{
59-
"id": workspaceID,
60-
"prefix": prefix,
102+
"id": opt.WorkspaceID,
103+
"prefix": opt.WithPrefix,
61104
"workspaceTool": g.globalOpts.WorkspaceTool,
62105
"env": g.globalOpts.Env,
63106
})
64107

65108
return err
66109
}
67110

68-
func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, workspaceID, filePath string, contents []byte) error {
111+
type WriteFileInWorkspaceOptions struct {
112+
WorkspaceID string
113+
}
114+
115+
func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, filePath string, contents []byte, opts ...WriteFileInWorkspaceOptions) error {
116+
var opt WriteFileInWorkspaceOptions
117+
for _, o := range opts {
118+
if o.WorkspaceID != "" {
119+
opt.WorkspaceID = o.WorkspaceID
120+
}
121+
}
122+
123+
if opt.WorkspaceID == "" {
124+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
125+
}
126+
69127
_, err := g.runBasicCommand(ctx, "workspaces/write-file", map[string]any{
70-
"id": workspaceID,
128+
"id": opt.WorkspaceID,
71129
"contents": contents,
72130
"filePath": filePath,
73131
"workspaceTool": g.globalOpts.WorkspaceTool,
@@ -77,9 +135,24 @@ func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, workspaceID, fileP
77135
return err
78136
}
79137

80-
func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, workspaceID, filePath string) error {
138+
type DeleteFileInWorkspaceOptions struct {
139+
WorkspaceID string
140+
}
141+
142+
func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, filePath string, opts ...DeleteFileInWorkspaceOptions) error {
143+
var opt DeleteFileInWorkspaceOptions
144+
for _, o := range opts {
145+
if o.WorkspaceID != "" {
146+
opt.WorkspaceID = o.WorkspaceID
147+
}
148+
}
149+
150+
if opt.WorkspaceID == "" {
151+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
152+
}
153+
81154
_, err := g.runBasicCommand(ctx, "workspaces/delete-file", map[string]any{
82-
"id": workspaceID,
155+
"id": opt.WorkspaceID,
83156
"filePath": filePath,
84157
"workspaceTool": g.globalOpts.WorkspaceTool,
85158
"env": g.globalOpts.Env,
@@ -88,9 +161,24 @@ func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, workspaceID, file
88161
return err
89162
}
90163

91-
func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, workspaceID, filePath string) ([]byte, error) {
164+
type ReadFileInWorkspaceOptions struct {
165+
WorkspaceID string
166+
}
167+
168+
func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, opts ...ReadFileInWorkspaceOptions) ([]byte, error) {
169+
var opt ReadFileInWorkspaceOptions
170+
for _, o := range opts {
171+
if o.WorkspaceID != "" {
172+
opt.WorkspaceID = o.WorkspaceID
173+
}
174+
}
175+
176+
if opt.WorkspaceID == "" {
177+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
178+
}
179+
92180
out, err := g.runBasicCommand(ctx, "workspaces/read-file", map[string]any{
93-
"id": workspaceID,
181+
"id": opt.WorkspaceID,
94182
"filePath": filePath,
95183
"workspaceTool": g.globalOpts.WorkspaceTool,
96184
"env": g.globalOpts.Env,

workspace_test.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestCreateAndDeleteWorkspace(t *testing.T) {
1313
t.Fatalf("Error creating workspace: %v", err)
1414
}
1515

16-
err = g.DeleteWorkspace(context.Background(), id)
16+
err = g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
1717
if err != nil {
1818
t.Errorf("Error deleting workspace: %v", err)
1919
}
@@ -26,18 +26,18 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
2626
}
2727

2828
t.Cleanup(func() {
29-
err := g.DeleteWorkspace(context.Background(), id)
29+
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
3030
if err != nil {
3131
t.Errorf("Error deleting workspace: %v", err)
3232
}
3333
})
3434

35-
err = g.WriteFileInWorkspace(context.Background(), id, "test.txt", []byte("test"))
35+
err = g.WriteFileInWorkspace(context.Background(), "test.txt", []byte("test"), WriteFileInWorkspaceOptions{WorkspaceID: id})
3636
if err != nil {
3737
t.Fatalf("Error creating file: %v", err)
3838
}
3939

40-
content, err := g.ReadFileInWorkspace(context.Background(), id, "test.txt")
40+
content, err := g.ReadFileInWorkspace(context.Background(), "test.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
4141
if err != nil {
4242
t.Errorf("Error reading file: %v", err)
4343
}
@@ -46,7 +46,7 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
4646
t.Errorf("Unexpected content: %s", content)
4747
}
4848

49-
err = g.DeleteFileInWorkspace(context.Background(), id, "test.txt")
49+
err = g.DeleteFileInWorkspace(context.Background(), "test.txt", DeleteFileInWorkspaceOptions{WorkspaceID: id})
5050
if err != nil {
5151
t.Errorf("Error deleting file: %v", err)
5252
}
@@ -59,34 +59,34 @@ func TestLsComplexWorkspace(t *testing.T) {
5959
}
6060

6161
t.Cleanup(func() {
62-
err := g.DeleteWorkspace(context.Background(), id)
62+
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
6363
if err != nil {
6464
t.Errorf("Error deleting workspace: %v", err)
6565
}
6666
})
6767

68-
err = g.WriteFileInWorkspace(context.Background(), id, "test/test1.txt", []byte("hello1"))
68+
err = g.WriteFileInWorkspace(context.Background(), "test/test1.txt", []byte("hello1"), WriteFileInWorkspaceOptions{WorkspaceID: id})
6969
if err != nil {
7070
t.Fatalf("Error creating file: %v", err)
7171
}
7272

73-
err = g.WriteFileInWorkspace(context.Background(), id, "test1/test2.txt", []byte("hello2"))
73+
err = g.WriteFileInWorkspace(context.Background(), "test1/test2.txt", []byte("hello2"), WriteFileInWorkspaceOptions{WorkspaceID: id})
7474
if err != nil {
7575
t.Fatalf("Error creating file: %v", err)
7676
}
7777

78-
err = g.WriteFileInWorkspace(context.Background(), id, "test1/test3.txt", []byte("hello3"))
78+
err = g.WriteFileInWorkspace(context.Background(), "test1/test3.txt", []byte("hello3"), WriteFileInWorkspaceOptions{WorkspaceID: id})
7979
if err != nil {
8080
t.Fatalf("Error creating file: %v", err)
8181
}
8282

83-
err = g.WriteFileInWorkspace(context.Background(), id, ".hidden.txt", []byte("hidden"))
83+
err = g.WriteFileInWorkspace(context.Background(), ".hidden.txt", []byte("hidden"), WriteFileInWorkspaceOptions{WorkspaceID: id})
8484
if err != nil {
8585
t.Fatalf("Error creating hidden file: %v", err)
8686
}
8787

8888
// List all files
89-
content, err := g.ListFilesInWorkspace(context.Background(), id)
89+
content, err := g.ListFilesInWorkspace(context.Background(), ListFilesInWorkspaceOptions{WorkspaceID: id})
9090
if err != nil {
9191
t.Fatalf("Error listing files: %v", err)
9292
}
@@ -96,7 +96,7 @@ func TestLsComplexWorkspace(t *testing.T) {
9696
}
9797

9898
// List files in subdirectory
99-
content, err = g.ListFilesInWorkspace(context.Background(), id, ListFilesInWorkspaceOptions{Prefix: "test1"})
99+
content, err = g.ListFilesInWorkspace(context.Background(), ListFilesInWorkspaceOptions{WorkspaceID: id, Prefix: "test1"})
100100
if err != nil {
101101
t.Fatalf("Error listing files: %v", err)
102102
}
@@ -106,13 +106,13 @@ func TestLsComplexWorkspace(t *testing.T) {
106106
}
107107

108108
// Remove all files with test1 prefix
109-
err = g.RemoveAllWithPrefix(context.Background(), id, "test1")
109+
err = g.RemoveAll(context.Background(), RemoveAllOptions{WorkspaceID: id, WithPrefix: "test1"})
110110
if err != nil {
111111
t.Fatalf("Error removing files: %v", err)
112112
}
113113

114114
// List files in subdirectory
115-
content, err = g.ListFilesInWorkspace(context.Background(), id)
115+
content, err = g.ListFilesInWorkspace(context.Background(), ListFilesInWorkspaceOptions{WorkspaceID: id})
116116
if err != nil {
117117
t.Fatalf("Error listing files: %v", err)
118118
}
@@ -132,7 +132,7 @@ func TestCreateAndDeleteWorkspaceS3(t *testing.T) {
132132
t.Fatalf("Error creating workspace: %v", err)
133133
}
134134

135-
err = g.DeleteWorkspace(context.Background(), id)
135+
err = g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
136136
if err != nil {
137137
t.Errorf("Error deleting workspace: %v", err)
138138
}
@@ -149,18 +149,18 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
149149
}
150150

151151
t.Cleanup(func() {
152-
err := g.DeleteWorkspace(context.Background(), id)
152+
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
153153
if err != nil {
154154
t.Errorf("Error deleting workspace: %v", err)
155155
}
156156
})
157157

158-
err = g.WriteFileInWorkspace(context.Background(), id, "test.txt", []byte("test"))
158+
err = g.WriteFileInWorkspace(context.Background(), "test.txt", []byte("test"), WriteFileInWorkspaceOptions{WorkspaceID: id})
159159
if err != nil {
160160
t.Fatalf("Error creating file: %v", err)
161161
}
162162

163-
content, err := g.ReadFileInWorkspace(context.Background(), id, "test.txt")
163+
content, err := g.ReadFileInWorkspace(context.Background(), "test.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
164164
if err != nil {
165165
t.Errorf("Error reading file: %v", err)
166166
}
@@ -169,7 +169,7 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
169169
t.Errorf("Unexpected content: %s", content)
170170
}
171171

172-
err = g.DeleteFileInWorkspace(context.Background(), id, "test.txt")
172+
err = g.DeleteFileInWorkspace(context.Background(), "test.txt", DeleteFileInWorkspaceOptions{WorkspaceID: id})
173173
if err != nil {
174174
t.Errorf("Error deleting file: %v", err)
175175
}
@@ -186,34 +186,34 @@ func TestLsComplexWorkspaceS3(t *testing.T) {
186186
}
187187

188188
t.Cleanup(func() {
189-
err := g.DeleteWorkspace(context.Background(), id)
189+
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
190190
if err != nil {
191191
t.Errorf("Error deleting workspace: %v", err)
192192
}
193193
})
194194

195-
err = g.WriteFileInWorkspace(context.Background(), id, "test/test1.txt", []byte("hello1"))
195+
err = g.WriteFileInWorkspace(context.Background(), "test/test1.txt", []byte("hello1"), WriteFileInWorkspaceOptions{WorkspaceID: id})
196196
if err != nil {
197197
t.Fatalf("Error creating file: %v", err)
198198
}
199199

200-
err = g.WriteFileInWorkspace(context.Background(), id, "test1/test2.txt", []byte("hello2"))
200+
err = g.WriteFileInWorkspace(context.Background(), "test1/test2.txt", []byte("hello2"), WriteFileInWorkspaceOptions{WorkspaceID: id})
201201
if err != nil {
202202
t.Fatalf("Error creating file: %v", err)
203203
}
204204

205-
err = g.WriteFileInWorkspace(context.Background(), id, "test1/test3.txt", []byte("hello3"))
205+
err = g.WriteFileInWorkspace(context.Background(), "test1/test3.txt", []byte("hello3"), WriteFileInWorkspaceOptions{WorkspaceID: id})
206206
if err != nil {
207207
t.Fatalf("Error creating file: %v", err)
208208
}
209209

210-
err = g.WriteFileInWorkspace(context.Background(), id, ".hidden.txt", []byte("hidden"))
210+
err = g.WriteFileInWorkspace(context.Background(), ".hidden.txt", []byte("hidden"), WriteFileInWorkspaceOptions{WorkspaceID: id})
211211
if err != nil {
212212
t.Fatalf("Error creating hidden file: %v", err)
213213
}
214214

215215
// List all files
216-
content, err := g.ListFilesInWorkspace(context.Background(), id)
216+
content, err := g.ListFilesInWorkspace(context.Background(), ListFilesInWorkspaceOptions{WorkspaceID: id})
217217
if err != nil {
218218
t.Fatalf("Error listing files: %v", err)
219219
}
@@ -223,7 +223,7 @@ func TestLsComplexWorkspaceS3(t *testing.T) {
223223
}
224224

225225
// List files in subdirectory
226-
content, err = g.ListFilesInWorkspace(context.Background(), id, ListFilesInWorkspaceOptions{Prefix: "test1"})
226+
content, err = g.ListFilesInWorkspace(context.Background(), ListFilesInWorkspaceOptions{WorkspaceID: id, Prefix: "test1"})
227227
if err != nil {
228228
t.Fatalf("Error listing files: %v", err)
229229
}
@@ -233,13 +233,13 @@ func TestLsComplexWorkspaceS3(t *testing.T) {
233233
}
234234

235235
// Remove all files with test1 prefix
236-
err = g.RemoveAllWithPrefix(context.Background(), id, "test1")
236+
err = g.RemoveAll(context.Background(), RemoveAllOptions{WorkspaceID: id, WithPrefix: "test1"})
237237
if err != nil {
238238
t.Fatalf("Error removing files: %v", err)
239239
}
240240

241241
// List files in subdirectory
242-
content, err = g.ListFilesInWorkspace(context.Background(), id)
242+
content, err = g.ListFilesInWorkspace(context.Background(), ListFilesInWorkspaceOptions{WorkspaceID: id})
243243
if err != nil {
244244
t.Fatalf("Error listing files: %v", err)
245245
}

0 commit comments

Comments
 (0)