@@ -268,6 +268,7 @@ export class ChatService {
268268 let fullReasoningContent = '' ;
269269 let hasReceivedData = false ;
270270 let lastTimings : ChatMessageTimings | undefined ;
271+ let streamFinished = false ;
271272
272273 try {
273274 let chunk = '' ;
@@ -283,18 +284,8 @@ export class ChatService {
283284 if ( line . startsWith ( 'data: ' ) ) {
284285 const data = line . slice ( 6 ) ;
285286 if ( data === '[DONE]' ) {
286- if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
287- const contextError = new Error (
288- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
289- ) ;
290- contextError . name = 'ContextError' ;
291- onError ?.( contextError ) ;
292- return ;
293- }
294-
295- onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
296-
297- return ;
287+ streamFinished = true ;
288+ continue ;
298289 }
299290
300291 try {
@@ -332,13 +323,17 @@ export class ChatService {
332323 }
333324 }
334325
335- if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
336- const contextError = new Error (
337- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
338- ) ;
339- contextError . name = 'ContextError' ;
340- onError ?.( contextError ) ;
341- return ;
326+ if ( streamFinished ) {
327+ if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
328+ const contextError = new Error (
329+ 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
330+ ) ;
331+ contextError . name = 'ContextError' ;
332+ onError ?.( contextError ) ;
333+ return ;
334+ }
335+
336+ onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
342337 }
343338 } catch ( error ) {
344339 const err = error instanceof Error ? error : new Error ( 'Stream error' ) ;
0 commit comments