()
| 52 | } |
| 53 | |
| 54 | protected async runMainLoop(): Promise<void> { |
| 55 | const session = this.session; |
| 56 | const messageBuffer = this.messageBuffer; |
| 57 | |
| 58 | const { server: happyServer, mcpServers } = await buildHapiMcpBridge(session.client); |
| 59 | this.happyServer = happyServer; |
| 60 | |
| 61 | const backend = createOpencodeBackend({ |
| 62 | cwd: session.path |
| 63 | }); |
| 64 | this.backend = backend; |
| 65 | |
| 66 | backend.onStderrError((error) => { |
| 67 | logger.debug('[opencode-remote] stderr error', error); |
| 68 | session.sendSessionEvent({ type: 'message', message: error.message }); |
| 69 | messageBuffer.addMessage(error.message, 'status'); |
| 70 | }); |
| 71 | |
| 72 | await backend.initialize(); |
| 73 | |
| 74 | const resumeSessionId = session.sessionId; |
| 75 | const mcpServerList = toAcpMcpServers(mcpServers); |
| 76 | let acpSessionId: string; |
| 77 | if (resumeSessionId) { |
| 78 | try { |
| 79 | acpSessionId = await backend.loadSession({ |
| 80 | sessionId: resumeSessionId, |
| 81 | cwd: session.path, |
| 82 | mcpServers: mcpServerList |
| 83 | }); |
| 84 | } catch (error) { |
| 85 | logger.warn('[opencode-remote] resume failed, starting new session', error); |
| 86 | session.sendSessionEvent({ |
| 87 | type: 'message', |
| 88 | message: 'OpenCode resume failed; starting a new session.' |
| 89 | }); |
| 90 | acpSessionId = await backend.newSession({ |
| 91 | cwd: session.path, |
| 92 | mcpServers: mcpServerList |
| 93 | }); |
| 94 | } |
| 95 | } else { |
| 96 | acpSessionId = await backend.newSession({ |
| 97 | cwd: session.path, |
| 98 | mcpServers: mcpServerList |
| 99 | }); |
| 100 | } |
| 101 | session.onSessionFound(acpSessionId); |
| 102 | |
| 103 | // Seed currentBackendModel from the ACP session metadata so the first |
| 104 | // batch — whose model the hub mirrors from the just-discovered session — |
| 105 | // does not trigger a redundant setModel on the very first turn. |
| 106 | const initialMetadata = backend.getSessionModelsMetadata?.(acpSessionId); |
| 107 | this.currentBackendModel = initialMetadata?.currentModelId ?? null; |
| 108 | this.defaultBackendModel = this.currentBackendModel; |
| 109 | const thoughtLevelOption = backend.getThoughtLevelConfigOption?.(acpSessionId); |
| 110 | this.currentBackendEffort = thoughtLevelOption?.currentValue ?? null; |
| 111 | this.defaultBackendEffort = this.currentBackendEffort; |
nothing calls this directly
no test coverage detected