diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index a9049c4..babb4da 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -197,6 +197,11 @@ private fun OpenClawRoot(appState: OpenClawAppState) { onSendAttachmentStub = { pickAttachment() }, + onRetryAttachment = { messageId -> + coroutineScope.launch { + appState.retryAttachment(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 a888a37..86e7845 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -153,6 +153,16 @@ class OpenClawAppState( } } + suspend fun retryAttachment(sessionId: String, messageId: String) { + runCatching { + repository.retryAttachment(sessionId, messageId) + feedbackMessage = "已重新发起附件上传" + }.onFailure { + syncError = it.message ?: "附件重试失败" + feedbackMessage = "附件重试失败" + } + } + fun updateTheme(theme: AppThemeMode) { themeMode = theme preferences.saveThemeMode(theme) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt index e99d0ff..adaa176 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/Models.kt @@ -27,6 +27,7 @@ data class ChatMessage( val toolCallId: String? = null, val contentType: String? = null, val sizeBytes: Int? = null, + val retryable: Boolean = false, ) enum class MessageRole { User, Assistant, System } 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 9ac7b9a..f6c15db 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -20,4 +20,5 @@ interface OpenClawRepository { bytes: ByteArray, contentType: String? = null, ) + suspend fun retryAttachment(sessionId: String, messageId: 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 ee41344..654068a 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -20,6 +20,13 @@ import kotlinx.coroutines.flow.collect import kotlinx.coroutines.isActive import kotlinx.coroutines.launch +private data class AttachmentRetryPayload( + val sessionId: String, + val fileName: String, + val bytes: ByteArray, + val contentType: String?, +) + class RemoteOpenClawRepository( private val config: ServerConfig, ) : OpenClawRepository { @@ -54,6 +61,7 @@ class RemoteOpenClawRepository( private var recentSessionId by mutableStateOf(null) private var messageStore by mutableStateOf>>(emptyMap()) private var streamStatusStore by mutableStateOf>(emptyMap()) + private val attachmentRetryStore = mutableMapOf() override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId } @@ -132,6 +140,7 @@ class RemoteOpenClawRepository( ) { val currentMessages = messageStore[sessionId].orEmpty() val localId = "local-attachment-$sessionId-${currentMessages.size + 1}" + attachmentRetryStore[localId] = AttachmentRetryPayload(sessionId, fileName, bytes, contentType) val pending = ChatMessage( id = localId, role = MessageRole.User, @@ -161,6 +170,7 @@ class RemoteOpenClawRepository( contentType = remote.contentType, sizeBytes = remote.sizeBytes, ) + attachmentRetryStore.remove(localId) val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) uploaded else msg } messageStore = messageStore + (sessionId to next) updateSessionPreview(sessionId, uploaded) @@ -174,6 +184,7 @@ class RemoteOpenClawRepository( kind = MessageKind.Attachment, contentType = contentType, sizeBytes = bytes.size, + retryable = true, ) val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) failed else msg } messageStore = messageStore + (sessionId to next) @@ -182,6 +193,54 @@ class RemoteOpenClawRepository( } } + override suspend fun retryAttachment(sessionId: String, messageId: String) { + val payload = attachmentRetryStore[messageId] ?: return + val current = messageStore[sessionId].orEmpty() + val uploading = current.map { msg -> + if (msg.id == messageId) msg.copy(status = "uploading", retryable = false) else msg + } + messageStore = messageStore + (sessionId to uploading) + updateSessionPreview(sessionId, uploading.lastOrNull()) + + runCatching { + val remote = api.uploadAttachment( + sessionId = payload.sessionId, + fileName = payload.fileName, + bytes = payload.bytes, + ) + val uploaded = ChatMessage( + id = remote.attachmentId, + role = MessageRole.User, + text = remote.fileName, + createdAt = remote.queuedAt, + status = "uploaded", + kind = MessageKind.Attachment, + contentType = remote.contentType, + sizeBytes = remote.sizeBytes, + ) + attachmentRetryStore.remove(messageId) + val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) uploaded else msg } + messageStore = messageStore + (sessionId to next) + updateSessionPreview(sessionId, uploaded) + }.onFailure { + val failed = ChatMessage( + id = messageId, + role = MessageRole.User, + text = payload.fileName, + createdAt = "刚刚", + status = "upload-failed", + kind = MessageKind.Attachment, + contentType = payload.contentType, + sizeBytes = payload.bytes.size, + retryable = true, + ) + val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) failed else msg } + messageStore = messageStore + (sessionId to next) + updateSessionPreview(sessionId, failed) + throw it + } + } + suspend fun refreshStatus() { val remote = api.getStatus() status = ServerStatus( 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 49b961e..d398d08 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 @@ -53,6 +53,7 @@ fun ChatScreen( onRetry: () -> Unit, onSend: (String) -> Unit, onSendAttachmentStub: () -> Unit, + onRetryAttachment: (String) -> Unit, ) { var input by remember { mutableStateOf("") } val listState = rememberLazyListState() @@ -172,6 +173,7 @@ fun ChatScreen( message = message, animateText = shouldAnimate, linkedToolName = linkedTool?.toolName, + onRetryAttachment = onRetryAttachment, ) } } @@ -230,6 +232,7 @@ private fun MessageBubble( message: ChatMessage, animateText: Boolean = false, linkedToolName: String? = null, + onRetryAttachment: (String) -> Unit, ) { val alignment = when (message.role) { MessageRole.User -> Alignment.CenterEnd @@ -358,6 +361,11 @@ private fun MessageBubble( color = MaterialTheme.colorScheme.onSurfaceVariant, ) } + if (message.retryable && message.status == "upload-failed") { + TextButton(onClick = { onRetryAttachment(message.id) }) { + Text("重试上传") + } + } } isTool -> { Text(