Skip to content

Commit 1fce3cc

Browse files
committed
feat: add file stat to workspace API
Signed-off-by: Donnie Adams <[email protected]>
1 parent d55120b commit 1fce3cc

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

workspace.go

+46
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"strings"
10+
"time"
1011
)
1112

1213
type NotFoundInWorkspaceError struct {
@@ -207,3 +208,48 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, op
207208

208209
return base64.StdEncoding.DecodeString(out)
209210
}
211+
212+
type StatFileInWorkspaceOptions struct {
213+
WorkspaceID string
214+
}
215+
216+
func (g *GPTScript) StatFileInWorkspace(ctx context.Context, filePath string, opts ...StatFileInWorkspaceOptions) (FileInfo, error) {
217+
var opt StatFileInWorkspaceOptions
218+
for _, o := range opts {
219+
if o.WorkspaceID != "" {
220+
opt.WorkspaceID = o.WorkspaceID
221+
}
222+
}
223+
224+
if opt.WorkspaceID == "" {
225+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
226+
}
227+
228+
out, err := g.runBasicCommand(ctx, "workspaces/stat-file", map[string]any{
229+
"id": opt.WorkspaceID,
230+
"filePath": filePath,
231+
"workspaceTool": g.globalOpts.WorkspaceTool,
232+
"env": g.globalOpts.Env,
233+
})
234+
if err != nil {
235+
if strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
236+
return FileInfo{}, newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
237+
}
238+
return FileInfo{}, err
239+
}
240+
241+
var info FileInfo
242+
err = json.Unmarshal([]byte(out), &info)
243+
if err != nil {
244+
return FileInfo{}, err
245+
}
246+
247+
return info, nil
248+
}
249+
250+
type FileInfo struct {
251+
WorkspaceID string
252+
Name string
253+
Size int64
254+
ModTime time.Time
255+
}

workspace_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
5353
t.Errorf("Unexpected content: %s", content)
5454
}
5555

56+
// Stat the file to ensure it exists
57+
fileInfo, err := g.StatFileInWorkspace(context.Background(), "test.txt", StatFileInWorkspaceOptions{WorkspaceID: id})
58+
if err != nil {
59+
t.Errorf("Error statting file: %v", err)
60+
}
61+
62+
if fileInfo.WorkspaceID != id {
63+
t.Errorf("Unexpected file workspace ID: %v", fileInfo.WorkspaceID)
64+
}
65+
66+
if fileInfo.Name != "test.txt" {
67+
t.Errorf("Unexpected file name: %s", fileInfo.Name)
68+
}
69+
70+
if fileInfo.Size != 4 {
71+
t.Errorf("Unexpected file size: %d", fileInfo.Size)
72+
}
73+
74+
if fileInfo.ModTime.IsZero() {
75+
t.Errorf("Unexpected file mod time: %v", fileInfo.ModTime)
76+
}
77+
5678
// Ensure we get the error we expect when trying to read a non-existent file
5779
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
5880
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
@@ -182,6 +204,28 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
182204
t.Errorf("Unexpected content: %s", content)
183205
}
184206

207+
// Stat the file to ensure it exists
208+
fileInfo, err := g.StatFileInWorkspace(context.Background(), "test.txt", StatFileInWorkspaceOptions{WorkspaceID: id})
209+
if err != nil {
210+
t.Errorf("Error statting file: %v", err)
211+
}
212+
213+
if fileInfo.WorkspaceID != id {
214+
t.Errorf("Unexpected file workspace ID: %v", fileInfo.WorkspaceID)
215+
}
216+
217+
if fileInfo.Name != "test.txt" {
218+
t.Errorf("Unexpected file name: %s", fileInfo.Name)
219+
}
220+
221+
if fileInfo.Size != 4 {
222+
t.Errorf("Unexpected file size: %d", fileInfo.Size)
223+
}
224+
225+
if fileInfo.ModTime.IsZero() {
226+
t.Errorf("Unexpected file mod time: %v", fileInfo.ModTime)
227+
}
228+
185229
// Ensure we get the error we expect when trying to read a non-existent file
186230
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
187231
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {

0 commit comments

Comments
 (0)