From b441ac9cc6b60ddd4a8179410af344632faae921 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 09:29:42 +0800 Subject: [PATCH] Add retry flow for failed text messages --- .../kotlin/cn/xiaobai/openclawapp/App.kt | 5 ++ .../openclawapp/app/OpenClawAppState.kt | 12 ++++ .../openclawapp/data/OpenClawRepository.kt | 1 + .../data/RemoteOpenClawRepository.kt | 63 +++++++++++++++++-- .../openclawapp/feature/chat/ChatScreen.kt | 10 +++ 5 files changed, 85 insertions(+), 6 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index babb4da..857eb33 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -202,6 +202,11 @@ private fun OpenClawRoot(appState: OpenClawAppState) { appState.retryAttachment(activeSession.id, messageId) } }, + onRetryMessage = { messageId -> + coroutineScope.launch { + appState.retryMessage(activeSession.id, messageId) + } + }, ) appState.selectedTab == MainTab.Sessions -> SessionListScreen( diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt index 86e7845..cec8ed4 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -124,6 +124,18 @@ class OpenClawAppState( sendingSessionId = null } + suspend fun retryMessage(sessionId: String, messageId: String) { + sendingSessionId = sessionId + runCatching { + repository.retryMessage(sessionId, messageId) + feedbackMessage = "已重新发送消息" + }.onFailure { + syncError = it.message ?: "消息重试失败" + feedbackMessage = "消息重试失败" + } + sendingSessionId = null + } + suspend fun updateDefaultAgent(agent: String) { runCatching { repository.updateDefaultAgent(agent) 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 f6c15db..6ea0678 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -13,6 +13,7 @@ interface OpenClawRepository { fun markSessionOpened(sessionId: String) fun togglePinned(sessionId: String): Boolean fun sendMessage(sessionId: String, text: String) + suspend fun retryMessage(sessionId: String, messageId: String) suspend fun updateDefaultAgent(agent: String) suspend fun uploadAttachmentStub( sessionId: String, 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 21f0dc1..bb1540e 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -27,6 +27,11 @@ private data class AttachmentRetryPayload( val contentType: String?, ) +private data class TextRetryPayload( + val sessionId: String, + val text: String, +) + class RemoteOpenClawRepository( private val config: ServerConfig, ) : OpenClawRepository { @@ -62,6 +67,7 @@ class RemoteOpenClawRepository( private var messageStore by mutableStateOf>>(emptyMap()) private var streamStatusStore by mutableStateOf>(emptyMap()) private val attachmentRetryStore = mutableMapOf() + private val textRetryStore = mutableMapOf() override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId } @@ -104,10 +110,12 @@ class RemoteOpenClawRepository( if (hasQueuedUser) return val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") } + val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}" + textRetryStore[localUserId] = TextRetryPayload(sessionId, text) messageStore = messageStore + ( sessionId to (withoutThinking + listOf( ChatMessage( - id = "local-user-$sessionId-${withoutThinking.size + 1}", + id = localUserId, role = MessageRole.User, text = text, createdAt = "现在", @@ -273,7 +281,33 @@ class RemoteOpenClawRepository( runCatching { api.sendMessage(sessionId, text) }.onFailure { - clearLocalPendingMessages(sessionId, text) + markTextMessageFailed(sessionId, text) + throw it + } + } + + override suspend fun retryMessage(sessionId: String, messageId: String) { + val payload = textRetryStore[messageId] ?: return + val current = messageStore[sessionId].orEmpty() + val retried = current.map { msg -> + if (msg.id == messageId) msg.copy(status = "queued", retryable = false, createdAt = "现在") else msg + } + val withThinking = if (retried.none { it.id.startsWith("local-thinking-") }) { + retried + ChatMessage( + id = "local-thinking-$sessionId-${retried.size + 1}", + role = MessageRole.Assistant, + text = "", + createdAt = "现在", + status = "thinking", + ) + } else retried + messageStore = messageStore + (sessionId to withThinking) + updateSessionPreview(sessionId, withThinking.lastOrNull()) + + runCatching { + api.sendMessage(sessionId, payload.text) + }.onFailure { + markTextMessageFailed(sessionId, payload.text, existingMessageId = messageId) throw it } } @@ -417,13 +451,26 @@ class RemoteOpenClawRepository( resortSessions(prioritizeId = recentSessionId) } - private fun clearLocalPendingMessages(sessionId: String, text: String) { + private fun markTextMessageFailed(sessionId: String, text: String, existingMessageId: String? = null) { val currentMessages = messageStore[sessionId].orEmpty() - val next = currentMessages.filterNot { message -> - message.id.startsWith("local-thinking-") || - (message.role == MessageRole.User && message.text == text && message.status == "queued") + val next = currentMessages.mapNotNull { message -> + when { + message.id.startsWith("local-thinking-") -> null + message.role == MessageRole.User && message.text == text && (existingMessageId == null || message.id == existingMessageId) -> { + val failedId = existingMessageId ?: message.id + textRetryStore.putIfAbsent(failedId, TextRetryPayload(sessionId, text)) + message.copy( + id = failedId, + status = "send-failed", + retryable = true, + createdAt = "刚刚", + ) + } + else -> message + } } messageStore = messageStore + (sessionId to next) + updateSessionPreview(sessionId, next.lastOrNull()) } private fun mergeSnapshotWithLocalPending( @@ -455,6 +502,10 @@ class RemoteOpenClawRepository( if (shouldKeepThinking) { addAll(pendingThinking) } + }.onEach { msg -> + if (msg.role == MessageRole.User && msg.status != "send-failed") { + textRetryStore.remove(msg.id) + } } } 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 d398d08..d47eb08 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 @@ -54,6 +54,7 @@ fun ChatScreen( onSend: (String) -> Unit, onSendAttachmentStub: () -> Unit, onRetryAttachment: (String) -> Unit, + onRetryMessage: (String) -> Unit, ) { var input by remember { mutableStateOf("") } val listState = rememberLazyListState() @@ -174,6 +175,7 @@ fun ChatScreen( animateText = shouldAnimate, linkedToolName = linkedTool?.toolName, onRetryAttachment = onRetryAttachment, + onRetryMessage = onRetryMessage, ) } } @@ -233,6 +235,7 @@ private fun MessageBubble( animateText: Boolean = false, linkedToolName: String? = null, onRetryAttachment: (String) -> Unit, + onRetryMessage: (String) -> Unit, ) { val alignment = when (message.role) { MessageRole.User -> Alignment.CenterEnd @@ -279,6 +282,8 @@ private fun MessageBubble( val toolResultText = if (shouldCollapseToolResult && !expanded) displayText.take(240).trimEnd() + "…" else displayText val displayStatus = when { animateText && visibleChars < message.text.length -> "streaming" + message.status == "queued" -> "发送中" + message.status == "send-failed" -> "发送失败" message.status == "uploading" -> "上传中" message.status == "uploaded" -> "已上传" message.status == "upload-failed" -> "上传失败" @@ -430,6 +435,11 @@ private fun MessageBubble( style = if (isSystemNotice) MaterialTheme.typography.bodyMedium else MaterialTheme.typography.bodyLarge, color = if (isSystemNotice) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSurface, ) + if (message.retryable && message.status == "send-failed") { + TextButton(onClick = { onRetryMessage(message.id) }) { + Text("重试发送") + } + } } } Row(