Skip to content

Commit d94e914

Browse files
authored
Merge pull request #73 from thedadams/improve-sdk-server-launch
feat: use new sdk server launch process
2 parents 3206909 + 86178ae commit d94e914

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/gptscript.ts

+25-27
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import http from "http"
22
import path from "path"
33
import child_process from "child_process"
44
import {fileURLToPath} from "url"
5-
import net from "net"
65

76
export interface GlobalOpts {
87
APIKey?: string
@@ -97,26 +96,22 @@ export class GPTScript {
9796
}
9897
})
9998

100-
const u = new URL(GPTScript.serverURL)
101-
if (u.port === "0") {
102-
const srv = net.createServer()
103-
const s = srv.listen(0, () => {
104-
GPTScript.serverURL = "http://" + u.hostname + ":" + String((s.address() as net.AddressInfo).port)
105-
srv.close()
99+
GPTScript.serverProcess = child_process.spawn(getCmdPath(), ["sys.sdkserver", "--listen-address", GPTScript.serverURL.replace("http://", "")], {
100+
env: env,
101+
stdio: ["pipe", "ignore", "pipe"]
102+
})
106103

107-
GPTScript.startGPTScriptProcess(env)
108-
})
109-
} else {
110-
GPTScript.startGPTScriptProcess(env)
111-
}
112-
}
113-
}
104+
GPTScript.serverProcess.stderr?.on("data", (data) => {
105+
let url = data.toString().trim()
106+
if (url.includes("=")) {
107+
url = url.substring(url.indexOf("=") + 1)
108+
}
114109

115-
private static startGPTScriptProcess(env: NodeJS.ProcessEnv) {
116-
GPTScript.serverProcess = child_process.spawn(getCmdPath(), ["sys.sdkserver", "--listen-address", GPTScript.serverURL.replace("http://", "")], {
117-
env: env,
118-
stdio: ["pipe"]
119-
})
110+
GPTScript.serverURL = `http://${url}`
111+
112+
GPTScript.serverProcess.stderr?.removeAllListeners()
113+
})
114+
}
120115
}
121116

122117
close(): void {
@@ -252,16 +247,19 @@ export class GPTScript {
252247
}
253248

254249
private async testGPTScriptURL(count: number): Promise<boolean> {
255-
try {
256-
await fetch(`${GPTScript.serverURL}/healthz`)
257-
return true
258-
} catch {
259-
if (count === 0) {
260-
throw new Error("Failed to wait for gptscript to be ready")
250+
while (count > 0) {
251+
try {
252+
await fetch(`${GPTScript.serverURL}/healthz`)
253+
return true
254+
} catch {
255+
if (count === 0) {
256+
}
257+
await new Promise(r => setTimeout(r, 500))
258+
count--
261259
}
262-
await new Promise(r => setTimeout(r, 500))
263-
return this.testGPTScriptURL(count - 1)
264260
}
261+
262+
throw new Error("Failed to wait for gptscript to be ready")
265263
}
266264
}
267265

0 commit comments

Comments
 (0)