@@ -2,10 +2,26 @@ package gptscript
22
33import (
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+
925func (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
81103type 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}
0 commit comments