Skip to content

chore: handle workspace ids from sdk natively #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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