@@ -262,10 +262,8 @@ export class ChatService {
262262 }
263263
264264 const decoder = new TextDecoder ( ) ;
265- let fullResponse = '' ;
265+ let aggregatedContent = '' ;
266266 let fullReasoningContent = '' ;
267- let regularContent = '' ;
268- let insideThinkTag = false ;
269267 let hasReceivedData = false ;
270268 let lastTimings : ChatMessageTimings | undefined ;
271269
@@ -283,7 +281,7 @@ export class ChatService {
283281 if ( line . startsWith ( 'data: ' ) ) {
284282 const data = line . slice ( 6 ) ;
285283 if ( data === '[DONE]' ) {
286- if ( ! hasReceivedData && fullResponse . length === 0 ) {
284+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
287285 const contextError = new Error (
288286 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
289287 ) ;
@@ -292,7 +290,7 @@ export class ChatService {
292290 return ;
293291 }
294292
295- onComplete ?.( regularContent , fullReasoningContent || undefined , lastTimings ) ;
293+ onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
296294
297295 return ;
298296 }
@@ -314,44 +312,25 @@ export class ChatService {
314312 }
315313 }
316314
317- if ( content ) {
318- hasReceivedData = true ;
319- fullResponse += content ;
320-
321- // Track the regular content before processing this chunk
322- const regularContentBefore = regularContent ;
323-
324- // Process content character by character to handle think tags
325- insideThinkTag = this . processContentForThinkTags (
326- content ,
327- insideThinkTag ,
328- ( ) => {
329- // Think content is ignored - we don't include it in API requests
330- } ,
331- ( regularChunk ) => {
332- regularContent += regularChunk ;
333- }
334- ) ;
335-
336- const newRegularContent = regularContent . slice ( regularContentBefore . length ) ;
337- if ( newRegularContent ) {
338- onChunk ?.( newRegularContent ) ;
339- }
340- }
341-
342- if ( reasoningContent ) {
343- hasReceivedData = true ;
344- fullReasoningContent += reasoningContent ;
345- onReasoningChunk ?.( reasoningContent ) ;
346- }
315+ if ( content ) {
316+ hasReceivedData = true ;
317+ aggregatedContent += content ;
318+ onChunk ?.( content ) ;
319+ }
320+
321+ if ( reasoningContent ) {
322+ hasReceivedData = true ;
323+ fullReasoningContent += reasoningContent ;
324+ onReasoningChunk ?.( reasoningContent ) ;
325+ }
347326 } catch ( e ) {
348327 console . error ( 'Error parsing JSON chunk:' , e ) ;
349328 }
350329 }
351330 }
352331 }
353332
354- if ( ! hasReceivedData && fullResponse . length === 0 ) {
333+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
355334 const contextError = new Error (
356335 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
357336 ) ;
@@ -558,51 +537,6 @@ export class ChatService {
558537 }
559538 }
560539
561- /**
562- * Processes content to separate thinking tags from regular content.
563- * Parses <think> and </think> tags to route content to appropriate handlers.
564- *
565- * @param content - The content string to process
566- * @param currentInsideThinkTag - Current state of whether we're inside a think tag
567- * @param addThinkContent - Callback to handle content inside think tags
568- * @param addRegularContent - Callback to handle regular content outside think tags
569- * @returns Boolean indicating if we're still inside a think tag after processing
570- * @private
571- */
572- private processContentForThinkTags (
573- content : string ,
574- currentInsideThinkTag : boolean ,
575- addThinkContent : ( chunk : string ) => void ,
576- addRegularContent : ( chunk : string ) => void
577- ) : boolean {
578- let i = 0 ;
579- let insideThinkTag = currentInsideThinkTag ;
580-
581- while ( i < content . length ) {
582- if ( ! insideThinkTag && content . substring ( i , i + 7 ) === '<think>' ) {
583- insideThinkTag = true ;
584- i += 7 ; // Skip the <think> tag
585- continue ;
586- }
587-
588- if ( insideThinkTag && content . substring ( i , i + 8 ) === '</think>' ) {
589- insideThinkTag = false ;
590- i += 8 ; // Skip the </think> tag
591- continue ;
592- }
593-
594- if ( insideThinkTag ) {
595- addThinkContent ( content [ i ] ) ;
596- } else {
597- addRegularContent ( content [ i ] ) ;
598- }
599-
600- i ++ ;
601- }
602-
603- return insideThinkTag ;
604- }
605-
606540 /**
607541 * Aborts any ongoing chat completion request.
608542 * Cancels the current request and cleans up the abort controller.
0 commit comments