mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
refactor(sse): batch parts directory-aware and serialize backend writes
This commit is contained in:
1 parent
94941eb7a0
commit
a363138dbd
9 files changed
+159
-102
No files matched your search
@@ -5,28 +5,28 @@ export interface QueuedSSEWriterInput {
|
||||
|
||||
export interface QueuedSSEWriter {
|
||||
writeSSE: (event: string, data: string) => void
|
||||
writeFrame: (frame: Uint8Array) => void
|
||||
close: () => void
|
||||
}
|
||||
|
||||
const sharedEncoder = new TextEncoder()
|
||||
|
||||
export function encodeSSEFrame(event: string, data: string): Uint8Array {
|
||||
const head = event ? `event: ${event}\n` : ''
|
||||
return sharedEncoder.encode(`${head}data: ${data}\n\n`)
|
||||
}
|
||||
|
||||
export function createQueuedSSEWriter(input: QueuedSSEWriterInput): QueuedSSEWriter {
|
||||
const encoder = new TextEncoder()
|
||||
let chain = Promise.resolve()
|
||||
let closed = false
|
||||
|
||||
const writeSSE = (event: string, data: string) => {
|
||||
const writeFrame = (frame: Uint8Array) => {
|
||||
if (closed) return
|
||||
|
||||
const lines: string[] = []
|
||||
if (event) lines.push(`event: ${event}`)
|
||||
lines.push(`data: ${data}`)
|
||||
lines.push('')
|
||||
lines.push('')
|
||||
const encoded = encoder.encode(lines.join('\n'))
|
||||
|
||||
chain = chain
|
||||
.then(async () => {
|
||||
if (closed) return
|
||||
await input.write(encoded)
|
||||
await input.write(frame)
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!closed) {
|
||||
@@ -36,9 +36,14 @@ export function createQueuedSSEWriter(input: QueuedSSEWriterInput): QueuedSSEWri
|
||||
})
|
||||
}
|
||||
|
||||
const writeSSE = (event: string, data: string) => {
|
||||
if (closed) return
|
||||
writeFrame(encodeSSEFrame(event, data))
|
||||
}
|
||||
|
||||
const close = () => {
|
||||
closed = true
|
||||
}
|
||||
|
||||
return { writeSSE, close }
|
||||
return { writeSSE, writeFrame, close }
|
||||
}
|
||||
@@ -42,6 +42,9 @@ export function createSSERoutes() {
|
||||
(event, data) => {
|
||||
queuedWriter.writeSSE(event, data)
|
||||
},
|
||||
(frame) => {
|
||||
queuedWriter.writeFrame(frame)
|
||||
},
|
||||
directories
|
||||
)
|
||||
|
||||
|
||||
@@ -3,13 +3,16 @@ import { logger } from '../utils/logger'
|
||||
import { ENV } from '@opencode-manager/shared/config/env'
|
||||
import { DEFAULTS } from '@opencode-manager/shared/config'
|
||||
import { getOpenCodeBasicAuthHeader, type OpenCodePasswordResolver } from './opencode/auth'
|
||||
import { encodeSSEFrame } from '../routes/sse-writer'
|
||||
|
||||
type SSEClientCallback = (event: string, data: string) => void
|
||||
type SSEClientFrameWriter = (frame: Uint8Array) => void
|
||||
type SSEEventListener = (directory: string, event: SSEEvent) => void
|
||||
|
||||
interface SSEClient {
|
||||
id: string
|
||||
callback: SSEClientCallback
|
||||
writeFrame: SSEClientFrameWriter
|
||||
directories: Set<string>
|
||||
visible: boolean
|
||||
activeSessionId: string | null
|
||||
@@ -95,10 +98,11 @@ class SSEAggregator {
|
||||
void this.connectUpstream()
|
||||
}
|
||||
|
||||
addClient(id: string, callback: SSEClientCallback, directories: string[]): () => void {
|
||||
addClient(id: string, callback: SSEClientCallback, writeFrame: SSEClientFrameWriter, directories: string[]): () => void {
|
||||
const client: SSEClient = {
|
||||
id,
|
||||
callback,
|
||||
writeFrame,
|
||||
directories: new Set(directories),
|
||||
visible: false,
|
||||
activeSessionId: null
|
||||
@@ -211,7 +215,7 @@ class SSEAggregator {
|
||||
if (!client || !client.directories.has(directory)) return
|
||||
|
||||
for (const item of items) {
|
||||
const payload = JSON.stringify({ type, properties: item, directory })
|
||||
const payload = JSON.stringify({ directory, payload: { type, properties: item } })
|
||||
try {
|
||||
client.callback('message', payload)
|
||||
} catch (error) {
|
||||
@@ -316,11 +320,13 @@ class SSEAggregator {
|
||||
try { listener(directory, parsed) } catch { /* ignore listener errors */ }
|
||||
})
|
||||
|
||||
const clientData = JSON.stringify({ ...parsed, directory })
|
||||
let frame: Uint8Array | undefined
|
||||
const getFrame = (): Uint8Array => (frame ??= encodeSSEFrame('message', data))
|
||||
|
||||
this.clients.forEach((client) => {
|
||||
if (client.directories.has(directory)) {
|
||||
try {
|
||||
client.callback('message', clientData)
|
||||
client.writeFrame(getFrame())
|
||||
} catch (error) {
|
||||
logger.error(`Failed to send to client ${client.id}:`, error)
|
||||
}
|
||||
@@ -478,8 +484,10 @@ export const sseAggregator = SSEAggregator.getInstance()
|
||||
|
||||
export function broadcastSSHHostKeyRequest(data: Record<string, unknown>): void {
|
||||
const event = JSON.stringify({
|
||||
type: 'ssh.host-key-request',
|
||||
properties: data,
|
||||
payload: {
|
||||
type: 'ssh.host-key-request',
|
||||
properties: data,
|
||||
},
|
||||
})
|
||||
sseAggregator.broadcastToAll('message', event)
|
||||
}
|
||||
Reference in new issue
Block a user