Skip to content

Commit 5885bf0

Browse files
authored
Merge pull request #635 from thedadams/set-ui-env-vars
fix: set common UI env vars regardless of whether a file is passed
2 parents d3cac6e + 904e97f commit 5885bf0

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

pkg/cli/gptscript.go

+18-21
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,25 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
332332

333333
// If the user is trying to launch the chat-builder UI, then set up the tool and options here.
334334
if r.UI {
335-
args = append([]string{uiTool()}, args...)
335+
if os.Getenv(system.BinEnvVar) == "" {
336+
gptOpt.Env = append(gptOpt.Env, system.BinEnvVar+"="+system.Bin())
337+
}
338+
339+
// Pass the corrected environment variables for SDK server options
340+
if r.DefaultModel != "" {
341+
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_DEFAULT_MODEL="+r.DefaultModel)
342+
}
343+
if len(r.CredentialOverride) > 0 {
344+
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_CREDENTIAL_OVERRIDE="+strings.Join(r.CredentialOverride, ","))
345+
}
336346

337347
// If args has more than one element, then the user has provided a file.
338-
if len(args) > 1 {
339-
if args[1] == "-" {
348+
if len(args) > 0 {
349+
file := args[0]
350+
if file == "-" {
340351
return fmt.Errorf("chat UI only supports files, cannot read from stdin")
341352
}
342353

343-
file := args[1]
344-
345354
// If the file is external, then set the SCRIPTS_PATH to the current working directory. Otherwise,
346355
// set it to the directory of the script and set the file to the base.
347356
if !(strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") || strings.HasPrefix(file, "github.com")) {
@@ -359,23 +368,9 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
359368
gptOpt.Env = append(gptOpt.Env, "SCRIPTS_PATH="+cwd)
360369
}
361370

362-
if os.Getenv(system.BinEnvVar) == "" {
363-
gptOpt.Env = append(gptOpt.Env, system.BinEnvVar+"="+system.Bin())
364-
}
365-
366-
// Pass the corrected environment variables for SDK server options
367-
if r.DefaultModel != "" {
368-
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_DEFAULT_MODEL="+r.DefaultModel)
369-
}
370-
if len(r.CredentialOverride) > 0 {
371-
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_CREDENTIAL_OVERRIDE="+strings.Join(r.CredentialOverride, ","))
372-
}
373-
374371
gptOpt.Env = append(gptOpt.Env, "UI_RUN_FILE="+file)
375-
376-
if len(args) > 2 {
377-
args = append(args, args[2:]...)
378-
}
372+
// Remove the file from args because the above line will pass it to the UI tool.
373+
args = args[1:]
379374
} else {
380375
cwd, err := os.Getwd()
381376
if err != nil {
@@ -386,6 +381,8 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
386381

387382
// The UI must run in daemon mode.
388383
r.Daemon = true
384+
// Use the UI tool as the first argument.
385+
args = append([]string{uiTool()}, args...)
389386
}
390387

391388
ctx := cmd.Context()

0 commit comments

Comments
 (0)