11import { Hono } from 'hono'
2- import { streamSSE } from 'hono/streaming'
2+ import { stream } from 'hono/streaming'
33import { sseAggregator } from '../services/sse-aggregator'
44import { SSESubscribeSchema } from '@opencode-manager/shared/schemas'
55import { logger } from '../utils/logger'
@@ -10,27 +10,38 @@ export function createSSERoutes() {
1010 app . get ( '/stream' , async ( c ) => {
1111 const directoriesParam = c . req . query ( 'directories' )
1212 const directories = directoriesParam ? directoriesParam . split ( ',' ) . filter ( Boolean ) : [ ]
13+ const clientId = `client_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } `
1314
14- return streamSSE ( c , async ( stream ) => {
15- const clientId = `client_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } `
15+ c . header ( 'Content-Type' , 'text/event-stream' )
16+ c . header ( 'Cache-Control' , 'no-cache, no-store, no-transform' )
17+ c . header ( 'Connection' , 'keep-alive' )
18+ c . header ( 'X-Accel-Buffering' , 'no' )
19+
20+ return stream ( c , async ( writer ) => {
21+ const encoder = new TextEncoder ( )
22+ const writeSSE = ( event : string , data : string ) => {
23+ const lines = [ ]
24+ if ( event ) lines . push ( `event: ${ event } ` )
25+ lines . push ( `data: ${ data } ` )
26+ lines . push ( '' )
27+ lines . push ( '' )
28+ writer . write ( encoder . encode ( lines . join ( '\n' ) ) )
29+ }
1630
1731 const cleanup = sseAggregator . addClient (
1832 clientId ,
1933 ( event , data ) => {
20- stream . writeSSE ( { event, data } )
34+ writeSSE ( event , data )
2135 } ,
2236 directories
2337 )
2438
25- stream . onAbort ( ( ) => {
39+ writer . onAbort ( ( ) => {
2640 cleanup ( )
2741 } )
2842
2943 try {
30- await stream . writeSSE ( {
31- event : 'connected' ,
32- data : JSON . stringify ( { clientId, directories, ...sseAggregator . getConnectionStatus ( ) } )
33- } )
44+ writeSSE ( 'connected' , JSON . stringify ( { clientId, directories, ...sseAggregator . getConnectionStatus ( ) } ) )
3445 } catch ( err ) {
3546 logger . error ( `Failed to send SSE connected event for ${ clientId } :` , err )
3647 }
0 commit comments