Skip to content

armgit / ente

Public Imported from GitHub
0

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

Prefix Ensu db binding types and split out the image module

Manav Rathi7dffa9ad2447
diff --git a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/RustChatRepository.kt b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/RustChatRepository.kt
index c19729c6b4..bf186fce23 100644
--- a/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/RustChatRepository.kt
+++ b/mobile/native/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/RustChatRepository.kt
@@ -9,9 +9,9 @@ import io.ente.ensu.chat.ChatMessage
import io.ente.ensu.chat.ChatSession
import io.ente.ensu.chat.MessageAuthor
import io.ente.ensu.chat.sessionTitleFromText
-import io.ente.ensu.bindings.AttachmentKind
-import io.ente.ensu.bindings.AttachmentMeta
-import io.ente.ensu.bindings.Sender
+import io.ente.ensu.bindings.DbAttachmentKind
+import io.ente.ensu.bindings.DbAttachmentMeta
+import io.ente.ensu.bindings.DbSender
import io.ente.ensu.bindings.EnsuDb
import io.ente.ensu.bindings.DbException
import java.io.File
@@ -66,8 +66,8 @@ class RustChatRepository(
sessionId = message.sessionUuid,
parentId = message.parentMessageUuid,
author = when (message.sender) {
- Sender.SELF_USER -> MessageAuthor.User
- Sender.OTHER -> MessageAuthor.Assistant
+ DbSender.SELF_USER -> MessageAuthor.User
+ DbSender.OTHER -> MessageAuthor.Assistant
},
text = message.text,
timestampMillis = message.createdAtUs / 1000,
@@ -77,8 +77,8 @@ class RustChatRepository(
name = meta.name,
sizeBytes = meta.size,
type = when (meta.kind) {
- AttachmentKind.IMAGE -> AttachmentType.Image
- AttachmentKind.DOCUMENT -> AttachmentType.Document
+ DbAttachmentKind.IMAGE -> AttachmentType.Image
+ DbAttachmentKind.DOCUMENT -> AttachmentType.Document
},
localPath = File(attachmentsDir, meta.id).absolutePath,
isUploading = false
@@ -96,11 +96,11 @@ class RustChatRepository(
attachments: List<Attachment>
): ChatMessage = withDbRecovery {
val meta = attachments.map { att ->
- AttachmentMeta(
+ DbAttachmentMeta(
id = att.id,
kind = when (att.type) {
- AttachmentType.Image -> AttachmentKind.IMAGE
- AttachmentType.Document -> AttachmentKind.DOCUMENT
+ AttachmentType.Image -> DbAttachmentKind.IMAGE
+ AttachmentType.Document -> DbAttachmentKind.DOCUMENT
},
size = att.sizeBytes,
name = att.name
@@ -110,9 +110,9 @@ class RustChatRepository(
val message = db.insertMessage(
sessionUuid = sessionId,
sender = if (author == MessageAuthor.User) {
- Sender.SELF_USER
+ DbSender.SELF_USER
} else {
- Sender.OTHER
+ DbSender.OTHER
},
text = text,
parentMessageUuid = parentId,
diff --git a/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
index 5ba9d0bd94..afc64092e9 100644
--- a/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
+++ b/mobile/native/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
@@ -1,8 +1,6 @@
import Foundation
import SwiftUI
-typealias LlmChatSession = Session
-
@MainActor
final class ChatViewModel: ObservableObject {
private struct ModelReadyKey: Equatable {
@@ -610,8 +608,8 @@ final class ChatViewModel: ObservableObject {
return buildSelectedPath(for: sessionId, childrenMap: childrenMap).last?.id
}()
- let meta: [AttachmentMeta] = attachments.map { attachment in
- AttachmentMeta(
+ let meta: [DbAttachmentMeta] = attachments.map { attachment in
+ DbAttachmentMeta(
id: attachment.id.uuidString.lowercased(),
kind: attachment.kind == .image ? .image : .document,
size: attachment.size,
@@ -1169,7 +1167,7 @@ final class ChatViewModel: ObservableObject {
}()
if isActiveGeneration {
- let meta: [AttachmentMeta] = []
+ let meta: [DbAttachmentMeta] = []
do {
let inserted = try chatDb.insertMessage(
sessionUuid: parent.sessionId.uuidString,
@@ -1501,7 +1499,7 @@ final class ChatViewModel: ObservableObject {
}
private nonisolated static func buildSessions(
- from loaded: [LlmChatSession],
+ from loaded: [DbSession],
chatDb: EnsuDb,
summaries: [String: String]
) -> [ChatSession] {
diff --git a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs b/rust/apps/ensu/src-tauri/src/commands/chat_db.rs
index 1618b9808d..cec3404f94 100644
--- a/rust/apps/ensu/src-tauri/src/commands/chat_db.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/chat_db.rs
@@ -58,7 +58,6 @@ impl From<DbError> for ApiError {
E::Sqlite(_) => "db_sqlite",
E::UnsupportedOperation(_) => "db_unsupported_operation",
E::Migration(_) => "db_migration",
- E::Image(_) => "db_image",
};
ApiError::new(code, e.to_string())
@@ -640,7 +639,8 @@ pub async fn chat_db_compress_attachment_image_file(path: String) -> Result<Vec<
let data = fs::read(&path).map_err(|err| {
ApiError::new("io", format!("failed to read image file '{path}': {err}"))
})?;
- db::compress_attachment_image(&data).map_err(|err| ApiError::new("image", err.to_string()))
+ ente_ensu::image::compress_attachment_image(&data)
+ .map_err(|err| ApiError::new("image", err.to_string()))
})
.await
.map_err(|_| image_thread_error())?
diff --git a/rust/bindings/uniffi/ensu/src/db.rs b/rust/bindings/uniffi/ensu/src/db.rs
index 9bce437400..2d00e837c4 100644
--- a/rust/bindings/uniffi/ensu/src/db.rs
+++ b/rust/bindings/uniffi/ensu/src/db.rs
@@ -15,7 +15,7 @@ impl From<core::Error> for DbError {
}
#[derive(Debug, Clone, uniffi::Record)]
-pub struct Session {
+pub struct DbSession {
pub uuid: String,
pub title: String,
pub created_at_us: i64,
@@ -26,54 +26,54 @@ pub struct Session {
}
#[derive(Debug, Clone, uniffi::Enum)]
-pub enum Sender {
+pub enum DbSender {
SelfUser,
Other,
}
-impl Sender {
+impl DbSender {
fn as_str(&self) -> &'static str {
match self {
- Sender::SelfUser => "self",
- Sender::Other => "other",
+ DbSender::SelfUser => "self",
+ DbSender::Other => "other",
}
}
}
#[derive(Debug, Clone, uniffi::Enum)]
-pub enum AttachmentKind {
+pub enum DbAttachmentKind {
Image,
Document,
}
-impl From<AttachmentKind> for core::AttachmentKind {
- fn from(value: AttachmentKind) -> Self {
+impl From<DbAttachmentKind> for core::AttachmentKind {
+ fn from(value: DbAttachmentKind) -> Self {
match value {
- AttachmentKind::Image => core::AttachmentKind::Image,
- AttachmentKind::Document => core::AttachmentKind::Document,
+ DbAttachmentKind::Image => core::AttachmentKind::Image,
+ DbAttachmentKind::Document => core::AttachmentKind::Document,
}
}
}
-impl From<core::AttachmentKind> for AttachmentKind {
+impl From<core::AttachmentKind> for DbAttachmentKind {
fn from(value: core::AttachmentKind) -> Self {
match value {
- core::AttachmentKind::Image => AttachmentKind::Image,
- core::AttachmentKind::Document => AttachmentKind::Document,
+ core::AttachmentKind::Image => DbAttachmentKind::Image,
+ core::AttachmentKind::Document => DbAttachmentKind::Document,
}
}
}
#[derive(Debug, Clone, uniffi::Record)]
-pub struct AttachmentMeta {
+pub struct DbAttachmentMeta {
pub id: String,
- pub kind: AttachmentKind,
+ pub kind: DbAttachmentKind,
pub size: i64,
pub name: String,
}
-impl From<AttachmentMeta> for core::AttachmentMeta {
- fn from(value: AttachmentMeta) -> Self {
+impl From<DbAttachmentMeta> for core::AttachmentMeta {
+ fn from(value: DbAttachmentMeta) -> Self {
core::AttachmentMeta {
id: value.id,
kind: value.kind.into(),
@@ -83,9 +83,9 @@ impl From<AttachmentMeta> for core::AttachmentMeta {
}
}
-impl From<core::AttachmentMeta> for AttachmentMeta {
+impl From<core::AttachmentMeta> for DbAttachmentMeta {
fn from(value: core::AttachmentMeta) -> Self {
- AttachmentMeta {
+ DbAttachmentMeta {
id: value.id,
kind: value.kind.into(),
size: value.size,
@@ -95,13 +95,13 @@ impl From<core::AttachmentMeta> for AttachmentMeta {
}
#[derive(Debug, Clone, uniffi::Record)]
-pub struct Message {
+pub struct DbMessage {
pub uuid: String,
pub session_uuid: String,
pub parent_message_uuid: Option<String>,
- pub sender: Sender,
+ pub sender: DbSender,
pub text: String,
- pub attachments: Vec<AttachmentMeta>,
+ pub attachments: Vec<DbAttachmentMeta>,
pub created_at_us: i64,
pub deleted_at_us: Option<i64>,
}
@@ -111,8 +111,8 @@ pub struct EnsuDb {
inner: core::Db<core::SqliteBackend>,
}
-fn to_session(session: core::Session) -> Session {
- Session {
+fn to_session(session: core::Session) -> DbSession {
+ DbSession {
uuid: session.uuid.to_string(),
title: session.title,
created_at_us: session.created_at,
@@ -123,14 +123,14 @@ fn to_session(session: core::Session) -> Session {
}
}
-fn to_message(message: core::Message) -> Message {
- Message {
+fn to_message(message: core::Message) -> DbMessage {
+ DbMessage {
uuid: message.uuid.to_string(),
session_uuid: message.session_uuid.to_string(),
parent_message_uuid: message.parent_message_uuid.map(|v| v.to_string()),
sender: match message.sender {
- core::Sender::SelfUser => Sender::SelfUser,
- core::Sender::Other => Sender::Other,
+ core::Sender::SelfUser => DbSender::SelfUser,
+ core::Sender::Other => DbSender::Other,
},
text: message.text,
attachments: message.attachments.into_iter().map(Into::into).collect(),
@@ -139,11 +139,6 @@ fn to_message(message: core::Message) -> Message {
}
}
-#[uniffi::export]
-pub fn compress_attachment_image(data: Vec<u8>) -> Result<Vec<u8>, DbError> {
- Ok(core::compress_attachment_image(&data)?)
-}
-
#[uniffi::export]
impl EnsuDb {
#[uniffi::constructor]
@@ -156,11 +151,11 @@ impl EnsuDb {
Ok(Self { inner })
}
- pub fn create_session(&self, title: String) -> Result<Session, DbError> {
+ pub fn create_session(&self, title: String) -> Result<DbSession, DbError> {
Ok(to_session(self.inner.create_session(&title)?))
}
- pub fn list_sessions(&self) -> Result<Vec<Session>, DbError> {
+ pub fn list_sessions(&self) -> Result<Vec<DbSession>, DbError> {
Ok(self
.inner
.list_sessions()?
@@ -169,7 +164,7 @@ impl EnsuDb {
.collect())
}
- pub fn get_session(&self, uuid: String) -> Result<Option<Session>, DbError> {
+ pub fn get_session(&self, uuid: String) -> Result<Option<DbSession>, DbError> {
let uuid = Uuid::parse_str(&uuid).map_err(|e| DbError::Message(e.to_string()))?;
Ok(self.inner.get_session(uuid)?.map(to_session))
}
@@ -187,11 +182,11 @@ impl EnsuDb {
pub fn insert_message(
&self,
session_uuid: String,
- sender: Sender,
+ sender: DbSender,
text: String,
parent_message_uuid: Option<String>,
- attachments: Vec<AttachmentMeta>,
- ) -> Result<Message, DbError> {
+ attachments: Vec<DbAttachmentMeta>,
+ ) -> Result<DbMessage, DbError> {
let session_uuid =
Uuid::parse_str(&session_uuid).map_err(|e| DbError::Message(e.to_string()))?;
let parent = parent_message_uuid
@@ -209,7 +204,7 @@ impl EnsuDb {
Ok(to_message(message))
}
- pub fn get_messages(&self, session_uuid: String) -> Result<Vec<Message>, DbError> {
+ pub fn get_messages(&self, session_uuid: String) -> Result<Vec<DbMessage>, DbError> {
let session_uuid =
Uuid::parse_str(&session_uuid).map_err(|e| DbError::Message(e.to_string()))?;
Ok(self
diff --git a/rust/bindings/uniffi/ensu/src/image.rs b/rust/bindings/uniffi/ensu/src/image.rs
new file mode 100644
index 0000000000..1c002a83fb
--- /dev/null
+++ b/rust/bindings/uniffi/ensu/src/image.rs
@@ -0,0 +1,13 @@
+use ente_ensu::image as core;
+use thiserror::Error;
+
+#[derive(Debug, Error, uniffi::Error)]
+pub enum ImageError {
+ #[error("{0}")]
+ Message(String),
+}
+
+#[uniffi::export]
+pub fn compress_attachment_image(data: Vec<u8>) -> Result<Vec<u8>, ImageError> {
+ core::compress_attachment_image(&data).map_err(|e| ImageError::Message(e.to_string()))
+}
diff --git a/rust/bindings/uniffi/ensu/src/lib.rs b/rust/bindings/uniffi/ensu/src/lib.rs
index 98433bbcac..458baabb52 100644
--- a/rust/bindings/uniffi/ensu/src/lib.rs
+++ b/rust/bindings/uniffi/ensu/src/lib.rs
@@ -2,5 +2,6 @@ uniffi::setup_scaffolding!("ensu");
pub mod config;
pub mod db;
+pub mod image;
pub mod llm;
pub mod transcription;
diff --git a/rust/crates/ensu/src/db/error.rs b/rust/crates/ensu/src/db/error.rs
index 8fcc804fd8..73bd8c56d8 100644
--- a/rust/crates/ensu/src/db/error.rs
+++ b/rust/crates/ensu/src/db/error.rs
@@ -42,9 +42,6 @@ pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
- #[error(transparent)]
- Image(#[from] ente_image::ImageError),
-
#[cfg(feature = "sqlite")]
#[error(transparent)]
Sqlite(#[from] rusqlite::Error),
diff --git a/rust/crates/ensu/src/db/mod.rs b/rust/crates/ensu/src/db/mod.rs
index e79de69372..01341ebe6f 100644
--- a/rust/crates/ensu/src/db/mod.rs
+++ b/rust/crates/ensu/src/db/mod.rs
@@ -6,7 +6,6 @@ pub mod chat;
pub mod crypto;
pub mod database;
pub mod error;
-pub mod image;
pub mod models;
pub mod traits;
@@ -15,9 +14,6 @@ pub use crate::db::backend::{Backend, BackendTx, Row, Value};
pub use crate::db::chat::ChatDb;
pub use crate::db::database::Db;
pub use crate::db::error::{Error, Result};
-pub use crate::db::image::{
- ATTACHMENT_IMAGE_JPEG_QUALITY, ATTACHMENT_IMAGE_MAX_LONG_EDGE, compress_attachment_image,
-};
pub use crate::db::models::{
Attachment, AttachmentKind, AttachmentMeta, EntityType, Message, Sender, Session,
SessionWithPreview,
diff --git a/rust/crates/ensu/src/db/image.rs b/rust/crates/ensu/src/image.rs
similarity index 90%
rename from rust/crates/ensu/src/db/image.rs
rename to rust/crates/ensu/src/image.rs
index c182a60bf8..5571e627dd 100644
--- a/rust/crates/ensu/src/db/image.rs
+++ b/rust/crates/ensu/src/image.rs
@@ -1,16 +1,14 @@
-use ente_image::image_compression::compress_image_bytes_to_jpeg;
-
-use crate::db::Result;
+use ente_image::{ImageError, image_compression::compress_image_bytes_to_jpeg};
pub const ATTACHMENT_IMAGE_MAX_LONG_EDGE: u32 = 512;
pub const ATTACHMENT_IMAGE_JPEG_QUALITY: u8 = 85;
-pub fn compress_attachment_image(image_bytes: &[u8]) -> Result<Vec<u8>> {
- Ok(compress_image_bytes_to_jpeg(
+pub fn compress_attachment_image(image_bytes: &[u8]) -> Result<Vec<u8>, ImageError> {
+ compress_image_bytes_to_jpeg(
image_bytes,
ATTACHMENT_IMAGE_MAX_LONG_EDGE,
ATTACHMENT_IMAGE_JPEG_QUALITY,
- )?)
+ )
}
#[cfg(test)]
diff --git a/rust/crates/ensu/src/lib.rs b/rust/crates/ensu/src/lib.rs
index 1a2f29fce9..f2dca486b7 100644
--- a/rust/crates/ensu/src/lib.rs
+++ b/rust/crates/ensu/src/lib.rs
@@ -1,5 +1,6 @@
pub mod config;
pub mod db;
+pub mod image;
pub mod llm;
#[cfg(feature = "transcription")]