Add retry flow for failed text messages
This commit is contained in:
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)
|
||||
}
|
||||
},
|
||||
onRetryMessage = { messageId ->
|
||||
coroutineScope.launch {
|
||||
appState.retryMessage(activeSession.id, messageId)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
appState.selectedTab == MainTab.Sessions -> SessionListScreen(
|
||||
|
||||
@@ -124,6 +124,18 @@ class OpenClawAppState(
|
||||
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) {
|
||||
runCatching {
|
||||
repository.updateDefaultAgent(agent)
|
||||
|
||||
@@ -13,6 +13,7 @@ interface OpenClawRepository {
|
||||
fun markSessionOpened(sessionId: String)
|
||||
fun togglePinned(sessionId: String): Boolean
|
||||
fun sendMessage(sessionId: String, text: String)
|
||||
suspend fun retryMessage(sessionId: String, messageId: String)
|
||||
suspend fun updateDefaultAgent(agent: String)
|
||||
suspend fun uploadAttachmentStub(
|
||||
sessionId: String,
|
||||
|
||||
+57
-6
@@ -27,6 +27,11 @@ private data class AttachmentRetryPayload(
|
||||
val contentType: String?,
|
||||
)
|
||||
|
||||
private data class TextRetryPayload(
|
||||
val sessionId: String,
|
||||
val text: String,
|
||||
)
|
||||
|
||||
class RemoteOpenClawRepository(
|
||||
private val config: ServerConfig,
|
||||
) : OpenClawRepository {
|
||||
@@ -62,6 +67,7 @@ class RemoteOpenClawRepository(
|
||||
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>()
|
||||
private val textRetryStore = mutableMapOf<String, TextRetryPayload>()
|
||||
|
||||
override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
|
||||
|
||||
@@ -104,10 +110,12 @@ class RemoteOpenClawRepository(
|
||||
if (hasQueuedUser) return
|
||||
|
||||
val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") }
|
||||
val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}"
|
||||
textRetryStore[localUserId] = TextRetryPayload(sessionId, text)
|
||||
messageStore = messageStore + (
|
||||
sessionId to (withoutThinking + listOf(
|
||||
ChatMessage(
|
||||
id = "local-user-$sessionId-${withoutThinking.size + 1}",
|
||||
id = localUserId,
|
||||
role = MessageRole.User,
|
||||
text = text,
|
||||
createdAt = "现在",
|
||||
@@ -273,7 +281,33 @@ class RemoteOpenClawRepository(
|
||||
runCatching {
|
||||
api.sendMessage(sessionId, text)
|
||||
}.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
|
||||
}
|
||||
}
|
||||
@@ -417,13 +451,26 @@ class RemoteOpenClawRepository(
|
||||
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 next = currentMessages.filterNot { message ->
|
||||
message.id.startsWith("local-thinking-") ||
|
||||
(message.role == MessageRole.User && message.text == text && message.status == "queued")
|
||||
val next = currentMessages.mapNotNull { message ->
|
||||
when {
|
||||
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)
|
||||
updateSessionPreview(sessionId, next.lastOrNull())
|
||||
}
|
||||
|
||||
private fun mergeSnapshotWithLocalPending(
|
||||
@@ -455,6 +502,10 @@ class RemoteOpenClawRepository(
|
||||
if (shouldKeepThinking) {
|
||||
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,
|
||||
onSendAttachmentStub: () -> Unit,
|
||||
onRetryAttachment: (String) -> Unit,
|
||||
onRetryMessage: (String) -> Unit,
|
||||
) {
|
||||
var input by remember { mutableStateOf("") }
|
||||
val listState = rememberLazyListState()
|
||||
@@ -174,6 +175,7 @@ fun ChatScreen(
|
||||
animateText = shouldAnimate,
|
||||
linkedToolName = linkedTool?.toolName,
|
||||
onRetryAttachment = onRetryAttachment,
|
||||
onRetryMessage = onRetryMessage,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -233,6 +235,7 @@ private fun MessageBubble(
|
||||
animateText: Boolean = false,
|
||||
linkedToolName: String? = null,
|
||||
onRetryAttachment: (String) -> Unit,
|
||||
onRetryMessage: (String) -> Unit,
|
||||
) {
|
||||
val alignment = when (message.role) {
|
||||
MessageRole.User -> Alignment.CenterEnd
|
||||
@@ -279,6 +282,8 @@ 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 == "queued" -> "发送中"
|
||||
message.status == "send-failed" -> "发送失败"
|
||||
message.status == "uploading" -> "上传中"
|
||||
message.status == "uploaded" -> "已上传"
|
||||
message.status == "upload-failed" -> "上传失败"
|
||||
@@ -430,6 +435,11 @@ private fun MessageBubble(
|
||||
style = if (isSystemNotice) MaterialTheme.typography.bodyMedium else MaterialTheme.typography.bodyLarge,
|
||||
color = if (isSystemNotice) MaterialTheme.colorScheme.onSurfaceVariant else MaterialTheme.colorScheme.onSurface,
|
||||
)
|
||||
if (message.retryable && message.status == "send-failed") {
|
||||
TextButton(onClick = { onRetryMessage(message.id) }) {
|
||||
Text("重试发送")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Row(
|
||||
|
||||
Reference in new issue
Block a user