mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
SSE proxy compatibility and worktree support (#79)
This commit is contained in:
1 parent
204e79c713
commit
ee5cb4173f
1 file changed
+20
-9
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user