-
Notifications
You must be signed in to change notification settings - Fork 8
/
workspace.go
256 lines (213 loc) · 6.27 KB
/
workspace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package gptscript
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
type NotFoundInWorkspaceError struct {
id string
name string
}
func (e *NotFoundInWorkspaceError) Error() string {
return fmt.Sprintf("not found: %s/%s", e.id, e.name)
}
func newNotFoundInWorkspaceError(id, name string) *NotFoundInWorkspaceError {
return &NotFoundInWorkspaceError{id: id, name: name}
}
func (g *GPTScript) CreateWorkspace(ctx context.Context, providerType string, fromWorkspaces ...string) (string, error) {
out, err := g.runBasicCommand(ctx, "workspaces/create", map[string]any{
"providerType": providerType,
"fromWorkspaceIDs": fromWorkspaces,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
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": workspaceID,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
return err
}
type ListFilesInWorkspaceOptions struct {
WorkspaceID string
Prefix string
}
func (g *GPTScript) ListFilesInWorkspace(ctx context.Context, opts ...ListFilesInWorkspaceOptions) ([]string, error) {
var opt ListFilesInWorkspaceOptions
for _, o := range opts {
if o.Prefix != "" {
opt.Prefix = o.Prefix
}
if o.WorkspaceID != "" {
opt.WorkspaceID = o.WorkspaceID
}
}
if opt.WorkspaceID == "" {
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
}
out, err := g.runBasicCommand(ctx, "workspaces/list", map[string]any{
"id": opt.WorkspaceID,
"prefix": opt.Prefix,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
if err != nil {
return nil, err
}
out = strings.TrimSpace(out)
if len(out) == 0 {
return nil, nil
}
var files []string
return files, json.Unmarshal([]byte(out), &files)
}
type RemoveAllOptions struct {
WorkspaceID string
WithPrefix string
}
func (g *GPTScript) RemoveAll(ctx context.Context, opts ...RemoveAllOptions) error {
var opt RemoveAllOptions
for _, o := range opts {
if o.WithPrefix != "" {
opt.WithPrefix = o.WithPrefix
}
if o.WorkspaceID != "" {
opt.WorkspaceID = o.WorkspaceID
}
}
if opt.WorkspaceID == "" {
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
}
_, err := g.runBasicCommand(ctx, "workspaces/remove-all-with-prefix", map[string]any{
"id": opt.WorkspaceID,
"prefix": opt.WithPrefix,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
return err
}
type WriteFileInWorkspaceOptions struct {
WorkspaceID string
}
func (g *GPTScript) WriteFileInWorkspace(ctx context.Context, filePath string, contents []byte, opts ...WriteFileInWorkspaceOptions) error {
var opt WriteFileInWorkspaceOptions
for _, o := range opts {
if o.WorkspaceID != "" {
opt.WorkspaceID = o.WorkspaceID
}
}
if opt.WorkspaceID == "" {
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
}
_, err := g.runBasicCommand(ctx, "workspaces/write-file", map[string]any{
"id": opt.WorkspaceID,
"contents": base64.StdEncoding.EncodeToString(contents),
"filePath": filePath,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
return err
}
type DeleteFileInWorkspaceOptions struct {
WorkspaceID string
}
func (g *GPTScript) DeleteFileInWorkspace(ctx context.Context, filePath string, opts ...DeleteFileInWorkspaceOptions) error {
var opt DeleteFileInWorkspaceOptions
for _, o := range opts {
if o.WorkspaceID != "" {
opt.WorkspaceID = o.WorkspaceID
}
}
if opt.WorkspaceID == "" {
opt.WorkspaceID = os.Getenv("GPTSCRIPT_WORKSPACE_ID")
}
_, err := g.runBasicCommand(ctx, "workspaces/delete-file", map[string]any{
"id": opt.WorkspaceID,
"filePath": filePath,
"workspaceTool": g.globalOpts.WorkspaceTool,
"env": g.globalOpts.Env,
})
if err != nil && strings.HasSuffix(err.Error(), fmt.Sprintf("not found: %s/%s", opt.WorkspaceID, filePath)) {
return newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
}
return err
}
type ReadFileInWorkspaceOptions struct {
WorkspaceID string
}
func (g *GPTScript) ReadFileInWorkspace(ctx context.Context, filePath string, opts ...ReadFileInWorkspaceOptions) ([]byte, error) {
var opt ReadFileInWorkspaceOptions
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/read-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 nil, newNotFoundInWorkspaceError(opt.WorkspaceID, filePath)
}
return nil, err
}
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
MimeType string
}