@@ -68,7 +68,7 @@ export interface StreamingOrchestrationParams {
6868 streamId : string
6969 chatId ?: string
7070 currentChat : any
71- conversationHistory : unknown [ ]
71+ isNewChat : boolean
7272 message : string
7373 titleModel : string
7474 titleProvider ?: string
@@ -83,7 +83,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
8383 streamId,
8484 chatId,
8585 currentChat,
86- conversationHistory ,
86+ isNewChat ,
8787 message,
8888 titleModel,
8989 titleProvider,
@@ -93,6 +93,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
9393
9494 let eventWriter : ReturnType < typeof createStreamEventWriter > | null = null
9595 let clientDisconnected = false
96+ const abortController = new AbortController ( )
9697
9798 return new ReadableStream ( {
9899 async start ( controller ) {
@@ -137,7 +138,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
137138 await pushEvent ( { type : 'chat_id' , chatId } )
138139 }
139140
140- if ( chatId && ! currentChat ?. title && conversationHistory . length === 0 ) {
141+ if ( chatId && ! currentChat ?. title && isNewChat ) {
141142 requestChatTitle ( { message, model : titleModel , provider : titleProvider } )
142143 . then ( async ( title ) => {
143144 if ( title ) {
@@ -153,6 +154,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
153154 try {
154155 await orchestrateCopilotStream ( requestPayload , {
155156 ...orchestrateOptions ,
157+ abortSignal : abortController . signal ,
156158 onEvent : async ( event ) => {
157159 await pushEvent ( event )
158160 } ,
@@ -161,6 +163,12 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
161163 await eventWriter . close ( )
162164 await setStreamMeta ( streamId , { status : 'complete' , userId } )
163165 } catch ( error ) {
166+ if ( clientDisconnected || abortController . signal . aborted ) {
167+ logger . info ( `[${ requestId } ] Stream aborted by client disconnect` )
168+ await eventWriter . close ( ) . catch ( ( ) => { } )
169+ await setStreamMeta ( streamId , { status : 'complete' , userId } )
170+ return
171+ }
164172 logger . error ( `[${ requestId } ] Orchestration error:` , error )
165173 await eventWriter . close ( )
166174 await setStreamMeta ( streamId , {
@@ -180,6 +188,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
180188 } ,
181189 async cancel ( ) {
182190 clientDisconnected = true
191+ abortController . abort ( )
183192 if ( eventWriter ) {
184193 await eventWriter . flush ( )
185194 }
0 commit comments