feat: enable remote sync flow with loading and error states
This commit is contained in:
1 parent
2d14cc5cc8
commit
d921919174
10 files changed
+203
-16
No files matched your search
@@ -41,6 +41,17 @@ kotlin {
|
||||
androidMain.dependencies {
|
||||
implementation(libs.compose.uiToolingPreview)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.ktor.client.okhttp)
|
||||
}
|
||||
val iosMain by getting {
|
||||
dependencies {
|
||||
implementation(libs.ktor.client.darwin)
|
||||
}
|
||||
}
|
||||
val jsMain by getting {
|
||||
dependencies {
|
||||
implementation(libs.ktor.client.js)
|
||||
}
|
||||
}
|
||||
commonMain.dependencies {
|
||||
implementation(libs.compose.runtime)
|
||||
|
||||
@@ -14,11 +14,13 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import cn.xiaobai.openclawapp.app.MainTab
|
||||
import cn.xiaobai.openclawapp.app.OpenClawAppState
|
||||
import cn.xiaobai.openclawapp.app.DataSourceMode
|
||||
import cn.xiaobai.openclawapp.data.ServerConfig
|
||||
import cn.xiaobai.openclawapp.feature.chat.ChatScreen
|
||||
import cn.xiaobai.openclawapp.feature.config.ServerConfigScreen
|
||||
@@ -28,6 +30,7 @@ import cn.xiaobai.openclawapp.feature.status.StatusScreen
|
||||
import cn.xiaobai.openclawapp.ui.OpenClawTheme
|
||||
import cn.xiaobai.openclawapp.ui.StatusPill
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun App() {
|
||||
@@ -68,6 +71,7 @@ fun App() {
|
||||
@Composable
|
||||
private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
val activeSession = appState.activeSessionId?.let(appState.repository::findSession)
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
LaunchedEffect(appState.feedbackMessage) {
|
||||
if (appState.feedbackMessage != null) {
|
||||
@@ -76,6 +80,12 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(appState.isConfigured, appState.dataSource) {
|
||||
if (appState.isConfigured && appState.dataSource == DataSourceMode.Remote) {
|
||||
appState.syncRemoteIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
CenterAlignedTopAppBar(
|
||||
@@ -135,8 +145,21 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
sessionTitle = activeSession.title,
|
||||
messages = appState.repository.messagesFor(activeSession.id),
|
||||
isSending = appState.isSending,
|
||||
isLoading = appState.isSyncing,
|
||||
errorMessage = appState.syncError,
|
||||
onBack = appState::closeChat,
|
||||
onSend = { text -> appState.repository.sendMessage(activeSession.id, text) },
|
||||
onRetry = {
|
||||
coroutineScope.launch {
|
||||
appState.clearSyncError()
|
||||
appState.openSession(activeSession.id)
|
||||
}
|
||||
},
|
||||
onSend = { text ->
|
||||
coroutineScope.launch {
|
||||
appState.sendMessage(activeSession.id, text)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
appState.selectedTab == MainTab.Sessions -> SessionListScreen(
|
||||
@@ -144,8 +167,20 @@ private fun OpenClawRoot(appState: OpenClawAppState) {
|
||||
sessions = appState.repository.sessions,
|
||||
recentSession = appState.repository.recentSession(),
|
||||
pinnedCount = appState.repository.pinnedCount(),
|
||||
onOpenSession = appState::openSession,
|
||||
isLoading = appState.isSyncing,
|
||||
errorMessage = appState.syncError,
|
||||
onOpenSession = { sessionId ->
|
||||
coroutineScope.launch {
|
||||
appState.openSession(sessionId)
|
||||
}
|
||||
},
|
||||
onTogglePin = appState::togglePin,
|
||||
onRetry = {
|
||||
coroutineScope.launch {
|
||||
appState.clearSyncError()
|
||||
appState.syncRemoteIfNeeded()
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
appState.selectedTab == MainTab.Status -> StatusScreen(
|
||||
|
||||
@@ -4,6 +4,7 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import cn.xiaobai.openclawapp.data.OpenClawRepository
|
||||
import cn.xiaobai.openclawapp.data.RemoteOpenClawRepository
|
||||
import cn.xiaobai.openclawapp.data.RepositoryFactory
|
||||
import cn.xiaobai.openclawapp.data.ServerConfig
|
||||
|
||||
@@ -15,6 +16,9 @@ class OpenClawAppState {
|
||||
var feedbackMessage by mutableStateOf<String?>(null)
|
||||
var themeMode by mutableStateOf(AppThemeMode.Dark)
|
||||
var dataSource by mutableStateOf(DataSourceMode.Mock)
|
||||
var isSyncing by mutableStateOf(false)
|
||||
var isSending by mutableStateOf(false)
|
||||
var syncError by mutableStateOf<String?>(null)
|
||||
var repository: OpenClawRepository by mutableStateOf(RepositoryFactory.createMock())
|
||||
|
||||
fun connect(config: ServerConfig) {
|
||||
@@ -24,6 +28,7 @@ class OpenClawAppState {
|
||||
DataSourceMode.Remote -> RepositoryFactory.createRemote(config)
|
||||
}
|
||||
isConfigured = true
|
||||
syncError = null
|
||||
feedbackMessage = when (dataSource) {
|
||||
DataSourceMode.Mock -> "已进入 Mock 模式"
|
||||
DataSourceMode.Remote -> "已连接到 ${config.baseUrl}"
|
||||
@@ -34,18 +39,60 @@ class OpenClawAppState {
|
||||
isConfigured = false
|
||||
activeSessionId = null
|
||||
selectedTab = MainTab.Sessions
|
||||
syncError = null
|
||||
feedbackMessage = "连接已断开"
|
||||
}
|
||||
|
||||
fun openSession(sessionId: String) {
|
||||
suspend fun syncRemoteIfNeeded() {
|
||||
val remote = repository as? RemoteOpenClawRepository ?: return
|
||||
isSyncing = true
|
||||
syncError = null
|
||||
runCatching {
|
||||
remote.refreshStatus()
|
||||
remote.refreshAgents()
|
||||
remote.refreshSessions()
|
||||
}.onFailure {
|
||||
syncError = it.message ?: "远程同步失败"
|
||||
feedbackMessage = "Remote 同步失败"
|
||||
}
|
||||
isSyncing = false
|
||||
}
|
||||
|
||||
suspend fun openSession(sessionId: String) {
|
||||
repository.markSessionOpened(sessionId)
|
||||
activeSessionId = sessionId
|
||||
val remote = repository as? RemoteOpenClawRepository ?: return
|
||||
isSyncing = true
|
||||
runCatching {
|
||||
remote.refreshMessages(sessionId)
|
||||
}.onFailure {
|
||||
syncError = it.message ?: "会话消息加载失败"
|
||||
feedbackMessage = "消息加载失败"
|
||||
}
|
||||
isSyncing = false
|
||||
}
|
||||
|
||||
fun closeChat() {
|
||||
activeSessionId = null
|
||||
}
|
||||
|
||||
suspend fun sendMessage(sessionId: String, text: String) {
|
||||
val remote = repository as? RemoteOpenClawRepository
|
||||
if (remote == null) {
|
||||
repository.sendMessage(sessionId, text)
|
||||
return
|
||||
}
|
||||
|
||||
isSending = true
|
||||
runCatching {
|
||||
remote.sendMessageRemote(sessionId, text)
|
||||
}.onFailure {
|
||||
syncError = it.message ?: "消息发送失败"
|
||||
feedbackMessage = "消息发送失败"
|
||||
}
|
||||
isSending = false
|
||||
}
|
||||
|
||||
fun updateDefaultAgent(agent: String) {
|
||||
serverConfig = serverConfig.copy(defaultAgent = agent)
|
||||
repository.status = repository.status.copy(defaultAgent = agent)
|
||||
@@ -76,6 +123,10 @@ class OpenClawAppState {
|
||||
fun clearFeedback() {
|
||||
feedbackMessage = null
|
||||
}
|
||||
|
||||
fun clearSyncError() {
|
||||
syncError = null
|
||||
}
|
||||
}
|
||||
|
||||
enum class MainTab {
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import androidx.compose.runtime.setValue
|
||||
import kotlin.random.Random
|
||||
|
||||
class MockOpenClawRepository : OpenClawRepository {
|
||||
override val availableAgents = listOf("main", "pm", "rd-lead", "backend-dev")
|
||||
override var availableAgents by mutableStateOf(listOf("main", "pm", "rd-lead", "backend-dev"))
|
||||
|
||||
override var sessions by mutableStateOf(
|
||||
listOf(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package cn.xiaobai.openclawapp.data
|
||||
|
||||
interface OpenClawRepository {
|
||||
val availableAgents: List<String>
|
||||
var availableAgents: List<String>
|
||||
val sessions: List<SessionSummary>
|
||||
var status: ServerStatus
|
||||
|
||||
|
||||
+6
-1
@@ -15,7 +15,7 @@ class RemoteOpenClawRepository(
|
||||
tokenProvider = { config.token },
|
||||
)
|
||||
}
|
||||
override val availableAgents = listOf("main")
|
||||
override var availableAgents by mutableStateOf(listOf("main"))
|
||||
|
||||
override var sessions by mutableStateOf<List<SessionSummary>>(emptyList())
|
||||
override var status by mutableStateOf(
|
||||
@@ -87,6 +87,11 @@ class RemoteOpenClawRepository(
|
||||
sessions = api.getSessions().map(SessionSummaryDto::toDomain)
|
||||
}
|
||||
|
||||
suspend fun refreshAgents() {
|
||||
availableAgents = api.getAgents().map { it.id.ifBlank { it.name } }
|
||||
.ifEmpty { listOf(config.defaultAgent) }
|
||||
}
|
||||
|
||||
suspend fun refreshMessages(sessionId: String) {
|
||||
val remoteMessages = api.getMessages(sessionId).map { dto ->
|
||||
ChatMessage(
|
||||
|
||||
@@ -15,6 +15,7 @@ import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
@@ -40,7 +41,11 @@ fun ChatScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
sessionTitle: String,
|
||||
messages: List<ChatMessage>,
|
||||
isSending: Boolean,
|
||||
isLoading: Boolean,
|
||||
errorMessage: String?,
|
||||
onBack: () -> Unit,
|
||||
onRetry: () -> Unit,
|
||||
onSend: (String) -> Unit,
|
||||
) {
|
||||
var input by remember { mutableStateOf("") }
|
||||
@@ -78,7 +83,31 @@ fun ChatScreen(
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
StatusPill(text = "在线", highlighted = true)
|
||||
StatusPill(text = if (isLoading) "同步中" else "在线", highlighted = true)
|
||||
}
|
||||
}
|
||||
|
||||
if (errorMessage != null) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer),
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(14.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
errorMessage,
|
||||
modifier = Modifier.weight(1f),
|
||||
color = MaterialTheme.colorScheme.onErrorContainer,
|
||||
)
|
||||
Button(onClick = onRetry) {
|
||||
Text("重试")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +124,7 @@ fun ChatScreen(
|
||||
}
|
||||
}
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
if (messages.isEmpty() && !isLoading) {
|
||||
EmptyStateCard(
|
||||
title = "还没有消息",
|
||||
subtitle = "这里以后会展示真实的会话历史、流式回复和工具执行状态。",
|
||||
@@ -129,7 +158,7 @@ fun ChatScreen(
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||
) {
|
||||
Text(
|
||||
"直接发一条消息给当前会话",
|
||||
if (isSending) "正在发送消息…" else "直接发一条消息给当前会话",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
@@ -145,16 +174,22 @@ fun ChatScreen(
|
||||
label = { Text("输入消息") },
|
||||
placeholder = { Text("给 OpenClaw 发一句话") },
|
||||
maxLines = 4,
|
||||
enabled = !isSending,
|
||||
)
|
||||
Button(
|
||||
onClick = {
|
||||
if (input.isNotBlank()) {
|
||||
if (input.isNotBlank() && !isSending) {
|
||||
onSend(input.trim())
|
||||
input = ""
|
||||
}
|
||||
},
|
||||
enabled = !isSending,
|
||||
) {
|
||||
Text("发送")
|
||||
if (isSending) {
|
||||
CircularProgressIndicator()
|
||||
} else {
|
||||
Text("发送")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+47
-3
@@ -6,11 +6,13 @@ import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.weight
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
@@ -34,8 +36,11 @@ fun SessionListScreen(
|
||||
sessions: List<SessionSummary>,
|
||||
recentSession: SessionSummary?,
|
||||
pinnedCount: Int,
|
||||
isLoading: Boolean,
|
||||
errorMessage: String?,
|
||||
onOpenSession: (String) -> Unit,
|
||||
onTogglePin: (String) -> Unit,
|
||||
onRetry: () -> Unit,
|
||||
) {
|
||||
var query by remember { mutableStateOf("") }
|
||||
val filteredSessions = sessions.filter {
|
||||
@@ -53,7 +58,7 @@ fun SessionListScreen(
|
||||
HeroCard(
|
||||
title = "OpenClaw Mobile",
|
||||
subtitle = "先把会话体验打磨得像个真正可用的 App,再接真实消息流。这样后面联调会轻松很多。",
|
||||
badge = "Mock Mode",
|
||||
badge = "${if (isLoading) "Syncing" else "Ready"}",
|
||||
)
|
||||
}
|
||||
|
||||
@@ -65,6 +70,45 @@ fun SessionListScreen(
|
||||
}
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
item {
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
CircularProgressIndicator()
|
||||
Text("正在同步远程会话和状态…")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (errorMessage != null) {
|
||||
item {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.errorContainer),
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
Text("同步失败", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.onErrorContainer)
|
||||
Text(errorMessage, color = MaterialTheme.colorScheme.onErrorContainer)
|
||||
Button(onClick = onRetry) {
|
||||
Text("重试")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (recentSession != null) {
|
||||
item {
|
||||
Card(
|
||||
@@ -105,11 +149,11 @@ fun SessionListScreen(
|
||||
)
|
||||
}
|
||||
|
||||
if (filteredSessions.isEmpty()) {
|
||||
if (filteredSessions.isEmpty() && !isLoading) {
|
||||
item {
|
||||
EmptyStateCard(
|
||||
title = "没有匹配的会话",
|
||||
subtitle = "现在还只是本地 Mock 数据。后面接入真实接口后,这里会变成真正的会话检索入口。",
|
||||
subtitle = "如果你当前走的是 Remote 模式,那下一步就该检查 BFF 接口返回和 DTO 映射了。",
|
||||
)
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.http.encodeURLPathPart
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@@ -31,13 +32,15 @@ class MobileGatewayApi(
|
||||
}
|
||||
|
||||
suspend fun getMessages(sessionId: String): List<ChatMessageDto> {
|
||||
return client.get("${baseUrl.trimEnd('/')}/api/sessions/$sessionId/messages") {
|
||||
val encodedId = sessionId.encodeURLPathPart()
|
||||
return client.get("${baseUrl.trimEnd('/')}/api/sessions/$encodedId/messages") {
|
||||
authHeader()
|
||||
}.body()
|
||||
}
|
||||
|
||||
suspend fun sendMessage(sessionId: String, message: String): SendMessageResponseDto {
|
||||
return client.post("${baseUrl.trimEnd('/')}/api/sessions/$sessionId/messages") {
|
||||
val encodedId = sessionId.encodeURLPathPart()
|
||||
return client.post("${baseUrl.trimEnd('/')}/api/sessions/$encodedId/messages") {
|
||||
authHeader()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(SendMessageRequestDto(message = message))
|
||||
|
||||
@@ -36,6 +36,9 @@ compose-components-resources = { module = "org.jetbrains.compose.components:comp
|
||||
compose-uiToolingPreview = { module = "org.jetbrains.compose.ui:ui-tooling-preview", version.ref = "composeMultiplatform" }
|
||||
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
|
||||
ktor-client-contentNegotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
|
||||
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
|
||||
ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "ktor" }
|
||||
ktor-client-js = { module = "io.ktor:ktor-client-js", version.ref = "ktor" }
|
||||
ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
|
||||
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
|
||||
|
||||
|
||||
Reference in new issue
Block a user