Add retry flow for failed attachment uploads

This commit is contained in:
OpenClaw committed 2026-03-07 09:17:59 +08:00
1 parent f4fe11e523
commit f6159b0017
6 files changed
+84

No files matched your search

@@ -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(
@@ -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)
@@ -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 }
@@ -20,4 +20,5 @@ interface OpenClawRepository {
bytes: ByteArray,
contentType: String? = null,
)
suspend fun retryAttachment(sessionId: String, messageId: String)
}
@@ -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<String?>(null)
private var messageStore by mutableStateOf<Map<String, List<ChatMessage>>>(emptyMap())
private var streamStatusStore by mutableStateOf<Map<String, String>>(emptyMap())
private val attachmentRetryStore = mutableMapOf<String, AttachmentRetryPayload>()
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(
@@ -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(