@@ -11,24 +11,21 @@ import (
11
11
)
12
12
13
13
func (s * server ) getDatasetTool (req datasetRequest ) string {
14
- if req .DatasetToolRepo != "" {
15
- return req .DatasetToolRepo
14
+ if req .DatasetTool != "" {
15
+ return req .DatasetTool
16
16
}
17
17
18
18
return s .datasetTool
19
19
}
20
20
21
21
type datasetRequest struct {
22
- Input string `json:"input"`
23
- WorkspaceID string `json:"workspaceID"`
24
- DatasetToolRepo string `json:"datasetToolRepo"`
25
- Env []string `json:"env"`
22
+ Input string `json:"input"`
23
+ DatasetTool string `json:"datasetTool"`
24
+ Env []string `json:"env"`
26
25
}
27
26
28
27
func (r datasetRequest ) validate (requireInput bool ) error {
29
- if r .WorkspaceID == "" {
30
- return fmt .Errorf ("workspaceID is required" )
31
- } else if requireInput && r .Input == "" {
28
+ if requireInput && r .Input == "" {
32
29
return fmt .Errorf ("input is required" )
33
30
} else if len (r .Env ) == 0 {
34
31
return fmt .Errorf ("env is required" )
@@ -38,10 +35,9 @@ func (r datasetRequest) validate(requireInput bool) error {
38
35
39
36
func (r datasetRequest ) opts (o gptscript.Options ) gptscript.Options {
40
37
opts := gptscript.Options {
41
- Cache : o .Cache ,
42
- Monitor : o .Monitor ,
43
- Runner : o .Runner ,
44
- Workspace : r .WorkspaceID ,
38
+ Cache : o .Cache ,
39
+ Monitor : o .Monitor ,
40
+ Runner : o .Runner ,
45
41
}
46
42
return opts
47
43
}
@@ -84,148 +80,19 @@ func (s *server) listDatasets(w http.ResponseWriter, r *http.Request) {
84
80
writeResponse (logger , w , map [string ]any {"stdout" : result })
85
81
}
86
82
87
- type createDatasetArgs struct {
88
- Name string `json:"datasetName"`
89
- Description string `json:"datasetDescription"`
90
- }
91
-
92
- func (a createDatasetArgs ) validate () error {
93
- if a .Name == "" {
94
- return fmt .Errorf ("datasetName is required" )
95
- }
96
- return nil
97
- }
98
-
99
- func (s * server ) createDataset (w http.ResponseWriter , r * http.Request ) {
100
- logger := gcontext .GetLogger (r .Context ())
101
-
102
- var req datasetRequest
103
- if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
104
- writeError (logger , w , http .StatusBadRequest , fmt .Errorf ("failed to decode request body: %w" , err ))
105
- return
106
- }
107
-
108
- if err := req .validate (true ); err != nil {
109
- writeError (logger , w , http .StatusBadRequest , err )
110
- return
111
- }
112
-
113
- g , err := gptscript .New (r .Context (), req .opts (s .gptscriptOpts ))
114
- if err != nil {
115
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to initialize gptscript: %w" , err ))
116
- return
117
- }
118
-
119
- var args createDatasetArgs
120
- if err := json .Unmarshal ([]byte (req .Input ), & args ); err != nil {
121
- writeError (logger , w , http .StatusBadRequest , fmt .Errorf ("failed to unmarshal input: %w" , err ))
122
- return
123
- }
124
-
125
- if err := args .validate (); err != nil {
126
- writeError (logger , w , http .StatusBadRequest , err )
127
- return
128
- }
129
-
130
- prg , err := loader .Program (r .Context (), s .getDatasetTool (req ), "Create Dataset" , loader.Options {
131
- Cache : g .Cache ,
132
- })
133
-
134
- if err != nil {
135
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to load program: %w" , err ))
136
- return
137
- }
138
-
139
- result , err := g .Run (r .Context (), prg , req .Env , req .Input )
140
- if err != nil {
141
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to run program: %w" , err ))
142
- return
143
- }
144
-
145
- writeResponse (logger , w , map [string ]any {"stdout" : result })
146
- }
147
-
148
- type addDatasetElementArgs struct {
149
- DatasetID string `json:"datasetID"`
150
- ElementName string `json:"elementName"`
151
- ElementDescription string `json:"elementDescription"`
152
- ElementContent string `json:"elementContent"`
153
- }
154
-
155
- func (a addDatasetElementArgs ) validate () error {
156
- if a .DatasetID == "" {
157
- return fmt .Errorf ("datasetID is required" )
158
- }
159
- if a .ElementName == "" {
160
- return fmt .Errorf ("elementName is required" )
161
- }
162
- if a .ElementContent == "" {
163
- return fmt .Errorf ("elementContent is required" )
164
- }
165
- return nil
166
- }
167
-
168
- func (s * server ) addDatasetElement (w http.ResponseWriter , r * http.Request ) {
169
- logger := gcontext .GetLogger (r .Context ())
170
-
171
- var req datasetRequest
172
- if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
173
- writeError (logger , w , http .StatusBadRequest , fmt .Errorf ("failed to decode request body: %w" , err ))
174
- return
175
- }
176
-
177
- if err := req .validate (true ); err != nil {
178
- writeError (logger , w , http .StatusBadRequest , err )
179
- return
180
- }
181
-
182
- g , err := gptscript .New (r .Context (), req .opts (s .gptscriptOpts ))
183
- if err != nil {
184
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to initialize gptscript: %w" , err ))
185
- return
186
- }
187
-
188
- var args addDatasetElementArgs
189
- if err := json .Unmarshal ([]byte (req .Input ), & args ); err != nil {
190
- writeError (logger , w , http .StatusBadRequest , fmt .Errorf ("failed to unmarshal input: %w" , err ))
191
- return
192
- }
193
-
194
- if err := args .validate (); err != nil {
195
- writeError (logger , w , http .StatusBadRequest , err )
196
- return
197
- }
198
-
199
- prg , err := loader .Program (r .Context (), s .getDatasetTool (req ), "Add Element" , loader.Options {
200
- Cache : g .Cache ,
201
- })
202
- if err != nil {
203
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to load program: %w" , err ))
204
- return
205
- }
206
-
207
- result , err := g .Run (r .Context (), prg , req .Env , req .Input )
208
- if err != nil {
209
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to run program: %w" , err ))
210
- return
211
- }
212
-
213
- writeResponse (logger , w , map [string ]any {"stdout" : result })
214
- }
215
-
216
83
type addDatasetElementsArgs struct {
217
- DatasetID string `json:"datasetID"`
218
- Elements []struct {
219
- Name string `json:"name"`
220
- Description string `json:"description"`
221
- Contents string `json:"contents"`
222
- }
84
+ DatasetID string `json:"datasetID"`
85
+ Name string `json:"name"`
86
+ Description string `json:"description"`
87
+ Elements []struct {
88
+ Name string `json:"name"`
89
+ Description string `json:"description"`
90
+ Contents string `json:"contents"`
91
+ BinaryContents []byte `json:"binaryContents"`
92
+ } `json:"elements"`
223
93
}
224
94
225
95
func (a addDatasetElementsArgs ) validate () error {
226
- if a .DatasetID == "" {
227
- return fmt .Errorf ("datasetID is required" )
228
- }
229
96
if len (a .Elements ) == 0 {
230
97
return fmt .Errorf ("elements is required" )
231
98
}
@@ -271,13 +138,7 @@ func (s *server) addDatasetElements(w http.ResponseWriter, r *http.Request) {
271
138
return
272
139
}
273
140
274
- elementsJSON , err := json .Marshal (args .Elements )
275
- if err != nil {
276
- writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to marshal elements: %w" , err ))
277
- return
278
- }
279
-
280
- result , err := g .Run (r .Context (), prg , req .Env , fmt .Sprintf (`{"datasetID":%q, "elements":%q}` , args .DatasetID , string (elementsJSON )))
141
+ result , err := g .Run (r .Context (), prg , req .Env , req .Input )
281
142
if err != nil {
282
143
writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to run program: %w" , err ))
283
144
return
@@ -347,15 +208,14 @@ func (s *server) listDatasetElements(w http.ResponseWriter, r *http.Request) {
347
208
348
209
type getDatasetElementArgs struct {
349
210
DatasetID string `json:"datasetID"`
350
- Element string `json:"element "`
211
+ Name string `json:"name "`
351
212
}
352
213
353
214
func (a getDatasetElementArgs ) validate () error {
354
215
if a .DatasetID == "" {
355
216
return fmt .Errorf ("datasetID is required" )
356
- }
357
- if a .Element == "" {
358
- return fmt .Errorf ("element is required" )
217
+ } else if a .Name == "" {
218
+ return fmt .Errorf ("name is required" )
359
219
}
360
220
return nil
361
221
}
@@ -391,7 +251,7 @@ func (s *server) getDatasetElement(w http.ResponseWriter, r *http.Request) {
391
251
return
392
252
}
393
253
394
- prg , err := loader .Program (r .Context (), s .getDatasetTool (req ), "Get Element SDK " , loader.Options {
254
+ prg , err := loader .Program (r .Context (), s .getDatasetTool (req ), "Get Element" , loader.Options {
395
255
Cache : g .Cache ,
396
256
})
397
257
if err != nil {
0 commit comments