π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Drop the percent sentinel; finish the typed-error edges
Manav Rathicb0be0008a6a
diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.ktindex 6d392e0c92..633dfb8cf7 100644--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/VoiceInput.kt@@ -452,6 +452,8 @@ internal class VoiceTranscriptionController(is TranscriptionException.VadNotDownloaded,is TranscriptionException.Transcribe -> "Voice model could not be loaded."is TranscriptionException.InvalidAudio -> "Could not detect speech in this recording."+ is TranscriptionException.StorageFull ->+ "Not enough storage space. Please free up space and try again."is TranscriptionException.Io -> "Could not transcribe voice input."}diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/DownloadProgressTracker.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/DownloadProgressTracker.ktindex 02d88c8c4b..2f4342b742 100644--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/DownloadProgressTracker.kt+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/DownloadProgressTracker.kt@@ -20,7 +20,7 @@ internal class DownloadProgressTracker(fun resolve(progress: DownloadProgress): ResolvedDownloadProgress {val isLoading = progress.phase == DownloadPhase.Loadingval isFinished = progress.phase == DownloadPhase.Ready- val rawPercent = progress.percent.takeIf { it >= 0 }+ val rawPercent = progress.percentval previousPercent = lastVisiblePercentval previousStatus = lastVisibleStatus@@ -49,7 +49,7 @@ internal class DownloadProgressTracker(percent = resolvedPercent,status = resolvedStatus,phase = progress.phase,- isDownloading = progress.percent >= 0 && !isFinished,+ isDownloading = progress.phase != DownloadPhase.Failed && !isFinished,isFinished = isFinished)}diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.ktindex 89e4fe163e..77d0f6da4f 100644--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt@@ -11,7 +11,7 @@ data class LlmModelTarget()data class DownloadProgress(- val percent: Int,+ val percent: Int?,val status: String,val failure: DownloadFailure? = null,val phase: DownloadPhase = DownloadPhase.Downloadingdiff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.ktindex 376d248a62..b6d67e9832 100644--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt@@ -265,7 +265,7 @@ class LlmProvider("${download.label} download is invalid")return@withContext DownloadProgress(- -1, failure.message, failure, DownloadPhase.Failed+ null, failure.message, failure, DownloadPhase.Failed)}}@@ -274,7 +274,7 @@ class LlmProvider(clearDownloadRecords(target)val failure = downloadFailure(row.reason)return@withContext DownloadProgress(- -1, failure.message, failure, DownloadPhase.Failed+ null, failure.message, failure, DownloadPhase.Failed)}}@@ -471,9 +471,7 @@ class LlmProvider(}} else {emptyPollCount = 0- if (progress.percent < 0) {- throw progress.failure ?: DownloadFailure.Failed(progress.status)- }+ progress.failure?.let { throw it }onProgress(progress)}pollCount += 1diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.ktindex f8fcfefdee..632a79ca4b 100644--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt@@ -1,25 +1,22 @@package io.ente.ensu.llm-import io.ente.ensu.device.isChatSupported-import io.ente.ensu.llm.LlmModelTarget-import io.ente.ensu.llm.LlmProvider-import io.ente.ensu.logging.FileLogRepository+import android.system.ErrnoException+import android.system.OsConstants+import io.ente.ensu.AppStateimport io.ente.ensu.bindings.ConfigDefaultsimport io.ente.ensu.bindings.DownloadErrorimport io.ente.ensu.bindings.LlmException-import android.system.ErrnoException-import android.system.OsConstants+import io.ente.ensu.device.isChatSupported+import io.ente.ensu.logging.FileLogRepositoryimport io.ente.ensu.logging.LogLevelimport io.ente.ensu.settings.SessionPreferencesDataStore-import io.ente.ensu.AppState-import io.ente.ensu.llm.ModelSettingsStateimport kotlinx.coroutines.CoroutineScope-import kotlinx.coroutines.delayimport kotlinx.coroutines.Job+import kotlinx.coroutines.delayimport kotlinx.coroutines.flow.MutableStateFlowimport kotlinx.coroutines.flow.update-import kotlinx.coroutines.launchimport kotlinx.coroutines.isActive+import kotlinx.coroutines.launchinternal class ModelSettingsActions(private val state: MutableStateFlow<AppState>,@@ -112,13 +109,13 @@ internal class ModelSettingsActions(scope.launch {val progress = llmProvider.currentDownloadProgress(target)if (progress != null) {- val isFailure = progress.percent < 0+ val isFailure = progress.phase == DownloadPhase.FailedpersistModelDownloadRequested(!isFailure)state.update { appState ->appState.copy(chat = appState.chat.copy(isDownloading = !isFailure,- downloadPercent = progress.percent.takeIf { it >= 0 },+ downloadPercent = progress.percent,downloadStatus = progress.status,downloadPhase = progress.phase,hasRequestedModelDownload = !isFailure@@ -342,7 +339,7 @@ internal class ModelSettingsActions(continue}emptyPollCount = 0- val isFailure = progress.percent < 0+ val isFailure = progress.phase == DownloadPhase.Failedif (isFailure) {persistModelDownloadRequested(false)}@@ -350,7 +347,7 @@ internal class ModelSettingsActions(appState.copy(chat = appState.chat.copy(isDownloading = !isFailure,- downloadPercent = progress.percent.takeIf { it >= 0 },+ downloadPercent = progress.percent,downloadStatus = progress.status,downloadPhase = progress.phase,hasRequestedModelDownload = if (isFailure) false else appState.chat.hasRequestedModelDownloaddiff --git a/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swiftindex 64676a5ca7..84a1aac6c5 100644--- a/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift+++ b/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift@@ -581,7 +581,7 @@ final class ChatViewModel: ObservableObject {self.isDownloading = falseself.downloadToast = DownloadToastState(phase: .errorDownload,- percent: -1,+ percent: nil,status: self.userFacingModelReadyError(error, wasDownloaded: false),offerRetryDownload: true)@@ -835,7 +835,7 @@ final class ChatViewModel: ObservableObject {await MainActor.run {self.downloadToast = DownloadToastState(phase: .errorDownload,- percent: -1,+ percent: nil,status: self.userFacingModelReadyError(error, wasDownloaded: false),offerRetryDownload: true)@@ -872,7 +872,7 @@ final class ChatViewModel: ObservableObject {await MainActor.run {self.downloadToast = DownloadToastState(phase: .errorLoad,- percent: -1,+ percent: nil,status: self.userFacingModelReadyError(error, wasDownloaded: true),offerRetryDownload: true)@@ -1022,7 +1022,7 @@ final class ChatViewModel: ObservableObject {streamingParentId = nildownloadToast = DownloadToastState(phase: .errorDownload,- percent: -1,+ percent: nil,status: self.userFacingModelReadyError(error, wasDownloaded: self.provider.isModelDownloaded(target: target)),offerRetryDownload: true)@@ -1234,12 +1234,12 @@ final class ChatViewModel: ObservableObject {}private func handleProgress(_ progress: DownloadProgress) {- if progress.percent == -1 {+ if progress.phase == .failed {if modelDownloadLoggedStart {logger.error("Model download failed", details: progress.status)modelDownloadLoggedStart = false}- downloadToast = DownloadToastState(phase: .errorDownload, percent: -1, status: progress.status, offerRetryDownload: true)+ downloadToast = DownloadToastState(phase: .errorDownload, percent: nil, status: progress.status, offerRetryDownload: true)isDownloading = falseisModelDownloaded = falsereturn@@ -1263,14 +1263,14 @@ final class ChatViewModel: ObservableObject {logger.info("Model download complete", details: progress.status)modelDownloadLoggedStart = false}- downloadToast = DownloadToastState(phase: .complete, percent: 100, status: progress.status, offerRetryDownload: false)+ downloadToast = DownloadToastState(phase: .ready, percent: 100, status: progress.status, offerRetryDownload: false)isDownloading = falseisModelDownloaded = truemodelDownloadSizeBytes = nilclearDownloadProgressMemory()Task { @MainActor intry? await Task.sleep(nanoseconds: 1_500_000_000)- if self.downloadToast?.phase == .complete {+ if self.downloadToast?.phase == .ready {self.downloadToast = nil}}@@ -2223,8 +2223,8 @@ private final class DownloadProgressTracker {private var lastVisibleStatus: String?func seed(from toast: DownloadToastState?) {- if let toast, toast.percent >= 0 {- lastVisiblePercent = toast.percent+ if let toast, let percent = toast.percent {+ lastVisiblePercent = percentlastVisibleStatus = toast.status} else {lastVisiblePercent = 0@@ -2240,7 +2240,7 @@ private final class DownloadProgressTracker {func resolve(_ progress: DownloadProgress) -> ResolvedDownloadProgress {let isLoading = progress.phase == .loadinglet isReady = progress.phase == .ready- let rawPercent = progress.percent >= 0 ? progress.percent : nil+ let rawPercent = progress.percentlet previousPercent = lastVisiblePercentlet previousStatus = lastVisibleStatusdiff --git a/mobile/native/apple/apps/ensu/Ensu/Chat/Components/ChatViewComponents.swift b/mobile/native/apple/apps/ensu/Ensu/Chat/Components/ChatViewComponents.swiftindex 9be6133170..0a5d0efd11 100644--- a/mobile/native/apple/apps/ensu/Ensu/Chat/Components/ChatViewComponents.swift+++ b/mobile/native/apple/apps/ensu/Ensu/Chat/Components/ChatViewComponents.swift@@ -21,7 +21,7 @@ struct DownloadOnboardingView: View {if let statusText, isLoadingModel {return statusText}- if let totalBytes, let percent = downloadPercent, percent >= 0 {+ if let totalBytes, let percent = downloadPercent {let clamped = min(max(percent, 0), 100)let downloaded = Int64(Double(totalBytes) * Double(clamped) / 100.0)return "Downloading... \(downloaded.formattedFileSize) / \(totalBytes.formattedFileSize)"@@ -62,7 +62,7 @@ struct DownloadOnboardingView: View {@ViewBuilderprivate var progressView: some View {- if let percent = downloadPercent, percent >= 0 {+ if let percent = downloadPercent {let clamped = min(max(percent, 0), 100)ProgressView(value: Double(clamped), total: 100).progressViewStyle(.linear)@@ -374,7 +374,7 @@ struct ModelProgressIndicator: View {let state: DownloadToastStatevar body: some View {- let clamped = min(max(state.percent, 0), 100)+ let clamped = min(max(state.percent ?? 0, 0), 100)if state.phase == .downloading {ProgressView(value: Double(clamped), total: 100).progressViewStyle(.circular)diff --git a/mobile/native/apple/apps/ensu/Ensu/Chat/Models/ChatModels.swift b/mobile/native/apple/apps/ensu/Ensu/Chat/Models/ChatModels.swiftindex c3172d2262..4f8abfff31 100644--- a/mobile/native/apple/apps/ensu/Ensu/Chat/Models/ChatModels.swift+++ b/mobile/native/apple/apps/ensu/Ensu/Chat/Models/ChatModels.swift@@ -126,14 +126,14 @@ struct DownloadToastState: Identifiable, Equatable {enum Phase {case downloadingcase loading- case complete+ case readycase errorDownloadcase errorLoad}let id = UUID()var phase: Phase- var percent: Int+ var percent: Int?var status: Stringvar offerRetryDownload: Bool}diff --git a/mobile/native/apple/apps/ensu/Ensu/Services/LlmProvider.swift b/mobile/native/apple/apps/ensu/Ensu/Services/LlmProvider.swiftindex e592dcb768..cebdbe5ff4 100644--- a/mobile/native/apple/apps/ensu/Ensu/Services/LlmProvider.swift+++ b/mobile/native/apple/apps/ensu/Ensu/Services/LlmProvider.swift@@ -10,7 +10,7 @@ struct LlmModelTarget: Equatable {}struct DownloadProgress: Equatable {- let percent: Int+ let percent: Int?let status: Stringvar failure: DownloadFailure? = nilvar phase: DownloadPhase = .downloading@@ -576,8 +576,8 @@ final class LlmProvider {}if let progress = await downloadManager.progress(for: managerTargets) {- if progress.percent == -1 {- throw progress.failure ?? DownloadFailure.failed(progress.status)+ if let failure = progress.failure {+ throw failure}onProgress(progress)}diff --git a/mobile/native/apple/apps/ensu/Ensu/Services/ModelDownloadManager.swift b/mobile/native/apple/apps/ensu/Ensu/Services/ModelDownloadManager.swiftindex f1f1be059d..04d4a5d8de 100644--- a/mobile/native/apple/apps/ensu/Ensu/Services/ModelDownloadManager.swift+++ b/mobile/native/apple/apps/ensu/Ensu/Services/ModelDownloadManager.swift@@ -110,7 +110,7 @@ final class ModelDownloadManager: NSObject {if let failed = firstFailure(for: targets) {let failure = failed.failure ?? .failed(failed.errorMessage ?? "Download failed")return DownloadProgress(- percent: -1, status: failure.message, failure: failure, phase: .failed+ percent: nil, status: failure.message, failure: failure, phase: .failed)}return nildiff --git a/rust/apps/ensu/src-tauri/src/commands/llm.rs b/rust/apps/ensu/src-tauri/src/commands/llm.rsindex f18780fd8d..7564f62e1a 100644--- a/rust/apps/ensu/src-tauri/src/commands/llm.rs+++ b/rust/apps/ensu/src-tauri/src/commands/llm.rs@@ -77,7 +77,11 @@ fn download_code(err: &download::Error) -> &'static str {download::Error::Validation(_) => "validation",download::Error::Network(_) => "network",download::Error::StorageFull => "storage_full",- _ => "download",+ download::Error::SizeMismatch { .. } => "size_mismatch",+ download::Error::Protocol(_) => "protocol",+ download::Error::InvalidTarget(_) => "invalid_target",+ download::Error::Io(_) => "io",+ download::Error::Json(_) => "json",}}diff --git a/rust/bindings/uniffi/ensu/src/transcription.rs b/rust/bindings/uniffi/ensu/src/transcription.rsindex 96539c1d62..0d5b1b0e10 100644--- a/rust/bindings/uniffi/ensu/src/transcription.rs+++ b/rust/bindings/uniffi/ensu/src/transcription.rs@@ -18,6 +18,8 @@ pub enum TranscriptionError {Download { error: DownloadError },#[error("{detail}")]Transcribe { detail: String },+ #[error("not enough storage space")]+ StorageFull,#[error("{detail}")]Io { detail: String },}@@ -36,6 +38,7 @@ impl From<transcription::TranscriptionError> for TranscriptionError {transcription::TranscriptionError::Transcribe(message) => {Self::Transcribe { detail: message }}+ transcription::TranscriptionError::StorageFull => Self::StorageFull,transcription::TranscriptionError::Io(err) => Self::Io {detail: err.to_string(),},diff --git a/rust/crates/ensu/src/transcription/mod.rs b/rust/crates/ensu/src/transcription/mod.rsindex 3ec3461afa..baada0d1df 100644--- a/rust/crates/ensu/src/transcription/mod.rs+++ b/rust/crates/ensu/src/transcription/mod.rs@@ -18,8 +18,20 @@ pub enum TranscriptionError {Download(#[from] crate::download::Error),#[error("{0}")]Transcribe(String),+ #[error("not enough storage space")]+ StorageFull,#[error(transparent)]- Io(#[from] std::io::Error),+ Io(std::io::Error),+}++impl From<std::io::Error> for TranscriptionError {+ fn from(err: std::io::Error) -> Self {+ if err.kind() == std::io::ErrorKind::StorageFull {+ Self::StorageFull+ } else {+ Self::Io(err)+ }+ }}impl From<transcribe_rs::TranscribeError> for TranscriptionError {