Add attachment fingerprints for pending matching

This commit is contained in:
OpenClaw committed 2026-03-07 13:58:09 +08:00
1 parent 39f16a8119
commit 485afdab02
5 files changed
+24

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 fingerprint: String? = null,
val retryable: Boolean = false,
)
@@ -50,6 +51,7 @@ fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem(
toolCallId = toolCallId,
contentType = contentType,
sizeBytes = sizeBytes,
fingerprint = attachmentFingerprint,
retryable = retryable,
)
@@ -74,5 +76,6 @@ fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage(
toolCallId = toolCallId,
contentType = contentType,
sizeBytes = sizeBytes,
attachmentFingerprint = fingerprint,
retryable = retryable,
)
@@ -27,6 +27,7 @@ data class ChatMessage(
val toolCallId: String? = null,
val contentType: String? = null,
val sizeBytes: Int? = null,
val attachmentFingerprint: String? = null,
val retryable: Boolean = false,
)
@@ -26,4 +26,5 @@ data class PendingSendItem(
val contentType: String? = null,
val base64Data: String? = null,
val sizeBytes: Int? = null,
val fingerprint: String? = null,
)
@@ -58,6 +58,7 @@ private data class PendingAttachmentSend(
val bytes: ByteArray,
val contentType: String?,
val createdAt: String,
val fingerprint: String,
)
internal data class PendingQueueRecoveryResult(
@@ -113,6 +114,14 @@ class RemoteOpenClawRepository(
private val autoRetryMessageIds = mutableSetOf<String>()
private var drainingSessionIds by mutableStateOf<Set<String>>(emptySet())
private fun attachmentFingerprint(fileName: String, bytes: ByteArray, contentType: String?): String {
var hash = 1125899907L
bytes.take(256).forEach { byte ->
hash = (hash * 1315423911L) xor (byte.toLong() and 0xff)
}
return listOf(fileName, contentType.orEmpty(), bytes.size.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()
@@ -173,6 +182,7 @@ class RemoteOpenClawRepository(
bytes = bytes,
contentType = item.contentType,
createdAt = item.createdAt,
fingerprint = item.fingerprint ?: attachmentFingerprint(item.fileName, bytes, item.contentType),
)
attachmentRetryStore[item.messageId] = AttachmentRetryPayload(
sessionId = item.sessionId,
@@ -293,6 +303,7 @@ class RemoteOpenClawRepository(
bytes = bytes,
contentType = contentType,
createdAt = "现在",
fingerprint = attachmentFingerprint(fileName, bytes, contentType),
)
val pending = ChatMessage(
id = localId,
@@ -303,6 +314,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment,
contentType = contentType,
sizeBytes = bytes.size,
attachmentFingerprint = attachmentFingerprint(fileName, bytes, contentType),
)
messageStore = messageStore + (sessionId to (currentMessages + pending))
updateSessionPreview(sessionId, pending)
@@ -322,6 +334,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment,
contentType = remote.contentType,
sizeBytes = remote.sizeBytes,
attachmentFingerprint = attachmentFingerprint(fileName, bytes, remote.contentType),
)
attachmentRetryStore.remove(localId)
attachmentOpenStore.remove(localId)
@@ -340,6 +353,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment,
contentType = contentType,
sizeBytes = bytes.size,
attachmentFingerprint = attachmentFingerprint(fileName, bytes, contentType),
retryable = true,
)
val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == localId) failed else msg }
@@ -382,6 +396,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment,
contentType = remote.contentType,
sizeBytes = remote.sizeBytes,
attachmentFingerprint = payload.fingerprint,
)
attachmentRetryStore.remove(messageId)
attachmentOpenStore.remove(messageId)
@@ -400,6 +415,7 @@ class RemoteOpenClawRepository(
kind = MessageKind.Attachment,
contentType = payload.contentType,
sizeBytes = payload.bytes.size,
attachmentFingerprint = payload.fingerprint,
retryable = true,
)
val next = messageStore[sessionId].orEmpty().map { msg -> if (msg.id == messageId) failed else msg }
@@ -468,6 +484,7 @@ class RemoteOpenClawRepository(
contentType = item.contentType,
base64Data = Base64.encode(item.bytes),
sizeBytes = item.bytes.size,
fingerprint = item.fingerprint,
)
}
)
@@ -674,6 +691,7 @@ class RemoteOpenClawRepository(
private fun matchesPendingLocalAttachment(local: ChatMessage, remote: ChatMessage): Boolean {
return remote.kind == MessageKind.Attachment &&
remote.text == local.text &&
(local.attachmentFingerprint == null || remote.attachmentFingerprint == null || remote.attachmentFingerprint == local.attachmentFingerprint) &&
(local.contentType == null || remote.contentType == local.contentType) &&
(local.sizeBytes == null || remote.sizeBytes == local.sizeBytes)
}