Skip to content

fix: pass the environment with the run when server is disabled #50

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
Aug 1, 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
27 changes: 20 additions & 7 deletions gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var (
const relativeToBinaryPath = "<me>"

type GPTScript struct {
url string
url string
globalEnv []string
}

func NewGPTScript(opts GlobalOptions) (*GPTScript, error) {
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down