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 c0f9a6b..c0d5f9c 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -125,7 +125,11 @@ class OpenClawAppState( isSyncing = true runCatching { remote.refreshMessages(sessionId) + val autoRetried = remote.retryAutoRecoverableMessages(sessionId) persistRecentMessageCache(remote, preferredSessionId = sessionId) + if (autoRetried) { + feedbackMessage = "已自动重试恢复的待发消息" + } }.onFailure { syncError = it.message ?: "会话消息加载失败" feedbackMessage = "消息加载失败" 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 1e52617..5a50a81 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -70,6 +70,7 @@ class RemoteOpenClawRepository( private val attachmentRetryStore = mutableMapOf() private val attachmentOpenStore = mutableMapOf() private val textRetryStore = mutableMapOf() + private val autoRetryMessageIds = mutableSetOf() override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId } @@ -331,6 +332,23 @@ class RemoteOpenClawRepository( updateSessionPreview(sessionId, merged.lastOrNull()) } + suspend fun retryAutoRecoverableMessages(sessionId: String): Boolean { + val candidates = messageStore[sessionId].orEmpty() + .filter { it.id in autoRetryMessageIds && it.role == MessageRole.User && it.kind == MessageKind.Text && it.status == "send-failed" } + .map { it.id } + if (candidates.isEmpty()) return false + + var attempted = false + for (messageId in candidates) { + attempted = true + autoRetryMessageIds.remove(messageId) + runCatching { + retryMessage(sessionId, messageId) + } + } + return attempted + } + suspend fun sendMessageRemote(sessionId: String, text: String) { sendMessage(sessionId, text) runCatching { @@ -347,6 +365,7 @@ class RemoteOpenClawRepository( val retried = current.map { msg -> if (msg.id == messageId) msg.copy(status = "queued", retryable = false, createdAt = "现在") else msg } + autoRetryMessageIds.remove(messageId) val withThinking = if (retried.none { it.id.startsWith("local-thinking-") }) { retried + ChatMessage( id = "local-thinking-$sessionId-${retried.size + 1}", @@ -533,6 +552,7 @@ class RemoteOpenClawRepository( return when { domain.role == MessageRole.User && domain.kind == MessageKind.Text && domain.status == "queued" -> { textRetryStore[domain.id] = TextRetryPayload(sessionId, domain.text) + autoRetryMessageIds.add(domain.id) domain.copy(status = "send-failed", retryable = true) } domain.role == MessageRole.User && domain.kind == MessageKind.Text && domain.status == "send-failed" -> { diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index e0cc7e9..9d0adb7 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -184,8 +184,9 @@ Android/iOS App - [x] App 重启后,缓存里的 `queued` 文本会恢复成可重试的 `send-failed` - [x] App 重启后,缓存里的 `uploading` 附件会恢复成明确失败态,避免假性卡死 - [x] 手动刷新/重新进入会话时,会保留本地尚未被远端确认的失败消息,避免刷新后直接消失 +- [x] 从缓存恢复出来的 `queued` 文本,在重新进入会话时会自动尝试补发一次 - [ ] 还没有离线发送队列 -- [ ] 还没有网络恢复后的自动补发 +- [ ] 还没有真正基于网络恢复信号的自动补发 ### 4.2 附件后端主链路 @@ -374,3 +375,4 @@ Android/iOS App - 2026-03-07:修复了 cached-data 相关编译错误:补回 `OpenClawAppState` 中缺失的 `isShowingCachedData` / `cacheSavedAt` 状态字段,并移除了 `MobileGatewayApi` 中未使用且不兼容的 `decodeBase64Bytes` import。 - 2026-03-07:补强发送失败状态缓存:发送/重试消息、上传/重试附件后会及时刷新最近消息缓存;App 重启后,缓存中的 `queued` 文本会转成可重试失败态,缓存中的 `uploading` 附件会转成明确失败态,避免 UI 长时间停留在假性发送中。 - 2026-03-07:补强消息刷新合并逻辑:手动刷新或重新进入会话时,若某条本地失败消息尚未被远端 history 确认,不会被 `refreshMessages()` 直接覆盖掉,而是继续保留在时间线里供用户重试。 +- 2026-03-07:新增第一版自动恢复补发:仅针对“从缓存恢复出来、原本处于 `queued` 的文本消息”,在重新进入会话时自动尝试补发一次;若仍失败,则继续保留为可手动重试状态。