@@ -587,6 +587,7 @@ function checkResult(result: AxiosResponse) {
587
587
* @param stream 消息流
588
588
*/
589
589
async function receiveStream ( stream : any ) : Promise < any > {
590
+ let temp = Buffer . from ( '' ) ;
590
591
return new Promise ( ( resolve , reject ) => {
591
592
// 消息初始化
592
593
const data = {
@@ -635,18 +636,28 @@ async function receiveStream(stream: any): Promise<any> {
635
636
if ( ! message || ! [ 2001 , 2008 ] . includes ( message . content_type ) )
636
637
return ;
637
638
const content = JSON . parse ( message . content ) ;
638
- if ( content . text ) {
639
- const text = content . text ;
640
- const exceptCharIndex = text . indexOf ( "�" ) ;
641
- data . choices [ 0 ] . message . content += text . substring ( 0 , exceptCharIndex == - 1 ? text . length : exceptCharIndex ) ;
642
- }
639
+ if ( content . text )
640
+ data . choices [ 0 ] . message . content += content . text ;
643
641
} catch ( err ) {
644
642
logger . error ( err ) ;
645
643
reject ( err ) ;
646
644
}
647
645
} ) ;
648
646
// 将流数据喂给SSE转换器
649
- stream . on ( "data" , ( buffer ) => parser . feed ( buffer . toString ( ) ) ) ;
647
+ stream . on ( "data" , ( buffer ) => {
648
+ // 检查buffer是否以完整UTF8字符结尾
649
+ if ( buffer . toString ( ) . indexOf ( '�' ) != - 1 ) {
650
+ // 如果不完整则累积buffer直到收到完整字符
651
+ temp = Buffer . concat ( [ temp , buffer ] ) ;
652
+ return ;
653
+ }
654
+ // 将之前累积的不完整buffer拼接
655
+ if ( temp . length > 0 ) {
656
+ buffer = Buffer . concat ( [ temp , buffer ] ) ;
657
+ temp = Buffer . from ( '' ) ;
658
+ }
659
+ parser . feed ( buffer . toString ( ) ) ;
660
+ } ) ;
650
661
stream . once ( "error" , ( err ) => reject ( err ) ) ;
651
662
stream . once ( "close" , ( ) => resolve ( data ) ) ;
652
663
} ) ;
@@ -662,6 +673,7 @@ async function receiveStream(stream: any): Promise<any> {
662
673
*/
663
674
function createTransStream ( stream : any , endCallback ?: Function ) {
664
675
let convId = "" ;
676
+ let temp = Buffer . from ( '' ) ;
665
677
// 消息创建时间
666
678
const created = util . unixTimestamp ( ) ;
667
679
// 创建转换流
@@ -710,8 +722,9 @@ function createTransStream(stream: any, endCallback?: Function) {
710
722
endCallback && endCallback ( convId ) ;
711
723
return ;
712
724
}
713
- if ( rawResult . event_type != 2001 )
725
+ if ( rawResult . event_type != 2001 ) {
714
726
return ;
727
+ }
715
728
const result = _ . attempt ( ( ) => JSON . parse ( rawResult . event_data ) ) ;
716
729
if ( _ . isError ( result ) )
717
730
throw new Error ( `Stream response invalid: ${ rawResult . event_data } ` ) ;
@@ -740,17 +753,14 @@ function createTransStream(stream: any, endCallback?: Function) {
740
753
return ;
741
754
const content = JSON . parse ( message . content ) ;
742
755
if ( content . text ) {
743
- const text = content . text ;
744
- const exceptCharIndex = text . indexOf ( "�" ) ;
745
- const chunk = text . substring ( 0 , exceptCharIndex == - 1 ? text . length : exceptCharIndex ) ;
746
756
transStream . write ( `data: ${ JSON . stringify ( {
747
757
id : convId ,
748
758
model : MODEL_NAME ,
749
759
object : "chat.completion.chunk" ,
750
760
choices : [
751
761
{
752
762
index : 0 ,
753
- delta : { role : "assistant" , content : chunk } ,
763
+ delta : { role : "assistant" , content : content . text } ,
754
764
finish_reason : null ,
755
765
} ,
756
766
] ,
@@ -763,7 +773,20 @@ function createTransStream(stream: any, endCallback?: Function) {
763
773
}
764
774
} ) ;
765
775
// 将流数据喂给SSE转换器
766
- stream . on ( "data" , ( buffer ) => parser . feed ( buffer . toString ( ) ) ) ;
776
+ stream . on ( "data" , ( buffer ) => {
777
+ // 检查buffer是否以完整UTF8字符结尾
778
+ if ( buffer . toString ( ) . indexOf ( '�' ) != - 1 ) {
779
+ // 如果不完整则累积buffer直到收到完整字符
780
+ temp = Buffer . concat ( [ temp , buffer ] ) ;
781
+ return ;
782
+ }
783
+ // 将之前累积的不完整buffer拼接
784
+ if ( temp . length > 0 ) {
785
+ buffer = Buffer . concat ( [ temp , buffer ] ) ;
786
+ temp = Buffer . from ( '' ) ;
787
+ }
788
+ parser . feed ( buffer . toString ( ) ) ;
789
+ } ) ;
767
790
stream . once (
768
791
"error" ,
769
792
( ) => ! transStream . closed && transStream . end ( "data: [DONE]\n\n" )
0 commit comments