Skip to content

Commit 415a4a9

Browse files
committed
fix: add temporary fix for event processing
Signed-off-by: Donnie Adams <[email protected]>
1 parent 3bbcf31 commit 415a4a9

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "v0.5.0",
44
"description": "Run gptscript in node.js",
55
"main": "dist/gptscript.js",
6-
"type": "module",
76
"repository": {
87
"type": "git",
98
"url": "git+https://github.com/gptscript-ai/node-gptscript.git"

src/gptscript.ts

+26-21
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,32 @@ export class Run {
184184
this.req = http.request(options, (res: http.IncomingMessage) => {
185185
this.state = RunState.Running
186186
res.on("data", (chunk: any) => {
187-
const c = chunk.toString().replace(/^(data: )/, "").trim()
188-
if (c === "[DONE]") {
189-
return
190-
}
191-
192-
let e: any
193-
try {
194-
e = JSON.parse(frag + c)
195-
} catch {
196-
frag += c
197-
return
198-
}
199-
frag = ""
200-
201-
if (e.stderr) {
202-
this.stderr = (this.stderr || "") + (typeof e.stderr === "string" ? e.stderr : JSON.stringify(e.stderr))
203-
} else if (e.stdout) {
204-
this.stdout = (this.stdout || "") + (typeof e.stdout === "string" ? e.stdout : JSON.stringify(e.stdout))
205-
} else {
206-
frag = this.emitEvent(frag + c)
187+
for (let line of (chunk.toString() + frag).split("\n")) {
188+
const c = line.replace(/^(data: )/, "").trim()
189+
if (!c) {
190+
continue
191+
}
192+
193+
if (c === "[DONE]") {
194+
return
195+
}
196+
197+
let e: any
198+
try {
199+
e = JSON.parse(c)
200+
} catch {
201+
frag = c
202+
return
203+
}
204+
frag = ""
205+
206+
if (e.stderr) {
207+
this.stderr = (this.stderr || "") + (typeof e.stderr === "string" ? e.stderr : JSON.stringify(e.stderr))
208+
} else if (e.stdout) {
209+
this.stdout = (this.stdout || "") + (typeof e.stdout === "string" ? e.stdout : JSON.stringify(e.stdout))
210+
} else {
211+
frag = this.emitEvent(c)
212+
}
207213
}
208214
})
209215

@@ -270,7 +276,6 @@ export class Run {
270276
if (!event) {
271277
continue
272278
}
273-
274279
let f: Frame
275280
try {
276281
f = JSON.parse(event) as Frame

0 commit comments

Comments
 (0)