Skip to content

fix: set common UI env vars regardless of whether a file is passed #635

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
Jul 15, 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
39 changes: 18 additions & 21 deletions pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,25 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {

// If the user is trying to launch the chat-builder UI, then set up the tool and options here.
if r.UI {
args = append([]string{uiTool()}, args...)
if os.Getenv(system.BinEnvVar) == "" {
gptOpt.Env = append(gptOpt.Env, system.BinEnvVar+"="+system.Bin())
}

// Pass the corrected environment variables for SDK server options
if r.DefaultModel != "" {
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_DEFAULT_MODEL="+r.DefaultModel)
}
if len(r.CredentialOverride) > 0 {
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_CREDENTIAL_OVERRIDE="+strings.Join(r.CredentialOverride, ","))
}

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

file := args[1]

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

if os.Getenv(system.BinEnvVar) == "" {
gptOpt.Env = append(gptOpt.Env, system.BinEnvVar+"="+system.Bin())
}

// Pass the corrected environment variables for SDK server options
if r.DefaultModel != "" {
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_DEFAULT_MODEL="+r.DefaultModel)
}
if len(r.CredentialOverride) > 0 {
gptOpt.Env = append(gptOpt.Env, "GPTSCRIPT_SDKSERVER_CREDENTIAL_OVERRIDE="+strings.Join(r.CredentialOverride, ","))
}

gptOpt.Env = append(gptOpt.Env, "UI_RUN_FILE="+file)

if len(args) > 2 {
args = append(args, args[2:]...)
}
Comment on lines -376 to -378
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this piece no longer needed? Just want to make sure its removal is not accidental.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did intend to remove it. The original intention of this code was to remove the file from the args and keep the remaining args. I have added logic to this PR for the same purpose.

// Remove the file from args because the above line will pass it to the UI tool.
args = args[1:]
} else {
cwd, err := os.Getwd()
if err != nil {
Expand All @@ -386,6 +381,8 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {

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

ctx := cmd.Context()
Expand Down