From 87863a7ef6405b088400fc014658907ae1073fdf Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 21 Oct 2024 08:21:09 -0700 Subject: [PATCH] chore: handle workspace ids from sdk natively --- pkg/engine/http.go | 6 ++++++ pkg/gptscript/gptscript.go | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/engine/http.go b/pkg/engine/http.go index b46a040a..304f89c4 100644 --- a/pkg/engine/http.go +++ b/pkg/engine/http.go @@ -75,6 +75,12 @@ func (e *Engine) runHTTP(ctx context.Context, prg *types.Program, tool types.Too return nil, err } + for _, env := range e.Env { + if strings.HasPrefix(env, "GPTSCRIPT_") { + req.Header.Add("X-GPTScript-Env", env) + } + } + req.Header.Set("X-GPTScript-Tool-Name", tool.Parameters.Name) if err := json.Unmarshal([]byte(input), &map[string]any{}); err == nil { diff --git a/pkg/gptscript/gptscript.go b/pkg/gptscript/gptscript.go index 679eb503..454debf6 100644 --- a/pkg/gptscript/gptscript.go +++ b/pkg/gptscript/gptscript.go @@ -168,6 +168,20 @@ func New(ctx context.Context, o ...Options) (*GPTScript, error) { } func (g *GPTScript) getEnv(env []string) ([]string, error) { + var ( + id string + ) + + scheme, rest, isScheme := strings.Cut(g.WorkspacePath, "://") + if isScheme && scheme == "directory" { + id = g.WorkspacePath + g.WorkspacePath = rest + } else if isScheme { + id = g.WorkspacePath + g.WorkspacePath = "" + g.DeleteWorkspaceOnClose = true + } + if g.WorkspacePath == "" { var err error g.WorkspacePath, err = os.MkdirTemp("", "gptscript-workspace-*") @@ -184,9 +198,12 @@ func (g *GPTScript) getEnv(env []string) ([]string, error) { if err := os.MkdirAll(g.WorkspacePath, 0700); err != nil { return nil, err } + if id == "" { + id = hash.ID(g.WorkspacePath) + } return slices.Concat(g.ExtraEnv, env, []string{ fmt.Sprintf("GPTSCRIPT_WORKSPACE_DIR=%s", g.WorkspacePath), - fmt.Sprintf("GPTSCRIPT_WORKSPACE_ID=%s", hash.ID(g.WorkspacePath)), + fmt.Sprintf("GPTSCRIPT_WORKSPACE_ID=%s", id), }), nil }