Skip to content

Commit

Permalink
fix: set common UI env vars regardless of whether a file is passed
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Jul 15, 2024
1 parent d3cac6e commit 904e97f
Showing 1 changed file with 18 additions and 21 deletions.
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:]...)
}
// 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

0 comments on commit 904e97f

Please sign in to comment.