From 23709a695f3aa80b8c783118eca06b420c021741 Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Thu, 1 Aug 2024 12:32:42 -0400 Subject: [PATCH] fix: pass the environment with the run when server is disabled If the server is disabled, then it is presumably running with some other environment. In this case, we need to ensure that the run passes the environment so all new environment variables (like prompt server) is properly passed with the run. Signed-off-by: Donnie Adams --- gptscript.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/gptscript.go b/gptscript.go index 8eb688c..753e278 100644 --- a/gptscript.go +++ b/gptscript.go @@ -25,7 +25,8 @@ var ( const relativeToBinaryPath = "" type GPTScript struct { - url string + url string + globalEnv []string } func NewGPTScript(opts GlobalOptions) (*GPTScript, error) { @@ -39,16 +40,18 @@ func NewGPTScript(opts GlobalOptions) (*GPTScript, error) { serverURL = os.Getenv("GPTSCRIPT_URL") } + if opts.Env == nil { + opts.Env = os.Environ() + } + + opts.Env = append(opts.Env, opts.toEnv()...) + if serverProcessCancel == nil && !disableServer { ctx, cancel := context.WithCancel(context.Background()) in, _ := io.Pipe() serverProcess = exec.CommandContext(ctx, getCommand(), "sys.sdkserver", "--listen-address", serverURL) - if opts.Env == nil { - opts.Env = os.Environ() - } - - serverProcess.Env = append(opts.Env[:], opts.toEnv()...) + serverProcess.Env = opts.Env[:] serverProcess.Stdin = in stdErr, err := serverProcess.StderrPipe() @@ -88,7 +91,15 @@ func NewGPTScript(opts GlobalOptions) (*GPTScript, error) { serverURL = strings.TrimSpace(serverURL) } - return &GPTScript{url: "http://" + serverURL}, nil + g := &GPTScript{ + url: "http://" + serverURL, + } + + if disableServer { + g.globalEnv = opts.Env[:] + } + + return g, nil } func readAddress(stdErr io.Reader) (string, error) { @@ -117,6 +128,7 @@ func (g *GPTScript) Close() { } func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) { + opts.Env = append(g.globalEnv, opts.Env...) return (&Run{ url: g.url, requestPath: "evaluate", @@ -127,6 +139,7 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef } func (g *GPTScript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) { + opts.Env = append(g.globalEnv, opts.Env...) return (&Run{ url: g.url, requestPath: "run",