diff --git a/src/gptscript.ts b/src/gptscript.ts index 378af1e..8b40746 100644 --- a/src/gptscript.ts +++ b/src/gptscript.ts @@ -83,9 +83,21 @@ export class GPTScript { this.ready = false GPTScript.instanceCount++ if (!GPTScript.serverURL) { - GPTScript.serverURL = "http://" + (process.env.GPTSCRIPT_URL || "127.0.0.1:0") + GPTScript.serverURL = process.env.GPTSCRIPT_URL || "http://127.0.0.1:0" + if (!GPTScript.serverURL.startsWith("http://") && !GPTScript.serverURL.startsWith("https://")) { + GPTScript.serverURL = "http://" + GPTScript.serverURL + } + } + + if (!this.opts.Env) { + this.opts.Env = [] } - if (GPTScript.instanceCount === 1 && process.env.GPTSCRIPT_DISABLE_SERVER !== "true") { + if (process.env.GPTSCRIPT_URL) { + this.opts.Env.push("GPTSCRIPT_URL=" + GPTScript.serverURL) + return + } + + if (GPTScript.instanceCount === 1) { let env = process.env if (this.opts.Env) { env = { @@ -121,6 +133,10 @@ export class GPTScript { } GPTScript.serverURL = `http://${url}` + if (!this.opts.Env) { + this.opts.Env = [] + } + this.opts.Env.push(`GPTSCRIPT_URL=${GPTScript.serverURL}`) GPTScript.serverProcess.stderr?.removeAllListeners() }) @@ -130,7 +146,7 @@ export class GPTScript { close(): void { GPTScript.instanceCount-- if (GPTScript.instanceCount === 0 && GPTScript.serverProcess) { - GPTScript.serverURL = "http://" + (process.env.GPTSCRIPT_URL || "127.0.0.1:0") + GPTScript.serverURL = process.env.GPTSCRIPT_URL || "http://127.0.0.1:0" GPTScript.serverProcess.kill("SIGTERM") GPTScript.serverProcess.stdin?.end() }