SSE proxy compatibility and worktree support (#79)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-01-25 21:13:07 -05:00
1 parent 204e79c713
commit ee5cb4173f
1 file changed
+20 -9
+20 -9
View File
@@ -1,5 +1,5 @@
import { Hono } from 'hono'
import { streamSSE } from 'hono/streaming'
import { stream } from 'hono/streaming'
import { sseAggregator } from '../services/sse-aggregator'
import { SSESubscribeSchema } from '@opencode-manager/shared/schemas'
import { logger } from '../utils/logger'
@@ -10,27 +10,38 @@ export function createSSERoutes() {
app.get('/stream', async (c) => {
const directoriesParam = c.req.query('directories')
const directories = directoriesParam ? directoriesParam.split(',').filter(Boolean) : []
const clientId = `client_${Date.now()}_${Math.random().toString(36).slice(2)}`
return streamSSE(c, async (stream) => {
const clientId = `client_${Date.now()}_${Math.random().toString(36).slice(2)}`
c.header('Content-Type', 'text/event-stream')
c.header('Cache-Control', 'no-cache, no-store, no-transform')
c.header('Connection', 'keep-alive')
c.header('X-Accel-Buffering', 'no')
return stream(c, async (writer) => {
const encoder = new TextEncoder()
const writeSSE = (event: string, data: string) => {
const lines = []
if (event) lines.push(`event: ${event}`)
lines.push(`data: ${data}`)
lines.push('')
lines.push('')
writer.write(encoder.encode(lines.join('\n')))
}
const cleanup = sseAggregator.addClient(
clientId,
(event, data) => {
stream.writeSSE({ event, data })
writeSSE(event, data)
},
directories
)
stream.onAbort(() => {
writer.onAbort(() => {
cleanup()
})
try {
await stream.writeSSE({
event: 'connected',
data: JSON.stringify({ clientId, directories, ...sseAggregator.getConnectionStatus() })
})
writeSSE('connected', JSON.stringify({ clientId, directories, ...sseAggregator.getConnectionStatus() }))
} catch (err) {
logger.error(`Failed to send SSE connected event for ${clientId}:`, err)
}