Skip to content

Commit e4d1acf

Browse files
authored
Merge pull request #50 from thedadams/pass-env-with-run
fix: pass the environment with the run when server is disabled
2 parents b67275f + 23709a6 commit e4d1acf

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

gptscript.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var (
2525
const relativeToBinaryPath = "<me>"
2626

2727
type GPTScript struct {
28-
url string
28+
url string
29+
globalEnv []string
2930
}
3031

3132
func NewGPTScript(opts GlobalOptions) (*GPTScript, error) {
@@ -39,16 +40,18 @@ func NewGPTScript(opts GlobalOptions) (*GPTScript, error) {
3940
serverURL = os.Getenv("GPTSCRIPT_URL")
4041
}
4142

43+
if opts.Env == nil {
44+
opts.Env = os.Environ()
45+
}
46+
47+
opts.Env = append(opts.Env, opts.toEnv()...)
48+
4249
if serverProcessCancel == nil && !disableServer {
4350
ctx, cancel := context.WithCancel(context.Background())
4451
in, _ := io.Pipe()
4552

4653
serverProcess = exec.CommandContext(ctx, getCommand(), "sys.sdkserver", "--listen-address", serverURL)
47-
if opts.Env == nil {
48-
opts.Env = os.Environ()
49-
}
50-
51-
serverProcess.Env = append(opts.Env[:], opts.toEnv()...)
54+
serverProcess.Env = opts.Env[:]
5255

5356
serverProcess.Stdin = in
5457
stdErr, err := serverProcess.StderrPipe()
@@ -88,7 +91,15 @@ func NewGPTScript(opts GlobalOptions) (*GPTScript, error) {
8891

8992
serverURL = strings.TrimSpace(serverURL)
9093
}
91-
return &GPTScript{url: "http://" + serverURL}, nil
94+
g := &GPTScript{
95+
url: "http://" + serverURL,
96+
}
97+
98+
if disableServer {
99+
g.globalEnv = opts.Env[:]
100+
}
101+
102+
return g, nil
92103
}
93104

94105
func readAddress(stdErr io.Reader) (string, error) {
@@ -117,6 +128,7 @@ func (g *GPTScript) Close() {
117128
}
118129

119130
func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
131+
opts.Env = append(g.globalEnv, opts.Env...)
120132
return (&Run{
121133
url: g.url,
122134
requestPath: "evaluate",
@@ -127,6 +139,7 @@ func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
127139
}
128140

129141
func (g *GPTScript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) {
142+
opts.Env = append(g.globalEnv, opts.Env...)
130143
return (&Run{
131144
url: g.url,
132145
requestPath: "run",

0 commit comments

Comments
 (0)