@@ -256,10 +256,8 @@ export class ChatService {
256
256
}
257
257
258
258
const decoder = new TextDecoder ( ) ;
259
- let fullResponse = '' ;
259
+ let aggregatedContent = '' ;
260
260
let fullReasoningContent = '' ;
261
- let regularContent = '' ;
262
- let insideThinkTag = false ;
263
261
let hasReceivedData = false ;
264
262
let lastTimings : ChatMessageTimings | undefined ;
265
263
@@ -277,7 +275,7 @@ export class ChatService {
277
275
if ( line . startsWith ( 'data: ' ) ) {
278
276
const data = line . slice ( 6 ) ;
279
277
if ( data === '[DONE]' ) {
280
- if ( ! hasReceivedData && fullResponse . length === 0 ) {
278
+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
281
279
const contextError = new Error (
282
280
'The request exceeds the available context size. Try increasing the context size or enable context shift.'
283
281
) ;
@@ -286,7 +284,7 @@ export class ChatService {
286
284
return ;
287
285
}
288
286
289
- onComplete ?.( regularContent , fullReasoningContent || undefined , lastTimings ) ;
287
+ onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
290
288
291
289
return ;
292
290
}
@@ -308,44 +306,25 @@ export class ChatService {
308
306
}
309
307
}
310
308
311
- if ( content ) {
312
- hasReceivedData = true ;
313
- fullResponse += content ;
314
-
315
- // Track the regular content before processing this chunk
316
- const regularContentBefore = regularContent ;
317
-
318
- // Process content character by character to handle think tags
319
- insideThinkTag = this . processContentForThinkTags (
320
- content ,
321
- insideThinkTag ,
322
- ( ) => {
323
- // Think content is ignored - we don't include it in API requests
324
- } ,
325
- ( regularChunk ) => {
326
- regularContent += regularChunk ;
327
- }
328
- ) ;
329
-
330
- const newRegularContent = regularContent . slice ( regularContentBefore . length ) ;
331
- if ( newRegularContent ) {
332
- onChunk ?.( newRegularContent ) ;
333
- }
334
- }
335
-
336
- if ( reasoningContent ) {
337
- hasReceivedData = true ;
338
- fullReasoningContent += reasoningContent ;
339
- onReasoningChunk ?.( reasoningContent ) ;
340
- }
309
+ if ( content ) {
310
+ hasReceivedData = true ;
311
+ aggregatedContent += content ;
312
+ onChunk ?.( content ) ;
313
+ }
314
+
315
+ if ( reasoningContent ) {
316
+ hasReceivedData = true ;
317
+ fullReasoningContent += reasoningContent ;
318
+ onReasoningChunk ?.( reasoningContent ) ;
319
+ }
341
320
} catch ( e ) {
342
321
console . error ( 'Error parsing JSON chunk:' , e ) ;
343
322
}
344
323
}
345
324
}
346
325
}
347
326
348
- if ( ! hasReceivedData && fullResponse . length === 0 ) {
327
+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
349
328
const contextError = new Error (
350
329
'The request exceeds the available context size. Try increasing the context size or enable context shift.'
351
330
) ;
@@ -552,51 +531,6 @@ export class ChatService {
552
531
}
553
532
}
554
533
555
- /**
556
- * Processes content to separate thinking tags from regular content.
557
- * Parses <think> and </think> tags to route content to appropriate handlers.
558
- *
559
- * @param content - The content string to process
560
- * @param currentInsideThinkTag - Current state of whether we're inside a think tag
561
- * @param addThinkContent - Callback to handle content inside think tags
562
- * @param addRegularContent - Callback to handle regular content outside think tags
563
- * @returns Boolean indicating if we're still inside a think tag after processing
564
- * @private
565
- */
566
- private processContentForThinkTags (
567
- content : string ,
568
- currentInsideThinkTag : boolean ,
569
- addThinkContent : ( chunk : string ) => void ,
570
- addRegularContent : ( chunk : string ) => void
571
- ) : boolean {
572
- let i = 0 ;
573
- let insideThinkTag = currentInsideThinkTag ;
574
-
575
- while ( i < content . length ) {
576
- if ( ! insideThinkTag && content . substring ( i , i + 7 ) === '<think>' ) {
577
- insideThinkTag = true ;
578
- i += 7 ; // Skip the <think> tag
579
- continue ;
580
- }
581
-
582
- if ( insideThinkTag && content . substring ( i , i + 8 ) === '</think>' ) {
583
- insideThinkTag = false ;
584
- i += 8 ; // Skip the </think> tag
585
- continue ;
586
- }
587
-
588
- if ( insideThinkTag ) {
589
- addThinkContent ( content [ i ] ) ;
590
- } else {
591
- addRegularContent ( content [ i ] ) ;
592
- }
593
-
594
- i ++ ;
595
- }
596
-
597
- return insideThinkTag ;
598
- }
599
-
600
534
/**
601
535
* Aborts any ongoing chat completion request.
602
536
* Cancels the current request and cleans up the abort controller.
0 commit comments