Expose SSE stream status in chat header

This commit is contained in:
OpenClaw committed 2026-03-07 08:54:21 +08:00
1 parent d9582d9b86
commit 151d665061
4 files changed
+28 -2

No files matched your search

@@ -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 {
@@ -4,6 +4,7 @@ interface OpenClawRepository {
var availableAgents: List<String>
val sessions: List<SessionSummary>
var status: ServerStatus
fun streamStatusFor(sessionId: String): String?
fun findSession(sessionId: String): SessionSummary?
fun messagesFor(sessionId: String): List<ChatMessage>
@@ -53,11 +53,14 @@ class RemoteOpenClawRepository(
private var recentSessionId by mutableStateOf<String?>(null)
private var messageStore by mutableStateOf<Map<String, List<ChatMessage>>>(emptyMap())
private var streamStatusStore by mutableStateOf<Map<String, String>>(emptyMap())
override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
override fun messagesFor(sessionId: String): List<ChatMessage> = 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)
}
}
@@ -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",
)
}
}