Cache recent session messages for cold starts
This commit is contained in:
1 parent
75c93f36d0
commit
4a82204e77
6 files changed
+99
No files matched your search
@@ -30,6 +30,9 @@ class OpenClawAppState(
|
|||||||
preferences.loadOverviewSnapshot()?.let { snapshot ->
|
preferences.loadOverviewSnapshot()?.let { snapshot ->
|
||||||
repository.hydrateOverview(snapshot)
|
repository.hydrateOverview(snapshot)
|
||||||
}
|
}
|
||||||
|
preferences.loadRecentMessageSnapshot()?.let { snapshot ->
|
||||||
|
repository.hydrateRecentMessages(snapshot)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun connect(config: ServerConfig, password: String) {
|
suspend fun connect(config: ServerConfig, password: String) {
|
||||||
@@ -81,6 +84,7 @@ class OpenClawAppState(
|
|||||||
remote.refreshAgents()
|
remote.refreshAgents()
|
||||||
remote.refreshSessions()
|
remote.refreshSessions()
|
||||||
preferences.saveOverviewSnapshot(remote.exportOverviewSnapshot())
|
preferences.saveOverviewSnapshot(remote.exportOverviewSnapshot())
|
||||||
|
activeSessionId?.let { preferences.saveRecentMessageSnapshot(remote.exportRecentMessageSnapshot(it)) }
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
syncError = it.message ?: "远程同步失败"
|
syncError = it.message ?: "远程同步失败"
|
||||||
feedbackMessage = "Remote 同步失败"
|
feedbackMessage = "Remote 同步失败"
|
||||||
@@ -109,6 +113,7 @@ class OpenClawAppState(
|
|||||||
isSyncing = true
|
isSyncing = true
|
||||||
runCatching {
|
runCatching {
|
||||||
remote.refreshMessages(sessionId)
|
remote.refreshMessages(sessionId)
|
||||||
|
preferences.saveRecentMessageSnapshot(remote.exportRecentMessageSnapshot(sessionId))
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
syncError = it.message ?: "会话消息加载失败"
|
syncError = it.message ?: "会话消息加载失败"
|
||||||
feedbackMessage = "消息加载失败"
|
feedbackMessage = "消息加载失败"
|
||||||
|
|||||||
@@ -46,11 +46,21 @@ class AppPreferences(
|
|||||||
settings.putString(KEY_OVERVIEW_SNAPSHOT, json.encodeToString(OverviewSnapshot.serializer(), snapshot))
|
settings.putString(KEY_OVERVIEW_SNAPSHOT, json.encodeToString(OverviewSnapshot.serializer(), snapshot))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun loadRecentMessageSnapshot(): MessageCacheSnapshot? {
|
||||||
|
val raw = settings.getStringOrNull(KEY_RECENT_MESSAGE_SNAPSHOT) ?: return null
|
||||||
|
return runCatching { json.decodeFromString<MessageCacheSnapshot>(raw) }.getOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun saveRecentMessageSnapshot(snapshot: MessageCacheSnapshot) {
|
||||||
|
settings.putString(KEY_RECENT_MESSAGE_SNAPSHOT, json.encodeToString(MessageCacheSnapshot.serializer(), snapshot))
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val KEY_BASE_URL = "server.base_url"
|
private const val KEY_BASE_URL = "server.base_url"
|
||||||
private const val KEY_TOKEN = "server.token"
|
private const val KEY_TOKEN = "server.token"
|
||||||
private const val KEY_DEFAULT_AGENT = "server.default_agent"
|
private const val KEY_DEFAULT_AGENT = "server.default_agent"
|
||||||
private const val KEY_THEME_MODE = "ui.theme_mode"
|
private const val KEY_THEME_MODE = "ui.theme_mode"
|
||||||
private const val KEY_OVERVIEW_SNAPSHOT = "cache.overview_snapshot"
|
private const val KEY_OVERVIEW_SNAPSHOT = "cache.overview_snapshot"
|
||||||
|
private const val KEY_RECENT_MESSAGE_SNAPSHOT = "cache.recent_message_snapshot"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package cn.xiaobai.openclawapp.data
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class MessageCacheSnapshot(
|
||||||
|
val sessionId: String,
|
||||||
|
val messages: List<MessageCacheItem> = emptyList(),
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class MessageCacheItem(
|
||||||
|
val id: String,
|
||||||
|
val role: String,
|
||||||
|
val text: String,
|
||||||
|
val createdAt: String,
|
||||||
|
val status: String? = null,
|
||||||
|
val kind: String? = null,
|
||||||
|
val toolName: String? = null,
|
||||||
|
val toolCallId: String? = null,
|
||||||
|
val contentType: String? = null,
|
||||||
|
val sizeBytes: Int? = null,
|
||||||
|
val retryable: Boolean = false,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun ChatMessage.toCacheItem(): MessageCacheItem = MessageCacheItem(
|
||||||
|
id = id,
|
||||||
|
role = when (role) {
|
||||||
|
MessageRole.User -> "user"
|
||||||
|
MessageRole.Assistant -> "assistant"
|
||||||
|
MessageRole.System -> "system"
|
||||||
|
},
|
||||||
|
text = text,
|
||||||
|
createdAt = createdAt,
|
||||||
|
status = status,
|
||||||
|
kind = when (kind) {
|
||||||
|
MessageKind.Text -> "text"
|
||||||
|
MessageKind.Thinking -> "thinking"
|
||||||
|
MessageKind.Tool -> "tool"
|
||||||
|
MessageKind.ToolResult -> "tool-result"
|
||||||
|
MessageKind.Attachment -> "attachment"
|
||||||
|
},
|
||||||
|
toolName = toolName,
|
||||||
|
toolCallId = toolCallId,
|
||||||
|
contentType = contentType,
|
||||||
|
sizeBytes = sizeBytes,
|
||||||
|
retryable = retryable,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun MessageCacheItem.toDomain(): ChatMessage = ChatMessage(
|
||||||
|
id = id,
|
||||||
|
role = when (role.lowercase()) {
|
||||||
|
"user" -> MessageRole.User
|
||||||
|
"assistant" -> MessageRole.Assistant
|
||||||
|
else -> MessageRole.System
|
||||||
|
},
|
||||||
|
text = text,
|
||||||
|
createdAt = createdAt,
|
||||||
|
status = status,
|
||||||
|
kind = when (kind?.lowercase()) {
|
||||||
|
"thinking" -> MessageKind.Thinking
|
||||||
|
"tool" -> MessageKind.Tool
|
||||||
|
"tool-result" -> MessageKind.ToolResult
|
||||||
|
"attachment" -> MessageKind.Attachment
|
||||||
|
else -> MessageKind.Text
|
||||||
|
},
|
||||||
|
toolName = toolName,
|
||||||
|
toolCallId = toolCallId,
|
||||||
|
contentType = contentType,
|
||||||
|
sizeBytes = sizeBytes,
|
||||||
|
retryable = retryable,
|
||||||
|
)
|
||||||
@@ -6,6 +6,7 @@ interface OpenClawRepository {
|
|||||||
var status: ServerStatus
|
var status: ServerStatus
|
||||||
fun streamStatusFor(sessionId: String): String?
|
fun streamStatusFor(sessionId: String): String?
|
||||||
fun hydrateOverview(snapshot: OverviewSnapshot)
|
fun hydrateOverview(snapshot: OverviewSnapshot)
|
||||||
|
fun hydrateRecentMessages(snapshot: MessageCacheSnapshot)
|
||||||
|
|
||||||
fun findSession(sessionId: String): SessionSummary?
|
fun findSession(sessionId: String): SessionSummary?
|
||||||
fun messagesFor(sessionId: String): List<ChatMessage>
|
fun messagesFor(sessionId: String): List<ChatMessage>
|
||||||
|
|||||||
+9
@@ -83,6 +83,10 @@ class RemoteOpenClawRepository(
|
|||||||
status = snapshot.status.toDomain()
|
status = snapshot.status.toDomain()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun hydrateRecentMessages(snapshot: MessageCacheSnapshot) {
|
||||||
|
messageStore = messageStore + (snapshot.sessionId to snapshot.messages.map { it.toDomain() })
|
||||||
|
}
|
||||||
|
|
||||||
override fun recentSession(): SessionSummary? = recentSessionId?.let(::findSession)
|
override fun recentSession(): SessionSummary? = recentSessionId?.let(::findSession)
|
||||||
|
|
||||||
override fun pinnedCount(): Int = sessions.count { it.isPinned }
|
override fun pinnedCount(): Int = sessions.count { it.isPinned }
|
||||||
@@ -298,6 +302,11 @@ class RemoteOpenClawRepository(
|
|||||||
status = status.toSnapshot(),
|
status = status.toSnapshot(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
fun exportRecentMessageSnapshot(sessionId: String): MessageCacheSnapshot = MessageCacheSnapshot(
|
||||||
|
sessionId = sessionId,
|
||||||
|
messages = messagesFor(sessionId).map { it.toCacheItem() },
|
||||||
|
)
|
||||||
|
|
||||||
suspend fun refreshAgents() {
|
suspend fun refreshAgents() {
|
||||||
availableAgents = api.getAgents().map { it.id.ifBlank { it.name } }
|
availableAgents = api.getAgents().map { it.id.ifBlank { it.name } }
|
||||||
.ifEmpty { listOf(config.defaultAgent) }
|
.ifEmpty { listOf(config.defaultAgent) }
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ Android/iOS App
|
|||||||
|
|
||||||
- [x] 通过 BFF 拉取真实 sessions 列表
|
- [x] 通过 BFF 拉取真实 sessions 列表
|
||||||
- [x] 本地缓存最近一次 overview(status + sessions + agents)用于冷启动兜底
|
- [x] 本地缓存最近一次 overview(status + sessions + agents)用于冷启动兜底
|
||||||
|
- [x] 本地缓存最近打开会话的消息列表用于聊天页冷启动兜底
|
||||||
|
- [x] 本地缓存最近打开会话的消息列表用于聊天页冷启动兜底
|
||||||
- [x] 通过 BFF 拉取真实 chat history
|
- [x] 通过 BFF 拉取真实 chat history
|
||||||
- [x] 会话列表支持:
|
- [x] 会话列表支持:
|
||||||
- 搜索
|
- 搜索
|
||||||
|
|||||||
Reference in new issue
Block a user