Add retry flow for failed text messages

This commit is contained in:
OpenClaw committed 2026-03-07 09:29:42 +08:00
1 parent 6cba3154ba
commit b441ac9cc6
5 files changed
+85 -6

No files matched your search

@@ -202,6 +202,11 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
appState.retryAttachment(activeSession.id, messageId) appState.retryAttachment(activeSession.id, messageId)
} }
}, },
onRetryMessage = { messageId ->
coroutineScope.launch {
appState.retryMessage(activeSession.id, messageId)
}
},
) )
appState.selectedTab == MainTab.Sessions -> SessionListScreen( appState.selectedTab == MainTab.Sessions -> SessionListScreen(
@@ -124,6 +124,18 @@ class OpenClawAppState(
sendingSessionId = null sendingSessionId = null
} }
suspend fun retryMessage(sessionId: String, messageId: String) {
sendingSessionId = sessionId
runCatching {
repository.retryMessage(sessionId, messageId)
feedbackMessage = "已重新发送消息"
}.onFailure {
syncError = it.message ?: "消息重试失败"
feedbackMessage = "消息重试失败"
}
sendingSessionId = null
}
suspend fun updateDefaultAgent(agent: String) { suspend fun updateDefaultAgent(agent: String) {
runCatching { runCatching {
repository.updateDefaultAgent(agent) repository.updateDefaultAgent(agent)
@@ -13,6 +13,7 @@ interface OpenClawRepository {
fun markSessionOpened(sessionId: String) fun markSessionOpened(sessionId: String)
fun togglePinned(sessionId: String): Boolean fun togglePinned(sessionId: String): Boolean
fun sendMessage(sessionId: String, text: String) fun sendMessage(sessionId: String, text: String)
suspend fun retryMessage(sessionId: String, messageId: String)
suspend fun updateDefaultAgent(agent: String) suspend fun updateDefaultAgent(agent: String)
suspend fun uploadAttachmentStub( suspend fun uploadAttachmentStub(
sessionId: String, sessionId: String,
@@ -27,6 +27,11 @@ private data class AttachmentRetryPayload(
val contentType: String?, val contentType: String?,
) )
private data class TextRetryPayload(
val sessionId: String,
val text: String,
)
class RemoteOpenClawRepository( class RemoteOpenClawRepository(
private val config: ServerConfig, private val config: ServerConfig,
) : OpenClawRepository { ) : OpenClawRepository {
@@ -62,6 +67,7 @@ class RemoteOpenClawRepository(
private var messageStore by mutableStateOf<Map<String, List<ChatMessage>>>(emptyMap()) private var messageStore by mutableStateOf<Map<String, List<ChatMessage>>>(emptyMap())
private var streamStatusStore by mutableStateOf<Map<String, String>>(emptyMap()) private var streamStatusStore by mutableStateOf<Map<String, String>>(emptyMap())
private val attachmentRetryStore = mutableMapOf<String, AttachmentRetryPayload>() private val attachmentRetryStore = mutableMapOf<String, AttachmentRetryPayload>()
private val textRetryStore = mutableMapOf<String, TextRetryPayload>()
override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId } override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
@@ -104,10 +110,12 @@ class RemoteOpenClawRepository(
if (hasQueuedUser) return if (hasQueuedUser) return
val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") } val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") }
val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}"
textRetryStore[localUserId] = TextRetryPayload(sessionId, text)
messageStore = messageStore + ( messageStore = messageStore + (
sessionId to (withoutThinking + listOf( sessionId to (withoutThinking + listOf(
ChatMessage( ChatMessage(
id = "local-user-$sessionId-${withoutThinking.size + 1}", id = localUserId,
role = MessageRole.User, role = MessageRole.User,
text = text, text = text,
createdAt = "现在", createdAt = "现在",
@@ -273,7 +281,33 @@ class RemoteOpenClawRepository(
runCatching { runCatching {
api.sendMessage(sessionId, text) api.sendMessage(sessionId, text)
}.onFailure { }.onFailure {
clearLocalPendingMessages(sessionId, text) markTextMessageFailed(sessionId, text)
throw it
}
}
override suspend fun retryMessage(sessionId: String, messageId: String) {
val payload = textRetryStore[messageId] ?: return
val current = messageStore[sessionId].orEmpty()
val retried = current.map { msg ->
if (msg.id == messageId) msg.copy(status = "queued", retryable = false, createdAt = "现在") else msg
}
val withThinking = if (retried.none { it.id.startsWith("local-thinking-") }) {
retried + ChatMessage(
id = "local-thinking-$sessionId-${retried.size + 1}",
role = MessageRole.Assistant,
text = "",
createdAt = "现在",
status = "thinking",
)
} else retried
messageStore = messageStore + (sessionId to withThinking)
updateSessionPreview(sessionId, withThinking.lastOrNull())
runCatching {
api.sendMessage(sessionId, payload.text)
}.onFailure {
markTextMessageFailed(sessionId, payload.text, existingMessageId = messageId)
throw it throw it
} }
} }
@@ -417,13 +451,26 @@ class RemoteOpenClawRepository(
resortSessions(prioritizeId = recentSessionId) resortSessions(prioritizeId = recentSessionId)
} }
private fun clearLocalPendingMessages(sessionId: String, text: String) { private fun markTextMessageFailed(sessionId: String, text: String, existingMessageId: String? = null) {
val currentMessages = messageStore[sessionId].orEmpty() val currentMessages = messageStore[sessionId].orEmpty()
val next = currentMessages.filterNot { message -> val next = currentMessages.mapNotNull { message ->
message.id.startsWith("local-thinking-") || when {
(message.role == MessageRole.User && message.text == text && message.status == "queued") message.id.startsWith("local-thinking-") -> null
message.role == MessageRole.User && message.text == text && (existingMessageId == null || message.id == existingMessageId) -> {
val failedId = existingMessageId ?: message.id
textRetryStore.putIfAbsent(failedId, TextRetryPayload(sessionId, text))
message.copy(
id = failedId,
status = "send-failed",
retryable = true,
createdAt = "刚刚",
)
}
else -> message
}
} }
messageStore = messageStore + (sessionId to next) messageStore = messageStore + (sessionId to next)
updateSessionPreview(sessionId, next.lastOrNull())
} }
private fun mergeSnapshotWithLocalPending( private fun mergeSnapshotWithLocalPending(
@@ -455,6 +502,10 @@ class RemoteOpenClawRepository(
if (shouldKeepThinking) { if (shouldKeepThinking) {
addAll(pendingThinking) addAll(pendingThinking)
} }
}.onEach { msg ->
if (msg.role == MessageRole.User && msg.status != "send-failed") {
textRetryStore.remove(msg.id)
}
} }
} }
@@ -54,6 +54,7 @@ fun ChatScreen(
onSend: (String) -> Unit, onSend: (String) -> Unit,
onSendAttachmentStub: () -> Unit, onSendAttachmentStub: () -> Unit,
onRetryAttachment: (String) -> Unit, onRetryAttachment: (String) -> Unit,
onRetryMessage: (String) -> Unit,
) { ) {
var input by remember { mutableStateOf("") } var input by remember { mutableStateOf("") }
val listState = rememberLazyListState() val listState = rememberLazyListState()
@@ -174,6 +175,7 @@ fun ChatScreen(
animateText = shouldAnimate, animateText = shouldAnimate,
linkedToolName = linkedTool?.toolName, linkedToolName = linkedTool?.toolName,
onRetryAttachment = onRetryAttachment, onRetryAttachment = onRetryAttachment,
onRetryMessage = onRetryMessage,
) )
} }
} }
@@ -233,6 +235,7 @@ private fun MessageBubble(
animateText: Boolean = false, animateText: Boolean = false,
linkedToolName: String? = null, linkedToolName: String? = null,
onRetryAttachment: (String) -> Unit, onRetryAttachment: (String) -> Unit,
onRetryMessage: (String) -> Unit,
) { ) {
val alignment = when (message.role) { val alignment = when (message.role) {
MessageRole.User -> Alignment.CenterEnd MessageRole.User -> Alignment.CenterEnd
@@ -279,6 +282,8 @@ private fun MessageBubble(
val toolResultText = if (shouldCollapseToolResult && !expanded) displayText.take(240).trimEnd() + "…" else displayText val toolResultText = if (shouldCollapseToolResult && !expanded) displayText.take(240).trimEnd() + "…" else displayText
val displayStatus = when { val displayStatus = when {
animateText && visibleChars < message.text.length -> "streaming" animateText && visibleChars < message.text.length -> "streaming"
message.status == "queued" -> "发送中"
message.status == "send-failed" -> "发送失败"
message.status == "uploading" -> "上传中" message.status == "uploading" -> "上传中"
message.status == "uploaded" -> "已上传" message.status == "uploaded" -> "已上传"
message.status == "upload-failed" -> "上传失败" message.status == "upload-failed" -> "上传失败"
@@ -430,6 +435,11 @@ private fun MessageBubble(
style = if (isSystemNotice) MaterialTheme.typography.bodyMedium else MaterialTheme.typography.bodyLarge, style = if (isSystemNotice) MaterialTheme.typography.bodyMedium else MaterialTheme.typography.bodyLarge,
color = if (isSystemNotice) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSurface, color = if (isSystemNotice) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSurface,
) )
if (message.retryable && message.status == "send-failed") {
TextButton(onClick = { onRetryMessage(message.id) }) {
Text("重试发送")
}
}
} }
} }
Row( Row(