@@ -44,6 +44,7 @@ import androidx.compose.runtime.mutableStateOf
4444import androidx.compose.runtime.remember
4545import androidx.compose.runtime.rememberCoroutineScope
4646import androidx.compose.runtime.setValue
47+ import androidx.compose.runtime.snapshotFlow
4748import androidx.compose.ui.Alignment
4849import androidx.compose.ui.Modifier
4950import androidx.compose.ui.geometry.CornerRadius
@@ -73,6 +74,7 @@ import androidx.wear.compose.material3.HorizontalPagerScaffold
7374import androidx.wear.compose.material3.ScreenScaffold
7475import androidx.wear.compose.material3.Text
7576import kotlinx.coroutines.delay
77+ import kotlinx.coroutines.flow.collect
7678import kotlinx.coroutines.isActive
7779import kotlinx.coroutines.launch
7880
@@ -430,6 +432,8 @@ private fun VoicePage(
430432 else ->
431433 ThreadVoiceMode (
432434 conversation = realtimeTalk.conversation,
435+ thinking =
436+ realtimeThinkingOverride || realtimeTalk.status == WearRealtimeTalkStatus .THINKING ,
433437 realtimeActive = realtimeTalk.active || realtimeCapturing,
434438 actionBusy = actionBusy,
435439 inputEnabled = inputEnabled,
@@ -695,6 +699,7 @@ private fun VoiceGestureLabel(
695699@Composable
696700private fun ThreadVoiceMode (
697701 conversation : List <WearRealtimeTalkEntry >,
702+ thinking : Boolean ,
698703 realtimeActive : Boolean ,
699704 actionBusy : Boolean ,
700705 inputEnabled : Boolean ,
@@ -703,6 +708,40 @@ private fun ThreadVoiceMode(
703708) {
704709 val colors = OpenClawWearTheme .colors
705710 val listState = rememberTransformingLazyColumnState()
711+ val coroutineScope = rememberCoroutineScope()
712+ val visibleConversation = conversation.takeLast(VISIBLE_REALTIME_ENTRY_COUNT )
713+ val contentRevision = wearThreadContentRevision(visibleConversation, thinking)
714+ val latestAnchorIndex = wearThreadLatestAnchorIndex(visibleConversation.size, thinking)
715+ var followState by remember { mutableStateOf(WearThreadFollowState ()) }
716+
717+ LaunchedEffect (listState) {
718+ snapshotFlow {
719+ WearThreadViewport (
720+ atLatest = ! listState.canScrollForward,
721+ scrollingBackward = listState.isScrollInProgress && listState.lastScrolledBackward,
722+ )
723+ }.collect { viewport ->
724+ followState =
725+ nextWearThreadFollowForViewport(
726+ state = followState,
727+ atLatest = viewport.atLatest,
728+ scrollingBackward = viewport.scrollingBackward,
729+ )
730+ }
731+ }
732+ LaunchedEffect (realtimeActive, contentRevision) {
733+ val update =
734+ nextWearThreadFollowForContent(
735+ state = followState,
736+ contentRevision = contentRevision,
737+ realtimeActive = realtimeActive,
738+ )
739+ followState = update.state
740+ if (update.scrollToLatest && latestAnchorIndex >= 0 ) {
741+ listState.requestScrollToItem(latestAnchorIndex)
742+ }
743+ }
744+
706745 Box (modifier = Modifier .fillMaxSize()) {
707746 TransformingLazyColumn (
708747 modifier =
@@ -714,7 +753,7 @@ private fun ThreadVoiceMode(
714753 horizontalAlignment = Alignment .CenterHorizontally ,
715754 verticalArrangement = Arrangement .spacedBy(7 .dp),
716755 ) {
717- if (conversation .isEmpty()) {
756+ if (visibleConversation .isEmpty() && ! thinking ) {
718757 item {
719758 Text (
720759 text = stringResource(R .string.no_live_conversation),
@@ -726,13 +765,46 @@ private fun ThreadVoiceMode(
726765 )
727766 }
728767 } else {
729- conversation
730- .takeLast(VISIBLE_REALTIME_ENTRY_COUNT )
731- .forEach { entry ->
732- item(key = entry.id) {
733- RealtimeTalkBubble (entry)
734- }
768+ visibleConversation.forEach { entry ->
769+ item(key = entry.id) {
770+ RealtimeTalkBubble (entry)
771+ }
772+ }
773+ if (thinking) {
774+ item(key = " realtime-thinking" ) {
775+ WearThreadThinking ()
735776 }
777+ }
778+ // Follow a trailing anchor: centering a growing bubble can hide its newly streamed tail.
779+ item(key = " realtime-thread-end" ) {
780+ Spacer (modifier = Modifier .height(1 .dp))
781+ }
782+ }
783+ }
784+ if (followState.hasNewContent) {
785+ Box (
786+ modifier =
787+ Modifier
788+ .align(Alignment .BottomCenter )
789+ .padding(bottom = 44 .dp)
790+ .background(colors.voiceAccentSoft, RoundedCornerShape (14 .dp))
791+ .border(1 .dp, colors.voiceAccent, RoundedCornerShape (14 .dp))
792+ .clickable(role = Role .Button ) {
793+ followState = wearThreadFollowLatest(followState)
794+ if (latestAnchorIndex >= 0 ) {
795+ coroutineScope.launch {
796+ listState.animateScrollToItem(latestAnchorIndex)
797+ }
798+ }
799+ }.padding(horizontal = 10 .dp, vertical = 5 .dp),
800+ contentAlignment = Alignment .Center ,
801+ ) {
802+ Text (
803+ text = " ${stringResource(R .string.new_messages)} ↓" ,
804+ color = colors.voiceAccent,
805+ fontSize = 9 .sp,
806+ fontWeight = FontWeight .Bold ,
807+ )
736808 }
737809 }
738810 Row (
@@ -796,6 +868,107 @@ private fun ThreadVoiceMode(
796868 }
797869}
798870
871+ @Composable
872+ private fun WearThreadThinking () {
873+ val colors = OpenClawWearTheme .colors
874+ Box (
875+ modifier =
876+ Modifier
877+ .fillMaxWidth()
878+ .padding(start = 12 .dp, end = 28 .dp)
879+ .background(colors.surfaceRaised, RoundedCornerShape (14 .dp))
880+ .border(1 .dp, colors.borderStrong, RoundedCornerShape (14 .dp))
881+ .padding(horizontal = 12 .dp, vertical = 8 .dp),
882+ ) {
883+ Text (
884+ text = " ${stringResource(R .string.thinking)} …" ,
885+ color = colors.textMuted,
886+ fontSize = 10 .sp,
887+ fontWeight = FontWeight .SemiBold ,
888+ )
889+ }
890+ }
891+
892+ internal data class WearThreadContentRevision (
893+ val entryCount : Int ,
894+ val latestEntryId : String? ,
895+ val latestText : String? ,
896+ val latestStreaming : Boolean ,
897+ val thinking : Boolean ,
898+ )
899+
900+ internal data class WearThreadFollowState (
901+ val contentRevision : WearThreadContentRevision ? = null ,
902+ val followingLatest : Boolean = true ,
903+ val hasNewContent : Boolean = false ,
904+ )
905+
906+ internal data class WearThreadFollowUpdate (
907+ val state : WearThreadFollowState ,
908+ val scrollToLatest : Boolean ,
909+ )
910+
911+ private data class WearThreadViewport (
912+ val atLatest : Boolean ,
913+ val scrollingBackward : Boolean ,
914+ )
915+
916+ internal fun wearThreadContentRevision (
917+ conversation : List <WearRealtimeTalkEntry >,
918+ thinking : Boolean ,
919+ ): WearThreadContentRevision {
920+ val latest = conversation.lastOrNull()
921+ return WearThreadContentRevision (
922+ entryCount = conversation.size,
923+ latestEntryId = latest?.id,
924+ latestText = latest?.text,
925+ latestStreaming = latest?.streaming == true ,
926+ thinking = thinking,
927+ )
928+ }
929+
930+ internal fun wearThreadLatestAnchorIndex (
931+ entryCount : Int ,
932+ thinking : Boolean ,
933+ ): Int = if (entryCount == 0 && ! thinking) - 1 else entryCount + if (thinking) 1 else 0
934+
935+ internal fun nextWearThreadFollowForContent (
936+ state : WearThreadFollowState ,
937+ contentRevision : WearThreadContentRevision ,
938+ realtimeActive : Boolean = true,
939+ ): WearThreadFollowUpdate {
940+ if (! realtimeActive) {
941+ return WearThreadFollowUpdate (
942+ state = WearThreadFollowState (),
943+ scrollToLatest = false ,
944+ )
945+ }
946+ if (state.contentRevision == contentRevision) {
947+ return WearThreadFollowUpdate (state = state, scrollToLatest = false )
948+ }
949+ return WearThreadFollowUpdate (
950+ state =
951+ state.copy(
952+ contentRevision = contentRevision,
953+ hasNewContent = ! state.followingLatest,
954+ ),
955+ scrollToLatest = state.followingLatest,
956+ )
957+ }
958+
959+ internal fun nextWearThreadFollowForViewport (
960+ state : WearThreadFollowState ,
961+ atLatest : Boolean ,
962+ scrollingBackward : Boolean ,
963+ ): WearThreadFollowState =
964+ when {
965+ atLatest -> state.copy(followingLatest = true , hasNewContent = false )
966+ scrollingBackward -> state.copy(followingLatest = false )
967+ else -> state
968+ }
969+
970+ internal fun wearThreadFollowLatest (state : WearThreadFollowState ): WearThreadFollowState = state.copy(followingLatest = true , hasNewContent = false )
971+
799972@Composable
800973private fun VoiceOrb (
801974 accent : Color ,
0 commit comments