fix: harden chat SSE handling to avoid crashes
This commit is contained in:
1 parent
b43e40a67e
commit
2595107244
2 files changed
+30
-8
No files matched your search
+17
-5
@@ -7,12 +7,16 @@ import cn.xiaobai.openclawapp.network.MobileGatewayApi
|
|||||||
import cn.xiaobai.openclawapp.network.SessionSummaryDto
|
import cn.xiaobai.openclawapp.network.SessionSummaryDto
|
||||||
import cn.xiaobai.openclawapp.network.SseSessionStream
|
import cn.xiaobai.openclawapp.network.SseSessionStream
|
||||||
import cn.xiaobai.openclawapp.network.createDefaultHttpClient
|
import cn.xiaobai.openclawapp.network.createDefaultHttpClient
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
|
import kotlinx.coroutines.currentCoroutineContext
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class RemoteOpenClawRepository(
|
class RemoteOpenClawRepository(
|
||||||
@@ -31,7 +35,7 @@ class RemoteOpenClawRepository(
|
|||||||
tokenProvider = { config.token },
|
tokenProvider = { config.token },
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
private val streamScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
private val streamScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
|
||||||
private var streamJob: Job? = null
|
private var streamJob: Job? = null
|
||||||
|
|
||||||
override var availableAgents by mutableStateOf(listOf("main"))
|
override var availableAgents by mutableStateOf(listOf("main"))
|
||||||
@@ -124,10 +128,18 @@ class RemoteOpenClawRepository(
|
|||||||
private fun subscribeSessionStream(sessionId: String) {
|
private fun subscribeSessionStream(sessionId: String) {
|
||||||
streamJob?.cancel()
|
streamJob?.cancel()
|
||||||
streamJob = streamScope.launch {
|
streamJob = streamScope.launch {
|
||||||
sseStream.connect(sessionId).collectLatest { snapshot ->
|
while (currentCoroutineContext().isActive) {
|
||||||
messageStore = messageStore + (
|
try {
|
||||||
sessionId to snapshot.messages.map { dto -> dto.toDomainMessage() }
|
sseStream.connect(sessionId).collectLatest { snapshot ->
|
||||||
)
|
messageStore = messageStore + (
|
||||||
|
sessionId to snapshot.messages.map { dto -> dto.toDomainMessage() }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} catch (_: CancellationException) {
|
||||||
|
throw _
|
||||||
|
} catch (_: Throwable) {
|
||||||
|
delay(1500)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-3
@@ -1,10 +1,11 @@
|
|||||||
package cn.xiaobai.openclawapp.network
|
package cn.xiaobai.openclawapp.network
|
||||||
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.prepareGet
|
|
||||||
import io.ktor.client.request.header
|
import io.ktor.client.request.header
|
||||||
|
import io.ktor.client.request.prepareGet
|
||||||
import io.ktor.client.statement.bodyAsChannel
|
import io.ktor.client.statement.bodyAsChannel
|
||||||
import io.ktor.http.HttpHeaders
|
import io.ktor.http.HttpHeaders
|
||||||
|
import io.ktor.http.isSuccess
|
||||||
import io.ktor.utils.io.readUTF8Line
|
import io.ktor.utils.io.readUTF8Line
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
@@ -15,6 +16,8 @@ class SseSessionStream(
|
|||||||
private val baseUrl: String,
|
private val baseUrl: String,
|
||||||
private val tokenProvider: () -> String,
|
private val tokenProvider: () -> String,
|
||||||
) {
|
) {
|
||||||
|
private val json = Json { ignoreUnknownKeys = true }
|
||||||
|
|
||||||
fun connect(sessionId: String): Flow<SessionSnapshotEventDto> = flow {
|
fun connect(sessionId: String): Flow<SessionSnapshotEventDto> = flow {
|
||||||
val url = "${baseUrl.trimEnd('/')}/api/sessions/${encodePath(sessionId)}/stream"
|
val url = "${baseUrl.trimEnd('/')}/api/sessions/${encodePath(sessionId)}/stream"
|
||||||
client.prepareGet(url) {
|
client.prepareGet(url) {
|
||||||
@@ -23,6 +26,10 @@ class SseSessionStream(
|
|||||||
header(HttpHeaders.Authorization, "Bearer $token")
|
header(HttpHeaders.Authorization, "Bearer $token")
|
||||||
}
|
}
|
||||||
}.execute { response ->
|
}.execute { response ->
|
||||||
|
if (!response.status.isSuccess()) {
|
||||||
|
throw IllegalStateException("SSE connect failed: HTTP ${response.status.value}")
|
||||||
|
}
|
||||||
|
|
||||||
val channel = response.bodyAsChannel()
|
val channel = response.bodyAsChannel()
|
||||||
var currentEvent: String? = null
|
var currentEvent: String? = null
|
||||||
val dataLines = mutableListOf<String>()
|
val dataLines = mutableListOf<String>()
|
||||||
@@ -34,8 +41,11 @@ class SseSessionStream(
|
|||||||
line.startsWith("data:") -> dataLines += line.removePrefix("data:").trim()
|
line.startsWith("data:") -> dataLines += line.removePrefix("data:").trim()
|
||||||
line.isBlank() -> {
|
line.isBlank() -> {
|
||||||
if (currentEvent == "session.snapshot" && dataLines.isNotEmpty()) {
|
if (currentEvent == "session.snapshot" && dataLines.isNotEmpty()) {
|
||||||
val dto = Json { ignoreUnknownKeys = true }.decodeFromString<SessionSnapshotEventDto>(dataLines.joinToString("\n"))
|
runCatching {
|
||||||
emit(dto)
|
json.decodeFromString<SessionSnapshotEventDto>(dataLines.joinToString("\n"))
|
||||||
|
}.onSuccess { dto ->
|
||||||
|
emit(dto)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
currentEvent = null
|
currentEvent = null
|
||||||
dataLines.clear()
|
dataLines.clear()
|
||||||
|
|||||||
Reference in new issue
Block a user