File tree 1 file changed +6
-3
lines changed
1 file changed +6
-3
lines changed Original file line number Diff line number Diff line change @@ -215,12 +215,14 @@ class OpenAI
215
215
create_stream_filter : ( chunk_callback) =>
216
216
assert types. function( chunk_callback) , " Must provide chunk_callback function when streaming response"
217
217
218
- accumulation_buffer = " "
218
+ accumulation_buffer, streamed = " "
219
219
220
220
( ... ) ->
221
221
chunk = ...
222
222
223
- if type ( chunk) == " string"
223
+ if chunk == nil
224
+ assert not streamed or accumulation_buffer\ match" ^%s*$" , " buffer not empty"
225
+ elseif type ( chunk) == " string"
224
226
accumulation_buffer ..= chunk
225
227
226
228
while true
@@ -233,8 +235,9 @@ class OpenAI
233
235
field, value, rest_evt = event\ match " ^(.-):%s+([^\r\n ]+)(.-)$"
234
236
switch field
235
237
when " data"
238
+ streamed = true
236
239
unless value== " [DONE]"
237
- chunk_callback ( json . decode value)
240
+ chunk_callback ( cjson . decode value)
238
241
when " event" , " id" , " retry" , " " -- comment
239
242
nil -- noop
240
243
when nil
You can’t perform that action at this time.
0 commit comments