π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
port llmchat db and mtmd updates to ensu
Prateek Sunal4977aa549fb9
diff --git a/rust/ensu/db/src/attachments_db.rs b/rust/ensu/db/src/attachments_db.rsindex 02ca71afc1..6e95749db2 100644--- a/rust/ensu/db/src/attachments_db.rs+++ b/rust/ensu/db/src/attachments_db.rs@@ -240,6 +240,18 @@ impl<B: Backend> AttachmentsDb<B> {rows.iter().map(Self::row_from_row).collect()}+ pub fn get_uploads_for_message(&self, message_uuid: Uuid) -> Result<Vec<AttachmentUploadRow>> {+ let rows = self.backend.query(+ "SELECT attachment_id, session_uuid, message_uuid, size, remote_id, upload_state, uploaded_at, updated_at \+ FROM attachments \+ WHERE message_uuid = ? \+ ORDER BY updated_at ASC",+ &[Value::Text(message_uuid.to_string())],+ )?;++ rows.iter().map(Self::row_from_row).collect()+ }+pub fn get_pending_uploads_for_message(&self,message_uuid: Uuid,diff --git a/rust/ensu/db/src/db.rs b/rust/ensu/db/src/db.rsindex ca529ba99f..f416d0fc49 100644--- a/rust/ensu/db/src/db.rs+++ b/rust/ensu/db/src/db.rs@@ -6,7 +6,9 @@ use zeroize::Zeroizing;use crate::backend::{Backend, BackendTx, RowExt, Value};use crate::crypto;use crate::migrations;-use crate::models::{AttachmentMeta, EntityType, Message, Sender, Session, StoredAttachment};+use crate::models::{+ AttachmentMeta, EntityType, Message, Sender, Session, SessionWithPreview, StoredAttachment,+};use crate::traits::{Clock, RandomUuidGen, SystemClock, UuidGen};use crate::{Error, Result};@@ -100,6 +102,29 @@ impl<B: Backend> ChatDb<B> {rows.iter().map(|row| self.session_from_row(row)).collect()}+ pub fn list_sessions_with_preview(&self) -> Result<Vec<SessionWithPreview>> {+ let sessions = self.list_sessions()?;+ let mut with_preview = Vec::with_capacity(sessions.len());++ for session in sessions {+ let messages = self.get_messages(session.uuid)?;+ let last_message_preview = messages.last().map(|message| message.text.clone());+ with_preview.push(SessionWithPreview {+ uuid: session.uuid,+ title: session.title,+ created_at: session.created_at,+ updated_at: session.updated_at,+ server_updated_at: session.server_updated_at,+ remote_id: session.remote_id,+ needs_sync: session.needs_sync,+ deleted_at: session.deleted_at,+ last_message_preview,+ });+ }++ Ok(with_preview)+ }+pub fn update_session_title(&self, uuid: Uuid, title: &str) -> Result<()> {let now = self.clock.now_us();let encrypted_title = crypto::encrypt_string(title, &self.key)?;@@ -306,19 +331,63 @@ impl<B: Backend> ChatDb<B> {],)?;- Ok(Message {- uuid: message_uuid,+ let _ = self.touch_session_remote(session_uuid, created_at);++ self.get_message(message_uuid)?.ok_or(Error::NotFound {+ entity: EntityType::Message,+ id: message_uuid,+ })+ }++ pub fn insert_message_with_uuid(+ &self,+ message_uuid: Uuid,+ session_uuid: Uuid,+ sender: &str,+ text: &str,+ parent: Option<Uuid>,+ attachments: Vec<AttachmentMeta>,+ created_at: i64,+ deleted_at: Option<i64>,+ ) -> Result<Message> {+ self.insert_message_with_uuid_and_state(+ message_uuid,session_uuid,- parent_message_uuid: parent,sender,- text: text.to_string(),+ text,+ parent,attachments,created_at,- remote_id: None,- server_updated_at: None,- needs_sync,deleted_at,- })+ false,+ )+ }++ pub fn get_message(&self, uuid: Uuid) -> Result<Option<Message>> {+ let row = self.backend.query_row(+ "SELECT message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, remote_id, server_updated_at, needs_sync, deleted_at \+ FROM messages WHERE message_uuid = ?",+ &[Value::Text(uuid.to_string())],+ )?;++ row.map(|row| self.message_from_row(&row)).transpose()+ }++ pub fn get_messages_for_sync(+ &self,+ session_uuid: Uuid,+ include_deleted: bool,+ ) -> Result<Vec<Message>> {+ if !include_deleted {+ return self.get_messages_needing_sync(session_uuid);+ }++ let rows = self.backend.query(+ "SELECT message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, remote_id, server_updated_at, needs_sync, deleted_at \+ FROM messages WHERE session_uuid = ? ORDER BY created_at ASC, message_uuid ASC",+ &[Value::Text(session_uuid.to_string())],+ )?;+ rows.iter().map(|row| self.message_from_row(row)).collect()}pub fn get_messages(&self, session_uuid: Uuid) -> Result<Vec<Message>> {diff --git a/rust/ensu/db/src/ensu.rs b/rust/ensu/db/src/ensu.rsindex da9637d749..5763d07a3b 100644--- a/rust/ensu/db/src/ensu.rs+++ b/rust/ensu/db/src/ensu.rs@@ -5,7 +5,7 @@ use uuid::Uuid;use crate::Result;use crate::attachments_db::{AttachmentUploadRow, AttachmentsDb, UploadState};use crate::db::ChatDb;-use crate::models::{AttachmentMeta, EntityType, Message, Session};+use crate::models::{Attachment, AttachmentMeta, EntityType, Message, Session, SessionWithPreview};use crate::traits::{Clock, UuidGen};/// High-level DB that ties together the main chat DB and the attachments upload-state DB.@@ -31,6 +31,10 @@ impl<B: crate::Backend> EnsuDb<B> {self.chat.list_sessions()}+ pub fn list_sessions_with_preview(&self) -> Result<Vec<SessionWithPreview>> {+ self.chat.list_sessions_with_preview()+ }+pub fn update_session_title(&self, uuid: Uuid, title: &str) -> Result<()> {self.chat.update_session_title(uuid, title)}@@ -95,28 +99,92 @@ impl<B: crate::Backend> EnsuDb<B> {sender: &str,text: &str,parent: Option<Uuid>,- attachments: Vec<AttachmentMeta>,+ attachments: Vec<Attachment>,created_at: i64,deleted_at: Option<i64>,needs_sync: bool,) -> Result<Message> {- self.chat.insert_message_with_uuid_and_state(+ let attachment_metas: Vec<AttachmentMeta> = attachments+ .iter()+ .cloned()+ .map(AttachmentMeta::from)+ .collect();+ let message = self.chat.insert_message_with_uuid_and_state(message_uuid,session_uuid,sender,text,parent,- attachments,+ attachment_metas,created_at,deleted_at,needs_sync,+ )?;++ for attachment in attachments {+ if attachment.uploaded_at.is_some() {+ self.attachments.upsert_attachment_with_state(+ &attachment.id,+ session_uuid,+ message_uuid,+ attachment.size,+ None,+ UploadState::Uploaded,+ )?;+ } else {+ self.attachments.upsert_pending_attachment(+ &attachment.id,+ session_uuid,+ message_uuid,+ attachment.size,+ )?;+ }+ }++ Ok(message)+ }++ pub fn insert_message_with_uuid(+ &self,+ message_uuid: Uuid,+ session_uuid: Uuid,+ sender: &str,+ text: &str,+ parent: Option<Uuid>,+ attachments: Vec<Attachment>,+ created_at: i64,+ deleted_at: Option<i64>,+ ) -> Result<Message> {+ self.insert_message_with_uuid_and_state(+ message_uuid,+ session_uuid,+ sender,+ text,+ parent,+ attachments,+ created_at,+ deleted_at,+ false,)}+ pub fn get_message(&self, uuid: Uuid) -> Result<Option<Message>> {+ self.chat.get_message(uuid)+ }+pub fn get_messages(&self, session_uuid: Uuid) -> Result<Vec<Message>> {self.chat.get_messages(session_uuid)}+ pub fn get_messages_for_sync(+ &self,+ session_uuid: Uuid,+ include_deleted: bool,+ ) -> Result<Vec<Message>> {+ self.chat+ .get_messages_for_sync(session_uuid, include_deleted)+ }+pub fn get_messages_needing_sync(&self, session_uuid: Uuid) -> Result<Vec<Message>> {self.chat.get_messages_needing_sync(session_uuid)}@@ -173,6 +241,10 @@ impl<B: crate::Backend> EnsuDb<B> {.get_pending_uploads_for_message(message_uuid)}+ pub fn get_uploads_for_message(&self, message_uuid: Uuid) -> Result<Vec<AttachmentUploadRow>> {+ self.attachments.get_uploads_for_message(message_uuid)+ }+pub fn delete_attachment_tracking_for_message(&self, message_uuid: Uuid) -> Result<()> {self.attachments.delete_attachment_tracking_for_message(message_uuid)diff --git a/rust/ensu/db/src/lib.rs b/rust/ensu/db/src/lib.rsindex 03ad30e80e..bcb7e9a20a 100644--- a/rust/ensu/db/src/lib.rs+++ b/rust/ensu/db/src/lib.rs@@ -20,7 +20,10 @@ pub use crate::backend::{Backend, BackendTx, Row, Value};pub use crate::db::ChatDb;pub use crate::ensu::EnsuDb;pub use crate::error::{Error, Result};-pub use crate::models::{AttachmentKind, AttachmentMeta, EntityType, Message, Sender, Session};+pub use crate::models::{+ Attachment, AttachmentKind, AttachmentMeta, EntityType, Message, Sender, Session,+ SessionWithPreview,+};pub use crate::sync_state_db::{SyncEntityState, SyncStateDb};pub use crate::traits::{AttachmentStore, Clock, FileMetaStore, FsAttachmentStore, MetaStore, RandomUuidGen,diff --git a/rust/ensu/db/src/models.rs b/rust/ensu/db/src/models.rsindex b735631355..4659770113 100644--- a/rust/ensu/db/src/models.rs+++ b/rust/ensu/db/src/models.rs@@ -13,6 +13,19 @@ pub struct Session {pub deleted_at: Option<i64>,}+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct SessionWithPreview {+ pub uuid: Uuid,+ pub title: String,+ pub created_at: i64,+ pub updated_at: i64,+ pub server_updated_at: Option<i64>,+ pub remote_id: Option<String>,+ pub needs_sync: bool,+ pub deleted_at: Option<i64>,+ pub last_message_preview: Option<String>,+}+#[derive(Debug, Clone, PartialEq, Eq)]pub struct Message {pub uuid: Uuid,@@ -36,6 +49,38 @@ pub struct AttachmentMeta {pub name: String,}+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct Attachment {+ pub id: String,+ pub kind: AttachmentKind,+ pub size: i64,+ pub name: String,+ pub uploaded_at: Option<i64>,+}++impl From<AttachmentMeta> for Attachment {+ fn from(meta: AttachmentMeta) -> Self {+ Self {+ id: meta.id,+ kind: meta.kind,+ size: meta.size,+ name: meta.name,+ uploaded_at: None,+ }+ }+}++impl From<Attachment> for AttachmentMeta {+ fn from(attachment: Attachment) -> Self {+ Self {+ id: attachment.id,+ kind: attachment.kind,+ size: attachment.size,+ name: attachment.name,+ }+ }+}+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "lowercase")]pub enum AttachmentKind {diff --git a/rust/ensu/inference/tool/patch_llama_mtmd.sh b/rust/ensu/inference/tool/patch_llama_mtmd.shindex 616432eff5..fc2d0fc610 100755--- a/rust/ensu/inference/tool/patch_llama_mtmd.sh+++ b/rust/ensu/inference/tool/patch_llama_mtmd.sh@@ -1,9 +1,14 @@#!/usr/bin/env bashset -euo pipefail+PYTHON_BIN="${PYTHON_BIN:-python3}"+if ! command -v "$PYTHON_BIN" >/dev/null 2>&1; then+ PYTHON_BIN=python+fi+ROOT="$(cd "$(dirname "$0")/.." && pwd)"CRATE_DIR="$(- python3 - <<PY+ "$PYTHON_BIN" - <<PYimport jsonimport osimport subprocess@@ -36,6 +41,11 @@ if [[ -z "$CRATE_DIR" ]]; thenexit 1fi+PY_CRATE_DIR="$CRATE_DIR"+if command -v cygpath >/dev/null 2>&1; then+ PY_CRATE_DIR="$(cygpath -w "$CRATE_DIR")"+fi+TOOLS_DIR="$CRATE_DIR/llama.cpp/tools"MTMD_DIR="$TOOLS_DIR/mtmd"CMAKE_FILE="$TOOLS_DIR/CMakeLists.txt"@@ -70,11 +80,13 @@ fiCLIP_CPP="$MTMD_DIR/clip.cpp"if [[ -f "$CLIP_CPP" ]]; then- python3 - <<PY+ PY_CRATE_DIR="$PY_CRATE_DIR" "$PYTHON_BIN" - <<PY+import osfrom pathlib import Pathimport sys-path = Path("$CLIP_CPP")+crate_dir = os.environ["PY_CRATE_DIR"]+path = Path(crate_dir) / "llama.cpp" / "tools" / "mtmd" / "clip.cpp"text = path.read_text(encoding="utf-8", errors="ignore")replacements = {@@ -105,11 +117,11 @@ if missing:sys.stderr.write(f"File: {path}\n")sys.exit(1)-marker = Path("$CRATE_DIR") / "llama.cpp" / "CMakeMtmdPatch.txt"+marker = Path(crate_dir) / "llama.cpp" / "CMakeMtmdPatch.txt"needs_rebuild = applied or not marker.exists()if needs_rebuild:marker.write_text("# mtmd patch applied\n")- cmake_root = Path("$CRATE_DIR") / "llama.cpp" / "CMakeLists.txt"+ cmake_root = Path(crate_dir) / "llama.cpp" / "CMakeLists.txt"if cmake_root.exists():cmake_root.touch()diff --git a/rust/ensu/sync/src/sync.rs b/rust/ensu/sync/src/sync.rsindex 6e1bc867f3..6124aacc4d 100644--- a/rust/ensu/sync/src/sync.rs+++ b/rust/ensu/sync/src/sync.rs@@ -6,9 +6,9 @@ use std::sync::atomic::{AtomicBool, Ordering};use zeroize::Zeroizing;use ensu_db::{- AttachmentKind, AttachmentMeta, AttachmentStore, Clock, EnsuDb, EntityType, FsAttachmentStore,- Message, Sender, Session, SqliteBackend, SyncEntityState, SyncStateDb, SystemClock,- UploadState,+ Attachment, AttachmentKind, AttachmentMeta, AttachmentStore, Clock, EnsuDb, EntityType,+ FsAttachmentStore, Message, Sender, Session, SqliteBackend, SyncEntityState, SyncStateDb,+ SystemClock, UploadState,};use ente_core::crypto::keys;use uuid::Uuid;@@ -366,13 +366,19 @@ impl SyncEngine {)?;let messages = offline_db.get_messages(session.uuid)?;for message in messages {+ let attachments: Vec<Attachment> = message+ .attachments+ .clone()+ .into_iter()+ .map(Attachment::from)+ .collect();let _ = self.db.insert_message_with_uuid_and_state(message.uuid,session.uuid,message.sender.as_str(),&message.text,message.parent_message_uuid,- message.attachments.clone(),+ attachments,message.created_at,message.deleted_at,true,