Skip to content

armgit / ente

Public Imported from GitHub
0

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

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.kt
index 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.kt
index 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.Loading
val isFinished = progress.phase == DownloadPhase.Ready
- val rawPercent = progress.percent.takeIf { it >= 0 }
+ val rawPercent = progress.percent
val previousPercent = lastVisiblePercent
val 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.kt
index 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.Downloading
diff --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.kt
index 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 += 1
diff --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.kt
index 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.AppState
import io.ente.ensu.bindings.ConfigDefaults
import io.ente.ensu.bindings.DownloadError
import io.ente.ensu.bindings.LlmException
-import android.system.ErrnoException
-import android.system.OsConstants
+import io.ente.ensu.device.isChatSupported
+import io.ente.ensu.logging.FileLogRepository
import io.ente.ensu.logging.LogLevel
import io.ente.ensu.settings.SessionPreferencesDataStore
-import io.ente.ensu.AppState
-import io.ente.ensu.llm.ModelSettingsState
import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.delay
import kotlinx.coroutines.Job
+import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
-import kotlinx.coroutines.launch
import kotlinx.coroutines.isActive
+import kotlinx.coroutines.launch
internal 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.Failed
persistModelDownloadRequested(!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.Failed
if (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.hasRequestedModelDownload
diff --git a/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
index 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 = false
self.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 = nil
downloadToast = 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 = false
isModelDownloaded = false
return
@@ -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 = false
isModelDownloaded = true
modelDownloadSizeBytes = nil
clearDownloadProgressMemory()
Task { @MainActor in
try? 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 = percent
lastVisibleStatus = toast.status
} else {
lastVisiblePercent = 0
@@ -2240,7 +2240,7 @@ private final class DownloadProgressTracker {
func resolve(_ progress: DownloadProgress) -> ResolvedDownloadProgress {
let isLoading = progress.phase == .loading
let isReady = progress.phase == .ready
- let rawPercent = progress.percent >= 0 ? progress.percent : nil
+ let rawPercent = progress.percent
let previousPercent = lastVisiblePercent
let previousStatus = lastVisibleStatus
diff --git a/mobile/native/apple/apps/ensu/Ensu/Chat/Components/ChatViewComponents.swift b/mobile/native/apple/apps/ensu/Ensu/Chat/Components/ChatViewComponents.swift
index 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 {
@ViewBuilder
private 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: DownloadToastState
var 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.swift
index 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 downloading
case loading
- case complete
+ case ready
case errorDownload
case errorLoad
}
let id = UUID()
var phase: Phase
- var percent: Int
+ var percent: Int?
var status: String
var offerRetryDownload: Bool
}
diff --git a/mobile/native/apple/apps/ensu/Ensu/Services/LlmProvider.swift b/mobile/native/apple/apps/ensu/Ensu/Services/LlmProvider.swift
index 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: String
var failure: DownloadFailure? = nil
var 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.swift
index 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 nil
diff --git a/rust/apps/ensu/src-tauri/src/commands/llm.rs b/rust/apps/ensu/src-tauri/src/commands/llm.rs
index 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.rs
index 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.rs
index 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 {