Add local client tokens for pending text matching

This commit is contained in:
OpenClaw committed 2026-03-07 14:04:12 +08:00
1 parent 485afdab02
commit dcd4b2adbd
5 files changed
+24 -1

No files matched your search

@@ -26,6 +26,7 @@ data class MessageCacheItem(
val toolCallId: String? = null,
val contentType: String? = null,
val sizeBytes: Int? = null,
val clientToken: String? = null,
val fingerprint: String? = null,
val retryable: Boolean = false,
)
@@ -51,6 +52,7 @@ fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem(
toolCallId = toolCallId,
contentType = contentType,
sizeBytes = sizeBytes,
clientToken = clientToken,
fingerprint = attachmentFingerprint,
retryable = retryable,
)
@@ -76,6 +78,7 @@ fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage(
toolCallId = toolCallId,
contentType = contentType,
sizeBytes = sizeBytes,
clientToken = clientToken,
attachmentFingerprint = fingerprint,
retryable = retryable,
)
@@ -27,6 +27,7 @@ data class ChatMessage(
val toolCallId: String? = null,
val contentType: String? = null,
val sizeBytes: Int? = null,
val clientToken: String? = null,
val attachmentFingerprint: String? = null,
val retryable: Boolean = false,
)
@@ -22,6 +22,7 @@ data class PendingSendItem(
val kind: PendingSendKind = PendingSendKind.Text,
val text: String = "",
val autoRetryEligible: Boolean = true,
val clientToken: String? = null,
val fileName: String = "",
val contentType: String? = null,
val base64Data: String? = null,
@@ -48,6 +48,7 @@ private data class PendingTextSend(
val sessionId: String,
val text: String,
val createdAt: String,
val clientToken: String,
val autoRetryEligible: Boolean = true,
)
@@ -122,6 +123,14 @@ class RemoteOpenClawRepository(
return listOf(fileName, contentType.orEmpty(), bytes.size.toString(), hash.toULong().toString(16)).joinToString("|")
}
private fun textClientToken(sessionId: String, text: String, ordinal: Int): String {
var hash = 1469598103934665603L
text.take(128).forEach { ch ->
hash = (hash xor ch.code.toLong()) * 1099511628211L
}
return listOf(sessionId, ordinal.toString(), text.length.toString(), hash.toULong().toString(16)).joinToString("|")
}
override fun findSession(sessionId: String): SessionSummary? = sessions.firstOrNull { it.id == sessionId }
override fun messagesFor(sessionId: String): List<ChatMessage> = messageStore[sessionId].orEmpty()
@@ -165,6 +174,7 @@ class RemoteOpenClawRepository(
sessionId = item.sessionId,
text = item.text,
createdAt = item.createdAt,
clientToken = item.clientToken ?: textClientToken(item.sessionId, item.text, item.messageId.hashCode()),
autoRetryEligible = item.autoRetryEligible,
)
textRetryStore[item.messageId] = TextRetryPayload(item.sessionId, item.text)
@@ -251,11 +261,13 @@ class RemoteOpenClawRepository(
val withoutThinking = currentMessages.filterNot { it.id.startsWith("local-thinking-") }
val localUserId = "local-user-$sessionId-${withoutThinking.size + 1}"
textRetryStore[localUserId] = TextRetryPayload(sessionId, text)
val clientToken = textClientToken(sessionId, text, withoutThinking.size + 1)
pendingTextQueue[localUserId] = PendingTextSend(
messageId = localUserId,
sessionId = sessionId,
text = text,
createdAt = "现在",
clientToken = clientToken,
autoRetryEligible = true,
)
val next = withoutThinking + listOf(
@@ -265,6 +277,7 @@ class RemoteOpenClawRepository(
text = text,
createdAt = "现在",
status = textStatusForLocalPhase(LocalMessagePhase.ManualProcessing),
clientToken = clientToken,
),
ChatMessage(
id = "local-thinking-$sessionId-${withoutThinking.size + 2}",
@@ -470,6 +483,7 @@ class RemoteOpenClawRepository(
kind = PendingSendKind.Text,
text = item.text,
autoRetryEligible = item.autoRetryEligible,
clientToken = item.clientToken,
)
}
)
@@ -685,6 +699,7 @@ class RemoteOpenClawRepository(
private fun matchesPendingLocalText(local: ChatMessage, remote: ChatMessage): Boolean {
return remote.role == MessageRole.User &&
remote.kind == MessageKind.Text &&
(local.clientToken == null || remote.clientToken == null || remote.clientToken == local.clientToken) &&
remote.text == local.text
}
@@ -887,13 +902,15 @@ class RemoteOpenClawRepository(
sessionId = sessionId,
text = text,
createdAt = "刚刚",
clientToken = message.clientToken ?: textClientToken(sessionId, text, failedId.hashCode()),
autoRetryEligible = autoRetryEligible,
)
message.copy(
id = failedId,
status = textStatusForLocalPhase(LocalMessagePhase.Failed),
retryable = true,
createdAt = "刚刚",
retryable = true,
clientToken = message.clientToken ?: textClientToken(sessionId, text, failedId.hashCode()),
)
}
else -> message