@@ -80,33 +80,25 @@ export class ChatService {
8080
8181 const currentConfig = config ( ) ;
8282
83- // Create or get abort controller for this conversation
8483 const requestId = conversationId || 'default' ;
8584
86- // Cancel any existing request for this conversation
8785 if ( this . abortControllers . has ( requestId ) ) {
8886 this . abortControllers . get ( requestId ) ?. abort ( ) ;
8987 }
9088
91- // Create new abort controller for this conversation
9289 const abortController = new AbortController ( ) ;
9390 this . abortControllers . set ( requestId , abortController ) ;
9491
95- // Convert database messages with attachments to API format if needed
9692 const normalizedMessages : ApiChatMessageData [ ] = messages
9793 . map ( ( msg ) => {
98- // Check if this is a DatabaseMessage by checking for DatabaseMessage-specific fields
9994 if ( 'id' in msg && 'convId' in msg && 'timestamp' in msg ) {
100- // This is a DatabaseMessage, convert it
10195 const dbMsg = msg as DatabaseMessage & { extra ?: DatabaseMessageExtra [ ] } ;
10296 return ChatService . convertMessageToChatServiceData ( dbMsg ) ;
10397 } else {
104- // This is already an ApiChatMessageData object
10598 return msg as ApiChatMessageData ;
10699 }
107100 } )
108101 . filter ( ( msg ) => {
109- // Filter out empty system messages
110102 if ( msg . role === 'system' ) {
111103 const content = typeof msg . content === 'string' ? msg . content : '' ;
112104
@@ -116,7 +108,6 @@ export class ChatService {
116108 return true ;
117109 } ) ;
118110
119- // Build base request body with system message injection
120111 const processedMessages = this . injectSystemMessage ( normalizedMessages ) ;
121112
122113 const requestBody : ApiChatCompletionRequest = {
@@ -185,7 +176,6 @@ export class ChatService {
185176 } ) ;
186177
187178 if ( ! response . ok ) {
188- // Use the new parseErrorResponse method to handle structured errors
189179 const error = await this . parseErrorResponse ( response ) ;
190180 if ( onError ) {
191181 onError ( error ) ;
@@ -240,7 +230,6 @@ export class ChatService {
240230 }
241231 throw userFriendlyError ;
242232 } finally {
243- // Clean up the abort controller for this conversation
244233 this . abortControllers . delete ( requestId ) ;
245234 }
246235 }
@@ -285,28 +274,19 @@ export class ChatService {
285274 try {
286275 let chunk = '' ;
287276 while ( true ) {
288- // Check if we've been aborted before reading more data
289- if ( abortSignal ?. aborted ) {
290- break ;
291- }
277+ if ( abortSignal ?. aborted ) break ;
292278
293279 const { done, value } = await reader . read ( ) ;
294280 if ( done ) break ;
295281
296- // Check again after async read
297- if ( abortSignal ?. aborted ) {
298- break ;
299- }
282+ if ( abortSignal ?. aborted ) break ;
300283
301284 chunk += decoder . decode ( value , { stream : true } ) ;
302285 const lines = chunk . split ( '\n' ) ;
303- chunk = lines . pop ( ) || '' ; // Save incomplete line for next read
286+ chunk = lines . pop ( ) || '' ;
304287
305288 for ( const line of lines ) {
306- // Check abort signal before processing each line
307- if ( abortSignal ?. aborted ) {
308- break ;
309- }
289+ if ( abortSignal ?. aborted ) break ;
310290
311291 if ( line . startsWith ( 'data: ' ) ) {
312292 const data = line . slice ( 6 ) ;
@@ -325,8 +305,6 @@ export class ChatService {
325305
326306 if ( timings || promptProgress ) {
327307 this . updateProcessingState ( timings , promptProgress , conversationId ) ;
328-
329- // Store the latest timing data
330308 if ( timings ) {
331309 lastTimings = timings ;
332310 }
@@ -335,7 +313,6 @@ export class ChatService {
335313 if ( content ) {
336314 hasReceivedData = true ;
337315 aggregatedContent += content ;
338- // Only call callback if not aborted
339316 if ( ! abortSignal ?. aborted ) {
340317 onChunk ?.( content ) ;
341318 }
@@ -344,7 +321,6 @@ export class ChatService {
344321 if ( reasoningContent ) {
345322 hasReceivedData = true ;
346323 fullReasoningContent += reasoningContent ;
347- // Only call callback if not aborted
348324 if ( ! abortSignal ?. aborted ) {
349325 onReasoningChunk ?.( reasoningContent ) ;
350326 }
@@ -355,16 +331,10 @@ export class ChatService {
355331 }
356332 }
357333
358- // Break outer loop if aborted during line processing
359- if ( abortSignal ?. aborted ) {
360- break ;
361- }
334+ if ( abortSignal ?. aborted ) break ;
362335 }
363336
364- // Don't call onComplete if we've been aborted
365- if ( abortSignal ?. aborted ) {
366- return ;
367- }
337+ if ( abortSignal ?. aborted ) return ;
368338
369339 if ( streamFinished ) {
370340 if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
@@ -569,14 +539,12 @@ export class ChatService {
569539 */
570540 public abort ( conversationId ?: string ) : void {
571541 if ( conversationId ) {
572- // Abort specific conversation
573542 const abortController = this . abortControllers . get ( conversationId ) ;
574543 if ( abortController ) {
575544 abortController . abort ( ) ;
576545 this . abortControllers . delete ( conversationId ) ;
577546 }
578547 } else {
579- // Abort all conversations
580548 for ( const controller of this . abortControllers . values ( ) ) {
581549 controller . abort ( ) ;
582550 }
@@ -638,7 +606,6 @@ export class ChatService {
638606
639607 return error ;
640608 } catch {
641- // If we can't parse the error response, return a generic error
642609 const fallback = new Error ( `Server error (${ response . status } ): ${ response . statusText } ` ) ;
643610 fallback . name = 'HttpError' ;
644611 return fallback ;
@@ -650,13 +617,11 @@ export class ChatService {
650617 promptProgress ?: ChatMessagePromptProgress ,
651618 conversationId ?: string
652619 ) : void {
653- // Calculate tokens per second from timing data
654620 const tokensPerSecond =
655621 timings ?. predicted_ms && timings ?. predicted_n
656622 ? ( timings . predicted_n / timings . predicted_ms ) * 1000
657623 : 0 ;
658624
659- // Update slots service with timing data (async but don't wait)
660625 slotsService
661626 . updateFromTimingData (
662627 {
0 commit comments