Skip to content

Commit c3ada36

Browse files
committed
fix: streamline JSON parsing and response handling in stream handler
1 parent 1972d1a commit c3ada36

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,11 @@ function handleResponse(query, targetText, textFromResponse) {
212212
if (textFromResponse !== '[DONE]') {
213213
try {
214214
const dataObj = JSON.parse(textFromResponse);
215+
// https://github.com/openai/openai-node/blob/master/src/resources/chat/completions.ts#L190
215216
const { choices } = dataObj;
216-
if (choices && choices[0] && choices[0].delta.content) {
217-
targetText += choices[0].delta.content;
217+
const delta = choices[0]?.delta?.content;
218+
if (delta) {
219+
targetText += delta;
218220
query.onStream({
219221
result: {
220222
from: query.detectFrom,

0 commit comments

Comments
 (0)