@@ -312,92 +312,161 @@ private fun ChatPage(
312312 onSpeakLatest : () -> Unit ,
313313 onStopSpeaking : () -> Unit ,
314314) {
315- val colors = OpenClawWearTheme .colors
316- WearPage (pageLabel = stringResource(R .string.chat)) {
317- item {
318- ConversationIdentity (
319- snapshot = snapshot,
320- actionBusy = actionBusy,
321- onSelectAgent = onSelectAgent,
322- onSelectSession = onSelectSession,
323- onSelectModel = onSelectModel,
315+ val listState = rememberTransformingLazyColumnState()
316+ val coroutineScope = rememberCoroutineScope()
317+ val visibleMessages = snapshot.messages.takeLast(VISIBLE_MESSAGE_COUNT )
318+ val streamingText = snapshot.streamingAssistantText?.takeIf (String ::isNotBlank)
319+ val hasAssistant = snapshot.messages.any { message -> message.chatRole == WearChatRole .ASSISTANT }
320+ val latestAnchorIndex =
321+ wearChatLatestAnchorIndex(
322+ visibleMessageCount = visibleMessages.size,
323+ hasStreaming = streamingText != null ,
324+ canAbort = canAbort,
325+ hasAssistant = hasAssistant,
326+ hasFailure = snapshot.failure != null ,
327+ )
328+ val contentRevision =
329+ wearChatContentRevision(
330+ sessionId = snapshot.activeSessionId,
331+ messages = visibleMessages,
332+ streamingText = streamingText,
333+ latestAnchorIndex = latestAnchorIndex,
334+ )
335+ var followState by remember(snapshot.activeSessionId) { mutableStateOf(WearThreadFollowState ()) }
336+
337+ LaunchedEffect (listState, snapshot.activeSessionId) {
338+ snapshotFlow {
339+ WearThreadViewport (
340+ atLatest = ! listState.canScrollForward,
341+ scrollingBackward = listState.isScrollInProgress && listState.lastScrolledBackward,
324342 )
343+ }.collect { viewport ->
344+ followState =
345+ nextWearThreadFollowForViewport(
346+ state = followState,
347+ atLatest = viewport.atLatest,
348+ scrollingBackward = viewport.scrollingBackward,
349+ )
325350 }
326- item {
327- ConversationStatus (
328- interaction = interaction,
329- speaking = speaking,
330- gatewayConnected = snapshot.gatewayState == WearGatewayState .CONNECTED ,
351+ }
352+ LaunchedEffect (snapshot.activeSessionId, contentRevision) {
353+ val update =
354+ nextWearThreadFollowForContent(
355+ state = followState,
356+ contentRevision = contentRevision,
331357 )
358+ followState = update.state
359+ if (update.scrollToLatest && latestAnchorIndex >= 0 ) {
360+ listState.requestScrollToItem(latestAnchorIndex)
332361 }
333- item {
334- Row (
335- modifier =
336- Modifier
337- .fillMaxWidth()
338- .padding(horizontal = 12 .dp),
339- horizontalArrangement = Arrangement .spacedBy(6 .dp),
340- ) {
341- ActionButton (
342- label = stringResource(R .string.talk),
343- enabled = inputEnabled && ! actionBusy && ! speaking,
344- onClick = onTalk,
345- modifier = Modifier .weight(1f ),
346- )
347- ActionButton (
348- label = stringResource(R .string.type),
349- enabled = inputEnabled && ! actionBusy && ! speaking,
350- onClick = onType,
351- modifier = Modifier .weight(1f ),
362+ }
363+
364+ Box (modifier = Modifier .fillMaxSize()) {
365+ WearPage (
366+ pageLabel = stringResource(R .string.chat),
367+ listState = listState,
368+ ) {
369+ item {
370+ ConversationIdentity (
371+ snapshot = snapshot,
372+ actionBusy = actionBusy,
373+ onSelectAgent = onSelectAgent,
374+ onSelectSession = onSelectSession,
375+ onSelectModel = onSelectModel,
352376 )
353377 }
354- }
355- if (canAbort) {
356378 item {
357- SecondaryButton (
358- label = stringResource( R .string.abort_run) ,
359- enabled = true ,
360- onClick = onAbort ,
379+ ConversationStatus (
380+ interaction = interaction ,
381+ speaking = speaking ,
382+ gatewayConnected = snapshot.gatewayState == WearGatewayState . CONNECTED ,
361383 )
362384 }
363- }
364- if (snapshot.messages.isEmpty() && snapshot.streamingAssistantText.isNullOrBlank()) {
365385 item {
366- EmptyConversation ()
386+ Row (
387+ modifier =
388+ Modifier
389+ .fillMaxWidth()
390+ .padding(horizontal = 12 .dp),
391+ horizontalArrangement = Arrangement .spacedBy(6 .dp),
392+ ) {
393+ ActionButton (
394+ label = stringResource(R .string.talk),
395+ enabled = inputEnabled && ! actionBusy && ! speaking,
396+ onClick = onTalk,
397+ modifier = Modifier .weight(1f ),
398+ )
399+ ActionButton (
400+ label = stringResource(R .string.type),
401+ enabled = inputEnabled && ! actionBusy && ! speaking,
402+ onClick = onType,
403+ modifier = Modifier .weight(1f ),
404+ )
405+ }
367406 }
368- } else {
369- snapshot.messages
370- .takeLast(VISIBLE_MESSAGE_COUNT )
371- .forEach { message ->
407+ if (canAbort) {
408+ item {
409+ SecondaryButton (
410+ label = stringResource(R .string.abort_run),
411+ enabled = true ,
412+ onClick = onAbort,
413+ )
414+ }
415+ }
416+ if (visibleMessages.isEmpty() && streamingText == null ) {
417+ item {
418+ EmptyConversation ()
419+ }
420+ } else {
421+ visibleMessages.forEach { message ->
372422 item(key = message.id ? : " ${message.role} :${message.timestamp} :${message.text.hashCode()} " ) {
373423 MessageBubble (message = message)
374424 }
375425 }
376- snapshot.streamingAssistantText
377- ?.takeIf (String ::isNotBlank)
378- ?.let { streaming ->
426+ streamingText?.let { streaming ->
379427 item {
380428 StreamingBubble (text = streaming)
381429 }
382430 }
383- }
384- if (snapshot.messages.any { message -> message.chatRole == WearChatRole .ASSISTANT }) {
385- item {
386- SecondaryButton (
387- label =
388- if (speaking) {
389- stringResource(R .string.stop_speaking)
390- } else {
391- stringResource(R .string.speak_reply)
392- },
393- enabled = ! actionBusy || speaking,
394- onClick = if (speaking) onStopSpeaking else onSpeakLatest,
395- )
431+ }
432+ if (hasAssistant) {
433+ item {
434+ SecondaryButton (
435+ label =
436+ if (speaking) {
437+ stringResource(R .string.stop_speaking)
438+ } else {
439+ stringResource(R .string.speak_reply)
440+ },
441+ enabled = ! actionBusy || speaking,
442+ onClick = if (speaking) onStopSpeaking else onSpeakLatest,
443+ )
444+ }
445+ }
446+ snapshot.failure?.let { failure ->
447+ item {
448+ InlineError (text = failureDetail(failure))
449+ }
450+ }
451+ if (latestAnchorIndex >= 0 ) {
452+ item(key = " chat-end" ) {
453+ Spacer (modifier = Modifier .height(1 .dp))
454+ }
396455 }
397456 }
398- snapshot.failure?.let { failure ->
399- item {
400- InlineError (text = failureDetail(failure))
457+ if (followState.hasNewContent) {
458+ NewMessagesAction (
459+ modifier =
460+ Modifier
461+ .align(Alignment .BottomCenter )
462+ .padding(bottom = 8 .dp),
463+ ) {
464+ followState = wearThreadFollowLatest(followState)
465+ if (latestAnchorIndex >= 0 ) {
466+ coroutineScope.launch {
467+ listState.animateScrollToItem(latestAnchorIndex)
468+ }
469+ }
401470 }
402471 }
403472 }
@@ -870,33 +939,18 @@ private fun ThreadVoiceMode(
870939 }
871940 }
872941 if (followState.hasNewContent) {
873- Box (
942+ NewMessagesAction (
874943 modifier =
875944 Modifier
876945 .align(Alignment .BottomCenter )
877- .padding(bottom = 44 .dp)
878- .minimumInteractiveComponentSize()
879- .background(colors.voiceAccentSoft, RoundedCornerShape (14 .dp))
880- .border(1 .dp, colors.voiceAccent, RoundedCornerShape (14 .dp))
881- .clickable(
882- role = Role .Button ,
883- onClickLabel = stringResource(R .string.show_new_messages),
884- ) {
885- followState = wearThreadFollowLatest(followState)
886- if (latestAnchorIndex >= 0 ) {
887- coroutineScope.launch {
888- listState.animateScrollToItem(latestAnchorIndex)
889- }
890- }
891- }.padding(horizontal = 10 .dp, vertical = 5 .dp),
892- contentAlignment = Alignment .Center ,
946+ .padding(bottom = 44 .dp),
893947 ) {
894- Text (
895- text = stringResource( R .string.new_messages) + " ↓ " ,
896- color = colors.voiceAccent,
897- fontSize = 10 .sp,
898- fontWeight = FontWeight . Bold ,
899- )
948+ followState = wearThreadFollowLatest(followState)
949+ if (latestAnchorIndex >= 0 ) {
950+ coroutineScope.launch {
951+ listState.animateScrollToItem(latestAnchorIndex)
952+ }
953+ }
900954 }
901955 }
902956 Row (
@@ -964,6 +1018,34 @@ private fun ThreadVoiceMode(
9641018 }
9651019}
9661020
1021+ @Composable
1022+ private fun NewMessagesAction (
1023+ modifier : Modifier ,
1024+ onClick : () -> Unit ,
1025+ ) {
1026+ val colors = OpenClawWearTheme .colors
1027+ Box (
1028+ modifier =
1029+ modifier
1030+ .minimumInteractiveComponentSize()
1031+ .background(colors.voiceAccentSoft, RoundedCornerShape (14 .dp))
1032+ .border(1 .dp, colors.voiceAccent, RoundedCornerShape (14 .dp))
1033+ .clickable(
1034+ role = Role .Button ,
1035+ onClickLabel = stringResource(R .string.show_new_messages),
1036+ onClick = onClick,
1037+ ).padding(horizontal = 10 .dp, vertical = 5 .dp),
1038+ contentAlignment = Alignment .Center ,
1039+ ) {
1040+ Text (
1041+ text = stringResource(R .string.new_messages) + " ↓" ,
1042+ color = colors.voiceAccent,
1043+ fontSize = 10 .sp,
1044+ fontWeight = FontWeight .Bold ,
1045+ )
1046+ }
1047+ }
1048+
9671049@Composable
9681050private fun WearThreadThinking () {
9691051 val colors = OpenClawWearTheme .colors
@@ -1023,6 +1105,48 @@ internal fun wearThreadContentRevision(
10231105 )
10241106}
10251107
1108+ internal fun wearChatContentRevision (
1109+ sessionId : String? ,
1110+ messages : List <WearChatMessage >,
1111+ streamingText : String? ,
1112+ latestAnchorIndex : Int ,
1113+ ): WearThreadContentRevision =
1114+ WearThreadContentRevision (
1115+ entryCount = messages.size,
1116+ latestEntryId = sessionId,
1117+ latestText =
1118+ buildString {
1119+ append(latestAnchorIndex)
1120+ messages.forEach { message ->
1121+ append(' \u0000 ' )
1122+ append(message.id)
1123+ append(' \u0001 ' )
1124+ append(message.role)
1125+ append(' \u0001 ' )
1126+ append(message.timestamp)
1127+ append(' \u0001 ' )
1128+ append(message.text)
1129+ }
1130+ append(' \u0000 ' )
1131+ append(streamingText)
1132+ },
1133+ latestStreaming = ! streamingText.isNullOrBlank(),
1134+ thinking = false ,
1135+ )
1136+
1137+ internal fun wearChatLatestAnchorIndex (
1138+ visibleMessageCount : Int ,
1139+ hasStreaming : Boolean ,
1140+ canAbort : Boolean ,
1141+ hasAssistant : Boolean ,
1142+ hasFailure : Boolean ,
1143+ ): Int {
1144+ if (visibleMessageCount == 0 && ! hasStreaming) return - 1
1145+ return CHAT_FIXED_ITEM_COUNT +
1146+ visibleMessageCount +
1147+ listOf (canAbort, hasStreaming, hasAssistant, hasFailure).count { it }
1148+ }
1149+
10261150internal fun wearThreadLatestAnchorIndex (
10271151 entryCount : Int ,
10281152 thinking : Boolean ,
@@ -1353,17 +1477,18 @@ private fun ConnectionStateScreen(
13531477@Composable
13541478private fun WearPage (
13551479 pageLabel : String ,
1480+ listState : androidx.wear.compose.foundation.lazy.TransformingLazyColumnState ? = null,
13561481 content : androidx.wear.compose.foundation.lazy.TransformingLazyColumnScope .() -> Unit ,
13571482) {
13581483 val colors = OpenClawWearTheme .colors
1359- val listState = rememberTransformingLazyColumnState()
1360- ScreenScaffold (scrollState = listState ) { contentPadding ->
1484+ val resolvedListState = listState ? : rememberTransformingLazyColumnState()
1485+ ScreenScaffold (scrollState = resolvedListState ) { contentPadding ->
13611486 TransformingLazyColumn (
13621487 modifier =
13631488 Modifier
13641489 .fillMaxSize()
13651490 .background(colors.canvas),
1366- state = listState ,
1491+ state = resolvedListState ,
13671492 contentPadding = contentPadding,
13681493 horizontalAlignment = Alignment .CenterHorizontally ,
13691494 verticalArrangement = Arrangement .spacedBy(8 .dp),
@@ -2062,5 +2187,6 @@ private fun failureDetail(failure: WearConversationFailure?): String =
20622187 -> stringResource(R .string.try_again)
20632188 }
20642189
2190+ private const val CHAT_FIXED_ITEM_COUNT = 4
20652191private const val VISIBLE_MESSAGE_COUNT = 8
20662192private const val VISIBLE_REALTIME_ENTRY_COUNT = 6
0 commit comments