@@ -500,6 +500,22 @@ export class ChatBaiduWenxin
500
500
this . accessToken = await this . getAccessToken ( ) ;
501
501
}
502
502
503
+ const findFirstNewlineIndex = ( data : Uint8Array ) => {
504
+ for ( let i = 0 ; i < data . length ; ) {
505
+ if ( data [ i ] === 10 ) return i ;
506
+ if ( ( data [ i ] & 0b11100000 ) === 0b11000000 ) {
507
+ i += 2 ;
508
+ } else if ( ( data [ i ] & 0b11110000 ) === 0b11100000 ) {
509
+ i += 3 ;
510
+ } else if ( ( data [ i ] & 0b11111000 ) === 0b11110000 ) {
511
+ i += 4 ;
512
+ } else {
513
+ i += 1 ;
514
+ }
515
+ }
516
+ return - 1 ;
517
+ } ;
518
+
503
519
const makeCompletionRequest = async ( ) => {
504
520
const url = `${ this . apiUrl } ?access_token=${ this . accessToken } ` ;
505
521
const response = await fetch ( url , {
@@ -532,7 +548,7 @@ export class ChatBaiduWenxin
532
548
const reader = response . body . getReader ( ) ;
533
549
534
550
const decoder = new TextDecoder ( "utf-8" ) ;
535
- let data = "" ;
551
+ let dataArrayBuffer = new Uint8Array ( 0 ) ;
536
552
537
553
let continueReading = true ;
538
554
while ( continueReading ) {
@@ -541,17 +557,30 @@ export class ChatBaiduWenxin
541
557
continueReading = false ;
542
558
break ;
543
559
}
544
- data += decoder . decode ( value ) ;
560
+ // merge the data first then decode in case of the Chinese characters are split between chunks
561
+ const mergedArray = new Uint8Array (
562
+ dataArrayBuffer . length + value . length
563
+ ) ;
564
+ mergedArray . set ( dataArrayBuffer ) ;
565
+ mergedArray . set ( value , dataArrayBuffer . length ) ;
566
+ dataArrayBuffer = mergedArray ;
545
567
546
568
let continueProcessing = true ;
547
569
while ( continueProcessing ) {
548
- const newlineIndex = data . indexOf ( "\n" ) ;
570
+ const newlineIndex = findFirstNewlineIndex ( dataArrayBuffer ) ;
549
571
if ( newlineIndex === - 1 ) {
550
572
continueProcessing = false ;
551
573
break ;
552
574
}
553
- const line = data . slice ( 0 , newlineIndex ) ;
554
- data = data . slice ( newlineIndex + 1 ) ;
575
+
576
+ const lineArrayBuffer = dataArrayBuffer . slice (
577
+ 0 ,
578
+ findFirstNewlineIndex ( dataArrayBuffer )
579
+ ) ;
580
+ const line = decoder . decode ( lineArrayBuffer ) ;
581
+ dataArrayBuffer = dataArrayBuffer . slice (
582
+ findFirstNewlineIndex ( dataArrayBuffer ) + 1
583
+ ) ;
555
584
556
585
if ( line . startsWith ( "data:" ) ) {
557
586
const event = new MessageEvent ( "message" , {
0 commit comments