@@ -52,13 +52,29 @@ export const handleResponses = async (c: Context) => {
5252 if ( isStreamingRequested ( payload ) && isAsyncIterable ( response ) ) {
5353 consola . debug ( "Forwarding native Responses stream" )
5454 return streamSSE ( c , async ( stream ) => {
55- for await ( const chunk of response ) {
56- consola . debug ( "Responses stream chunk:" , JSON . stringify ( chunk ) )
57- await stream . writeSSE ( {
58- id : ( chunk as { id ?: string } ) . id ,
59- event : ( chunk as { event ?: string } ) . event ,
60- data : ( chunk as { data ?: string } ) . data ?? "" ,
61- } )
55+ const pingInterval = setInterval ( async ( ) => {
56+ try {
57+ await stream . writeSSE ( {
58+ event : "ping" ,
59+ data : JSON . stringify ( { timestamp : Date . now ( ) } ) ,
60+ } )
61+ } catch ( error ) {
62+ consola . warn ( "Failed to send ping:" , error )
63+ clearInterval ( pingInterval )
64+ }
65+ } , 3000 )
66+
67+ try {
68+ for await ( const chunk of response ) {
69+ consola . debug ( "Responses stream chunk:" , JSON . stringify ( chunk ) )
70+ await stream . writeSSE ( {
71+ id : ( chunk as { id ?: string } ) . id ,
72+ event : ( chunk as { event ?: string } ) . event ,
73+ data : ( chunk as { data ?: string } ) . data ?? "" ,
74+ } )
75+ }
76+ } finally {
77+ clearInterval ( pingInterval )
6278 }
6379 } )
6480 }
0 commit comments