From 527fac9025f2b86be8add312e2cc5e61d7b7cc72 Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 12:54:47 +0800 Subject: [PATCH] Persist pending attachment queue --- .../kotlin/cn/xiaobai/openclawapp/App.kt | 4 +- .../openclawapp/app/OpenClawAppState.kt | 20 +-- .../openclawapp/data/OpenClawRepository.kt | 1 + .../openclawapp/data/PendingSendQueue.kt | 13 +- .../data/RemoteOpenClawRepository.kt | 144 +++++++++++++++--- docs/MVP_PROGRESS.md | 3 +- 6 files changed, 147 insertions(+), 38 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index 20b8e54..c810a6b 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -196,8 +196,8 @@ private fun OpenClawRoot(appState: OpenClawAppState) { streamStatus = appState.repository.streamStatusFor(activeSession.id), pendingQueueStatus = appState.repository.pendingQueueStatus(activeSession.id).let { queue -> when { - queue.isDraining && queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发消息,系统正在按本地队列继续发送。" - queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发消息未完成,可继续重试或等待下一次自动处理。" + queue.isDraining && queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项,系统正在按本地队列继续处理。" + queue.sessionCount > 0 -> "当前会话还有 ${queue.sessionCount} 条待发项未完成,可继续重试或等待下一次自动处理。" else -> null } }, 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 d20f481..957d522 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -35,6 +35,9 @@ class OpenClawAppState( cacheSavedAt = snapshot.savedAt.ifBlank { null } hydrated = true } + preferences.loadPendingSendQueue()?.let { queue -> + repository.hydratePendingSendQueue(queue) + } preferences.loadRecentMessageSnapshotBucket()?.let { bucket -> repository.hydrateRecentMessages(bucket) if (cacheSavedAt == null) { @@ -42,9 +45,6 @@ class OpenClawAppState( } hydrated = true } - preferences.loadPendingSendQueue()?.let { queue -> - repository.hydratePendingSendQueue(queue) - } isShowingCachedData = hydrated } @@ -96,12 +96,13 @@ class OpenClawAppState( remote.refreshStatus() remote.refreshAgents() remote.refreshSessions() - val drained = remote.drainPendingTextQueue() + val drainedText = remote.drainPendingTextQueue() + val drainedAttachments = remote.drainPendingAttachmentQueue() preferences.saveOverviewSnapshot(remote.exportOverviewSnapshot()) persistRecentMessageCache(remote) persistPendingSendQueue(remote) - if (drained) { - feedbackMessage = "已尝试继续发送待发消息" + if (drainedText || drainedAttachments) { + feedbackMessage = "已尝试继续处理本地待发队列" } isShowingCachedData = false }.onFailure { @@ -135,13 +136,14 @@ class OpenClawAppState( runCatching { remote.refreshMessages(sessionId) val autoRetried = remote.retryAutoRecoverableMessages(sessionId) - val drained = remote.drainPendingTextQueue(sessionId) + val drainedText = remote.drainPendingTextQueue(sessionId) + val drainedAttachments = remote.drainPendingAttachmentQueue(sessionId) persistRecentMessageCache(remote, preferredSessionId = sessionId) persistPendingSendQueue(remote) if (autoRetried) { feedbackMessage = "已自动重试恢复的待发消息" - } else if (drained) { - feedbackMessage = "已尝试继续发送当前会话的待发消息" + } else if (drainedText || drainedAttachments) { + feedbackMessage = "已尝试继续处理当前会话的待发队列" } }.onFailure { syncError = it.message ?: "会话消息加载失败" 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 8478cc4..f23d9df 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -19,6 +19,7 @@ interface OpenClawRepository { fun sendMessage(sessionId: String, text: String) suspend fun retryMessage(sessionId: String, messageId: String) suspend fun drainPendingTextQueue(sessionId: String? = null): Boolean + suspend fun drainPendingAttachmentQueue(sessionId: String? = null): Boolean suspend fun updateDefaultAgent(agent: String) suspend fun uploadAttachmentStub( sessionId: String, diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt index 217d7ce..ed7270f 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/PendingSendQueue.kt @@ -8,11 +8,22 @@ data class PendingSendQueue( val savedAt: String = "", ) +@Serializable +enum class PendingSendKind { + Text, + Attachment, +} + @Serializable data class PendingSendItem( val messageId: String, val sessionId: String, - val text: String, val createdAt: String, + val kind: PendingSendKind = PendingSendKind.Text, + val text: String = "", val autoRetryEligible: Boolean = true, + val fileName: String = "", + val contentType: String? = null, + val base64Data: String? = null, + val sizeBytes: Int? = null, ) 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 2ae97cf..39b5c29 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -41,6 +41,15 @@ private data class PendingTextSend( val autoRetryEligible: Boolean = true, ) +private data class PendingAttachmentSend( + val messageId: String, + val sessionId: String, + val fileName: String, + val bytes: ByteArray, + val contentType: String?, + val createdAt: String, +) + class RemoteOpenClawRepository( private val config: ServerConfig, ) : OpenClawRepository { @@ -80,6 +89,7 @@ class RemoteOpenClawRepository( private val attachmentOpenStore = mutableMapOf() private val textRetryStore = mutableMapOf() private val pendingTextQueue = mutableMapOf() + private val pendingAttachmentQueue = mutableMapOf() private val autoRetryMessageIds = mutableSetOf() private var drainingSessionIds by mutableStateOf>(emptySet()) @@ -88,10 +98,11 @@ class RemoteOpenClawRepository( override fun messagesFor(sessionId: String): List = messageStore[sessionId].orEmpty() override fun pendingQueueStatus(sessionId: String?): PendingQueueStatus { - val items = pendingTextQueue.values.filter { sessionId == null || it.sessionId == sessionId } + val textItems = pendingTextQueue.values.filter { sessionId == null || it.sessionId == sessionId } + val attachmentItems = pendingAttachmentQueue.values.filter { sessionId == null || it.sessionId == sessionId } return PendingQueueStatus( - totalCount = pendingTextQueue.size, - sessionCount = items.size, + totalCount = pendingTextQueue.size + pendingAttachmentQueue.size, + sessionCount = textItems.size + attachmentItems.size, isDraining = sessionId?.let { it in drainingSessionIds } ?: drainingSessionIds.isNotEmpty(), ) } @@ -116,16 +127,43 @@ class RemoteOpenClawRepository( override fun hydratePendingSendQueue(queue: PendingSendQueue) { queue.items.forEach { item -> - pendingTextQueue[item.messageId] = PendingTextSend( - messageId = item.messageId, - sessionId = item.sessionId, - text = item.text, - createdAt = item.createdAt, - autoRetryEligible = item.autoRetryEligible, - ) - textRetryStore[item.messageId] = TextRetryPayload(item.sessionId, item.text) - if (item.autoRetryEligible) { - autoRetryMessageIds.add(item.messageId) + when (item.kind) { + PendingSendKind.Text -> { + pendingTextQueue[item.messageId] = PendingTextSend( + messageId = item.messageId, + sessionId = item.sessionId, + text = item.text, + createdAt = item.createdAt, + autoRetryEligible = item.autoRetryEligible, + ) + textRetryStore[item.messageId] = TextRetryPayload(item.sessionId, item.text) + if (item.autoRetryEligible) { + autoRetryMessageIds.add(item.messageId) + } + } + PendingSendKind.Attachment -> { + val encoded = item.base64Data ?: return@forEach + val bytes = Base64.decode(encoded) + pendingAttachmentQueue[item.messageId] = PendingAttachmentSend( + messageId = item.messageId, + sessionId = item.sessionId, + fileName = item.fileName, + bytes = bytes, + contentType = item.contentType, + createdAt = item.createdAt, + ) + attachmentRetryStore[item.messageId] = AttachmentRetryPayload( + sessionId = item.sessionId, + fileName = item.fileName, + bytes = bytes, + contentType = item.contentType, + ) + attachmentOpenStore[item.messageId] = AttachmentOpenPayload( + fileName = item.fileName, + bytes = bytes, + contentType = item.contentType, + ) + } } } } @@ -226,6 +264,14 @@ class RemoteOpenClawRepository( val localId = "local-attachment-$sessionId-${currentMessages.size + 1}" attachmentRetryStore[localId] = AttachmentRetryPayload(sessionId, fileName, bytes, contentType) attachmentOpenStore[localId] = AttachmentOpenPayload(fileName, bytes, contentType) + pendingAttachmentQueue[localId] = PendingAttachmentSend( + messageId = localId, + sessionId = sessionId, + fileName = fileName, + bytes = bytes, + contentType = contentType, + createdAt = "现在", + ) val pending = ChatMessage( id = localId, role = MessageRole.User, @@ -257,6 +303,7 @@ class RemoteOpenClawRepository( ) attachmentRetryStore.remove(localId) attachmentOpenStore.remove(localId) + pendingAttachmentQueue.remove(localId) attachmentOpenStore[remote.attachmentId] = AttachmentOpenPayload(remote.fileName, bytes, remote.contentType) val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) uploaded else msg } messageStore = messageStore + (sessionId to next) @@ -307,6 +354,7 @@ class RemoteOpenClawRepository( ) attachmentRetryStore.remove(messageId) attachmentOpenStore.remove(messageId) + pendingAttachmentQueue.remove(messageId) attachmentOpenStore[remote.attachmentId] = AttachmentOpenPayload(remote.fileName, payload.bytes, remote.contentType) val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) uploaded else msg } messageStore = messageStore + (sessionId to next) @@ -365,17 +413,34 @@ class RemoteOpenClawRepository( ) fun exportPendingSendQueue(): PendingSendQueue = PendingSendQueue( - items = pendingTextQueue.values - .sortedBy { it.createdAt } - .map { item -> - PendingSendItem( - messageId = item.messageId, - sessionId = item.sessionId, - text = item.text, - createdAt = item.createdAt, - autoRetryEligible = item.autoRetryEligible, - ) - }, + items = buildList { + addAll( + pendingTextQueue.values.sortedBy { it.createdAt }.map { item -> + PendingSendItem( + messageId = item.messageId, + sessionId = item.sessionId, + createdAt = item.createdAt, + kind = PendingSendKind.Text, + text = item.text, + autoRetryEligible = item.autoRetryEligible, + ) + } + ) + addAll( + pendingAttachmentQueue.values.sortedBy { it.createdAt }.map { item -> + PendingSendItem( + messageId = item.messageId, + sessionId = item.sessionId, + createdAt = item.createdAt, + kind = PendingSendKind.Attachment, + fileName = item.fileName, + contentType = item.contentType, + base64Data = Base64.encode(item.bytes), + sizeBytes = item.bytes.size, + ) + } + ) + }.sortedBy { it.createdAt }, savedAt = status.updatedAt, ) @@ -497,6 +562,35 @@ class RemoteOpenClawRepository( } } + override suspend fun drainPendingAttachmentQueue(sessionId: String?): Boolean { + val candidates = pendingAttachmentQueue.values + .filter { sessionId == null || it.sessionId == sessionId } + .sortedBy { it.createdAt } + .map { it.messageId } + if (candidates.isEmpty()) return false + + val targetSessionIds = pendingAttachmentQueue.values + .filter { sessionId == null || it.sessionId == sessionId } + .map { it.sessionId } + .toSet() + drainingSessionIds = drainingSessionIds + targetSessionIds + try { + var drained = false + for (messageId in candidates) { + val payload = pendingAttachmentQueue[messageId] ?: continue + drained = true + runCatching { + retryAttachment(payload.sessionId, messageId) + }.onFailure { + return drained + } + } + return drained + } finally { + drainingSessionIds = drainingSessionIds - targetSessionIds + } + } + private fun clearPendingTextMessage(sessionId: String, text: String) { val targetId = pendingTextQueue.values .filter { it.sessionId == sessionId && it.text == text } @@ -744,7 +838,7 @@ class RemoteOpenClawRepository( domain.copy(retryable = true) } domain.kind == MessageKind.Attachment && domain.status == "uploading" -> { - domain.copy(status = "upload-failed", retryable = false) + domain.copy(status = "upload-failed", retryable = true) } else -> domain } diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index 63f20d7..d93ef2d 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -198,7 +198,8 @@ Android/iOS App - [x] 聊天页已新增轻量 pending queue 状态提示:可看到当前会话剩余待发条数,以及系统是否正在按本地队列继续发送 - [x] 已有保守版 queue drain:`openSession()` 会尝试继续发送当前会话的待发文本,`syncRemoteIfNeeded()` 会顺序尝试全局待发文本,失败即停 - [x] 聊天页已显式区分 queue drain 状态:队列自动处理中的文本会显示“队列发送中” -- [ ] 还没有完整的离线发送队列(目前先覆盖文本,不含附件) +- [x] pending queue 已开始同时覆盖文本与附件:附件待上传项也会独立持久化,并在重新进入会话或手动同步时继续尝试上传 +- [ ] 还没有完整的离线发送队列(当前已覆盖文本 + 附件,但还没有更完整的网络感知、后台 drain 与统一淘汰策略) - [ ] 还没有真正基于网络恢复信号的自动补发 ### 4.2 附件后端主链路