From 376315dc9a26c3b038c2f0797b371e97b945a136 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Fri, 18 Oct 2024 16:04:13 -0400 Subject: [PATCH] chore: move the workspace-provider tool to a daemon This also adds support in the daemon implementation for dynamic paths, something that is needed for the workspace-provider daemon to work. Signed-off-by: Donnie Adams --- pkg/engine/http.go | 11 ++++++++++- pkg/sdkserver/workspaces.go | 13 ++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/engine/http.go b/pkg/engine/http.go index a81f1bb3..b46a040a 100644 --- a/pkg/engine/http.go +++ b/pkg/engine/http.go @@ -18,6 +18,11 @@ const DaemonURLSuffix = ".daemon.gptscript.local" func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Tool, input string) (cmdRet *Return, cmdErr error) { envMap := map[string]string{} + for _, env := range appendInputAsEnv(nil, input) { + k, v, _ := strings.Cut(env, "=") + envMap[k] = v + } + for _, env := range e.Env { k, v, _ := strings.Cut(env, "=") envMap[k] = v @@ -25,7 +30,7 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too toolURL := strings.Split(tool.Instructions, "\n")[0][2:] toolURL = os.Expand(toolURL, func(s string) string { - return envMap[s] + return url.PathEscape(envMap[s]) }) parsed, err := url.Parse(toolURL) @@ -61,6 +66,10 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too }, nil } + if body, ok := envMap["BODY"]; ok { + input = body + } + req, err := http.NewRequestWithContext(ctx, http.MethodPost, toolURL, strings.NewReader(input)) if err != nil { return nil, err diff --git a/pkg/sdkserver/workspaces.go b/pkg/sdkserver/workspaces.go index 0cbf94b2..dd2df692 100644 --- a/pkg/sdkserver/workspaces.go +++ b/pkg/sdkserver/workspaces.go @@ -1,6 +1,7 @@ package sdkserver import ( + "encoding/base64" "encoding/json" "fmt" "net/http" @@ -175,8 +176,7 @@ func (s *server) removeAllWithPrefixInWorkspace(w http.ResponseWriter, r *http.R type writeFileInWorkspaceRequest struct { workspaceCommonRequest `json:",inline"` FilePath string `json:"filePath"` - Contents string `json:"contents"` - Base64EncodedInput bool `json:"base64EncodedInput"` + Contents []byte `json:"contents"` } func (s *server) writeFileInWorkspace(w http.ResponseWriter, r *http.Request) { @@ -198,8 +198,8 @@ func (s *server) writeFileInWorkspace(w http.ResponseWriter, r *http.Request) { prg, reqObject.Env, fmt.Sprintf( - `{"workspace_id": "%s", "file_path": "%s", "file_contents": "%s", "write_file_base64_encoded_input": %t}`, - reqObject.ID, reqObject.FilePath, reqObject.Contents, reqObject.Base64EncodedInput, + `{"workspace_id": "%s", "file_path": "%s", "body": "%s"}`, + reqObject.ID, reqObject.FilePath, base64.StdEncoding.EncodeToString(reqObject.Contents), ), ) if err != nil { @@ -249,7 +249,6 @@ func (s *server) removeFileInWorkspace(w http.ResponseWriter, r *http.Request) { type readFileInWorkspaceRequest struct { workspaceCommonRequest `json:",inline"` FilePath string `json:"filePath"` - Base64EncodeOutput bool `json:"base64EncodeOutput"` } func (s *server) readFileInWorkspace(w http.ResponseWriter, r *http.Request) { @@ -271,8 +270,8 @@ func (s *server) readFileInWorkspace(w http.ResponseWriter, r *http.Request) { prg, reqObject.Env, fmt.Sprintf( - `{"workspace_id": "%s", "file_path": "%s", "read_file_base64_encode_output": %t}`, - reqObject.ID, reqObject.FilePath, reqObject.Base64EncodeOutput, + `{"workspace_id": "%s", "file_path": "%s"}`, + reqObject.ID, reqObject.FilePath, ), ) if err != nil {