Skip to content

Commit

Permalink
fix: add temporary fix for event processing
Browse files Browse the repository at this point in the history
Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed May 2, 2024
1 parent 3bbcf31 commit 415a4a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "v0.5.0",
"description": "Run gptscript in node.js",
"main": "dist/gptscript.js",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/gptscript-ai/node-gptscript.git"
Expand Down
47 changes: 26 additions & 21 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,32 @@ export class Run {
this.req = http.request(options, (res: http.IncomingMessage) => {
this.state = RunState.Running
res.on("data", (chunk: any) => {
const c = chunk.toString().replace(/^(data: )/, "").trim()
if (c === "[DONE]") {
return
}

let e: any
try {
e = JSON.parse(frag + c)
} catch {
frag += c
return
}
frag = ""

if (e.stderr) {
this.stderr = (this.stderr || "") + (typeof e.stderr === "string" ? e.stderr : JSON.stringify(e.stderr))
} else if (e.stdout) {
this.stdout = (this.stdout || "") + (typeof e.stdout === "string" ? e.stdout : JSON.stringify(e.stdout))
} else {
frag = this.emitEvent(frag + c)
for (let line of (chunk.toString() + frag).split("\n")) {
const c = line.replace(/^(data: )/, "").trim()
if (!c) {
continue
}

if (c === "[DONE]") {
return
}

let e: any
try {
e = JSON.parse(c)
} catch {
frag = c
return
}
frag = ""

if (e.stderr) {
this.stderr = (this.stderr || "") + (typeof e.stderr === "string" ? e.stderr : JSON.stringify(e.stderr))
} else if (e.stdout) {
this.stdout = (this.stdout || "") + (typeof e.stdout === "string" ? e.stdout : JSON.stringify(e.stdout))
} else {
frag = this.emitEvent(c)
}
}
})

Expand Down Expand Up @@ -270,7 +276,6 @@ export class Run {
if (!event) {
continue
}

let f: Frame
try {
f = JSON.parse(event) as Frame
Expand Down

0 comments on commit 415a4a9

Please sign in to comment.