Expose SSE stream status in chat header
This commit is contained in:
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,
|
isSending = appState.sendingSessionId == activeSession.id,
|
||||||
isLoading = appState.isSyncing,
|
isLoading = appState.isSyncing,
|
||||||
errorMessage = appState.syncError,
|
errorMessage = appState.syncError,
|
||||||
|
streamStatus = appState.repository.streamStatusFor(activeSession.id),
|
||||||
onBack = appState::closeChat,
|
onBack = appState::closeChat,
|
||||||
onRetry = {
|
onRetry = {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ interface OpenClawRepository {
|
|||||||
var availableAgents: List<String>
|
var availableAgents: List<String>
|
||||||
val sessions: List<SessionSummary>
|
val sessions: List<SessionSummary>
|
||||||
var status: ServerStatus
|
var status: ServerStatus
|
||||||
|
fun streamStatusFor(sessionId: String): String?
|
||||||
|
|
||||||
fun findSession(sessionId: String): SessionSummary?
|
fun findSession(sessionId: String): SessionSummary?
|
||||||
fun messagesFor(sessionId: String): List<ChatMessage>
|
fun messagesFor(sessionId: String): List<ChatMessage>
|
||||||
|
|||||||
+8
@@ -53,11 +53,14 @@ class RemoteOpenClawRepository(
|
|||||||
|
|
||||||
private var recentSessionId by mutableStateOf<String?>(null)
|
private var recentSessionId by mutableStateOf<String?>(null)
|
||||||
private var messageStore by mutableStateOf<Map<String, List<ChatMessage>>>(emptyMap())
|
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 findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
|
||||||
|
|
||||||
override fun messagesFor(sessionId: String): List<ChatMessage> = messageStore[sessionId].orEmpty()
|
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 recentSession(): SessionSummary? = recentSessionId?.let(::findSession)
|
||||||
|
|
||||||
override fun pinnedCount(): Int = sessions.count { it.isPinned }
|
override fun pinnedCount(): Int = sessions.count { it.isPinned }
|
||||||
@@ -160,9 +163,11 @@ class RemoteOpenClawRepository(
|
|||||||
|
|
||||||
private fun subscribeSessionStream(sessionId: String) {
|
private fun subscribeSessionStream(sessionId: String) {
|
||||||
streamJob?.cancel()
|
streamJob?.cancel()
|
||||||
|
streamStatusStore = streamStatusStore + (sessionId to "connecting")
|
||||||
streamJob = streamScope.launch {
|
streamJob = streamScope.launch {
|
||||||
while (currentCoroutineContext().isActive) {
|
while (currentCoroutineContext().isActive) {
|
||||||
try {
|
try {
|
||||||
|
streamStatusStore = streamStatusStore + (sessionId to "live")
|
||||||
sseStream.connect(sessionId).collect { event ->
|
sseStream.connect(sessionId).collect { event ->
|
||||||
when (event) {
|
when (event) {
|
||||||
is SessionStreamEvent.Snapshot -> {
|
is SessionStreamEvent.Snapshot -> {
|
||||||
@@ -238,6 +243,7 @@ class RemoteOpenClawRepository(
|
|||||||
is SessionStreamEvent.Failed -> {
|
is SessionStreamEvent.Failed -> {
|
||||||
val dto = event.payload
|
val dto = event.payload
|
||||||
if (!dto.recoverable) {
|
if (!dto.recoverable) {
|
||||||
|
streamStatusStore = streamStatusStore + (sessionId to "failed")
|
||||||
messageStore = messageStore + (
|
messageStore = messageStore + (
|
||||||
sessionId to (messageStore[sessionId].orEmpty() + ChatMessage(
|
sessionId to (messageStore[sessionId].orEmpty() + ChatMessage(
|
||||||
id = "stream-failed-$sessionId",
|
id = "stream-failed-$sessionId",
|
||||||
@@ -251,6 +257,7 @@ class RemoteOpenClawRepository(
|
|||||||
updateSessionPreview(sessionId, messageStore[sessionId]?.lastOrNull())
|
updateSessionPreview(sessionId, messageStore[sessionId]?.lastOrNull())
|
||||||
return@collect
|
return@collect
|
||||||
}
|
}
|
||||||
|
streamStatusStore = streamStatusStore + (sessionId to "reconnecting")
|
||||||
// recoverable: will retry via outer while loop
|
// recoverable: will retry via outer while loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,6 +265,7 @@ class RemoteOpenClawRepository(
|
|||||||
} catch (e: CancellationException) {
|
} catch (e: CancellationException) {
|
||||||
throw e
|
throw e
|
||||||
} catch (_: Throwable) {
|
} catch (_: Throwable) {
|
||||||
|
streamStatusStore = streamStatusStore + (sessionId to "reconnecting")
|
||||||
delay(1500)
|
delay(1500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ fun ChatScreen(
|
|||||||
isSending: Boolean,
|
isSending: Boolean,
|
||||||
isLoading: Boolean,
|
isLoading: Boolean,
|
||||||
errorMessage: String?,
|
errorMessage: String?,
|
||||||
|
streamStatus: String?,
|
||||||
onBack: () -> Unit,
|
onBack: () -> Unit,
|
||||||
onRetry: () -> Unit,
|
onRetry: () -> Unit,
|
||||||
onSend: (String) -> Unit,
|
onSend: (String) -> Unit,
|
||||||
@@ -82,12 +83,27 @@ fun ChatScreen(
|
|||||||
Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||||
Text(sessionTitle, style = MaterialTheme.typography.titleLarge)
|
Text(sessionTitle, style = MaterialTheme.typography.titleLarge)
|
||||||
Text(
|
Text(
|
||||||
if (isSending) "消息请求已提交,正在等待 SSE 首帧…" else "当前会话已接入实时消息流(SSE)。",
|
when {
|
||||||
|
isSending -> "消息请求已提交,正在等待 SSE 首帧…"
|
||||||
|
streamStatus == "reconnecting" -> "SSE 连接中断,正在重连…"
|
||||||
|
streamStatus == "failed" -> "SSE 已断开,请手动重试。"
|
||||||
|
streamStatus == "connecting" -> "正在建立 SSE 连接…"
|
||||||
|
else -> "当前会话已接入实时消息流(SSE)。"
|
||||||
|
},
|
||||||
style = MaterialTheme.typography.bodySmall,
|
style = MaterialTheme.typography.bodySmall,
|
||||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
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",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user