Skip to content

Commit 657dcee

Browse files
authored
feat: simplify workspace API and add support for s3 (#73)
Signed-off-by: Donnie Adams <[email protected]>
1 parent 91a600f commit 657dcee

File tree

3 files changed

+175
-174
lines changed

3 files changed

+175
-174
lines changed

opts.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package gptscript
33
// GlobalOptions allows specification of settings that are used for every call made.
44
// These options can be overridden by the corresponding Options.
55
type GlobalOptions struct {
6-
URL string `json:"url"`
7-
Token string `json:"token"`
8-
OpenAIAPIKey string `json:"APIKey"`
9-
OpenAIBaseURL string `json:"BaseURL"`
10-
DefaultModel string `json:"DefaultModel"`
11-
DefaultModelProvider string `json:"DefaultModelProvider"`
12-
CacheDir string `json:"CacheDir"`
13-
Env []string `json:"env"`
14-
DatasetToolRepo string `json:"DatasetToolRepo"`
15-
WorkspaceTool string `json:"WorkspaceTool"`
6+
URL string `json:"url"`
7+
Token string `json:"token"`
8+
OpenAIAPIKey string `json:"APIKey"`
9+
OpenAIBaseURL string `json:"BaseURL"`
10+
DefaultModel string `json:"DefaultModel"`
11+
DefaultModelProvider string `json:"DefaultModelProvider"`
12+
CacheDir string `json:"CacheDir"`
13+
Env []string `json:"env"`
14+
DatasetToolRepo string `json:"DatasetToolRepo"`
15+
WorkspaceTool string `json:"WorkspaceTool"`
16+
WorkspaceDirectoryDataHome string `json:"WorkspaceDirectoryDataHome"`
1617
}
1718

1819
func (g GlobalOptions) toEnv() []string {
@@ -29,6 +30,9 @@ func (g GlobalOptions) toEnv() []string {
2930
if g.DefaultModelProvider != "" {
3031
args = append(args, "GPTSCRIPT_SDKSERVER_DEFAULT_MODEL_PROVIDER="+g.DefaultModelProvider)
3132
}
33+
if g.WorkspaceDirectoryDataHome != "" {
34+
args = append(args, "GPTSCRIPT_WORKSPACE_DIR="+g.WorkspaceDirectoryDataHome)
35+
}
3236

3337
return args
3438
}

workspace.go

+33-114
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package gptscript
33
import (
44
"context"
55
"encoding/base64"
6-
"encoding/json"
76
"strings"
87
)
98

10-
func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string) (string, error) {
9+
func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fromWorkspaces ...string) (string, error) {
1110
out, err := g.runBasicCommand(ctx, "workspaces/create", map[string]any{
12-
"provider": providerType,
13-
"workspaceTool": g.globalOpts.WorkspaceTool,
11+
"providerType": providerType,
12+
"fromWorkspaceIDs": fromWorkspaces,
13+
"workspaceTool": g.globalOpts.WorkspaceTool,
14+
"directoryDataHome": g.globalOpts.WorkspaceDirectoryDataHome,
15+
"env": g.globalOpts.Env,
1416
})
1517
if err != nil {
1618
return "", err
@@ -19,156 +21,72 @@ func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string) (s
1921
return strings.TrimSpace(out), nil
2022
}
2123

22-
type DeleteWorkspaceOptions struct {
23-
IgnoreNotFound bool
24-
}
25-
26-
func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string, opts ...DeleteWorkspaceOptions) error {
27-
var opt DeleteWorkspaceOptions
28-
for _, o := range opts {
29-
opt.IgnoreNotFound = opt.IgnoreNotFound || o.IgnoreNotFound
30-
}
24+
func (g *GPTScript) DeleteWorkspace(ctx context.Context, workspaceID string) error {
3125
_, err := g.runBasicCommand(ctx, "workspaces/delete", map[string]any{
32-
"id": workspaceID,
33-
"ignoreNotFound": opt.IgnoreNotFound,
34-
"workspaceTool": g.globalOpts.WorkspaceTool,
35-
})
36-
37-
return err
38-
}
39-
40-
type CreateDirectoryInWorkspaceOptions struct {
41-
IgnoreExists bool
42-
}
43-
44-
func (g *GPTScript) CreateDirectoryInWorkspace(ctx context.Context, workspaceID, dir string, opts ...CreateDirectoryInWorkspaceOptions) error {
45-
var opt CreateDirectoryInWorkspaceOptions
46-
for _, o := range opts {
47-
opt.IgnoreExists = opt.IgnoreExists || o.IgnoreExists
48-
}
49-
50-
_, err := g.runBasicCommand(ctx, "workspaces/mkdir", map[string]any{
5126
"id": workspaceID,
52-
"directoryName": dir,
53-
"ignoreExists": opt.IgnoreExists,
5427
"workspaceTool": g.globalOpts.WorkspaceTool,
55-
})
56-
57-
return err
58-
}
59-
60-
type DeleteDirectoryInWorkspaceOptions struct {
61-
IgnoreNotFound bool
62-
MustBeEmpty bool
63-
}
64-
65-
func (g *GPTScript) DeleteDirectoryInWorkspace(ctx context.Context, workspaceID, dir string, opts ...DeleteDirectoryInWorkspaceOptions) error {
66-
var opt DeleteDirectoryInWorkspaceOptions
67-
for _, o := range opts {
68-
o.IgnoreNotFound = opt.IgnoreNotFound || o.IgnoreNotFound
69-
o.MustBeEmpty = opt.MustBeEmpty || o.MustBeEmpty
70-
}
71-
72-
_, err := g.runBasicCommand(ctx, "workspaces/rmdir", map[string]any{
73-
"id": workspaceID,
74-
"directoryName": dir,
75-
"ignoreNotFound": opt.IgnoreNotFound,
76-
"mustBeEmpty": opt.MustBeEmpty,
77-
"workspaceTool": g.globalOpts.WorkspaceTool,
28+
"env": g.globalOpts.Env,
7829
})
7930

8031
return err
8132
}
8233

8334
type ListFilesInWorkspaceOptions struct {
84-
SubDir string
85-
NonRecursive bool
86-
ExcludeHidden bool
87-
}
88-
89-
type WorkspaceContent struct {
90-
ID, Path, FileName string
91-
Children []WorkspaceContent
35+
Prefix string
9236
}
9337

94-
func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, workspaceID string, opts ...ListFilesInWorkspaceOptions) (*WorkspaceContent, error) {
38+
func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, workspaceID string, opts ...ListFilesInWorkspaceOptions) ([]string, error) {
9539
var opt ListFilesInWorkspaceOptions
9640
for _, o := range opts {
97-
if o.SubDir != "" {
98-
opt.SubDir = o.SubDir
41+
if o.Prefix != "" {
42+
opt.Prefix = o.Prefix
9943
}
100-
opt.NonRecursive = opt.NonRecursive || o.NonRecursive
101-
opt.ExcludeHidden = opt.ExcludeHidden || o.ExcludeHidden
10244
}
10345

10446
out, err := g.runBasicCommand(ctx, "workspaces/list", map[string]any{
10547
"id": workspaceID,
106-
"subDir": opt.SubDir,
107-
"excludeHidden": opt.ExcludeHidden,
108-
"nonRecursive": opt.NonRecursive,
48+
"prefix": opt.Prefix,
10949
"workspaceTool": g.globalOpts.WorkspaceTool,
110-
"json": true,
50+
"env": g.globalOpts.Env,
11151
})
11252
if err != nil {
11353
return nil, err
11454
}
11555

116-
var content []WorkspaceContent
117-
err = json.Unmarshal([]byte(out), &content)
118-
if err != nil {
119-
return nil, err
120-
}
121-
122-
if len(content) == 0 {
123-
return &WorkspaceContent{ID: workspaceID}, nil
124-
}
125-
126-
return &content[0], nil
56+
// The first line of the output is the workspace ID, ignore it.
57+
return strings.Split(strings.TrimSpace(out), "\n")[1:], nil
12758
}
12859

129-
type CreateFileInWorkspaceOptions struct {
130-
MustNotExist bool
131-
WithoutCreate bool
132-
CreateDirs bool
133-
}
60+
func (g *GPTScript) RemoveAllWithPrefix(ctx context.Context, workspaceID, prefix string) error {
61+
_, err := g.runBasicCommand(ctx, "workspaces/remove-all-with-prefix", map[string]any{
62+
"id": workspaceID,
63+
"prefix": prefix,
64+
"workspaceTool": g.globalOpts.WorkspaceTool,
65+
"env": g.globalOpts.Env,
66+
})
13467

135-
func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, workspaceID, filePath string, contents []byte, opts ...CreateFileInWorkspaceOptions) error {
136-
var opt CreateFileInWorkspaceOptions
137-
for _, o := range opts {
138-
opt.MustNotExist = opt.MustNotExist || o.MustNotExist
139-
opt.WithoutCreate = opt.WithoutCreate || o.WithoutCreate
140-
opt.CreateDirs = opt.CreateDirs || o.CreateDirs
141-
}
68+
return err
69+
}
14270

71+
func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, workspaceID, filePath string, contents []byte) error {
14372
_, err := g.runBasicCommand(ctx, "workspaces/write-file", map[string]any{
14473
"id": workspaceID,
14574
"contents": base64.StdEncoding.EncodeToString(contents),
14675
"filePath": filePath,
147-
"mustNotExist": opt.MustNotExist,
148-
"withoutCreate": opt.WithoutCreate,
149-
"createDirs": opt.CreateDirs,
15076
"workspaceTool": g.globalOpts.WorkspaceTool,
15177
"base64EncodedInput": true,
78+
"env": g.globalOpts.Env,
15279
})
15380

15481
return err
15582
}
15683

157-
type DeleteFileInWorkspaceOptions struct {
158-
IgnoreNotFound bool
159-
}
160-
161-
func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, workspaceID, filePath string, opts ...DeleteFileInWorkspaceOptions) error {
162-
var opt DeleteFileInWorkspaceOptions
163-
for _, o := range opts {
164-
opt.IgnoreNotFound = opt.IgnoreNotFound || o.IgnoreNotFound
165-
}
166-
84+
func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, workspaceID, filePath string) error {
16785
_, err := g.runBasicCommand(ctx, "workspaces/delete-file", map[string]any{
168-
"id": workspaceID,
169-
"filePath": filePath,
170-
"ignoreNotFound": opt.IgnoreNotFound,
171-
"workspaceTool": g.globalOpts.WorkspaceTool,
86+
"id": workspaceID,
87+
"filePath": filePath,
88+
"workspaceTool": g.globalOpts.WorkspaceTool,
89+
"env": g.globalOpts.Env,
17290
})
17391

17492
return err
@@ -180,6 +98,7 @@ func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, workspaceID, filePa
18098
"filePath": filePath,
18199
"workspaceTool": g.globalOpts.WorkspaceTool,
182100
"base64EncodeOutput": true,
101+
"env": g.globalOpts.Env,
183102
})
184103
if err != nil {
185104
return nil, err

0 commit comments

Comments
 (0)