Cache overview snapshot for cold starts

This commit is contained in:
OpenClaw committed 2026-03-07 10:01:51 +08:00
1 parent 0a261af380
commit 75c93f36d0
6 files changed
+100

No files matched your search

@@ -26,6 +26,12 @@ class OpenClawAppState(
var repository: OpenClawRepository by mutableStateOf(RepositoryFactory.createRemote(serverConfig))
var isConfigured by mutableStateOf(serverConfig.baseUrl.isNotBlank() && serverConfig.token.isNotBlank())
init {
preferences.loadOverviewSnapshot()?.let { snapshot ->
repository.hydrateOverview(snapshot)
}
}
suspend fun connect(config: ServerConfig, password: String) {
isConnecting = true
configError = null
@@ -74,6 +80,7 @@ class OpenClawAppState(
remote.refreshStatus()
remote.refreshAgents()
remote.refreshSessions()
preferences.saveOverviewSnapshot(remote.exportOverviewSnapshot())
}.onFailure {
syncError = it.message ?: "远程同步失败"
feedbackMessage = "Remote 同步失败"
@@ -89,6 +96,7 @@ class OpenClawAppState(
remote.refreshStatus()
remote.refreshAgents()
remote.refreshSessions()
preferences.saveOverviewSnapshot(remote.exportOverviewSnapshot())
}.onFailure {
syncError = it.message ?: "远程同步失败"
}
@@ -2,10 +2,12 @@ package cn.xiaobai.openclawapp.data
import cn.xiaobai.openclawapp.app.AppThemeMode
import com.russhwolf.settings.Settings
import kotlinx.serialization.json.Json
class AppPreferences(
private val settings: Settings = Settings(),
) {
private val json = Json { ignoreUnknownKeys = true }
fun loadServerConfig(): ServerConfig {
return ServerConfig(
baseUrl = settings.getStringOrNull(KEY_BASE_URL) ?: "http://192.168.31.116:8788",
@@ -35,10 +37,20 @@ class AppPreferences(
settings.putString(KEY_THEME_MODE, themeMode.name)
}
fun loadOverviewSnapshot(): OverviewSnapshot? {
val raw = settings.getStringOrNull(KEY_OVERVIEW_SNAPSHOT) ?: return null
return runCatching { json.decodeFromString<OverviewSnapshot>(raw) }.getOrNull()
}
fun saveOverviewSnapshot(snapshot: OverviewSnapshot) {
settings.putString(KEY_OVERVIEW_SNAPSHOT, json.encodeToString(OverviewSnapshot.serializer(), snapshot))
}
companion object {
private const val KEY_BASE_URL = "server.base_url"
private const val KEY_TOKEN = "server.token"
private const val KEY_DEFAULT_AGENT = "server.default_agent"
private const val KEY_THEME_MODE = "ui.theme_mode"
private const val KEY_OVERVIEW_SNAPSHOT = "cache.overview_snapshot"
}
}
@@ -5,6 +5,7 @@ interface OpenClawRepository {
val sessions: List<SessionSummary>
var status: ServerStatus
fun streamStatusFor(sessionId: String): String?
fun hydrateOverview(snapshot: OverviewSnapshot)
fun findSession(sessionId: String): SessionSummary?
fun messagesFor(sessionId: String): List<ChatMessage>
@@ -0,0 +1,66 @@
package cn.xiaobai.openclawapp.data
import kotlinx.serialization.Serializable
@Serializable
data class OverviewSnapshot(
val availableAgents: List<String> = emptyList(),
val sessions: List<OverviewSessionSnapshot> = emptyList(),
val status: OverviewStatusSnapshot = OverviewStatusSnapshot(),
)
@Serializable
data class OverviewSessionSnapshot(
val id: String,
val title: String,
val lastMessage: String,
val updatedAt: String,
val isPinned: Boolean = false,
val unreadCount: Int = 0,
val streamStatus: String? = null,
)
@Serializable
data class OverviewStatusSnapshot(
val online: Boolean = false,
val openClawVersion: String = "-",
val currentModel: String = "-",
val defaultAgent: String = "main",
val updatedAt: String = "-",
)
fun ServerStatus.toSnapshot(): OverviewStatusSnapshot = OverviewStatusSnapshot(
online = online,
openClawVersion = openClawVersion,
currentModel = currentModel,
defaultAgent = defaultAgent,
updatedAt = updatedAt,
)
fun SessionSummary.toSnapshot(): OverviewSessionSnapshot = OverviewSessionSnapshot(
id = id,
title = title,
lastMessage = lastMessage,
updatedAt = updatedAt,
isPinned = isPinned,
unreadCount = unreadCount,
streamStatus = streamStatus,
)
fun OverviewStatusSnapshot.toDomain(): ServerStatus = ServerStatus(
online = online,
openClawVersion = openClawVersion,
currentModel = currentModel,
defaultAgent = defaultAgent,
updatedAt = updatedAt,
)
fun OverviewSessionSnapshot.toDomain(): SessionSummary = SessionSummary(
id = id,
title = title,
lastMessage = lastMessage,
updatedAt = updatedAt,
isPinned = isPinned,
unreadCount = unreadCount,
streamStatus = streamStatus,
)
@@ -77,6 +77,12 @@ class RemoteOpenClawRepository(
override fun streamStatusFor(sessionId: String): String? = streamStatusStore[sessionId]
override fun hydrateOverview(snapshot: OverviewSnapshot) {
availableAgents = snapshot.availableAgents
sessions = snapshot.sessions.map { it.toDomain() }
status = snapshot.status.toDomain()
}
override fun recentSession(): SessionSummary? = recentSessionId?.let(::findSession)
override fun pinnedCount(): Int = sessions.count { it.isPinned }
@@ -286,6 +292,12 @@ class RemoteOpenClawRepository(
resortSessions(prioritizeId = recentSessionId)
}
fun exportOverviewSnapshot(): OverviewSnapshot = OverviewSnapshot(
availableAgents = availableAgents,
sessions = sessions.map { it.toSnapshot() },
status = status.toSnapshot(),
)
suspend fun refreshAgents() {
availableAgents = api.getAgents().map { it.id.ifBlank { it.name } }
.ifEmpty { listOf(config.defaultAgent) }
+1
View File
@@ -44,6 +44,7 @@ Android/iOS App
### 2.2 会话基础能力
- [x] 通过 BFF 拉取真实 sessions 列表
- [x] 本地缓存最近一次 overview(status + sessions + agents)用于冷启动兜底
- [x] 通过 BFF 拉取真实 chat history
- [x] 会话列表支持:
- 搜索