π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix pending sends and idle model cleanup on mobile
setalp77de98de77fc
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.ktindex a7e7b5b3fa..22e2b27815 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt@@ -96,8 +96,10 @@ class LlmProvider(): Unit = withModelContext {modelLoadMutex.withLock {currentCoroutineContext().ensureActive()- if (loadedContextLength(selection) != null || !isChatModelReady(selection))- return@withLock+ if (!isChatModelReady(selection)) return@withLock+ unloadTranscriptionModelIfLoaded()+ currentCoroutineContext().ensureActive()+ if (loadedContextLength(selection) != null) return@withLocktry {ensureModelReadyLocked(selection, {}, allowRecovery = false, shouldDownload = false)currentCoroutineContext().ensureActive()diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.ktindex bc37e2affc..9fb9e0dd57 100644--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt@@ -389,8 +389,10 @@ internal class ModelSettingsActions(latest.chat.isDownloading ||latest.knowledge.packs.values.any { it.enabled } ||notesStore.state.value.collections.isNotEmpty()- )+ ) {+ if (warmupOwner == owner) cancelChatWarmup()return@launch+ }llmProvider.prewarmChatModelIfDownloaded(selection, owner)} catch (error: CancellationException) {throw errordiff --git a/apple/apps/ensu/Ensu/Chat/ChatView.swift b/apple/apps/ensu/Ensu/Chat/ChatView.swiftindex 0bad160168..91b2509e77 100644--- a/apple/apps/ensu/Ensu/Chat/ChatView.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatView.swift@@ -342,6 +342,7 @@ struct ChatView: View {text: $viewModel.draftText,attachments: $viewModel.draftAttachments,isGenerating: viewModel.isGenerating,+ isSendPending: viewModel.isSendPending,isDownloading: viewModel.isDownloading,editingMessage: editingMessage,isProcessingAttachments: viewModel.isProcessingAttachments,diff --git a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swiftindex 2e5bea7bd1..5e3e605fb2 100644--- a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift+++ b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift@@ -73,6 +73,7 @@ final class ChatViewModel: ObservableObject {return streamingParentId}@Published var isGenerating: Bool = false+ @Published private(set) var isSendPending: Bool = false@Published var isDownloading: Bool = false@Published var isProcessingAttachments: Bool = false@Published var draftText: String = ""@@ -117,6 +118,8 @@ final class ChatViewModel: ObservableObject {private var reloadTask: Task<Void, Never>?private let rootId = UUID(uuid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))private var generationTask: Task<Void, Never>?+ private var pendingSendTask: Task<Void, Never>?+ private var pendingSendId: UUID?private var modelDownloadTask: Task<Void, Never>?private var voiceTransientErrorTask: Task<Void, Never>?private var sharedModelReadyTask: Task<Void, Error>?@@ -293,7 +296,10 @@ final class ChatViewModel: ObservableObject {guard !isGenerating, !isDownloading, !voiceInputState.isWorking,!voiceTranscriber.hasActiveTasks,knowledgeStore.packs.allSatisfy({ !$0.enabled }), notesStore.collections.isEmpty- else { return }+ else {+ if warmupAttempt?.owner == owner { cancelChatWarmup() }+ return+ }try await provider.prewarmChatModelIfDownloaded(selection, owner: owner)} catch {guard !Task.isCancelled, !isCancellation(error), warmupAttempt?.owner == owner,@@ -326,6 +332,7 @@ final class ChatViewModel: ObservableObject {knowledgeStore.downloadsAllowed = capability.isChatSupportedlogger.info("Chat device capability evaluated", details: "\(capability)")guard !capability.isChatSupported else { return }+ cancelPendingSend()showUnsupportedDeviceDialog = trueisDownloading = falsedownloadToast = nil@@ -382,6 +389,7 @@ final class ChatViewModel: ObservableObject {}private func resetGenerationState(stopRequested: Bool = false) {+ cancelPendingSend()conversationStatus = nilgenerationTask?.cancel()provider.stopGeneration(invalidatePreparation: true)@@ -479,6 +487,7 @@ final class ChatViewModel: ObservableObject {}func cancelEditing() {+ cancelPendingSend()discardUnstoredAttachments(draftAttachments)editingMessageId = nildraftText = ""@@ -692,27 +701,56 @@ final class ChatViewModel: ObservableObject {}}+ private func cancelPendingSend() {+ guard isSendPending else { return }+ pendingSendTask?.cancel()+ clearPendingSend()+ }++ private func clearPendingSend() {+ pendingSendTask = nil+ pendingSendId = nil+ isSendPending = false+ isDownloading = false+ if downloadToast?.phase == .loading || downloadToast?.phase == .downloading {+ downloadToast = nil+ clearDownloadProgressMemory()+ }+ }+func sendDraft() {- guard !isGenerating && !isDownloading else { return }+ guard !isGenerating && !isDownloading && !isSendPending else { return }guard !isChatUnsupported else {showUnsupportedDeviceDialog = truereturn}- let trimmed = draftText.trimmingCharacters(in: .whitespacesAndNewlines)+ let text = draftText+ let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)let attachments = draftAttachmentsguard !trimmed.isEmpty || !attachments.isEmpty else { return }- Task { @MainActor in- let selection = self.modelSettings.currentSelection()+ let sessionId = currentSessionId+ let editingId = editingMessageId+ let selection = modelSettings.currentSelection()+ let sendId = UUID()+ pendingSendId = sendId+ isSendPending = true+ pendingSendTask = Task { @MainActor in+ defer {+ if self.pendingSendId == sendId {+ self.clearPendingSend()+ }+ }+ guard !Task.isCancelled, self.pendingSendId == sendId else { return }self.hasRequestedModelDownload = truedo {- try await self.ensureChatModelReady(selection)+ try await self.ensureChatModelReady(selection, pendingSendId: sendId)} catch {- if self.isCancellation(error) {- return- }+ guard !Task.isCancelled, self.pendingSendId == sendId,+ !self.isCancellation(error)+ else { return }self.isDownloading = falseself.downloadToast = DownloadToastState(phase: .errorDownload,@@ -725,8 +763,14 @@ final class ChatViewModel: ObservableObject {return}- let sessionId = self.currentSessionId ?? self.createSessionForDraft()- self.sendDraftMessage(trimmed: trimmed, attachments: attachments, sessionId: sessionId)+ guard !Task.isCancelled, self.pendingSendId == sendId,+ self.currentSessionId == sessionId, self.editingMessageId == editingId,+ self.draftText == text, self.draftAttachments == attachments,+ self.modelSettings.currentSelection() == selection+ else { return }+ let destination = sessionId ?? self.createSessionForDraft()+ self.sendDraftMessage(+ trimmed: trimmed, attachments: attachments, sessionId: destination)}}@@ -880,18 +924,23 @@ final class ChatViewModel: ObservableObject {ModelReadyKey(id: selection.id, requestedContextLength: selection.contextLength)}- private func ensureChatModelReady(_ selection: LlmModelSelection) async throws {+ private func ensureChatModelReady(+ _ selection: LlmModelSelection, pendingSendId: UUID? = nil+ ) async throws {if isChatUnsupported {throw UnsupportedDeviceMemoryError(capability: deviceCapability)}var retryCount = 0while true {do {+ try Task.checkCancellation()try await provider.ensureModelReady(selection) { progress inTask { @MainActor in+ if let pendingSendId, self.pendingSendId != pendingSendId { return }self.handleProgress(progress)}}+ try Task.checkCancellation()return} catch {if isCancellation(error) || !shouldRetryModelDownload(error, retryCount: retryCount)@@ -1070,6 +1119,7 @@ final class ChatViewModel: ObservableObject {userNode = messageStore[sessionId]?.first(where: { $0.id == parentId })}guard let userNode else { return }+ cancelPendingSend()priorGeneration?.cancel()priorSummary?.cancel()provider.stopGeneration()@@ -1083,6 +1133,7 @@ final class ChatViewModel: ObservableObject {}func changeBranch(for message: RenderedChatMessage, delta: Int) {+ cancelPendingSend()if isGenerating {stopGenerating()}@@ -1153,6 +1204,7 @@ final class ChatViewModel: ObservableObject {defer {notesScope.close()settleGenerationIfActive(generationId: generationId, sessionId: userNode.sessionId)+ refreshChatWarmup()}await notesStore.awaitMaintenance()_ = await priorGeneration?.resultdiff --git a/apple/apps/ensu/Ensu/Chat/MessageInputView.swift b/apple/apps/ensu/Ensu/Chat/MessageInputView.swiftindex 09f08dcc17..f54dbfb662 100644--- a/apple/apps/ensu/Ensu/Chat/MessageInputView.swift+++ b/apple/apps/ensu/Ensu/Chat/MessageInputView.swift@@ -7,6 +7,7 @@ struct MessageInputView: View {@Binding var text: String@Binding var attachments: [ChatAttachment]let isGenerating: Bool+ let isSendPending: Boollet isDownloading: Boollet editingMessage: RenderedChatMessage?let isProcessingAttachments: Bool@@ -30,15 +31,13 @@ struct MessageInputView: View {private let placeholder = "Write a message..."private var canSend: Bool {- let hasContent =- !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || !attachments.isEmpty- return hasContent && !isGenerating && !isDownloading && !voiceInputState.blocksSend+ !isGenerating && isSendEnabled}private var isSendEnabled: Bool {let hasContent =!text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || !attachments.isEmpty- return hasContent && !isDownloading && !voiceInputState.blocksSend+ return hasContent && !isDownloading && !isSendPending && !voiceInputState.blocksSend}private var isImageAttachmentLimitReached: Bool {diff --git a/apple/apps/ensu/Ensu/Services/LlmProvider.swift b/apple/apps/ensu/Ensu/Services/LlmProvider.swiftindex 6fe9cfeb85..ce9fad7175 100644--- a/apple/apps/ensu/Ensu/Services/LlmProvider.swift+++ b/apple/apps/ensu/Ensu/Services/LlmProvider.swift@@ -250,7 +250,10 @@ actor LlmProvider {_ selection: LlmModelSelection, owner: UUID) async throws {try await withModelLock {- guard loadedContextLength(selection) == nil, isChatModelReady(selection) else { return }+ guard isChatModelReady(selection) else { return }+ unloadTranscriptionModelIfLoaded()+ try Task.checkCancellation()+ guard loadedContextLength(selection) == nil else { return }do {try await ensureModelReadyLocked(selection, onProgress: { _ in }, allowRecovery: false, shouldDownload: false)