@@ -29,17 +29,17 @@ import { createOAuthRoutes } from './routes/oauth'
2929import { createSSERoutes } from './routes/sse'
3030import { createSSHRoutes } from './routes/ssh'
3131import { createNotificationRoutes } from './routes/notifications'
32- import { createMemoryRoutes } from './routes/memory'
3332import { createMcpOauthProxyRoutes } from './routes/mcp-oauth-proxy'
3433import { createAuthRoutes , createAuthInfoRoutes , syncAdminFromEnv } from './routes/auth'
3534import { createAuth } from './auth'
3635import { createAuthMiddleware } from './auth/middleware'
3736import { createPromptTemplateRoutes } from './routes/prompt-templates'
37+ import { createInternalRoutes } from './routes/internal'
3838import { sseAggregator } from './services/sse-aggregator'
3939import { ensureDirectoryExists , writeFileContent , fileExists , readFileContent } from './services/file-operations'
4040import { SettingsService } from './services/settings'
4141import { opencodeServerManager } from './services/opencode-single-server'
42- import { proxyRequest , proxyMcpAuthStart , proxyMcpAuthAuthenticate } from './services/proxy '
42+ import { createOpenCodeClient } from './services/opencode/client '
4343import { NotificationService } from './services/notification'
4444import { ScheduleRunner , ScheduleService } from './services/schedules'
4545import { migrateGlobalSkills } from './services/skills'
@@ -85,6 +85,7 @@ app.use('/*', cors({
8585const db = initializeDatabase ( DB_PATH )
8686const auth = createAuth ( db )
8787const requireAuth = createAuthMiddleware ( auth )
88+ const openCodeClient = createOpenCodeClient ( ( ) => new SettingsService ( db ) . getOpenCodeServerPassword ( ) )
8889
8990import { DEFAULT_AGENTS_MD } from './constants'
9091
@@ -262,6 +263,8 @@ try {
262263 await gitAuthService . initialize ( ipcServer , db )
263264 logger . info ( `Git IPC server running at ${ ipcServer . ipcHandlePath } ` )
264265
266+ await syncAdminFromEnv ( auth , db )
267+
265268 opencodeServerManager . setDatabase ( db )
266269 const openCodeStatus = await openCodeSupervisor . start ( )
267270 if ( openCodeStatus . healthy ) {
@@ -270,12 +273,11 @@ try {
270273 logger . warn ( `OpenCode server unavailable after startup recovery: ${ openCodeStatus . lastError ?? openCodeStatus . state } ` )
271274 }
272275
273- await syncAdminFromEnv ( auth , db )
274276} catch ( error ) {
275277 logger . error ( 'Failed to initialize workspace:' , error )
276278}
277279
278- const scheduleService = new ScheduleService ( db )
280+ const scheduleService = new ScheduleService ( db , openCodeClient )
279281const scheduleRunnerInstance = new ScheduleRunner ( scheduleService )
280282
281283const notificationService = new NotificationService ( db )
@@ -299,28 +301,34 @@ if (ENV.VAPID.PUBLIC_KEY && ENV.VAPID.PRIVATE_KEY) {
299301 } )
300302}
301303
304+ sseAggregator . setPendingActionsFetcher ( openCodeClient )
305+ sseAggregator . setPasswordResolver ( ( ) => new SettingsService ( db ) . getOpenCodeServerPassword ( ) )
306+ sseAggregator . start ( )
307+
302308void scheduleRunnerInstance . start ( )
303309
310+ const settingsService = new SettingsService ( db )
311+
304312app . route ( '/api/auth' , createAuthRoutes ( auth ) )
305313app . route ( '/api/auth-info' , createAuthInfoRoutes ( auth , db ) )
306314app . route ( '/api/health' , createHealthRoutes ( db , openCodeSupervisor ) )
307315
308- app . route ( '/api/mcp-oauth-proxy' , createMcpOauthProxyRoutes ( requireAuth ) )
316+ app . route ( '/api/mcp-oauth-proxy' , createMcpOauthProxyRoutes ( openCodeClient , requireAuth ) )
317+ app . route ( '/api/internal' , createInternalRoutes ( db , scheduleService , notificationService , settingsService ) )
309318
310319const protectedApi = new Hono ( )
311320protectedApi . use ( '/*' , requireAuth )
312321
313- protectedApi . route ( '/repos' , createRepoRoutes ( db , gitAuthService , scheduleService , openCodeSupervisor ) )
314- protectedApi . route ( '/settings' , createSettingsRoutes ( db , gitAuthService , openCodeSupervisor ) )
322+ protectedApi . route ( '/repos' , createRepoRoutes ( db , gitAuthService , scheduleService , openCodeClient , openCodeSupervisor ) )
323+ protectedApi . route ( '/settings' , createSettingsRoutes ( db , gitAuthService , openCodeClient , openCodeSupervisor ) )
315324protectedApi . route ( '/files' , createFileRoutes ( ) )
316- protectedApi . route ( '/providers' , createProvidersRoutes ( db , openCodeSupervisor ) )
317- protectedApi . route ( '/oauth' , createOAuthRoutes ( openCodeSupervisor ) )
325+ protectedApi . route ( '/providers' , createProvidersRoutes ( db , openCodeClient , openCodeSupervisor ) )
326+ protectedApi . route ( '/oauth' , createOAuthRoutes ( openCodeClient , openCodeSupervisor ) )
318327protectedApi . route ( '/tts' , createTTSRoutes ( db ) )
319328protectedApi . route ( '/stt' , createSTTRoutes ( db ) )
320329protectedApi . route ( '/sse' , createSSERoutes ( ) )
321330protectedApi . route ( '/ssh' , createSSHRoutes ( gitAuthService ) )
322331protectedApi . route ( '/notifications' , createNotificationRoutes ( notificationService ) )
323- protectedApi . route ( '/memory' , createMemoryRoutes ( db ) )
324332protectedApi . route ( '/prompt-templates' , createPromptTemplateRoutes ( db ) )
325333protectedApi . route ( '/schedules' , createScheduleRoutes ( scheduleService ) )
326334
@@ -329,18 +337,17 @@ app.route('/api', protectedApi)
329337app . post ( '/api/opencode/mcp/:name/auth' , requireAuth , async ( c ) => {
330338 const serverName = c . req . param ( 'name' )
331339 const directory = c . req . query ( 'directory' )
332- return proxyMcpAuthStart ( serverName , directory )
340+ return openCodeClient . startMcpAuth ( serverName , directory )
333341} )
334342
335343app . post ( '/api/opencode/mcp/:name/auth/authenticate' , requireAuth , async ( c ) => {
336344 const serverName = c . req . param ( 'name' )
337345 const directory = c . req . query ( 'directory' )
338- return proxyMcpAuthAuthenticate ( serverName , directory )
346+ return openCodeClient . authenticateMcp ( serverName , directory )
339347} )
340348
341349app . all ( '/api/opencode/*' , requireAuth , async ( c ) => {
342- const request = c . req . raw
343- return proxyRequest ( request )
350+ return openCodeClient . forwardRaw ( c . req . raw )
344351} )
345352
346353const isProduction = ENV . SERVER . NODE_ENV === 'production'
0 commit comments