Skip to content

enhance: add support for gptscript.env file in workspace #554

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
Jun 26, 2024
Merged
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
42 changes: 42 additions & 0 deletions pkg/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ func SysExec(_ context.Context, env []string, input string, progress chan<- stri
}
combined = io.MultiWriter(&out, &pw)
)

if envvars, err := getWorkspaceEnvFileContents(env); err == nil {
env = append(env, envvars...)
}

cmd.Env = env
cmd.Dir = params.Directory
cmd.Stdout = combined
Expand All @@ -355,6 +360,43 @@ func (pw *progressWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func getWorkspaceEnvFileContents(envs []string) ([]string, error) {
dir, err := getWorkspaceDir(envs)
if err != nil {
return nil, err
}

file := filepath.Join(dir, "gptscript.env")

// Lock the file to prevent concurrent writes from other tool calls.
locker.RLock(file)
defer locker.RUnlock(file)

// This is optional, so no errors are returned if the file does not exist.
log.Debugf("Reading file %s", file)
data, err := os.ReadFile(file)
if errors.Is(err, fs.ErrNotExist) {
log.Debugf("The file %s does not exist", file)
return []string{}, nil
} else if err != nil {
log.Debugf("Failed to read file %s: %v", file, err.Error())
return []string{}, nil
}

lines := strings.Split(string(data), "\n")
var envContents []string

for _, line := range lines {
line = strings.TrimSpace(line)
if strings.Contains(line, "=") {
envContents = append(envContents, line)
}
}

return envContents, nil

}

func getWorkspaceDir(envs []string) (string, error) {
for _, env := range envs {
dir, ok := strings.CutPrefix(env, "GPTSCRIPT_WORKSPACE_DIR=")
Expand Down
Loading