@@ -50,6 +50,8 @@ func (s *server) addRoutes(mux *http.ServeMux) {
50
50
mux .HandleFunc ("POST /run" , s .execHandler )
51
51
mux .HandleFunc ("POST /evaluate" , s .execHandler )
52
52
53
+ mux .HandleFunc ("POST /load" , s .load )
54
+
53
55
mux .HandleFunc ("POST /parse" , s .parse )
54
56
mux .HandleFunc ("POST /fmt" , s .fmtDocument )
55
57
@@ -212,6 +214,42 @@ func (s *server) execHandler(w http.ResponseWriter, r *http.Request) {
212
214
s .execAndStream (ctx , programLoader , logger , w , opts , reqObject .ChatState , reqObject .Input , reqObject .SubTool , def )
213
215
}
214
216
217
+ // load will load the file and return the corresponding Program.
218
+ func (s * server ) load (w http.ResponseWriter , r * http.Request ) {
219
+ logger := gcontext .GetLogger (r .Context ())
220
+ reqObject := new (loadRequest )
221
+ if err := json .NewDecoder (r .Body ).Decode (reqObject ); err != nil {
222
+ writeError (logger , w , http .StatusBadRequest , fmt .Errorf ("invalid request body: %w" , err ))
223
+ return
224
+ }
225
+
226
+ logger .Debugf ("parsing file: file=%s, content=%s" , reqObject .File , reqObject .Content )
227
+
228
+ var (
229
+ prg types.Program
230
+ err error
231
+ cache = s .client .Cache
232
+ )
233
+
234
+ if reqObject .DisableCache {
235
+ cache = nil
236
+ }
237
+
238
+ if reqObject .Content != "" {
239
+ prg , err = loader .ProgramFromSource (r .Context (), reqObject .Content , reqObject .SubTool , loader.Options {Cache : cache })
240
+ } else if reqObject .File != "" {
241
+ prg , err = loader .Program (r .Context (), reqObject .File , reqObject .SubTool , loader.Options {Cache : cache })
242
+ } else {
243
+ prg , err = loader .ProgramFromSource (r .Context (), reqObject .ToolDefs .String (), reqObject .SubTool , loader.Options {Cache : cache })
244
+ }
245
+ if err != nil {
246
+ writeError (logger , w , http .StatusInternalServerError , fmt .Errorf ("failed to load program: %w" , err ))
247
+ return
248
+ }
249
+
250
+ writeResponse (logger , w , map [string ]any {"program" : prg })
251
+ }
252
+
215
253
// parse will parse the file and return the corresponding Document.
216
254
func (s * server ) parse (w http.ResponseWriter , r * http.Request ) {
217
255
logger := gcontext .GetLogger (r .Context ())
0 commit comments