@@ -256,10 +256,8 @@ export class ChatService {
256256 }
257257
258258 const decoder = new TextDecoder ( ) ;
259- let fullResponse = '' ;
259+ let aggregatedContent = '' ;
260260 let fullReasoningContent = '' ;
261- let regularContent = '' ;
262- let insideThinkTag = false ;
263261 let hasReceivedData = false ;
264262 let lastTimings : ChatMessageTimings | undefined ;
265263
@@ -277,7 +275,7 @@ export class ChatService {
277275 if ( line . startsWith ( 'data: ' ) ) {
278276 const data = line . slice ( 6 ) ;
279277 if ( data === '[DONE]' ) {
280- if ( ! hasReceivedData && fullResponse . length === 0 ) {
278+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
281279 const contextError = new Error (
282280 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
283281 ) ;
@@ -286,7 +284,7 @@ export class ChatService {
286284 return ;
287285 }
288286
289- onComplete ?.( regularContent , fullReasoningContent || undefined , lastTimings ) ;
287+ onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
290288
291289 return ;
292290 }
@@ -308,44 +306,25 @@ export class ChatService {
308306 }
309307 }
310308
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+ }
341320 } catch ( e ) {
342321 console . error ( 'Error parsing JSON chunk:' , e ) ;
343322 }
344323 }
345324 }
346325 }
347326
348- if ( ! hasReceivedData && fullResponse . length === 0 ) {
327+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
349328 const contextError = new Error (
350329 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
351330 ) ;
@@ -552,51 +531,6 @@ export class ChatService {
552531 }
553532 }
554533
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-
600534 /**
601535 * Aborts any ongoing chat completion request.
602536 * Cancels the current request and cleans up the abort controller.
0 commit comments