Skip to content

Commit

Permalink
Merge pull request #446 from thedadams/ui-prompt-env-fixes-2
Browse files Browse the repository at this point in the history
fix: clear prompt env vars when running the UI
  • Loading branch information
thedadams authored Jun 6, 2024
2 parents 8527fa5 + 04fc99b commit bc4f016
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,12 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
}, gptOpt.Env, toolInput, r.SaveChatStateFile)
}

if r.UI {
// If the UI is running, then all prompts should go through the SDK and the UI.
// Not clearing ExtraEnv here would mean that the prompts would go through the terminal.
gptScript.ExtraEnv = nil
}

s, err := gptScript.Run(cmd.Context(), prg, gptOpt.Env, toolInput)
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions pkg/gptscript/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ func New(opts *Options) (*GPTScript, error) {
closeServer()
return nil, err
}
opts.Env = append(opts.Env, extraEnv...)
oaiClient.SetEnvs(opts.Env)
oaiClient.SetEnvs(extraEnv)

remoteClient := remote.New(runner, opts.Env, cacheClient, cliCfg, opts.CredentialContext)
remoteClient := remote.New(runner, extraEnv, cacheClient, cliCfg, opts.CredentialContext)
if err := registry.AddClient(remoteClient); err != nil {
closeServer()
return nil, err
Expand Down
13 changes: 10 additions & 3 deletions pkg/prompt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ import (
)

func NewServer(ctx context.Context, envs []string) ([]string, error) {
var extraEnvs []string
for _, env := range envs {
_, v, ok := strings.Cut(env, types.PromptTokenEnvVar+"=")
if ok && v != "" {
return nil, nil
for _, prefix := range []string{types.PromptURLEnvVar, types.PromptTokenEnvVar} {
v, ok := strings.CutPrefix(env, prefix+"=")
if ok && v != "" {
extraEnvs = append(extraEnvs, env)
}
}
}

if len(extraEnvs) == 2 {
return extraEnvs, nil
}

l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdkserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *server) execHandler(w http.ResponseWriter, r *http.Request) {
// Don't overwrite the PromptURLEnvVar if it is already set in the environment.
var promptTokenAlreadySet bool
for _, env := range reqObject.Env {
if strings.HasPrefix(env, types.PromptTokenEnvVar+"=") {
if v, ok := strings.CutPrefix(env, types.PromptTokenEnvVar+"="); ok && v != "" {
promptTokenAlreadySet = true
break
}
Expand Down

0 comments on commit bc4f016

Please sign in to comment.