Skip to content

Commit 25d959a

Browse files
committed
Read with revision updates
Signed-off-by: Donnie Adams <[email protected]>
1 parent 965e8af commit 25d959a

File tree

3 files changed

+60
-43
lines changed

3 files changed

+60
-43
lines changed

gptscript_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestMain(m *testing.M) {
3030
panic(fmt.Sprintf("error creating gptscript: %s", err))
3131
}
3232

33-
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY"), WorkspaceTool: "/Users/thedadams/code/workspace-provider"})
33+
g, err = NewGPTScript(GlobalOptions{OpenAIAPIKey: os.Getenv("OPENAI_API_KEY")})
3434
if err != nil {
3535
gFirst.Close()
3636
panic(fmt.Sprintf("error creating gptscript: %s", err))

workspace.go

+37-12
Original file line numberDiff line numberDiff line change
@@ -214,34 +214,59 @@ func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, filePath string,
214214
}
215215

216216
type ReadFileInWorkspaceOptions struct {
217-
WorkspaceID string
218-
WithLatestRevisionID bool
217+
WorkspaceID string
219218
}
220219

221-
type ReadFileInWorkspaceResponse struct {
220+
func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, opts ...ReadFileInWorkspaceOptions) ([]byte, error) {
221+
var opt ReadFileInWorkspaceOptions
222+
for _, o := range opts {
223+
if o.WorkspaceID != "" {
224+
opt.WorkspaceID = o.WorkspaceID
225+
}
226+
}
227+
228+
if opt.WorkspaceID == "" {
229+
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
230+
}
231+
232+
out, err := g.runBasicCommand(ctx, "workspaces/read-file", map[string]any{
233+
"id": opt.WorkspaceID,
234+
"filePath": filePath,
235+
"workspaceTool": g.globalOpts.WorkspaceTool,
236+
"env": g.globalOpts.Env,
237+
})
238+
if err != nil {
239+
if strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
240+
return nil, newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
241+
}
242+
return nil, err
243+
}
244+
245+
return base64.StdEncoding.DecodeString(out)
246+
}
247+
248+
type ReadFileWithRevisionInWorkspaceResponse struct {
222249
Content []byte `json:"content"`
223250
RevisionID string `json:"revisionID"`
224251
}
225252

