Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(ensu): improve mobile chat interruption and keyboard UX

Prateek Sunal7d4e68701877
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/AppViewModel.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/AppViewModel.kt
index 15d933583b..609eb3c37c 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/AppViewModel.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/AppViewModel.kt
@@ -85,11 +85,6 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
}
}
- val email = credentialStore.getEmail()
- if (credentialStore.isLoggedIn() && !email.isNullOrBlank()) {
- store.signIn(email)
- }
-
viewModelScope.launch {
authService.storedEndpointFlow.collectLatest { endpoint ->
authService.updateEndpoint(endpoint)
@@ -105,6 +100,11 @@ class AppViewModel(application: Application) : AndroidViewModel(application) {
store.hydrateModelDownloadRequested(sessionPreferences.modelDownloadRequested.first())
store.bootstrap(viewModelScope)
+ val email = credentialStore.getEmail()
+ if (credentialStore.isLoggedIn() && !email.isNullOrBlank()) {
+ store.signIn(email)
+ }
+
advancedSettingsDataStore.settingsFlow.drop(1).collectLatest { settings ->
store.applyPersistedSettings(
developerSettings = settings.developerSettings,
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
index 0d1f8d68c9..5f9da2c4bb 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
@@ -632,16 +632,18 @@ private fun AssistantMessageBubble(
showMenu = showMenu,
pressOffset = pressOffset,
onDismiss = { showMenu = false },
- actions = listOf(
- MessageAction("Copy", HugeIcons.Copy01Icon) {
+ actions = buildList {
+ add(MessageAction("Copy", HugeIcons.Copy01Icon) {
haptic.perform(HapticFeedbackType.TextHandleMove)
clipboard.setText(AnnotatedString(stripHiddenMessageParts(message.text)))
- },
- MessageAction("Retry", HugeIcons.RepeatIcon) {
- haptic.perform(HapticFeedbackType.TextHandleMove)
- onRetry()
+ })
+ if (!message.isSynthetic) {
+ add(MessageAction("Retry", HugeIcons.RepeatIcon) {
+ haptic.perform(HapticFeedbackType.TextHandleMove)
+ onRetry()
+ })
}
- )
+ }
)
}
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatView.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatView.kt
index cb116f5d4a..6a2aeea4fe 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatView.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatView.kt
@@ -11,11 +11,19 @@ import androidx.compose.animation.togetherWith
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.material.icons.Icons
+import androidx.compose.material.icons.rounded.KeyboardArrowDown
+import androidx.compose.material3.Icon
+import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
@@ -24,7 +32,10 @@ import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
+import androidx.compose.ui.layout.onGloballyPositioned
+import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp
import io.ente.ensu.designsystem.EnsuColor
@@ -54,6 +65,9 @@ fun ChatView(
onOverflowTrim: () -> Unit,
onOverflowCancel: () -> Unit
) {
+ val density = LocalDensity.current
+ var inputBarHeightDp by remember { mutableStateOf(0.dp) }
+
val showDownloadOnboarding by remember(
chatState.isModelDownloaded,
chatState.messages,
@@ -177,7 +191,10 @@ fun ChatView(
modifier = Modifier
.fillMaxWidth()
.navigationBarsPadding()
- .background(EnsuColor.backgroundBase()),
+ .background(EnsuColor.backgroundBase())
+ .onGloballyPositioned { coords ->
+ inputBarHeightDp = with(density) { coords.size.height.toDp() }
+ },
messageText = chatState.messageText,
attachments = chatState.attachments,
editingMessage = editingMessage,
@@ -201,6 +218,31 @@ fun ChatView(
}
}
+ val imeVisible = WindowInsets.ime.getBottom(density) > 0
+ if (imeVisible && inputBarHeightDp > 0.dp) {
+ IconButton(
+ onClick = { focusManager.clearFocus() },
+ modifier = Modifier
+ .align(Alignment.BottomEnd)
+ .imePadding()
+ .padding(
+ end = EnsuSpacing.pageHorizontal.dp,
+ bottom = inputBarHeightDp + EnsuSpacing.sm.dp
+ )
+ .background(
+ color = EnsuColor.fillFaint(),
+ shape = CircleShape
+ )
+ ) {
+ Icon(
+ imageVector = Icons.Rounded.KeyboardArrowDown,
+ contentDescription = "Dismiss keyboard",
+ modifier = Modifier.padding(7.dp),
+ tint = EnsuColor.textPrimary()
+ )
+ }
+ }
+
val status = chatState.downloadStatus
val isLoading = status?.contains("Loading", ignoreCase = true) == true
val showToast by remember(status, chatState.isDownloading, showDownloadOnboarding, isLoading) {
diff --git a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt
index e17a7800d6..3e7793fbab 100644
--- a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt
+++ b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/model/ChatModels.kt
@@ -16,6 +16,7 @@ data class ChatMessage(
val timestampMillis: Long,
val attachments: List<Attachment> = emptyList(),
val isInterrupted: Boolean = false,
+ val isSynthetic: Boolean = false,
val tokensPerSecond: Double? = null,
val branchCount: Int = 1
)
diff --git a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/ChatStoreActions.kt b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/ChatStoreActions.kt
index 258af2166a..e93a2f0eba 100644
--- a/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/ChatStoreActions.kt
+++ b/mobile/native/android/apps/ensu/domain/src/main/kotlin/io/ente/ensu/domain/store/ChatStoreActions.kt
@@ -277,6 +277,7 @@ internal class ChatStoreActions(
fun stopGeneration() {
stopRequested = true
llmProvider.stopGeneration()
+ generationJob?.cancel()
}
fun retryAssistantMessage(messageId: String) {
@@ -936,7 +937,25 @@ internal class ChatStoreActions(
message.copy(branchCount = max(1, siblings.size))
}
- return displayMessages to branchSelectionIndices
+ val finalMessages = if (
+ displayMessages.size >= 2
+ && displayMessages.last().author == MessageAuthor.User
+ && !state.value.chat.isGenerating
+ ) {
+ val lastUser = displayMessages.last()
+ displayMessages + ChatMessage(
+ id = "interrupted-placeholder-${lastUser.id}",
+ sessionId = sessionId,
+ parentId = lastUser.id,
+ author = MessageAuthor.Assistant,
+ text = "Response was interrupted",
+ timestampMillis = lastUser.timestampMillis,
+ isInterrupted = true,
+ isSynthetic = true
+ )
+ } else displayMessages
+
+ return finalMessages to branchSelectionIndices
}
private fun buildSelectedPath(sessionId: String): List<ChatMessage> {
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatView.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatView.swift
index d4c9f1de9f..863a59b864 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatView.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatView.swift
@@ -370,6 +370,28 @@ struct ChatView: View {
.background(EnsuColor.backgroundBase)
.zIndex(2)
}
+
+ #if os(iOS)
+ if keyboard.isVisible {
+ Button {
+ viewState.didDismissKeyboard = true
+ isInputFocused = false
+ hideKeyboard()
+ } label: {
+ Image(systemName: "chevron.down")
+ .font(.system(size: 14, weight: .semibold))
+ .foregroundStyle(EnsuColor.textPrimary)
+ .frame(width: 32, height: 32)
+ .background(.ultraThinMaterial)
+ .clipShape(Circle())
+ .shadow(color: .black.opacity(0.15), radius: 4, y: 2)
+ }
+ .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottomTrailing)
+ .padding(.trailing, EnsuSpacing.pageHorizontal)
+ .padding(.bottom, viewState.inputBarHeight + EnsuSpacing.sm)
+ .zIndex(3)
+ }
+ #endif
}
.animation(.easeInOut(duration: 0.32), value: viewState.sessionTransitionId)
}
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatViewModel.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatViewModel.swift
index b50179f18d..c3b872681d 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatViewModel.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/ChatViewModel.swift
@@ -1170,6 +1170,7 @@ final class ChatViewModel: ObservableObject {
func stopGenerating() {
stopRequested = true
provider.stopGeneration()
+ generationTask?.cancel()
}
func confirmOverflowTrim() {
@@ -2188,11 +2189,26 @@ final class ChatViewModel: ObservableObject {
timestamp: node.timestamp,
attachments: node.attachments,
isInterrupted: node.isInterrupted,
+ isSynthetic: false,
tokensPerSecond: node.tokensPerSecond,
branchIndex: max(1, index + 1),
branchCount: max(1, siblings.count)
)
}
+
+ if messages.count >= 2,
+ let lastMessage = messages.last,
+ lastMessage.role == .user,
+ !isGenerating {
+ messages.append(ChatMessage(
+ id: UUID(),
+ role: .assistant,
+ text: "Response was interrupted",
+ timestamp: lastMessage.timestamp,
+ isInterrupted: true,
+ isSynthetic: true
+ ))
+ }
}
private func buildSelectedPath(for sessionId: UUID, childrenMap: [UUID: [MessageNode]]) -> [MessageNode] {
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageList/MessageListComponents.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageList/MessageListComponents.swift
index 443ba394e0..f9cba5826f 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageList/MessageListComponents.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageList/MessageListComponents.swift
@@ -188,14 +188,16 @@ struct AssistantMessageBubbleView: View {
hapticTap()
onCopy()
}
- Button("Retry") {
- hapticMedium()
- onRetry()
+ if !message.isSynthetic {
+ Button("Retry") {
+ hapticMedium()
+ onRetry()
+ }
}
}
#endif
- if showsMetadata {
+ if showsMetadata && !message.isSynthetic {
HStack(spacing: EnsuSpacing.sm) {
TimestampView(date: message.timestamp)
if message.branchCount > 1 {
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift
index b173c936ae..4ae2ebf36b 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/MessageListView.swift
@@ -285,7 +285,7 @@ struct MessageListView: View {
let fallbackPadding = EnsuSpacing.xxxl + EnsuSpacing.xxl + 60
let measuredPadding = inputBarHeight > 0 ? inputBarHeight + EnsuSpacing.md : fallbackPadding
let basePadding = max(fallbackPadding, measuredPadding)
- return basePadding
+ return basePadding + keyboardHeight
}
private func scheduleInitialScroll(_ proxy: ScrollViewProxy) {
diff --git a/mobile/native/darwin/Apps/ensu/ensu/Chat/Models/ChatModels.swift b/mobile/native/darwin/Apps/ensu/ensu/Chat/Models/ChatModels.swift
index d1630ae32a..9a33caca4a 100644
--- a/mobile/native/darwin/Apps/ensu/ensu/Chat/Models/ChatModels.swift
+++ b/mobile/native/darwin/Apps/ensu/ensu/Chat/Models/ChatModels.swift
@@ -74,6 +74,7 @@ struct ChatMessage: Identifiable, Equatable {
var timestamp: Date
var attachments: [ChatAttachment]
var isInterrupted: Bool
+ var isSynthetic: Bool
var tokensPerSecond: Double?
var branchIndex: Int
var branchCount: Int
@@ -85,6 +86,7 @@ struct ChatMessage: Identifiable, Equatable {
timestamp: Date = Date(),
attachments: [ChatAttachment] = [],
isInterrupted: Bool = false,
+ isSynthetic: Bool = false,
tokensPerSecond: Double? = nil,
branchIndex: Int = 1,
branchCount: Int = 1
@@ -95,6 +97,7 @@ struct ChatMessage: Identifiable, Equatable {
self.timestamp = timestamp
self.attachments = attachments
self.isInterrupted = isInterrupted
+ self.isSynthetic = isSynthetic
self.tokensPerSecond = tokensPerSecond
self.branchIndex = branchIndex
self.branchCount = branchCount