File tree 1 file changed +13
-6
lines changed
1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -319,8 +319,8 @@ function translate(query, completion) {
319
319
const header = buildHeader ( isAzureServiceProvider , apiKey ) ;
320
320
const body = buildRequestBody ( model , isChatGPTModel , query ) ;
321
321
322
- // 初始化拼接结果变量
323
- let targetText = "" ;
322
+ let targetText = "" ; // 初始化拼接结果变量
323
+ let buffer = "" ; // 新增 buffer 变量
324
324
( async ( ) => {
325
325
await $http . streamRequest ( {
326
326
method : "POST" ,
@@ -338,14 +338,21 @@ function translate(query, completion) {
338
338
} ,
339
339
} ) ;
340
340
} else {
341
- const lines = streamData . text . split ( '\n' ) . filter ( line => line ) ;
342
- lines . forEach ( line => {
343
- const match = line . match ( / ^ d a t a : ( .* ) / ) ;
341
+ // 将新的数据添加到缓冲变量中
342
+ buffer += streamData . text ;
343
+ // 检查缓冲变量是否包含一个完整的消息
344
+ while ( true ) {
345
+ const match = buffer . match ( / d a t a : ( .* ?} ) \n / ) ;
344
346
if ( match ) {
347
+ // 如果是一个完整的消息,处理它并从缓冲变量中移除
345
348
const textFromResponse = match [ 1 ] . trim ( ) ;
346
349
targetText = handleResponse ( query , isChatGPTModel , targetText , textFromResponse ) ;
350
+ buffer = buffer . slice ( match [ 0 ] . length ) ;
351
+ } else {
352
+ // 如果没有完整的消息,等待更多的数据
353
+ break ;
347
354
}
348
- } ) ;
355
+ }
349
356
}
350
357
} ,
351
358
handler : ( result ) => {
You can’t perform that action at this time.
0 commit comments