Skip to content

Commit

Permalink
feat: add workspace API to SDK
Browse files Browse the repository at this point in the history
This change also changes the error behavior when running tools that are
simply wrapped commands. Previously, all such tools would not return an
error, rather an error message in hopes that the LLM would retry.
However, if the tool is just a command (i.e. has no parent), then it
should return an error so that the caller doesn't have to guess whether
an error occurred.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Oct 14, 2024
1 parent eefe829 commit c90b3fe
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@ func (e *Engine) runCommand(ctx Context, tool types.Tool, input string, toolCate
result = stdout

if err := cmd.Run(); err != nil {
if toolCategory == NoCategory {
if toolCategory == NoCategory && ctx.Parent != nil {
// If this is a sub-call, then don't return the error; return the error as a message so that the LLM can retry.
return fmt.Sprintf("ERROR: got (%v) while running tool, OUTPUT: %s", err, stdoutAndErr), nil
}
log.Errorf("failed to run tool [%s] cmd %v: %v", tool.Parameters.Name, cmd.Args, err)
combinedOutput = stdoutAndErr.String()
return "", fmt.Errorf("ERROR: %s: %w", result, err)
return "", fmt.Errorf("ERROR: %s: %w", stdoutAndErr, err)
}

combinedOutput = stdoutAndErr.String()
Expand Down
10 changes: 5 additions & 5 deletions pkg/sdkserver/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *server) listDatasets(w http.ResponseWriter, r *http.Request) {
return
}

prg, err := loader.Program(r.Context(), "List Datasets from "+req.getToolRepo(), "", loader.Options{
prg, err := loader.Program(r.Context(), req.getToolRepo(), "List Datasets", loader.Options{
Cache: g.Cache,
})

Expand Down Expand Up @@ -123,7 +123,7 @@ func (s *server) createDataset(w http.ResponseWriter, r *http.Request) {
return
}

prg, err := loader.Program(r.Context(), "Create Dataset from "+req.getToolRepo(), "", loader.Options{
prg, err := loader.Program(r.Context(), req.getToolRepo(), "Create Dataset", loader.Options{
Cache: g.Cache,
})

Expand Down Expand Up @@ -192,7 +192,7 @@ func (s *server) addDatasetElement(w http.ResponseWriter, r *http.Request) {
return
}

prg, err := loader.Program(r.Context(), "Add Element from "+req.getToolRepo(), "", loader.Options{
prg, err := loader.Program(r.Context(), req.getToolRepo(), "Add Element", loader.Options{
Cache: g.Cache,
})
if err != nil {
Expand Down Expand Up @@ -251,7 +251,7 @@ func (s *server) listDatasetElements(w http.ResponseWriter, r *http.Request) {
return
}

prg, err := loader.Program(r.Context(), "List Elements from "+req.getToolRepo(), "", loader.Options{
prg, err := loader.Program(r.Context(), req.getToolRepo(), "List Elements", loader.Options{
Cache: g.Cache,
})
if err != nil {
Expand Down Expand Up @@ -314,7 +314,7 @@ func (s *server) getDatasetElement(w http.ResponseWriter, r *http.Request) {
return
}

prg, err := loader.Program(r.Context(), "Get Element from "+req.getToolRepo(), "", loader.Options{
prg, err := loader.Program(r.Context(), req.getToolRepo(), "Get Element", loader.Options{
Cache: g.Cache,
})
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/sdkserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func (s *server) addRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /datasets/list-elements", s.listDatasetElements)
mux.HandleFunc("POST /datasets/get-element", s.getDatasetElement)
mux.HandleFunc("POST /datasets/add-element", s.addDatasetElement)

mux.HandleFunc("POST /workspaces/create", s.createWorkspace)
mux.HandleFunc("POST /workspaces/delete", s.deleteWorkspace)
mux.HandleFunc("POST /workspaces/list", s.listWorkspaceContents)
mux.HandleFunc("POST /workspaces/mkdir", s.mkDirInWorkspace)
mux.HandleFunc("POST /workspaces/rmdir", s.rmDirInWorkspace)
mux.HandleFunc("POST /workspaces/write-file", s.writeFileInWorkspace)
mux.HandleFunc("POST /workspaces/delete-file", s.removeFileInWorkspace)
mux.HandleFunc("POST /workspaces/read-file", s.readFileInWorkspace)
}

// health just provides an endpoint for checking whether the server is running and accessible.
Expand Down
Loading

0 comments on commit c90b3fe

Please sign in to comment.