Stabilize session sorting with sort keys

This commit is contained in:
OpenClaw committed 2026-03-07 11:35:31 +08:00
1 parent 439c0a4a7b
commit a4a6632707
2 files changed
+29 -1

No files matched your search

@@ -67,6 +67,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 sessionSortKeyStore = mutableMapOf<String, Long>()
private val attachmentRetryStore = mutableMapOf<String, AttachmentRetryPayload>()
private val attachmentOpenStore = mutableMapOf<String, AttachmentOpenPayload>()
private val textRetryStore = mutableMapOf<String, TextRetryPayload>()
@@ -81,6 +82,7 @@ class RemoteOpenClawRepository(
override fun hydrateOverview(snapshot: OverviewSnapshot) {
availableAgents = snapshot.availableAgents
sessions = snapshot.sessions.map { it.toDomain() }
rebuildSessionSortKeys(sessions)
status = snapshot.status.toDomain()
}
@@ -299,6 +301,7 @@ class RemoteOpenClawRepository(
suspend fun refreshSessions() {
sessions = api.getSessions().map { dto -> dto.toDomain() }
rebuildSessionSortKeys(sessions)
resortSessions(prioritizeId = recentSessionId)
}
@@ -529,6 +532,7 @@ class RemoteOpenClawRepository(
val existing = sessions.firstOrNull { it.id == sessionId }
val fallbackUpdatedAt = existing?.updatedAt ?: "刚刚"
val updatedAt = lastMessage?.createdAt ?: fallbackUpdatedAt
sessionSortKeyStore[sessionId] = computeSessionSortKey(sessionId, updatedAt)
sessions = sessions.map { session ->
if (session.id == sessionId) {
session.copy(
@@ -648,10 +652,33 @@ class RemoteOpenClawRepository(
}
}
private fun rebuildSessionSortKeys(items: List<SessionSummary>) {
val total = items.size.toLong()
items.forEachIndexed { index, session ->
val fallback = (total - index).coerceAtLeast(1L)
sessionSortKeyStore[session.id] = parseDisplaySortKey(session.updatedAt) ?: fallback
}
}
private fun computeSessionSortKey(sessionId: String, updatedAt: String): Long {
return when (updatedAt) {
"现在", "刚刚" -> (sessionSortKeyStore.values.maxOrNull() ?: 0L) + 1L
else -> parseDisplaySortKey(updatedAt) ?: (sessionSortKeyStore[sessionId] ?: 0L)
}
}
private fun parseDisplaySortKey(value: String?): Long? {
val text = value?.trim().orEmpty()
if (text.isBlank()) return null
val digits = text.filter(Char::isDigit)
return if (digits.length >= 12) digits.take(12).toLongOrNull() else null
}
private fun resortSessions(prioritizeId: String?) {
sessions = sessions.sortedWith(
compareByDescending<SessionSummary> { it.id == prioritizeId }
.thenByDescending { it.isPinned }
.thenByDescending { sessionSortKeyStore[it.id] ?: 0L }
.thenByDescending { it.updatedAt }
)
}