feat(android): resume from the seq, and walk back through the session

The chat screen asked the claw for everything the cache did not hold by
counting events, and a session that ran for weeks cost the whole record
the first time it opened here. The stream now names each frame's seq and
the cache stores it, so a reopening screen sends since=<seq> and receives
only the gap. The cache migrates in place: the old count of events is
exactly the last seq plus one, because the record numbers densely.

A screen with no cache opens on the newest turn and walks back through
the new history route when the reader reaches the top, one whole turn
per page, so the phone does not hold a weeks-long transcript to show the
last few messages. Pages are folded through the same reducer the stream
uses and prepended, since the list runs oldest first: appended, they
would land at the newest end. A reset comment (the record no longer
holds the cursor) drops the mirror, and the tail that follows rebuilds
it. The outcome tag colours a steered turn as an accent rather than a
failure, matching the web UI.
This commit is contained in:
vhaudiquet committed 2026-11-08 18:01:52 +01:00
1 parent 70d9bb0d34
commit fa0e3d4470
7 files changed
+277 -51

No files matched your search

@@ -31,6 +31,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
@@ -58,6 +59,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -81,6 +83,7 @@ import rs.rhema.phone.data.AgentIdentityCache
import rs.rhema.phone.di.AppContainer
import rs.rhema.phone.net.ClawResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
@@ -178,7 +181,25 @@ fun ChatScreen(container: AppContainer, sessionKey: String, onBack: () -> Unit)
// bottom edge. A streaming reply grows against that anchor, so
// the newest text stays on screen without scroll bookkeeping,
// and a reader scrolled into history is never yanked back.
val listState = rememberLazyListState()
// Older turns are fetched, not cached: the claw holds the record
// and the screen opens on its newest turn, so the walk back
// starts when the reader reaches the top (higher indices are
// older here) or when the transcript is too short to fill the
// screen — which reads as "the last visible item is the last
// item" either way.
LaunchedEffect(listState, controller) {
snapshotFlow {
val info = listState.layoutInfo
info.visibleItemsInfo.lastOrNull()?.index to info.totalItemsCount
}
.distinctUntilChanged()
.collect { (last, total) ->
if (last != null && total > 0 && last >= total - 2) controller.loadOlder()
}
}
LazyColumn(
state = listState,
modifier = Modifier
.weight(1f)
.fillMaxWidth(),
@@ -189,6 +210,18 @@ fun ChatScreen(container: AppContainer, sessionKey: String, onBack: () -> Unit)
items(turnGroups(controller.items).asReversed(), key = { it.key }) { group ->
TurnGroupRow(group, container, controller, face)
}
// The last declared item sits at the top of a reversed list.
if (controller.loadingOlder) {
item(key = "older") {
Text(
stringResource(R.string.chat_loading_earlier),
style = MaterialTheme.typography.labelSmall,
fontFamily = JadeiteMono,
color = JadeiteText3,
modifier = Modifier.fillMaxWidth(),
)
}
}
}
controller.sendError?.let { err ->
@@ -678,11 +711,19 @@ private fun ReplyBlock(item: ChatController.Reply) {
@Composable
private fun OutcomeTag(outcome: String) {
// A steered turn was cut short by the next message, not failed: the
// reply that replaced it streams right below, so the tag reads as an
// accent, not an error — the same distinction the web UI draws.
val color = when {
outcome.equals("Failed", ignoreCase = true) -> JadeiteError
outcome.equals("Steered", ignoreCase = true) -> JadeiteAccentText
else -> JadeiteText3
}
Text(
outcome,
style = MaterialTheme.typography.labelSmall,
fontFamily = JadeiteMono,
color = if (outcome.equals("Failed", ignoreCase = true)) JadeiteError else JadeiteText3,
color = color,
)
}