Skip to content

Commit becb16a

Browse files
refactor: opencode event stream integration and SSE aggregator updates (#212)
1 parent 348319d commit becb16a

22 files changed

Lines changed: 1151 additions & 1035 deletions

backend/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ if (ENV.VAPID.PUBLIC_KEY && ENV.VAPID.PRIVATE_KEY) {
302302
})
303303
}
304304

305+
sseAggregator.setPendingActionsFetcher(openCodeClient)
306+
sseAggregator.start()
307+
305308
void scheduleRunnerInstance.start()
306309

307310
app.route('/api/auth', createAuthRoutes(auth))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ENV } from '@opencode-manager/shared/config/env'
2+
3+
export function buildOpenCodeBasicAuthHeader(): string | null {
4+
const password = ENV.OPENCODE.SERVER_PASSWORD
5+
const username = ENV.OPENCODE.SERVER_USERNAME
6+
if (!password) return null
7+
const token = Buffer.from(`${username}:${password}`).toString('base64')
8+
return `Basic ${token}`
9+
}

backend/src/services/opencode/client.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from '../../utils/logger'
22
import { ENV } from '@opencode-manager/shared/config/env'
3+
import { buildOpenCodeBasicAuthHeader } from './auth'
34

45
export interface ForwardRequest {
56
method: string
@@ -40,7 +41,7 @@ export interface OpenCodeClient {
4041

4142
export interface FetchOpenCodeClientConfig {
4243
baseUrl: string
43-
basicAuth: string
44+
basicAuth: string | null
4445
fetchFn?: typeof fetch
4546
}
4647

@@ -239,11 +240,7 @@ export class FetchOpenCodeClient implements OpenCodeClient {
239240

240241
export function createOpenCodeClient(): OpenCodeClient {
241242
const baseUrl = `http://${ENV.OPENCODE.HOST}:${ENV.OPENCODE.PORT}`
242-
const password = ENV.OPENCODE.SERVER_PASSWORD
243-
const username = ENV.OPENCODE.SERVER_USERNAME
244-
const basicAuth = password
245-
? `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`
246-
: ''
243+
const basicAuth = buildOpenCodeBasicAuthHeader()
247244

248245
return new FetchOpenCodeClient({ baseUrl, basicAuth })
249246
}

backend/src/services/schedules.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,9 @@ function getSessionStatusType(event: SSEEvent): string | null {
315315
}
316316

317317
function createSessionMonitor(directory: string, sessionId: string): SessionMonitor {
318-
const clientId = `schedule-monitor-${sessionId}-${Date.now()}`
319318
let errorText: string | null = null
320319
let idle = false
321320

322-
const removeClient = sseAggregator.addClient(clientId, () => {}, [directory])
323321
const unsubscribe = sseAggregator.onEvent((eventDirectory, event) => {
324322
if (eventDirectory !== directory) {
325323
return
@@ -347,10 +345,7 @@ function createSessionMonitor(directory: string, sessionId: string): SessionMoni
347345
return {
348346
getErrorText: () => errorText,
349347
isIdle: () => idle,
350-
dispose: () => {
351-
unsubscribe()
352-
removeClient()
353-
},
348+
dispose: unsubscribe,
354349
}
355350
}
356351

0 commit comments

Comments
 (0)