Skip to content

Commit e8d62c6

Browse files
hack: add load method
1 parent 657256e commit e8d62c6

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Diff for: pkg/builtin/builtin.go

+5
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,11 @@ func invalidArgument(input string, err error) string {
681681
func SysContext(ctx context.Context, _ []string, _ string, _ chan<- string) (string, error) {
682682
engineContext, _ := engine.FromContext(ctx)
683683

684+
// Don't get the current call, because that is SysContext, but get the calling tool, if possible
685+
if engineContext.Parent != nil {
686+
engineContext = engineContext.Parent
687+
}
688+
684689
callContext := *engineContext.GetCallContext()
685690
callContext.ID = ""
686691
callContext.ParentID = ""

Diff for: pkg/sdkserver/routes.go

+38
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (s *server) addRoutes(mux *http.ServeMux) {
5050
mux.HandleFunc("POST /run", s.execHandler)
5151
mux.HandleFunc("POST /evaluate", s.execHandler)
5252

53+
mux.HandleFunc("POST /load", s.load)
54+
5355
mux.HandleFunc("POST /parse", s.parse)
5456
mux.HandleFunc("POST /fmt", s.fmtDocument)
5557

@@ -212,6 +214,42 @@ func (s *server) execHandler(w http.ResponseWriter, r *http.Request) {
212214
s.execAndStream(ctx, programLoader, logger, w, opts, reqObject.ChatState, reqObject.Input, reqObject.SubTool, def)
213215
}
214216

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+
215253
// parse will parse the file and return the corresponding Document.
216254
func (s *server) parse(w http.ResponseWriter, r *http.Request) {
217255
logger := gcontext.GetLogger(r.Context())

Diff for: pkg/sdkserver/types.go

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ func (f *file) String() string {
8282
return f.File
8383
}
8484

85+
type loadRequest struct {
86+
content `json:",inline"`
87+
88+
ToolDefs toolDefs `json:"toolDefs,inline"`
89+
DisableCache bool `json:"disableCache"`
90+
SubTool string `json:"subTool,omitempty"`
91+
File string `json:"file"`
92+
}
93+
8594
type parseRequest struct {
8695
parser.Options `json:",inline"`
8796
content `json:",inline"`

0 commit comments

Comments
 (0)