From fbe64e3cdcad152ce48deb13ce41d70e3f6b777c Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Sat, 7 Mar 2026 09:05:18 +0800 Subject: [PATCH] Wire attachment uploads into chat timeline --- .../kotlin/cn/xiaobai/openclawapp/App.kt | 6 ++- .../openclawapp/app/OpenClawAppState.kt | 10 +++++ .../openclawapp/data/OpenClawRepository.kt | 1 + .../data/RemoteOpenClawRepository.kt | 44 +++++++++++++++++++ .../openclawapp/feature/chat/ChatScreen.kt | 18 +++++++- 5 files changed, 77 insertions(+), 2 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt index 28e31fb..a6feb9d 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/App.kt @@ -179,7 +179,11 @@ private fun OpenClawRoot(appState: OpenClawAppState) { }, onSendAttachmentStub = { coroutineScope.launch { - appState.sendMessage(activeSession.id, "[附件占位] demo.txt") + appState.uploadAttachmentStub( + sessionId = activeSession.id, + fileName = "demo.txt", + bytes = "demo attachment from mobile".encodeToByteArray(), + ) } }, ) 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 148a59a..a2e77b2 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -138,6 +138,16 @@ class OpenClawAppState( } } + suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) { + runCatching { + repository.uploadAttachmentStub(sessionId, fileName, bytes) + 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/OpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt index af925de..0356c6d 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -14,4 +14,5 @@ interface OpenClawRepository { fun togglePinned(sessionId: String): Boolean fun sendMessage(sessionId: String, text: String) suspend fun updateDefaultAgent(agent: String) + suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) } 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 9cf36ec..d2d725a 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -124,6 +124,49 @@ class RemoteOpenClawRepository( ) } + override suspend fun uploadAttachmentStub(sessionId: String, fileName: String, bytes: ByteArray) { + val currentMessages = messageStore[sessionId].orEmpty() + val localId = "local-attachment-$sessionId-${currentMessages.size + 1}" + val pending = ChatMessage( + id = localId, + role = MessageRole.User, + text = fileName, + createdAt = "现在", + status = "uploading", + kind = MessageKind.Attachment, + ) + messageStore = messageStore + (sessionId to (currentMessages + pending)) + updateSessionPreview(sessionId, pending) + + runCatching { + val remote = api.uploadAttachment(sessionId, fileName, bytes) + val uploaded = ChatMessage( + id = remote.attachmentId, + role = MessageRole.User, + text = remote.fileName, + createdAt = remote.queuedAt, + status = "uploaded", + kind = MessageKind.Attachment, + ) + val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) uploaded else msg } + messageStore = messageStore + (sessionId to next) + updateSessionPreview(sessionId, uploaded) + }.onFailure { + val failed = ChatMessage( + id = localId, + role = MessageRole.User, + text = fileName, + createdAt = "刚刚", + status = "upload-failed", + kind = MessageKind.Attachment, + ) + val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) failed else msg } + messageStore = messageStore + (sessionId to next) + updateSessionPreview(sessionId, failed) + throw it + } + } + suspend fun refreshStatus() { val remote = api.getStatus() status = ServerStatus( @@ -392,6 +435,7 @@ class RemoteOpenClawRepository( "thinking" -> MessageKind.Thinking "tool" -> MessageKind.Tool "tool-result" -> MessageKind.ToolResult + "attachment" -> MessageKind.Attachment else -> MessageKind.Text } } 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 630cb43..db10974 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 @@ -243,8 +243,11 @@ private fun MessageBubble( val isThinking = message.status == "thinking" || message.kind == MessageKind.Thinking val isTool = message.kind == MessageKind.Tool val isToolResult = message.kind == MessageKind.ToolResult - val isSystemNotice = message.role == MessageRole.System && !isToolResult && !isTool + val isAttachment = message.kind == MessageKind.Attachment + val isSystemNotice = message.role == MessageRole.System && !isToolResult && !isTool && !isAttachment val bubbleColor = when { + isAttachment && message.status == "upload-failed" -> MaterialTheme.colorScheme.errorContainer + isAttachment -> MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.82f) isToolResult && message.status == "tool-error" -> MaterialTheme.colorScheme.errorContainer isToolResult -> MaterialTheme.colorScheme.tertiaryContainer isTool -> MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.7f) @@ -273,6 +276,9 @@ 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 == "uploading" -> "上传中" + message.status == "uploaded" -> "已上传" + message.status == "upload-failed" -> "上传失败" message.status == "tool-running" -> "执行中" message.status == "tool-done" -> "已完成" message.status == "tool-error" -> "失败" @@ -332,6 +338,16 @@ private fun MessageBubble( ) } } + isAttachment -> { + Text( + text = "📎 附件", + style = MaterialTheme.typography.titleSmall, + ) + Text( + text = displayText, + style = MaterialTheme.typography.bodyMedium, + ) + } isTool -> { Text( text = "🛠 ${message.toolName ?: "工具调用"}",