11import { runChat } from './sessions.functions'
22import type { QueryClient } from '@tanstack/react-query'
3- import type {
4- AdkEvent ,
5- AdkSession ,
6- ChatMessage ,
7- SourceItem ,
8- } from './adk'
3+ import type { AdkEvent , AdkSession , ChatMessage , SourceItem } from './adk'
94
105export interface ChatSessionState {
116 messages : Array < ChatMessage >
@@ -76,8 +71,8 @@ export async function startChatStream({
7671 for await ( const event of stream ) {
7772 processEventIntoCache ( queryClient , sessionId , event )
7873 }
79- } catch ( err ) {
80- if ( err instanceof Error && err . name === 'AbortError' ) {
74+ } catch ( err : any ) {
75+ if ( err . name === 'AbortError' || err . message ?. includes ( 'aborted' ) ) {
8176 // Expected when a stream is canceled
8277 return
8378 }
@@ -165,16 +160,17 @@ export function applyEventToState(
165160 // console.info('applyEventToState', event);
166161
167162 // Skip function responses
168- const eventParts = event . content . parts . filter ( p => ! p . functionResponse ) ;
163+ const eventParts = event . content . parts . filter ( ( p ) => ! p . functionResponse )
169164
170165 if ( event . content . role === 'user' ) {
171166 // Don't insert user message if it's just a function response.
172167 // We may store the map of function response as a separate map when we need tool response in UI.
173- if ( eventParts . length === 0 ) return prev ;
168+ if ( eventParts . length === 0 ) return prev
174169
175170 // event is user message, just append message
176171 return {
177- ...prev , messages : [
172+ ...prev ,
173+ messages : [
178174 ...prev . messages ,
179175 {
180176 id : genId ( ) ,
@@ -183,7 +179,7 @@ export function applyEventToState(
183179 parts : [ ...eventParts ] ,
184180 timestamp : new Date ( ) ,
185181 } ,
186- ]
182+ ] ,
187183 }
188184 }
189185
@@ -192,9 +188,11 @@ export function applyEventToState(
192188 // Agent parts (text & tool calls)
193189 if ( event . content . role === 'model' ) {
194190 const last = messages [ messages . length - 1 ]
195- const isLastStillStreaming = last ?. role === 'model' &&
196- last ?. isStreaming &&
197- ( last ?. author || 'writer' ) === ( event . author || 'writer' )
191+ const isLastStillStreaming =
192+ last &&
193+ last . role === 'model' &&
194+ last . isStreaming &&
195+ ( last . author || 'writer' ) === ( event . author || 'writer' )
198196
199197 if ( ! isLastStillStreaming ) {
200198 messages = [
@@ -206,7 +204,9 @@ export function applyEventToState(
206204 parts : [ ...eventParts ] ,
207205 isStreaming : event . partial !== false ,
208206 timestamp : new Date ( ) ,
209- langfuseTraceId : event . customMetadata ?. [ 'langfuse_trace_id' ] as string | undefined ,
207+ langfuseTraceId : event . customMetadata ?. [ 'langfuse_trace_id' ] as
208+ | string
209+ | undefined ,
210210 } ,
211211 ]
212212 } else if ( ! event . partial ) {
@@ -235,7 +235,7 @@ export function applyEventToState(
235235
236236 // If last part is not a text part, push the text part as a new part
237237 const lastPart = updatedParts [ updatedParts . length - 1 ]
238- if ( lastPart ? .text === undefined ) {
238+ if ( lastPart . text === undefined ) {
239239 updatedParts . push ( { ...part } )
240240 continue
241241 }
@@ -253,13 +253,13 @@ export function applyEventToState(
253253 ...last ,
254254 parts : updatedParts ,
255255 isStreaming : true ,
256- } ,
256+ } as ChatMessage ,
257257 ]
258258 }
259259 }
260260
261261 // Grounding metadata (Sources)
262- let sources = prev . sources ;
262+ let sources = prev . sources
263263 if ( event . groundingMetadata ?. groundingChunks ) {
264264 const newSources : Array < SourceItem > =
265265 event . groundingMetadata . groundingChunks
0 commit comments