File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed
Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " oneping" ,
3- "version" : " 1.0.5 " ,
3+ "version" : " 1.0.6 " ,
44 "description" : " Query LLMs and Whisper straight from the web." ,
55 "type" : " module" ,
66 "author" : " Doug Hanley" ,
Original file line number Diff line number Diff line change 11// a javascript mini-library for making chat completion requests to an LLM provider
22
3+ // general utilities
4+
5+ function robustParse ( json ) {
6+ try {
7+ return JSON . parse ( json ) ;
8+ } catch ( e ) {
9+ console . error ( e ) ;
10+ return null ;
11+ }
12+ }
13+
314//
415// authorization
516//
@@ -67,7 +78,8 @@ function* stream_openai(response) {
6778 if ( block . length == 0 ) continue ;
6879 const [ match , data0 ] = / ^ d a t a : ( .* ) $ / . exec ( block )
6980 if ( data0 == '[DONE]' ) break ;
70- const data = JSON . parse ( data0 ) ;
81+ const data = robustParse ( data0 ) ;
82+ if ( data == null ) continue ;
7183 const text = data . choices [ 0 ] . delta . content ;
7284 if ( text != null ) yield text ;
7385 }
@@ -79,7 +91,8 @@ function* stream_anthropic(chunk) {
7991 const [ line1 , line2 ] = block . split ( '\n' )
8092 const [ match1 , event ] = / ^ e v e n t : ( .* ) $ / . exec ( line1 )
8193 const [ match2 , data0 ] = / ^ d a t a : ( .* ) $ / . exec ( line2 )
82- const data = JSON . parse ( data0 ) ;
94+ const data = robustParse ( data0 ) ;
95+ if ( data == null ) continue ;
8396 if ( event == 'content_block_start' ) {
8497 yield data . content_block . text ;
8598 } else if ( event == 'content_block_delta' ) {
You can’t perform that action at this time.
0 commit comments