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 e906a0a..c0f9a6b 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -94,8 +94,7 @@ class OpenClawAppState( remote.refreshAgents() remote.refreshSessions() preferences.saveOverviewSnapshot(remote.exportOverviewSnapshot()) - val recentIds = listOfNotNull(activeSessionId, remote.recentSession()?.id).distinct().take(3) - preferences.saveRecentMessageSnapshotBucket(remote.exportRecentMessageSnapshotBucket(recentIds)) + persistRecentMessageCache(remote) isShowingCachedData = false }.onFailure { syncError = it.message ?: "远程同步失败" @@ -126,8 +125,7 @@ class OpenClawAppState( isSyncing = true runCatching { remote.refreshMessages(sessionId) - val recentIds = listOf(sessionId) + repository.sessions.map { it.id } - preferences.saveRecentMessageSnapshotBucket(remote.exportRecentMessageSnapshotBucket(recentIds.take(3))) + persistRecentMessageCache(remote, preferredSessionId = sessionId) }.onFailure { syncError = it.message ?: "会话消息加载失败" feedbackMessage = "消息加载失败" @@ -144,7 +142,9 @@ class OpenClawAppState( sendingSessionId = sessionId runCatching { remote.sendMessageRemote(sessionId, text) + persistRecentMessageCache(remote, preferredSessionId = sessionId) }.onFailure { + persistRecentMessageCache(remote, preferredSessionId = sessionId) syncError = it.message ?: "消息发送失败" feedbackMessage = "消息发送失败" } @@ -155,8 +155,10 @@ class OpenClawAppState( sendingSessionId = sessionId runCatching { repository.retryMessage(sessionId, messageId) + (repository as? RemoteOpenClawRepository)?.let { persistRecentMessageCache(it, preferredSessionId = sessionId) } feedbackMessage = "已重新发送消息" }.onFailure { + (repository as? RemoteOpenClawRepository)?.let { persistRecentMessageCache(it, preferredSessionId = sessionId) } syncError = it.message ?: "消息重试失败" feedbackMessage = "消息重试失败" } @@ -185,8 +187,10 @@ class OpenClawAppState( ) { runCatching { repository.uploadAttachmentStub(sessionId, fileName, bytes, contentType) + (repository as? RemoteOpenClawRepository)?.let { persistRecentMessageCache(it, preferredSessionId = sessionId) } feedbackMessage = "附件已加入发送队列" }.onFailure { + (repository as? RemoteOpenClawRepository)?.let { persistRecentMessageCache(it, preferredSessionId = sessionId) } syncError = it.message ?: "附件上传失败" feedbackMessage = "附件上传失败" } @@ -195,8 +199,10 @@ class OpenClawAppState( suspend fun retryAttachment(sessionId: String, messageId: String) { runCatching { repository.retryAttachment(sessionId, messageId) + (repository as? RemoteOpenClawRepository)?.let { persistRecentMessageCache(it, preferredSessionId = sessionId) } feedbackMessage = "已重新发起附件上传" }.onFailure { + (repository as? RemoteOpenClawRepository)?.let { persistRecentMessageCache(it, preferredSessionId = sessionId) } syncError = it.message ?: "附件重试失败" feedbackMessage = "附件重试失败" } @@ -227,6 +233,17 @@ class OpenClawAppState( fun clearConfigError() { configError = null } + + private fun persistRecentMessageCache( + remote: RemoteOpenClawRepository, + preferredSessionId: String? = activeSessionId, + ) { + val recentIds = listOfNotNull(preferredSessionId, remote.recentSession()?.id) + .plus(repository.sessions.map { it.id }) + .distinct() + .take(3) + preferences.saveRecentMessageSnapshotBucket(remote.exportRecentMessageSnapshotBucket(recentIds)) + } } enum class MainTab { 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 053a3b8..a616ba7 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -84,7 +84,11 @@ class RemoteOpenClawRepository( } override fun hydrateRecentMessages(bucket: MessageCacheBucket) { - val restored = bucket.items.associate { item -> item.sessionId to item.messages.map { it.toDomain() } } + val restored = bucket.items.associate { item -> + item.sessionId to item.messages + .map { cached -> restoreCachedMessage(item.sessionId, cached) } + .filterNot { it.id.startsWith("local-thinking-") } + } messageStore = messageStore + restored } @@ -522,6 +526,24 @@ class RemoteOpenClawRepository( updateSessionPreview(sessionId, next.lastOrNull()) } + private fun restoreCachedMessage(sessionId: String, cached: MessageCacheItem): ChatMessage { + val domain = cached.toDomain() + return when { + domain.role == MessageRole.User && domain.kind == MessageKind.Text && domain.status == "queued" -> { + textRetryStore[domain.id] = TextRetryPayload(sessionId, domain.text) + domain.copy(status = "send-failed", retryable = true) + } + domain.role == MessageRole.User && domain.kind == MessageKind.Text && domain.status == "send-failed" -> { + textRetryStore[domain.id] = TextRetryPayload(sessionId, domain.text) + domain.copy(retryable = true) + } + domain.kind == MessageKind.Attachment && domain.status == "uploading" -> { + domain.copy(status = "upload-failed", retryable = false) + } + else -> domain + } + } + private fun mergeSnapshotWithLocalPending( remoteMessages: List, localPending: List,