@@ -2,10 +2,26 @@ package gptscript
2
2
3
3
import (
4
4
"context"
5
+ "encoding/base64"
6
+ "encoding/json"
7
+ "fmt"
5
8
"os"
6
9
"strings"
7
10
)
8
11
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
+
9
25
func (g * GPTScript ) CreateWorkspace (ctx context.Context , providerType string , fromWorkspaces ... string ) (string , error ) {
10
26
out , err := g .runBasicCommand (ctx , "workspaces/create" , map [string ]any {
11
27
"providerType" : providerType ,
@@ -75,7 +91,13 @@ func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, opts ...ListFilesI
75
91
return nil , err
76
92
}
77
93
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 )
79
101
}
80
102
81
103
type RemoveAllOptions struct {
@@ -126,7 +148,7 @@ func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, filePath string, c
126
148
127
149
_ , err := g .runBasicCommand (ctx , "workspaces/write-file" , map [string ]any {
128
150
"id" : opt .WorkspaceID ,
129
- "contents" : contents ,
151
+ "contents" : base64 . StdEncoding . EncodeToString ( contents ) ,
130
152
"filePath" : filePath ,
131
153
"workspaceTool" : g .globalOpts .WorkspaceTool ,
132
154
"env" : g .globalOpts .Env ,
@@ -158,6 +180,10 @@ func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, filePath string,
158
180
"env" : g .globalOpts .Env ,
159
181
})
160
182
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
+
161
187
return err
162
188
}
163
189
@@ -184,8 +210,11 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, op
184
210
"env" : g .globalOpts .Env ,
185
211
})
186
212
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
+ }
187
216
return nil , err
188
217
}
189
218
190
- return [] byte (out ), nil
219
+ return base64 . StdEncoding . DecodeString (out )
191
220
}
0 commit comments