Skip to content

Commit

Permalink
chore: handle workspace ids from sdk natively
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Oct 21, 2024
1 parent 3661044 commit 87863a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/engine/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 18 additions & 1 deletion pkg/gptscript/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-*")
Expand All @@ -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
}

Expand Down

0 comments on commit 87863a7

Please sign in to comment.