@@ -3,14 +3,16 @@ package gptscript
3
3
import (
4
4
"context"
5
5
"encoding/base64"
6
- "encoding/json"
7
6
"strings"
8
7
)
9
8
10
- func (g * GPTScript ) CreateWorkspace (ctx context.Context , providerType string ) (string , error ) {
9
+ func (g * GPTScript ) CreateWorkspace (ctx context.Context , providerType string , fromWorkspaces ... string ) (string , error ) {
11
10
out , err := g .runBasicCommand (ctx , "workspaces/create" , map [string ]any {
12
- "provider" : providerType ,
13
- "workspaceTool" : g .globalOpts .WorkspaceTool ,
11
+ "providerType" : providerType ,
12
+ "fromWorkspaceIDs" : fromWorkspaces ,
13
+ "workspaceTool" : g .globalOpts .WorkspaceTool ,
14
+ "directoryDataHome" : g .globalOpts .WorkspaceDirectoryDataHome ,
15
+ "env" : g .globalOpts .Env ,
14
16
})
15
17
if err != nil {
16
18
return "" , err
@@ -19,156 +21,72 @@ func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string) (s
19
21
return strings .TrimSpace (out ), nil
20
22
}
21
23
22
- type DeleteWorkspaceOptions struct {
23
- IgnoreNotFound bool
24
- }
25
-
26
- func (g * GPTScript ) DeleteWorkspace (ctx context.Context , workspaceID string , opts ... DeleteWorkspaceOptions ) error {
27
- var opt DeleteWorkspaceOptions
28
- for _ , o := range opts {
29
- opt .IgnoreNotFound = opt .IgnoreNotFound || o .IgnoreNotFound
30
- }
24
+ func (g * GPTScript ) DeleteWorkspace (ctx context.Context , workspaceID string ) error {
31
25
_ , err := g .runBasicCommand (ctx , "workspaces/delete" , map [string ]any {
32
- "id" : workspaceID ,
33
- "ignoreNotFound" : opt .IgnoreNotFound ,
34
- "workspaceTool" : g .globalOpts .WorkspaceTool ,
35
- })
36
-
37
- return err
38
- }
39
-
40
- type CreateDirectoryInWorkspaceOptions struct {
41
- IgnoreExists bool
42
- }
43
-
44
- func (g * GPTScript ) CreateDirectoryInWorkspace (ctx context.Context , workspaceID , dir string , opts ... CreateDirectoryInWorkspaceOptions ) error {
45
- var opt CreateDirectoryInWorkspaceOptions
46
- for _ , o := range opts {
47
- opt .IgnoreExists = opt .IgnoreExists || o .IgnoreExists
48
- }
49
-
50
- _ , err := g .runBasicCommand (ctx , "workspaces/mkdir" , map [string ]any {
51
26
"id" : workspaceID ,
52
- "directoryName" : dir ,
53
- "ignoreExists" : opt .IgnoreExists ,
54
27
"workspaceTool" : g .globalOpts .WorkspaceTool ,
55
- })
56
-
57
- return err
58
- }
59
-
60
- type DeleteDirectoryInWorkspaceOptions struct {
61
- IgnoreNotFound bool
62
- MustBeEmpty bool
63
- }
64
-
65
- func (g * GPTScript ) DeleteDirectoryInWorkspace (ctx context.Context , workspaceID , dir string , opts ... DeleteDirectoryInWorkspaceOptions ) error {
66
- var opt DeleteDirectoryInWorkspaceOptions
67
- for _ , o := range opts {
68
- o .IgnoreNotFound = opt .IgnoreNotFound || o .IgnoreNotFound
69
- o .MustBeEmpty = opt .MustBeEmpty || o .MustBeEmpty
70
- }
71
-
72
- _ , err := g .runBasicCommand (ctx , "workspaces/rmdir" , map [string ]any {
73
- "id" : workspaceID ,
74
- "directoryName" : dir ,
75
- "ignoreNotFound" : opt .IgnoreNotFound ,
76
- "mustBeEmpty" : opt .MustBeEmpty ,
77
- "workspaceTool" : g .globalOpts .WorkspaceTool ,
28
+ "env" : g .globalOpts .Env ,
78
29
})
79
30
80
31
return err
81
32
}
82
33
83
34
type ListFilesInWorkspaceOptions struct {
84
- SubDir string
85
- NonRecursive bool
86
- ExcludeHidden bool
87
- }
88
-
89
- type WorkspaceContent struct {
90
- ID , Path , FileName string
91
- Children []WorkspaceContent
35
+ Prefix string
92
36
}
93
37
94
- func (g * GPTScript ) ListFilesInWorkspace (ctx context.Context , workspaceID string , opts ... ListFilesInWorkspaceOptions ) (* WorkspaceContent , error ) {
38
+ func (g * GPTScript ) ListFilesInWorkspace (ctx context.Context , workspaceID string , opts ... ListFilesInWorkspaceOptions ) ([] string , error ) {
95
39
var opt ListFilesInWorkspaceOptions
96
40
for _ , o := range opts {
97
- if o .SubDir != "" {
98
- opt .SubDir = o .SubDir
41
+ if o .Prefix != "" {
42
+ opt .Prefix = o .Prefix
99
43
}
100
- opt .NonRecursive = opt .NonRecursive || o .NonRecursive
101
- opt .ExcludeHidden = opt .ExcludeHidden || o .ExcludeHidden
102
44
}
103
45
104
46
out , err := g .runBasicCommand (ctx , "workspaces/list" , map [string ]any {
105
47
"id" : workspaceID ,
106
- "subDir" : opt .SubDir ,
107
- "excludeHidden" : opt .ExcludeHidden ,
108
- "nonRecursive" : opt .NonRecursive ,
48
+ "prefix" : opt .Prefix ,
109
49
"workspaceTool" : g .globalOpts .WorkspaceTool ,
110
- "json " : true ,
50
+ "env " : g . globalOpts . Env ,
111
51
})
112
52
if err != nil {
113
53
return nil , err
114
54
}
115
55
116
- var content []WorkspaceContent
117
- err = json .Unmarshal ([]byte (out ), & content )
118
- if err != nil {
119
- return nil , err
120
- }
121
-
122
- if len (content ) == 0 {
123
- return & WorkspaceContent {ID : workspaceID }, nil
124
- }
125
-
126
- return & content [0 ], nil
56
+ // The first line of the output is the workspace ID, ignore it.
57
+ return strings .Split (strings .TrimSpace (out ), "\n " )[1 :], nil
127
58
}
128
59
129
- type CreateFileInWorkspaceOptions struct {
130
- MustNotExist bool
131
- WithoutCreate bool
132
- CreateDirs bool
133
- }
60
+ func (g * GPTScript ) RemoveAllWithPrefix (ctx context.Context , workspaceID , prefix string ) error {
61
+ _ , err := g .runBasicCommand (ctx , "workspaces/remove-all-with-prefix" , map [string ]any {
62
+ "id" : workspaceID ,
63
+ "prefix" : prefix ,
64
+ "workspaceTool" : g .globalOpts .WorkspaceTool ,
65
+ "env" : g .globalOpts .Env ,
66
+ })
134
67
135
- func (g * GPTScript ) WriteFileInWorkspace (ctx context.Context , workspaceID , filePath string , contents []byte , opts ... CreateFileInWorkspaceOptions ) error {
136
- var opt CreateFileInWorkspaceOptions
137
- for _ , o := range opts {
138
- opt .MustNotExist = opt .MustNotExist || o .MustNotExist
139
- opt .WithoutCreate = opt .WithoutCreate || o .WithoutCreate
140
- opt .CreateDirs = opt .CreateDirs || o .CreateDirs
141
- }
68
+ return err
69
+ }
142
70
71
+ func (g * GPTScript ) WriteFileInWorkspace (ctx context.Context , workspaceID , filePath string , contents []byte ) error {
143
72
_ , err := g .runBasicCommand (ctx , "workspaces/write-file" , map [string ]any {
144
73
"id" : workspaceID ,
145
74
"contents" : base64 .StdEncoding .EncodeToString (contents ),
146
75
"filePath" : filePath ,
147
- "mustNotExist" : opt .MustNotExist ,
148
- "withoutCreate" : opt .WithoutCreate ,
149
- "createDirs" : opt .CreateDirs ,
150
76
"workspaceTool" : g .globalOpts .WorkspaceTool ,
151
77
"base64EncodedInput" : true ,
78
+ "env" : g .globalOpts .Env ,
152
79
})
153
80
154
81
return err
155
82
}
156
83
157
- type DeleteFileInWorkspaceOptions struct {
158
- IgnoreNotFound bool
159
- }
160
-
161
- func (g * GPTScript ) DeleteFileInWorkspace (ctx context.Context , workspaceID , filePath string , opts ... DeleteFileInWorkspaceOptions ) error {
162
- var opt DeleteFileInWorkspaceOptions
163
- for _ , o := range opts {
164
- opt .IgnoreNotFound = opt .IgnoreNotFound || o .IgnoreNotFound
165
- }
166
-
84
+ func (g * GPTScript ) DeleteFileInWorkspace (ctx context.Context , workspaceID , filePath string ) error {
167
85
_ , err := g .runBasicCommand (ctx , "workspaces/delete-file" , map [string ]any {
168
- "id" : workspaceID ,
169
- "filePath" : filePath ,
170
- "ignoreNotFound " : opt . IgnoreNotFound ,
171
- "workspaceTool " : g .globalOpts .WorkspaceTool ,
86
+ "id" : workspaceID ,
87
+ "filePath" : filePath ,
88
+ "workspaceTool " : g . globalOpts . WorkspaceTool ,
89
+ "env " : g .globalOpts .Env ,
172
90
})
173
91
174
92
return err
@@ -180,6 +98,7 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, workspaceID, filePa
180
98
"filePath" : filePath ,
181
99
"workspaceTool" : g .globalOpts .WorkspaceTool ,
182
100
"base64EncodeOutput" : true ,
101
+ "env" : g .globalOpts .Env ,
183
102
})
184
103
if err != nil {
185
104
return nil , err
0 commit comments