226-
func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, opts ...ReadFileInWorkspaceOptions) (*ReadFileInWorkspaceResponse, error) {
253+
func (g *GPTScript) ReadFileWithRevisionInWorkspace(ctx context.Context, filePath string, opts ...ReadFileInWorkspaceOptions) (*ReadFileWithRevisionInWorkspaceResponse, error) {
227254
var opt ReadFileInWorkspaceOptions
228255
for _, o := range opts {
229256
if o.WorkspaceID != "" {
230257
opt.WorkspaceID = o.WorkspaceID
231258
}
232-
opt.WithLatestRevisionID = opt.WithLatestRevisionID || o.WithLatestRevisionID
233259
}
234260

235261
if opt.WorkspaceID == "" {
236262
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
237263
}
238264

239-
out, err := g.runBasicCommand(ctx, "workspaces/read-file", map[string]any{
240-
"id": opt.WorkspaceID,
241-
"filePath": filePath,
242-
"withLatestRevisionID": opt.WithLatestRevisionID,
243-
"workspaceTool": g.globalOpts.WorkspaceTool,
244-
"env": g.globalOpts.Env,
265+
out, err := g.runBasicCommand(ctx, "workspaces/read-file-with-revision", map[string]any{
266+
"id": opt.WorkspaceID,
267+
"filePath": filePath,
268+
"workspaceTool": g.globalOpts.WorkspaceTool,
269+
"env": g.globalOpts.Env,
245270
})
246271
if err != nil {
247272
if strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
@@ -250,7 +275,7 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, op
250275
return nil, err
251276
}
252277

253-
var resp ReadFileInWorkspaceResponse
278+
var resp ReadFileWithRevisionInWorkspaceResponse
254279
err = json.Unmarshal([]byte(out), &resp)
255280
if err != nil {
256281
return nil, err

workspace_test.go

+22-30
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func TestCreateAndDeleteWorkspaceFromWorkspace(t *testing.T) {
5959
t.Fatalf("Error reading file: %v", err)
6060
}
6161

62-
if !bytes.Equal(content.Content, []byte("hello world")) {
63-
t.Errorf("Unexpected content: %s", content.Content)
62+
if !bytes.Equal(content, []byte("hello world")) {
63+
t.Errorf("Unexpected content: %s", content)
6464
}
6565

6666
err = g.DeleteWorkspace(context.Background(), id)
@@ -92,26 +92,22 @@ func TestWriteReadAndDeleteFileFromWorkspace(t *testing.T) {
9292
t.Errorf("Error reading file: %v", err)
9393
}
9494

95-
if !bytes.Equal(content.Content, []byte("test")) {
96-
t.Errorf("Unexpected content: %s", content.Content)
97-
}
98-
99-
if content.RevisionID != "" {
100-
t.Errorf("Unexpected file revision ID when not requesting it: %s", content.RevisionID)
95+
if !bytes.Equal(content, []byte("test")) {
96+
t.Errorf("Unexpected content: %s", content)
10197
}
10298

10399
// Read the file and request the revision ID
104-
content, err = g.ReadFileInWorkspace(context.Background(), "test.txt", ReadFileInWorkspaceOptions{WorkspaceID: id, WithLatestRevisionID: true})
100+
contentWithRevision, err := g.ReadFileWithRevisionInWorkspace(context.Background(), "test.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
105101
if err != nil {
106102
t.Errorf("Error reading file: %v", err)
107103
}
108104

109-
if !bytes.Equal(content.Content, []byte("test")) {
110-
t.Errorf("Unexpected content: %s", content.Content)
105+
if !bytes.Equal(contentWithRevision.Content, []byte("test")) {
106+
t.Errorf("Unexpected content: %s", contentWithRevision.Content)
111107
}
112108

113-
if content.RevisionID == "" {
114-
t.Errorf("Expected file revision ID when requesting it: %s", content.RevisionID)
109+
if contentWithRevision.RevisionID == "" {
110+
t.Errorf("Expected file revision ID when requesting it: %s", contentWithRevision.RevisionID)
115111
}
116112

117113
// Stat the file to ensure it exists
@@ -553,8 +549,8 @@ func TestCreateAndDeleteWorkspaceFromWorkspaceS3(t *testing.T) {
553549
t.Errorf("Error reading file: %v", err)
554550
}
555551

556-
if !bytes.Equal(content.Content, []byte("hello world")) {
557-
t.Errorf("Unexpected content: %s", content.Content)
552+
if !bytes.Equal(content, []byte("hello world")) {
553+
t.Errorf("Unexpected content: %s", content)
558554
}
559555

560556
err = g.DeleteWorkspace(context.Background(), id)
@@ -597,8 +593,8 @@ func TestCreateAndDeleteDirectoryWorkspaceFromWorkspaceS3(t *testing.T) {
597593
t.Errorf("Error reading file: %v", err)
598594
}
599595

600-
if !bytes.Equal(content.Content, []byte("hello world")) {
601-
t.Errorf("Unexpected content: %s", content.Content)
596+
if !bytes.Equal(content, []byte("hello world")) {
597+
t.Errorf("Unexpected content: %s", content)
602598
}
603599

604600
err = g.DeleteWorkspace(context.Background(), id)
@@ -648,8 +644,8 @@ func TestCreateAndDeleteS3WorkspaceFromWorkspaceDirectory(t *testing.T) {
648644
t.Fatalf("Error reading file: %v", err)
649645
}
650646

651-
if !bytes.Equal(content.Content, []byte("hello world")) {
652-
t.Errorf("Unexpected content: %s", content.Content)
647+
if !bytes.Equal(content, []byte("hello world")) {
648+
t.Errorf("Unexpected content: %s", content)
653649
}
654650

655651
err = g.DeleteWorkspace(context.Background(), id)
@@ -685,26 +681,22 @@ func TestWriteReadAndDeleteFileFromWorkspaceS3(t *testing.T) {
685681
t.Errorf("Error reading file: %v", err)
686682
}
687683

688-
if !bytes.Equal(content.Content, []byte("test")) {
689-
t.Errorf("Unexpected content: %s", content.Content)
690-
}
691-
692-
if content.RevisionID != "" {
693-
t.Errorf("Unexpected file revision ID when not requesting it: %s", content.RevisionID)
684+
if !bytes.Equal(content, []byte("test")) {
685+
t.Errorf("Unexpected content: %s", content)
694686
}
695687

696688
// Read the file and request the revision ID
697-
content, err = g.ReadFileInWorkspace(context.Background(), "test.txt", ReadFileInWorkspaceOptions{WorkspaceID: id, WithLatestRevisionID: true})
689+
contentWithRevision, err := g.ReadFileWithRevisionInWorkspace(context.Background(), "test.txt", ReadFileInWorkspaceOptions{WorkspaceID: id})
698690
if err != nil {
699691
t.Errorf("Error reading file: %v", err)
700692
}
701693

702-
if !bytes.Equal(content.Content, []byte("test")) {
703-
t.Errorf("Unexpected content: %s", content.Content)
694+
if !bytes.Equal(contentWithRevision.Content, []byte("test")) {
695+
t.Errorf("Unexpected content: %s", contentWithRevision.Content)
704696
}
705697

706-
if content.RevisionID == "" {
707-
t.Errorf("Expected file revision ID when requesting it: %s", content.RevisionID)
698+
if contentWithRevision.RevisionID == "" {
699+
t.Errorf("Expected file revision ID when requesting it: %s", contentWithRevision.RevisionID)
708700
}
709701

710702
// Stat the file to ensure it exists

0 commit comments

Comments
 (0)