Wire attachment uploads into chat timeline

This commit is contained in:
OpenClaw committed 2026-03-07 09:05:18 +08:00
1 parent 3672554706
commit fbe64e3cdc
5 files changed
+77 -2

No files matched your search

@@ -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(),
)
}
},
)
@@ -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)
@@ -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)
}
@@ -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
}
}
@@ -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 ?: "工具调用"}",