diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt index cec8ed4..8a88800 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/app/OpenClawAppState.kt @@ -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 ?: "远程同步失败" } diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/AppPreferences.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/AppPreferences.kt index 4d57572..f0a984c 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/AppPreferences.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/AppPreferences.kt @@ -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(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" } } diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt index 872b218..7af06b9 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OpenClawRepository.kt @@ -5,6 +5,7 @@ interface OpenClawRepository { val sessions: List var status: ServerStatus fun streamStatusFor(sessionId: String): String? + fun hydrateOverview(snapshot: OverviewSnapshot) fun findSession(sessionId: String): SessionSummary? fun messagesFor(sessionId: String): List diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OverviewSnapshot.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OverviewSnapshot.kt new file mode 100644 index 0000000..61c43a0 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/OverviewSnapshot.kt @@ -0,0 +1,66 @@ +package cn.xiaobai.openclawapp.data + +import kotlinx.serialization.Serializable + +@Serializable +data class OverviewSnapshot( + val availableAgents: List = emptyList(), + val sessions: List = 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, +) diff --git a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt index 66e5d68..32f0b13 100644 --- a/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt +++ b/composeApp/src/commonMain/kotlin/cn/xiaobai/openclawapp/data/RemoteOpenClawRepository.kt @@ -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) } diff --git a/docs/MVP_PROGRESS.md b/docs/MVP_PROGRESS.md index 8e0f476..5242072 100644 --- a/docs/MVP_PROGRESS.md +++ b/docs/MVP_PROGRESS.md @@ -44,6 +44,7 @@ Android/iOS App ### 2.2 会话基础能力 - [x] 通过 BFF 拉取真实 sessions 列表 +- [x] 本地缓存最近一次 overview(status + sessions + agents)用于冷启动兜底 - [x] 通过 BFF 拉取真实 chat history - [x] 会话列表支持: - 搜索