@@ -1,6 +1,7 @@
1package soy.iko.opencode.core
1package soy.iko.opencode.core
3import kotlinx.coroutines.CoroutineScope
3import kotlinx.coroutines.CoroutineScope
4+import kotlinx.coroutines.Job
4import kotlinx.coroutines.delay
5import kotlinx.coroutines.delay
5import kotlinx.coroutines.flow.MutableStateFlow
6import kotlinx.coroutines.flow.MutableStateFlow
6import kotlinx.coroutines.flow.StateFlow
7import kotlinx.coroutines.flow.StateFlow
@@ -29,7 +30,6 @@ class OpencodeCore(
29 private var username = ""
30 private var username = ""
30 private var password = ""
31 private var password = ""
31 private var authRequired = false
32 private var authRequired = false
32− private var authed = false
33 private var connected = false
33 private var connected = false
34 private var loading = false
34 private var loading = false
35 private var error: String? = null
35 private var error: String? = null
@@ -40,6 +40,7 @@ class OpencodeCore(
40 private val messages = mutableListOf<MsgState>()
40 private val messages = mutableListOf<MsgState>()
41 private var draft = ""
41 private var draft = ""
42 private var generating = false
42 private var generating = false
43+ private var sseConnected = true
43 private var localSeq = 0L
44 private var localSeq = 0L
45 private val _view = MutableStateFlow(snapshot())
46 private val _view = MutableStateFlow(snapshot())
@@ -47,44 +48,123 @@ class OpencodeCore(
48 private var sse: SseClient? = null
49 private var sse: SseClient? = null
49 private var sseClosedIntentionally = false
50 private var sseClosedIntentionally = false
51+ private var reconnectAttempts = 0
52+ private var reconnectJob: Job? = null
54+ // Guards against overlapping session refreshes: rapid taps launch multiple
55+ // loadSessions coroutines and the slowest-to-return would otherwise win.
56+ private var loadSessionsJob: Job? = null
57+ // Tracks the in-flight createSession coroutine so navigating away can cancel it
58+ // before it dispatches SelectSession into a screen the user already left.
59+ private var createSessionJob: Job? = null
60+ // Tracks the in-flight loadMessages coroutine. Rapidly switching sessions launches
61+ // several; cancel the prior one so only the newest fetch runs (saves needless requests).
62+ private var loadMessagesJob: Job? = null
63+ // Tracks the in-flight probeHealth coroutine. Rapid double-taps on Connect would
64+ // otherwise launch two concurrent health probes; cancel the prior one first.
65+ private var connectJob: Job? = null
66+ // Tracks the in-flight DeleteSession coroutine so navigating away can cancel it
67+ // before its response handler mutates the sessions list / screen the user already left.
68+ private var deleteSessionJob: Job? = null
69+ // Tracks the in-flight sendMessage coroutine so navigating away or switching sessions
70+ // can cancel it before its response handler touches stale message state.
71+ private var sendMessageJob: Job? = null
72+ // Tracks the in-flight abort coroutine (CancelGeneration) so rapid cancel taps don't
73+ // fire multiple abort requests, and navigating away can cancel it.
74+ private var abortJob: Job? = null
75+ // Message IDs removed via SSE; a late MessageUpdated for one must not resurrect it.
76+ private val removedMessageIds = mutableSetOf<String>()
51 // ── Event entry point ──────────────────────────────────────────────────────
78 // ── Event entry point ──────────────────────────────────────────────────────
52 fun dispatch(event: Event) {
79 fun dispatch(event: Event) {
53 when (event) {
80 when (event) {
81+ // Store the field verbatim while typing; normalizing here would fight
82+ // the user's edits (e.g. eat the "://" as they type it). The URL is
83+ // normalized once, on Connect.
84+ // Clear any prior error as soon as the user edits a field, so a stale
85+ // message (e.g. "Invalid credentials") doesn't linger while they correct it.
54− is Event.ServerUrlChanged -> { serverUrl = normalizeUrl(event.url); emit() }
86+ is Event.ServerUrlChanged -> { serverUrl = event.url; if (error != null) error = null; emit() }
55− is Event.UsernameChanged -> { username = event.value; emit() }
87+ is Event.UsernameChanged -> { username = event.value; if (error != null) error = null; emit() }
56− is Event.PasswordChanged -> { password = event.value; emit() }
88+ is Event.PasswordChanged -> { password = event.value; if (error != null) error = null; emit() }
58 Event.Connect -> {
90 Event.Connect -> {
91+ serverUrl = normalizeUrl(serverUrl)
59 if (serverUrl.isEmpty()) { error = "Enter a server URL"; emit(); return }
92 if (serverUrl.isEmpty()) { error = "Enter a server URL"; emit(); return }
93+ // The auth fields are showing but empty — sending the request now
94+ // would just 401 again in a loop. Prompt for credentials instead.
95+ if (authRequired && !hasCredentials()) { error = "Enter username and password"; emit(); return }
96+ // Leave authRequired as-is; probeHealth sets it from the response (true on
97+ // 401, false on success). Resetting it here just flickers the auth fields.
60− loading = true; error = null; authRequired = false; emit()
98+ loading = true; error = null; emit()
61− scope.launch { probeHealth() }
100+ connectJob = scope.launch { probeHealth() }
103+ Event.LoadSessions -> {
104+ if (!connected) { error = "Not connected"; emit(); return }
105+ loading = true; emit()
106+ loadSessionsJob?.cancel()
64− Event.LoadSessions -> { loading = true; emit(); scope.launch { loadSessions() } }
107+ loadSessionsJob = scope.launch { loadSessions() }
66 is Event.SelectSession -> {
110 is Event.SelectSession -> {
111+ // The session may have been deleted (e.g. via SSE) while the list was on
112+ // screen; opening it would only 404 on loadMessages. Bail with an error.
67− currentSessionTitle = sessions.firstOrNull { it.id == event.id }?.title.orEmpty()
113+ val session = sessions.firstOrNull { it.id == event.id }
114+ if (session == null) { error = "Session no longer exists"; emit(); return }
115+ currentSessionTitle = session.title
68 currentSessionId = event.id
116 currentSessionId = event.id
69 messages.clear()
117 messages.clear()
118+ removedMessageIds.clear()
70 screen = Screen.Chat
119 screen = Screen.Chat
71 loading = true
120 loading = true
72 error = null
122 error = null
124+ connectJob?.cancel()
126+ sendMessageJob?.cancel()
127+ deleteSessionJob?.cancel()
128+ loadMessagesJob?.cancel()
74− scope.launch { loadMessages(event.id) }
129+ loadMessagesJob = scope.launch { loadMessages(event.id) }
132+ Event.CreateSession -> {
133+ if (!connected) { error = "Not connected"; emit(); return }
134+ loading = true; error = null; emit()
135+ createSessionJob?.cancel()
77− Event.CreateSession -> { loading = true; emit(); scope.launch { createSession() } }
136+ createSessionJob = scope.launch { createSession() }
79 is Event.DeleteSession -> {
139 is Event.DeleteSession -> {
140+ if (!connected) { error = "Not connected"; emit(); return }
142+ // Delete on the server first; only drop it from the UI once that
143+ // succeeds, so a failed request leaves the session recoverable.
144+ loading = true; error = null; emit()
145+ deleteSessionJob?.cancel()
146+ deleteSessionJob = scope.launch {
147+ http.delete("$serverUrl/session/$id", authHeader()).fold(
148+ onSuccess = { resp ->
149+ if (resp.code in 200..299) {
80− sessions.removeAll { it.id == event.id }
150+ sessions.removeAll { it.id == id }
81− if (isCurrent(event.id)) {
151+ if (isCurrent(id)) {
82− currentSessionId = null
152+ currentSessionId = null
83− messages.clear()
153+ messages.clear()
84− screen = Screen.Sessions
154+ screen = Screen.Sessions
157+ error = "Failed to delete session"
162+ error = "Failed to delete session"
87− scope.launch { http.delete("$serverUrl/session/${event.id}", authHeader()) }
90 is Event.DraftChanged -> { draft = event.text; emit() }
170 is Event.DraftChanged -> { draft = event.text; emit() }
@@ -92,26 +172,57 @@ class OpencodeCore(
92 is Event.SendMessage -> sendMessage(event.text)
172 is Event.SendMessage -> sendMessage(event.text)
94 Event.CancelGeneration -> {
174 Event.CancelGeneration -> {
95− generating = false; emit()
175+ generating = false
176+ messages.forEach { it.streaming = false }
96 val id = currentSessionId ?: return
178 val id = currentSessionId ?: return
179+ // Debounce rapid cancel taps: cancel any in-flight abort before firing
180+ // another so we don't stack duplicate abort requests.
97− scope.launch { http.postJson("$serverUrl/session/$id/abort", "{}", authHeader()) }
182+ abortJob = scope.launch { http.postJson("$serverUrl/session/$id/abort", "{}", authHeader()) }
100 Event.NavigateToSessions -> {
185 Event.NavigateToSessions -> {
101 screen = Screen.Sessions
186 screen = Screen.Sessions
102 currentSessionId = null
187 currentSessionId = null
103 messages.clear()
188 messages.clear()
189+ removedMessageIds.clear()
104 generating = false
191 generating = false
192+ // Clear any error from the chat (e.g. a failed send) so it doesn't
193+ // linger on the sessions list after the user navigates back.
197+ connectJob?.cancel()
199+ createSessionJob?.cancel()
200+ deleteSessionJob?.cancel()
201+ sendMessageJob?.cancel()
202+ loadMessagesJob?.cancel()
203+ loadSessionsJob?.cancel()
106− scope.launch { loadSessions() }
204+ loadSessionsJob = scope.launch { loadSessions() }
109 Event.NavigateToConnect -> {
207 Event.NavigateToConnect -> {
110 screen = Screen.Connect
208 screen = Screen.Connect
111 connected = false
209 connected = false
112 sessions.clear()
212 sessions.clear()
113 messages.clear()
213 messages.clear()
214+ removedMessageIds.clear()
114 currentSessionId = null
215 currentSessionId = null
217+ // Reset to the default so a late-firing close callback from the old
218+ // SSE connection can't flash the reconnecting banner on the next connect.
220+ connectJob?.cancel()
222+ createSessionJob?.cancel()
223+ deleteSessionJob?.cancel()
224+ sendMessageJob?.cancel()
225+ loadMessagesJob?.cancel()
115 closeSse()
226 closeSse()
@@ -125,26 +236,44 @@ class OpencodeCore(
125 if (connected && sse == null) startSse()
236 if (connected && sse == null) startSse()
128− fun shutdown() = closeSse()
239+ fun shutdown() {
240+ // Cancel every tracked coroutine before tearing down the stream so none can
241+ // emit into a cancelled scope after the ViewModel is cleared.
242+ connectJob?.cancel()
243+ loadSessionsJob?.cancel()
244+ createSessionJob?.cancel()
245+ loadMessagesJob?.cancel()
246+ deleteSessionJob?.cancel()
247+ sendMessageJob?.cancel()
130 // ── Effects ────────────────────────────────────────────────────────────────
252 // ── Effects ────────────────────────────────────────────────────────────────
131 private fun sendMessage(raw: String) {
253 private fun sendMessage(raw: String) {
132 val text = raw.trim()
254 val text = raw.trim()
133 val session = currentSessionId ?: return
255 val session = currentSessionId ?: return
134 if (text.isEmpty()) return
256 if (text.isEmpty()) return
257+ // Debounce rapid double-taps on send: a send is already in flight, so a second
258+ // tap would create a duplicate user message and fire a duplicate request.
259+ if (generating) return
135 localSeq += 1
260 localSeq += 1
136− val user = MsgState("local-$localSeq", "user", 0).apply {
261+ val user = MsgState("local-$localSeq", "user", System.currentTimeMillis()).apply {
137 status = MessageStatus.Pending
262 status = MessageStatus.Pending
138 textParts["local"] = text
263 textParts["local"] = text
140 messages.add(user)
265 messages.add(user)
266+ // Keep the draft text so we can restore it if the send fails; otherwise the
267+ // user's message is lost and they'd have to retype it.
268+ val savedDraft = draft
141 draft = ""
269 draft = ""
142 generating = true
270 generating = true
143 error = null
271 error = null
146 val body = Protocol.json.encodeToString(PromptBody.serializer(), PromptBody(listOf(TextInput(text = text))))
274 val body = Protocol.json.encodeToString(PromptBody.serializer(), PromptBody(listOf(TextInput(text = text))))
275+ sendMessageJob?.cancel()
147− scope.launch {
276+ sendMessageJob = scope.launch {
148 val result = http.postJson("$serverUrl/session/$session/prompt_async", body, authHeader())
277 val result = http.postJson("$serverUrl/session/$session/prompt_async", body, authHeader())
149 result.fold(
278 result.fold(
150 onSuccess = { resp ->
279 onSuccess = { resp ->
@@ -153,12 +282,16 @@ class OpencodeCore(
154 messages.firstOrNull { it.id == user.id }?.status = MessageStatus.Failed
283 messages.firstOrNull { it.id == user.id }?.status = MessageStatus.Failed
155 generating = false
284 generating = false
285+ messages.forEach { it.streaming = false }
156 error = "Server returned status ${resp.code}"
287 error = "Server returned status ${resp.code}"
159 onFailure = { e ->
290 onFailure = { e ->
160 messages.firstOrNull { it.id == user.id }?.status = MessageStatus.Failed
291 messages.firstOrNull { it.id == user.id }?.status = MessageStatus.Failed
161 generating = false
292 generating = false
293+ messages.forEach { it.streaming = false }
162 error = "Request failed: ${e.message}"
295 error = "Request failed: ${e.message}"
@@ -173,11 +306,17 @@ class OpencodeCore(
174 resp.code == 401 -> {
307 resp.code == 401 -> {
175 loading = false
308 loading = false
309+ // A prior connection may have succeeded (connected = true); a
310+ // re-probe returning 401 (e.g. expired credentials) means we're
311+ // no longer authenticated, so drop the connected flag.
313+ // Always reveal the auth fields so the user can see/edit
314+ // their credentials — even when the ones supplied were wrong.
176− if (hasCreds) error = "Invalid credentials" else authRequired = true
316+ if (hasCreds) error = "Invalid credentials"
179 resp.code in 200..299 -> {
319 resp.code in 200..299 -> {
180− if (hasCreds) authed = true
181 connected = true
320 connected = true
182 authRequired = false
321 authRequired = false
183 error = null
322 error = null
@@ -185,10 +324,14 @@ class OpencodeCore(
185 loading = true
324 loading = true
187 startSse()
326 startSse()
327+ // Route through loadSessionsJob so a prior in-flight loadSessions
328+ // (e.g. from a rapid double-connect) is cancelled first.
329+ loadSessionsJob?.cancel()
188− loadSessions()
330+ loadSessionsJob = scope.launch { loadSessions() }
190 else -> {
332 else -> {
191 loading = false
333 loading = false
192 error = "Server returned status ${resp.code}"
335 error = "Server returned status ${resp.code}"
@@ -196,6 +339,7 @@ class OpencodeCore(
197 onFailure = { e ->
340 onFailure = { e ->
198 loading = false
341 loading = false
199 error = "Connection failed: ${e.message}"
343 error = "Connection failed: ${e.message}"
@@ -203,31 +347,38 @@ class OpencodeCore(
205 private suspend fun loadSessions() {
349 private suspend fun loadSessions() {
207 http.get("$serverUrl/session", authHeader()).fold(
350 http.get("$serverUrl/session", authHeader()).fold(
208 onSuccess = { resp ->
351 onSuccess = { resp ->
209 if (resp.code in 200..299) {
352 if (resp.code in 200..299) {
210 sessions.clear()
353 sessions.clear()
211 sessions.addAll(Protocol.parseSessions(resp.body).map { SessionView(it.id, displayTitle(it.title)) })
354 sessions.addAll(Protocol.parseSessions(resp.body).map { SessionView(it.id, displayTitle(it.title)) })
355+ // Impose a stable alphabetical order so the list doesn't shuffle with
356+ // whatever order the server happens to return sessions in.
357+ sessions.sortBy { it.title.lowercase() }
213 error = "Server returned status ${resp.code}"
359 error = "Server returned status ${resp.code}"
216 onFailure = { e -> error = "Request failed: ${e.message}" },
362 onFailure = { e -> error = "Request failed: ${e.message}" },
221 private suspend fun createSession() {
368 private suspend fun createSession() {
223 http.postJson("$serverUrl/session", "{}", authHeader()).fold(
369 http.postJson("$serverUrl/session", "{}", authHeader()).fold(
224 onSuccess = { resp ->
370 onSuccess = { resp ->
225 val session = if (resp.code in 200..299) Protocol.parseSession(resp.body) else null
371 val session = if (resp.code in 200..299) Protocol.parseSession(resp.body) else null
226 if (session != null) {
372 if (session != null) {
227 if (sessions.none { it.id == session.id }) {
373 if (sessions.none { it.id == session.id }) {
228 sessions.add(0, SessionView(session.id, displayTitle(session.title)))
374 sessions.add(0, SessionView(session.id, displayTitle(session.title)))
375+ // Restore the alphabetical order immediately (matching loadSessions /
376+ // SessionUpserted) so the new session doesn't sit out-of-order at the
377+ // top until the SSE SessionUpserted event arrives to re-sort.
378+ sessions.sortBy { it.title.lowercase() }
381+ // SelectSession takes over the loading state (loads messages).
231 dispatch(Event.SelectSession(session.id))
382 dispatch(Event.SelectSession(session.id))
@@ -235,16 +386,18 @@ class OpencodeCore(
236 onFailure = { e -> error = "Request failed: ${e.message}" },
387 onFailure = { e -> error = "Request failed: ${e.message}" },
241 private suspend fun loadMessages(sessionId: String) {
393 private suspend fun loadMessages(sessionId: String) {
242 http.get("$serverUrl/session/$sessionId/message", authHeader()).fold(
394 http.get("$serverUrl/session/$sessionId/message", authHeader()).fold(
243 onSuccess = { resp ->
395 onSuccess = { resp ->
244− if (!isCurrent(sessionId)) return // user navigated away while loading
396+ if (!isCurrent(sessionId)) { loading = false; return } // user navigated away while loading
245 loading = false
397 loading = false
246 if (resp.code in 200..299) {
398 if (resp.code in 200..299) {
247 messages.clear()
399 messages.clear()
400+ removedMessageIds.clear()
248 messages.addAll(Protocol.parseMessages(resp.body).map(::messageFromWire))
401 messages.addAll(Protocol.parseMessages(resp.body).map(::messageFromWire))
249 generating = messages.any { it.streaming }
402 generating = messages.any { it.streaming }
@@ -253,8 +406,9 @@ class OpencodeCore(
255 onFailure = { e ->
408 onFailure = { e ->
256− if (!isCurrent(sessionId)) return
409+ if (!isCurrent(sessionId)) { loading = false; return }
257 loading = false
410 loading = false
258 error = "Request failed: ${e.message}"
412 error = "Request failed: ${e.message}"
@@ -269,7 +423,13 @@ class OpencodeCore(
269 url = "$serverUrl/event",
423 url = "$serverUrl/event",
270 auth = authHeader(),
424 auth = authHeader(),
271 onEvent = { json -> scope.launch { handleServerEvent(json) } },
425 onEvent = { json -> scope.launch { handleServerEvent(json) } },
272− onStateChange = { open -> if (!open) scheduleReconnect() },
426+ onStateChange = { open ->
429+ if (open) reconnectAttempts = 0 else scheduleReconnect()
274 sse = client
434 sse = client
275 client.connect()
435 client.connect()
@@ -277,29 +437,43 @@ class OpencodeCore(
278 private fun closeSse() {
438 private fun closeSse() {
279 sseClosedIntentionally = true
439 sseClosedIntentionally = true
440+ reconnectJob?.cancel()
280 sse?.close()
442 sse?.close()
281 sse = null
443 sse = null
284 private fun scheduleReconnect() {
446 private fun scheduleReconnect() {
285 if (sseClosedIntentionally || !connected) return
447 if (sseClosedIntentionally || !connected) return
448+ // Exponential backoff: 2s, 4s, 8s, 16s, capped at 30s. Reset to 0 once the
449+ // stream reconnects (onStateChange(true)) so a healthy server starts fresh.
450+ val delayMs = (2000L * (1L shl reconnectAttempts)).coerceAtMost(30_000L)
451+ if (delayMs < 30_000L) reconnectAttempts += 1
452+ // Cancel any pending reconnect so rapid network flapping can't stack multiple
453+ // coroutines that each open a new SSE connection.
454+ reconnectJob?.cancel()
286− scope.launch {
455+ reconnectJob = scope.launch {
287− delay(2000)
456+ delay(delayMs)
288 if (!sseClosedIntentionally && connected && screen != Screen.Connect) startSse()
457 if (!sseClosedIntentionally && connected && screen != Screen.Connect) startSse()
292 private fun handleServerEvent(json: String) {
461 private fun handleServerEvent(json: String) {
293 val event = Protocol.parseEvent(json) ?: return
462 val event = Protocol.parseEvent(json) ?: return
294− applyEvent(event)
463+ // Only re-render if the event actually changed our state; applyEvent returns
295− emit()
464+ // false for events that don't apply (other session, unknown message, …).
465+ if (applyEvent(event)) emit()
468+ /** Apply a server event to the model. Returns true iff any state actually changed. */
298− private fun applyEvent(event: Protocol.Event) {
469+ private fun applyEvent(event: Protocol.Event): Boolean {
299 when (event) {
470 when (event) {
300 is Protocol.Event.MessageUpdated -> {
471 is Protocol.Event.MessageUpdated -> {
301 val info = event.info
472 val info = event.info
302− if (info.role != "assistant" || !isCurrent(info.sessionId)) return
473+ if (info.role != "assistant" || !isCurrent(info.sessionId)) return false
474+ // A MessageRemoved may have arrived before this (late/out-of-order) update;
475+ // don't re-create a message the server has already deleted.
476+ if (info.id in removedMessageIds) return false
303 val streaming = info.time.completed == null && info.error == null
477 val streaming = info.time.completed == null && info.error == null
304 val existing = messages.firstOrNull { it.id == info.id }
478 val existing = messages.firstOrNull { it.id == info.id }
305 if (existing != null) {
479 if (existing != null) {
@@ -308,38 +482,59 @@ class OpencodeCore(
309 messages.add(MsgState(info.id, "assistant", info.time.created).apply { this.streaming = streaming })
483 messages.add(MsgState(info.id, "assistant", info.time.created).apply { this.streaming = streaming })
311− info.error?.let { error = it.message(); generating = false }
485+ info.error?.let { error = it.message(); messages.forEach { m -> m.streaming = false }; generating = false }
314 is Protocol.Event.PartUpdated -> {
489 is Protocol.Event.PartUpdated -> {
315 val part = event.part
490 val part = event.part
316− val msg = messages.firstOrNull { it.id == part.messageId } ?: return
491+ val msg = messages.firstOrNull { it.id == part.messageId } ?: return false
492+ // Synthetic text parts are intentionally skipped; report no change so we
493+ // don't trigger a needless re-render/emit for them.
317− when (part) {
494+ return when (part) {
318− is Protocol.Part.Text -> if (!part.synthetic) {
495+ is Protocol.Part.Text -> if (part.synthetic) {
319 msg.textParts[part.id] = pickText(part.text, event.delta, msg.textParts[part.id])
498 msg.textParts[part.id] = pickText(part.text, event.delta, msg.textParts[part.id])
321− is Protocol.Part.Reasoning ->
501+ is Protocol.Part.Reasoning -> {
322 msg.reasoningParts[part.id] = pickText(part.text, event.delta, msg.reasoningParts[part.id])
502 msg.reasoningParts[part.id] = pickText(part.text, event.delta, msg.reasoningParts[part.id])
323− is Protocol.Part.Tool ->
505+ is Protocol.Part.Tool -> {
324 msg.toolParts[part.id] = ToolView(part.tool, part.status, part.title)
506 msg.toolParts[part.id] = ToolView(part.tool, part.status, part.title)
512+ is Protocol.Event.PartRemoved -> {
328− is Protocol.Event.PartRemoved -> messages.firstOrNull { it.id == event.messageId }?.remove(event.partId)
513+ val msg = messages.firstOrNull { it.id == event.messageId } ?: return false
514+ msg.remove(event.partId)
518+ is Protocol.Event.MessageRemoved -> {
519+ // Remember the ID so a late MessageUpdated can't resurrect the message.
520+ removedMessageIds.add(event.messageId)
330− is Protocol.Event.MessageRemoved -> messages.removeAll { it.id == event.messageId }
521+ return messages.removeAll { it.id == event.messageId }
332− is Protocol.Event.SessionIdle -> if (isCurrent(event.sessionId)) {
524+ is Protocol.Event.SessionIdle -> {
525+ if (!isCurrent(event.sessionId)) return false
333 generating = false
526 generating = false
334 messages.forEach { it.streaming = false }
527 messages.forEach { it.streaming = false }
337 is Protocol.Event.SessionError -> {
531 is Protocol.Event.SessionError -> {
338 val scoped = event.sessionId == null || isCurrent(event.sessionId)
532 val scoped = event.sessionId == null || isCurrent(event.sessionId)
339− if (scoped) {
533+ if (!scoped) return false
340− event.error?.let { error = it.message() }
534+ event.error?.let { error = it.message() }
341− generating = false
535+ generating = false
342− }
536+ messages.forEach { it.streaming = false }
345 is Protocol.Event.SessionUpserted -> {
540 is Protocol.Event.SessionUpserted -> {
@@ -350,19 +545,26 @@ class OpencodeCore(
351 sessions.add(0, SessionView(event.info.id, title))
546 sessions.add(0, SessionView(event.info.id, title))
548+ // Keep the list alphabetically sorted (matching loadSessions) so an
549+ // upserted session doesn't jump to the top and break the ordering.
550+ sessions.sortBy { it.title.lowercase() }
353 if (isCurrent(event.info.id)) currentSessionTitle = title
551 if (isCurrent(event.info.id)) currentSessionTitle = title
356− is Protocol.Event.SessionDeleted -> event.sessionId?.let { id ->
555+ is Protocol.Event.SessionDeleted -> {
556+ val id = event.sessionId ?: return false
357− sessions.removeAll { it.id == id }
557+ val removed = sessions.removeAll { it.id == id }
358 if (isCurrent(id)) {
558 if (isCurrent(id)) {
359 currentSessionId = null
559 currentSessionId = null
360 messages.clear()
560 messages.clear()
361 screen = Screen.Sessions
561 screen = Screen.Sessions
365− Protocol.Event.Other -> Unit
567+ Protocol.Event.Other -> return false
@@ -382,6 +584,7 @@ class OpencodeCore(
382 messages = messages.map { it.toView() },
584 messages = messages.map { it.toView() },
383 draft = draft,
585 draft = draft,
384 generating = generating,
586 generating = generating,
587+ sseConnected = sseConnected,
387 private fun emit() { _view.value = snapshot() }
590 private fun emit() { _view.value = snapshot() }
@@ -444,7 +647,16 @@ private fun messageFromWire(env: Protocol.WireMessageEnvelope): MsgState {
444private fun pickText(text: String, delta: String?, prior: String?): String =
647private fun pickText(text: String, delta: String?, prior: String?): String =
445 if (text.isNotEmpty()) text else (prior.orEmpty() + (delta ?: ""))
648 if (text.isNotEmpty()) text else (prior.orEmpty() + (delta ?: ""))
447−private fun normalizeUrl(url: String): String = url.trim().trimEnd('/')
650+private fun normalizeUrl(url: String): String {
651+ val trimmed = url.trim().trimEnd('/')
652+ // Default to http:// when the user didn't type a scheme (e.g. "192.168.1.10:4096").
653+ // Without a scheme OkHttp's URL parser throws and the connect attempt crashes.
655+ trimmed.isEmpty() -> ""
656+ "://" in trimmed -> trimmed
657+ else -> "http://$trimmed"
449private fun displayTitle(title: String): String = title.trim().ifEmpty { "Untitled" }
661private fun displayTitle(title: String): String = title.trim().ifEmpty { "Untitled" }