diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index fefce64..21a2c94 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -164,6 +164,7 @@ private fun OpenClawRoot(appState: OpenClawAppState) { isSending = appState.sendingSessionId == activeSession.id, isLoading = appState.isSyncing, errorMessage = appState.syncError, + streamStatus = appState.repository.streamStatusFor(activeSession.id), onBack = appState::closeChat, onRetry = { coroutineScope.launch { diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt index 36bcada..af925de 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -4,6 +4,7 @@ interface OpenClawRepository { var availableAgents: List val sessions: List var status: ServerStatus + fun streamStatusFor(sessionId: String): String? fun findSession(sessionId: String): SessionSummary? fun messagesFor(sessionId: String): List diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt index 8ea1ff4..053794b 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -53,11 +53,14 @@ class RemoteOpenClawRepository( private var recentSessionId by mutableStateOf(null) private var messageStore by mutableStateOf>>(emptyMap()) + private var streamStatusStore by mutableStateOf>(emptyMap()) override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId } override fun messagesFor(sessionId: String): List = messageStore[sessionId].orEmpty() + override fun streamStatusFor(sessionId: String): String? = streamStatusStore[sessionId] + override fun recentSession(): SessionSummary? = recentSessionId?.let(::findSession) override fun pinnedCount(): Int = sessions.count { it.isPinned } @@ -160,9 +163,11 @@ class RemoteOpenClawRepository( private fun subscribeSessionStream(sessionId: String) { streamJob?.cancel() + streamStatusStore = streamStatusStore + (sessionId to "connecting") streamJob = streamScope.launch { while (currentCoroutineContext().isActive) { try { + streamStatusStore = streamStatusStore + (sessionId to "live") sseStream.connect(sessionId).collect { event -> when (event) { is SessionStreamEvent.Snapshot -> { @@ -238,6 +243,7 @@ class RemoteOpenClawRepository( is SessionStreamEvent.Failed -> { val dto = event.payload if (!dto.recoverable) { + streamStatusStore = streamStatusStore + (sessionId to "failed") messageStore = messageStore + ( sessionId to (messageStore[sessionId].orEmpty() + ChatMessage( id = "stream-failed-$sessionId", @@ -251,6 +257,7 @@ class RemoteOpenClawRepository( updateSessionPreview(sessionId, messageStore[sessionId]?.lastOrNull()) return@collect } + streamStatusStore = streamStatusStore + (sessionId to "reconnecting") // recoverable: will retry via outer while loop } } @@ -258,6 +265,7 @@ class RemoteOpenClawRepository( } catch (e: CancellationException) { throw e } catch (_: Throwable) { + streamStatusStore = streamStatusStore + (sessionId to "reconnecting") delay(1500) } } diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt index 5e98896..383b851 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/feature/chat/ChatScreen.kt @@ -48,6 +48,7 @@ fun ChatScreen( isSending: Boolean, isLoading: Boolean, errorMessage: String?, + streamStatus: String?, onBack: () -> Unit, onRetry: () -> Unit, onSend: (String) -> Unit, @@ -82,12 +83,27 @@ fun ChatScreen( Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) { Text(sessionTitle, style = MaterialTheme.typography.titleLarge) Text( - if (isSending) "消息请求已提交,正在等待 SSE 首帧…" else "当前会话已接入实时消息流(SSE)。", + when { + isSending -> "消息请求已提交,正在等待 SSE 首帧…" + streamStatus == "reconnecting" -> "SSE 连接中断,正在重连…" + streamStatus == "failed" -> "SSE 已断开,请手动重试。" + streamStatus == "connecting" -> "正在建立 SSE 连接…" + else -> "当前会话已接入实时消息流(SSE)。" + }, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) } - StatusPill(text = if (isLoading) "同步中" else "在线", highlighted = true) + StatusPill( + text = when { + isLoading -> "同步中" + streamStatus == "reconnecting" -> "重连中" + streamStatus == "failed" -> "已断流" + streamStatus == "connecting" -> "连接中" + else -> "在线" + }, + highlighted = streamStatus != "failed", + ) } }