Skip to content

Workspace api file stat #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestDatasets(t *testing.T) {
require.NoError(t, err)

defer func() {
_ = g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: workspaceID})
_ = g.DeleteWorkspace(context.Background(), workspaceID)
}()

// Create a dataset
Expand Down
65 changes: 50 additions & 15 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"strings"
"time"
)

type NotFoundInWorkspaceError struct {
Expand Down Expand Up @@ -36,24 +37,13 @@ func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fr
return strings.TrimSpace(out), nil
}

type DeleteWorkspaceOptions struct {
WorkspaceID string
}

func (g *GPTScript) DeleteWorkspace(ctx context.Context, opts ...DeleteWorkspaceOptions) error {
var opt DeleteWorkspaceOptions
for _, o := range opts {
if o.WorkspaceID != "" {
opt.WorkspaceID = o.WorkspaceID
}
}

if opt.WorkspaceID == "" {
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string) error {
if workspaceID == "" {
return fmt.Errorf("workspace ID cannot be empty")
}

_, err := g.runBasicCommand(ctx, "workspaces/delete", map[string]any{
"id": opt.WorkspaceID,
"id": workspaceID,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
Expand Down Expand Up @@ -218,3 +208,48 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, op

return base64.StdEncoding.DecodeString(out)
}

type StatFileInWorkspaceOptions struct {
WorkspaceID string
}

func (g *GPTScript) StatFileInWorkspace(ctx context.Context, filePath string, opts ...StatFileInWorkspaceOptions) (FileInfo, error) {
var opt StatFileInWorkspaceOptions
for _, o := range opts {
if o.WorkspaceID != "" {
opt.WorkspaceID = o.WorkspaceID
}
}

if opt.WorkspaceID == "" {
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
}

out, err := g.runBasicCommand(ctx, "workspaces/stat-file", map[string]any{
"id": opt.WorkspaceID,
"filePath": filePath,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
if err != nil {
if strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
return FileInfo{}, newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
}
return FileInfo{}, err
}

var info FileInfo
err = json.Unmarshal([]byte(out), &info)
if err != nil {
return FileInfo{}, err
}

return info, nil
}

type FileInfo struct {
WorkspaceID string
Name string
Size int64
ModTime time.Time
}
62 changes: 56 additions & 6 deletions workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
"testing"
)

func TestWorkspaceIDRequiredForDelete(t *testing.T) {
if err := g.DeleteWorkspace(context.Background(), ""); err == nil {
t.Error("Expected error but got nil")
}
}

func TestCreateAndDeleteWorkspace(t *testing.T) {
id, err := g.CreateWorkspace(context.Background(), "directory")
if err != nil {
t.Fatalf("Error creating workspace: %v", err)
}

err = g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
err = g.DeleteWorkspace(context.Background(), id)
if err != nil {
t.Errorf("Error deleting workspace: %v", err)
}
Expand All @@ -27,7 +33,7 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
}

t.Cleanup(func() {
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
err := g.DeleteWorkspace(context.Background(), id)
if err != nil {
t.Errorf("Error deleting workspace: %v", err)
}
Expand All @@ -47,6 +53,28 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
t.Errorf("Unexpected content: %s", content)
}

// Stat the file to ensure it exists
fileInfo, err := g.StatFileInWorkspace(context.Background(), "test.txt", StatFileInWorkspaceOptions{WorkspaceID: id})
if err != nil {
t.Errorf("Error statting file: %v", err)
}

if fileInfo.WorkspaceID != id {
t.Errorf("Unexpected file workspace ID: %v", fileInfo.WorkspaceID)
}

if fileInfo.Name != "test.txt" {
t.Errorf("Unexpected file name: %s", fileInfo.Name)
}

if fileInfo.Size != 4 {
t.Errorf("Unexpected file size: %d", fileInfo.Size)
}

if fileInfo.ModTime.IsZero() {
t.Errorf("Unexpected file mod time: %v", fileInfo.ModTime)
}

// Ensure we get the error we expect when trying to read a non-existent file
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
Expand All @@ -66,7 +94,7 @@ func TestLsComplexWorkspace(t *testing.T) {
}

t.Cleanup(func() {
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
err := g.DeleteWorkspace(context.Background(), id)
if err != nil {
t.Errorf("Error deleting workspace: %v", err)
}
Expand Down Expand Up @@ -139,7 +167,7 @@ func TestCreateAndDeleteWorkspaceS3(t *testing.T) {
t.Fatalf("Error creating workspace: %v", err)
}

err = g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
err = g.DeleteWorkspace(context.Background(), id)
if err != nil {
t.Errorf("Error deleting workspace: %v", err)
}
Expand All @@ -156,7 +184,7 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
}

t.Cleanup(func() {
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
err := g.DeleteWorkspace(context.Background(), id)
if err != nil {
t.Errorf("Error deleting workspace: %v", err)
}
Expand All @@ -176,6 +204,28 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
t.Errorf("Unexpected content: %s", content)
}

// Stat the file to ensure it exists
fileInfo, err := g.StatFileInWorkspace(context.Background(), "test.txt", StatFileInWorkspaceOptions{WorkspaceID: id})
if err != nil {
t.Errorf("Error statting file: %v", err)
}

if fileInfo.WorkspaceID != id {
t.Errorf("Unexpected file workspace ID: %v", fileInfo.WorkspaceID)
}

if fileInfo.Name != "test.txt" {
t.Errorf("Unexpected file name: %s", fileInfo.Name)
}

if fileInfo.Size != 4 {
t.Errorf("Unexpected file size: %d", fileInfo.Size)
}

if fileInfo.ModTime.IsZero() {
t.Errorf("Unexpected file mod time: %v", fileInfo.ModTime)
}

// Ensure we get the error we expect when trying to read a non-existent file
_, err = g.ReadFileInWorkspace(context.Background(), "test1.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
if nf := (*NotFoundInWorkspaceError)(nil); !errors.As(err, &nf) {
Expand All @@ -199,7 +249,7 @@ func TestLsComplexWorkspaceS3(t *testing.T) {
}

t.Cleanup(func() {
err := g.DeleteWorkspace(context.Background(), DeleteWorkspaceOptions{WorkspaceID: id})
err := g.DeleteWorkspace(context.Background(), id)
if err != nil {
t.Errorf("Error deleting workspace: %v", err)
}
Expand Down