Skip to content

armgit / ente

Public Imported from GitHub
0

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

Merge llmchat-common-rust into llm-chat-ui

Prateek Sunal38f5d0024e9d
diff --git a/rust/llmchat/db/src/db.rs b/rust/llmchat/db/src/db.rs
index 4e63a20bbb..314ed8d8a1 100644
--- a/rust/llmchat/db/src/db.rs
+++ b/rust/llmchat/db/src/db.rs
@@ -129,7 +129,15 @@ impl<B: Backend> ChatDb<B> {
pub fn get_sessions_needing_sync(&self) -> Result<Vec<Session>> {
let rows = self.backend.query(
"SELECT session_uuid, title, created_at, updated_at, remote_id, needs_sync, deleted_at \
- FROM sessions WHERE needs_sync = 1 AND deleted_at IS NULL ORDER BY updated_at DESC",
+ FROM sessions \
+ WHERE deleted_at IS NULL \
+ AND (needs_sync = 1 OR EXISTS (\
+ SELECT 1 FROM messages \
+ WHERE messages.session_uuid = sessions.session_uuid \
+ AND messages.needs_sync = 1 \
+ AND messages.deleted_at IS NULL\
+ )) \
+ ORDER BY updated_at DESC",
&[],
)?;
rows.iter().map(|row| self.session_from_row(row)).collect()
@@ -151,8 +159,8 @@ impl<B: Backend> ChatDb<B> {
self.backend.transaction(|tx| {
tx.execute(
- "INSERT INTO messages (message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, deleted_at) \
- VALUES (?, ?, ?, ?, ?, ?, ?, NULL)",
+ "INSERT INTO messages (message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, needs_sync, deleted_at) \
+ VALUES (?, ?, ?, ?, ?, ?, ?, 1, NULL)",
&[
Value::Text(message_uuid.to_string()),
Value::Text(session_uuid.to_string()),
@@ -180,19 +188,29 @@ impl<B: Backend> ChatDb<B> {
text: text.to_string(),
attachments,
created_at: now,
+ needs_sync: true,
deleted_at: None,
})
}
pub fn get_messages(&self, session_uuid: Uuid) -> Result<Vec<Message>> {
let rows = self.backend.query(
- "SELECT message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, deleted_at \
+ "SELECT message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, needs_sync, deleted_at \
FROM messages WHERE session_uuid = ? AND deleted_at IS NULL 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_needing_sync(&self, session_uuid: Uuid) -> Result<Vec<Message>> {
+ let rows = self.backend.query(
+ "SELECT message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, needs_sync, deleted_at \
+ FROM messages WHERE session_uuid = ? AND needs_sync = 1 AND deleted_at IS NULL 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 update_message_text(&self, uuid: Uuid, text: &str) -> Result<()> {
let now = self.clock.now_us();
let encrypted_text = crypto::encrypt_string(text, &self.key)?;
@@ -208,7 +226,7 @@ impl<B: Backend> ChatDb<B> {
let session_uuid = Uuid::parse_str(&row.get_string(0)?)?;
let affected = tx.execute(
- "UPDATE messages SET text = ? WHERE message_uuid = ? AND deleted_at IS NULL",
+ "UPDATE messages SET text = ?, needs_sync = 1 WHERE message_uuid = ? AND deleted_at IS NULL",
&[Value::Blob(encrypted_text), Value::Text(uuid.to_string())],
)?;
ensure_row_updated(affected, EntityType::Message, uuid)?;
@@ -251,6 +269,14 @@ impl<B: Backend> ChatDb<B> {
ensure_row_updated(affected, EntityType::Session, uuid)
}
+ pub fn mark_message_synced(&self, uuid: Uuid) -> Result<()> {
+ let affected = self.backend.execute(
+ "UPDATE messages SET needs_sync = 0 WHERE message_uuid = ? AND deleted_at IS NULL",
+ &[Value::Text(uuid.to_string())],
+ )?;
+ ensure_row_updated(affected, EntityType::Message, uuid)
+ }
+
pub fn upsert_session_from_remote(
&self,
session_uuid: Uuid,
@@ -263,7 +289,7 @@ impl<B: Backend> ChatDb<B> {
"INSERT INTO sessions (session_uuid, title, created_at, updated_at, remote_id, needs_sync, deleted_at) \
VALUES (?, ?, ?, ?, ?, 0, NULL) \
ON CONFLICT(session_uuid) DO UPDATE SET \
- title = excluded.title, \
+ title = CASE WHEN sessions.needs_sync = 1 THEN sessions.title ELSE excluded.title END, \
created_at = MIN(sessions.created_at, excluded.created_at), \
updated_at = MAX(sessions.updated_at, excluded.updated_at), \
remote_id = COALESCE(sessions.remote_id, excluded.remote_id), \
@@ -314,8 +340,8 @@ impl<B: Backend> ChatDb<B> {
let attachments_json = self.serialize_attachments(&attachments)?;
self.backend.execute(
- "INSERT INTO messages (message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, deleted_at) \
- VALUES (?, ?, ?, ?, ?, ?, ?, NULL) \
+ "INSERT INTO messages (message_uuid, session_uuid, parent_message_uuid, sender, text, attachments, created_at, needs_sync, deleted_at) \
+ VALUES (?, ?, ?, ?, ?, ?, ?, 0, NULL) \
ON CONFLICT(message_uuid) DO UPDATE SET \
session_uuid = excluded.session_uuid, \
parent_message_uuid = excluded.parent_message_uuid, \
@@ -323,6 +349,7 @@ impl<B: Backend> ChatDb<B> {
text = excluded.text, \
attachments = excluded.attachments, \
created_at = MIN(messages.created_at, excluded.created_at), \
+ needs_sync = CASE WHEN messages.needs_sync = 1 THEN 1 ELSE excluded.needs_sync END, \
deleted_at = NULL",
&[
Value::Text(message_uuid.to_string()),
@@ -349,6 +376,7 @@ impl<B: Backend> ChatDb<B> {
text: text.to_string(),
attachments,
created_at,
+ needs_sync: false,
deleted_at: None,
})
}
@@ -443,7 +471,8 @@ impl<B: Backend> ChatDb<B> {
let attachments_json = row.get_optional_string(5)?;
let attachments = self.deserialize_attachments(attachments_json)?;
let created_at = row.get_i64(6)?;
- let deleted_at = row.get_optional_i64(7)?;
+ let needs_sync = bool_from_i64(row.get_i64(7)?)?;
+ let deleted_at = row.get_optional_i64(8)?;
Ok(Message {
uuid,
@@ -453,6 +482,7 @@ impl<B: Backend> ChatDb<B> {
text,
attachments,
created_at,
+ needs_sync,
deleted_at,
})
}
diff --git a/rust/llmchat/db/src/llmchat.rs b/rust/llmchat/db/src/llmchat.rs
index b282014cc5..179d1178b9 100644
--- a/rust/llmchat/db/src/llmchat.rs
+++ b/rust/llmchat/db/src/llmchat.rs
@@ -69,6 +69,10 @@ impl<B: crate::Backend> LlmChatDb<B> {
self.chat.get_messages(session_uuid)
}
+ pub fn get_messages_needing_sync(&self, session_uuid: Uuid) -> Result<Vec<Message>> {
+ self.chat.get_messages_needing_sync(session_uuid)
+ }
+
pub fn update_message_text(&self, uuid: Uuid, text: &str) -> Result<()> {
self.chat.update_message_text(uuid, text)
}
@@ -127,6 +131,10 @@ impl<B: crate::Backend> LlmChatDb<B> {
self.chat.mark_session_synced(uuid, remote_id)
}
+ pub fn mark_message_synced(&self, uuid: Uuid) -> Result<()> {
+ self.chat.mark_message_synced(uuid)
+ }
+
pub fn get_sessions_needing_sync(&self) -> Result<Vec<Session>> {
self.chat.get_sessions_needing_sync()
}
diff --git a/rust/llmchat/db/src/migrations.rs b/rust/llmchat/db/src/migrations.rs
index 1bb0f3e0e1..3c2cafbfde 100644
--- a/rust/llmchat/db/src/migrations.rs
+++ b/rust/llmchat/db/src/migrations.rs
@@ -2,14 +2,26 @@ use crate::{Backend, Error, Result};
use crate::backend::RowExt;
use crate::schema;
-pub const LATEST_VERSION: i64 = 1;
+pub const LATEST_VERSION: i64 = 2;
pub fn migrate<B: Backend>(backend: &B) -> Result<()> {
let version = user_version(backend)?;
match version {
0 => {
backend.execute_batch(schema::CREATE_ALL)?;
- backend.execute("PRAGMA user_version = 1;", &[])?;
+ backend.execute("PRAGMA user_version = 2;", &[])?;
+ Ok(())
+ }
+ 1 => {
+ backend.execute(
+ "ALTER TABLE messages ADD COLUMN needs_sync INTEGER NOT NULL DEFAULT 1 CHECK(needs_sync IN (0,1));",
+ &[],
+ )?;
+ backend.execute(
+ "UPDATE messages SET needs_sync = 0 WHERE session_uuid IN (SELECT session_uuid FROM sessions WHERE needs_sync = 0);",
+ &[],
+ )?;
+ backend.execute("PRAGMA user_version = 2;", &[])?;
Ok(())
}
LATEST_VERSION => Ok(()),
diff --git a/rust/llmchat/db/src/models.rs b/rust/llmchat/db/src/models.rs
index 3842545a68..4404421012 100644
--- a/rust/llmchat/db/src/models.rs
+++ b/rust/llmchat/db/src/models.rs
@@ -21,6 +21,7 @@ pub struct Message {
pub text: String,
pub attachments: Vec<AttachmentMeta>,
pub created_at: i64,
+ pub needs_sync: bool,
pub deleted_at: Option<i64>,
}
diff --git a/rust/llmchat/db/src/schema.rs b/rust/llmchat/db/src/schema.rs
index db16595ab3..7e40d05ccf 100644
--- a/rust/llmchat/db/src/schema.rs
+++ b/rust/llmchat/db/src/schema.rs
@@ -21,6 +21,7 @@ CREATE TABLE IF NOT EXISTS messages (
text BLOB NOT NULL,
attachments TEXT,
created_at INTEGER NOT NULL,
+ needs_sync INTEGER NOT NULL DEFAULT 1 CHECK(needs_sync IN (0,1)),
deleted_at INTEGER,
FOREIGN KEY (session_uuid) REFERENCES sessions(session_uuid) ON DELETE CASCADE
);
@@ -52,6 +53,7 @@ CREATE TABLE IF NOT EXISTS messages (
text BLOB NOT NULL,
attachments TEXT,
created_at INTEGER NOT NULL,
+ needs_sync INTEGER NOT NULL DEFAULT 1 CHECK(needs_sync IN (0,1)),
deleted_at INTEGER,
FOREIGN KEY (session_uuid) REFERENCES sessions(session_uuid) ON DELETE CASCADE
);
diff --git a/rust/llmchat/sync/Cargo.lock b/rust/llmchat/sync/Cargo.lock
index f436820728..a3834418c9 100644
--- a/rust/llmchat/sync/Cargo.lock
+++ b/rust/llmchat/sync/Cargo.lock
@@ -12,6 +12,15 @@ dependencies = [
"generic-array",
]
+[[package]]
+name = "aho-corasick"
+version = "1.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
+dependencies = [
+ "memchr",
+]
+
[[package]]
name = "argon2"
version = "0.5.3"
@@ -36,12 +45,224 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
+[[package]]
+name = "ascii-canvas"
+version = "3.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8824ecca2e851cec16968d54a01dd372ef8f95b244fb84b84e70128be347c3c6"
+dependencies = [
+ "term",
+]
+
+[[package]]
+name = "assert-json-diff"
+version = "2.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12"
+dependencies = [
+ "serde",
+ "serde_json",
+]
+
+[[package]]
+name = "async-attributes"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a3203e79f4dd9bdda415ed03cf14dae5a2bf775c683a00f94e9cd1faf0f596e5"
+dependencies = [
+ "quote",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "async-channel"
+version = "1.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35"
+dependencies = [
+ "concurrent-queue",
+ "event-listener 2.5.3",
+ "futures-core",
+]
+
+[[package]]
+name = "async-channel"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "924ed96dd52d1b75e9c1a3e6275715fd320f5f9439fb5a4a11fa51f4221158d2"
+dependencies = [
+ "concurrent-queue",
+ "event-listener-strategy",
+ "futures-core",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "async-executor"
+version = "1.13.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "497c00e0fd83a72a79a39fcbd8e3e2f055d6f6c7e025f3b3d91f4f8e76527fb8"
+dependencies = [
+ "async-task",
+ "concurrent-queue",
+ "fastrand",
+ "futures-lite",
+ "pin-project-lite",
+ "slab",
+]
+
+[[package]]
+name = "async-global-executor"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05b1b633a2115cd122d73b955eadd9916c18c8f510ec9cd1686404c60ad1c29c"
+dependencies = [
+ "async-channel 2.5.0",
+ "async-executor",
+ "async-io",
+ "async-lock",
+ "blocking",
+ "futures-lite",
+ "once_cell",
+]
+
+[[package]]
+name = "async-io"
+version = "2.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "456b8a8feb6f42d237746d4b3e9a178494627745c3c56c6ea55d92ba50d026fc"
+dependencies = [
+ "autocfg",
+ "cfg-if",
+ "concurrent-queue",
+ "futures-io",
+ "futures-lite",
+ "parking",
+ "polling",
+ "rustix",
+ "slab",
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "async-lock"
+version = "3.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "290f7f2596bd5b78a9fec8088ccd89180d7f9f55b94b0576823bbbdc72ee8311"
+dependencies = [
+ "event-listener 5.4.1",
+ "event-listener-strategy",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "async-object-pool"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "333c456b97c3f2d50604e8b2624253b7f787208cb72eb75e64b0ad11b221652c"
+dependencies = [
+ "async-std",
+]
+
+[[package]]
+name = "async-process"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fc50921ec0055cdd8a16de48773bfeec5c972598674347252c0399676be7da75"
+dependencies = [
+ "async-channel 2.5.0",
+ "async-io",
+ "async-lock",
+ "async-signal",
+ "async-task",
+ "blocking",
+ "cfg-if",
+ "event-listener 5.4.1",
+ "futures-lite",
+ "rustix",
+]
+
+[[package]]
+name = "async-signal"
+version = "0.2.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43c070bbf59cd3570b6b2dd54cd772527c7c3620fce8be898406dd3ed6adc64c"
+dependencies = [
+ "async-io",
+ "async-lock",
+ "atomic-waker",
+ "cfg-if",
+ "futures-core",
+ "futures-io",
+ "rustix",
+ "signal-hook-registry",
+ "slab",
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "async-std"
+version = "1.13.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2c8e079a4ab67ae52b7403632e4618815d6db36d2a010cfe41b02c1b1578f93b"
+dependencies = [
+ "async-attributes",
+ "async-channel 1.9.0",
+ "async-global-executor",
+ "async-io",
+ "async-lock",
+ "async-process",
+ "crossbeam-utils",
+ "futures-channel",
+ "futures-core",
+ "futures-io",
+ "futures-lite",
+ "gloo-timers",
+ "kv-log-macro",
+ "log",
+ "memchr",
+ "once_cell",
+ "pin-project-lite",
+ "pin-utils",
+ "slab",
+ "wasm-bindgen-futures",
+]
+
+[[package]]
+name = "async-task"
+version = "4.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de"
+
+[[package]]
+name = "async-trait"
+version = "0.1.89"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.114",
+]
+
[[package]]
name = "atomic-waker"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
+[[package]]
+name = "autocfg"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
+
+[[package]]
+name = "base64"
+version = "0.21.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
+
[[package]]
name = "base64"
version = "0.22.1"
@@ -54,6 +275,32 @@ version = "1.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06"
+[[package]]
+name = "basic-cookies"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67bd8fd42c16bdb08688243dc5f0cc117a3ca9efeeaba3a345a18a6159ad96f7"
+dependencies = [
+ "lalrpop",
+ "lalrpop-util",
+ "regex",
+]
+
+[[package]]
+name = "bit-set"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
+dependencies = [
+ "bit-vec",
+]
+
+[[package]]
+name = "bit-vec"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
+
[[package]]
name = "bitflags"
version = "2.10.0"
@@ -98,6 +345,19 @@ dependencies = [
"generic-array",
]
+[[package]]
+name = "blocking"
+version = "1.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e83f8d02be6967315521be875afa792a316e28d57b5a2d401897e2a7921b7f21"
+dependencies = [
+ "async-channel 2.5.0",
+ "async-task",
+ "futures-io",
+ "futures-lite",
+ "piper",
+]
+
[[package]]
name = "bumpalo"
version = "3.19.1"
@@ -154,6 +414,15 @@ dependencies = [
"zeroize",
]
+[[package]]
+name = "concurrent-queue"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973"
+dependencies = [
+ "crossbeam-utils",
+]
+
[[package]]
name = "constant_time_eq"
version = "0.4.2"
@@ -195,6 +464,18 @@ dependencies = [
"libc",
]
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
+
+[[package]]
+name = "crunchy"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
+
[[package]]
name = "crypto-common"
version = "0.1.7"
@@ -243,7 +524,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
]
[[package]]
@@ -257,6 +538,27 @@ dependencies = [
"subtle",
]
+[[package]]
+name = "dirs-next"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
+dependencies = [
+ "cfg-if",
+ "dirs-sys-next",
+]
+
+[[package]]
+name = "dirs-sys-next"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
+dependencies = [
+ "libc",
+ "redox_users",
+ "winapi",
+]
+
[[package]]
name = "displaydoc"
version = "0.2.5"
@@ -265,7 +567,22 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
+]
+
+[[package]]
+name = "either"
+version = "1.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
+
+[[package]]
+name = "ena"
+version = "0.14.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d248bdd43ce613d87415282f69b9bb99d947d290b10962dd6c56233312c2ad5"
+dependencies = [
+ "log",
]
[[package]]
@@ -282,7 +599,7 @@ name = "ente-core"
version = "0.0.1"
dependencies = [
"argon2",
- "base64",
+ "base64 0.22.1",
"blake2b_simd",
"crypto_secretstream",
"getrandom 0.2.17",
@@ -294,7 +611,7 @@ dependencies = [
"serde",
"serde_json",
"subtle",
- "thiserror",
+ "thiserror 2.0.18",
"x25519-dalek",
"xsalsa20poly1305",
"zeroize",
@@ -316,6 +633,33 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "event-listener"
+version = "2.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
+
+[[package]]
+name = "event-listener"
+version = "5.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab"
+dependencies = [
+ "concurrent-queue",
+ "parking",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "event-listener-strategy"
+version = "0.5.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93"
+dependencies = [
+ "event-listener 5.4.1",
+ "pin-project-lite",
+]
+
[[package]]
name = "fallible-iterator"
version = "0.3.0"
@@ -346,6 +690,12 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db"
+[[package]]
+name = "fixedbitset"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
+
[[package]]
name = "fnv"
version = "1.0.7"
@@ -389,6 +739,30 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
+[[package]]
+name = "futures-lite"
+version = "2.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad"
+dependencies = [
+ "fastrand",
+ "futures-core",
+ "futures-io",
+ "parking",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "futures-macro"
+version = "0.3.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.114",
+]
+
[[package]]
name = "futures-sink"
version = "0.3.31"
@@ -409,6 +783,7 @@ checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-core",
"futures-io",
+ "futures-macro",
"futures-sink",
"futures-task",
"memchr",
@@ -454,6 +829,18 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "gloo-timers"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbb143cf96099802033e0d4f4963b19fd2e0b728bcf076cd9cf7f6634f092994"
+dependencies = [
+ "futures-channel",
+ "futures-core",
+ "js-sys",
+ "wasm-bindgen",
+]
+
[[package]]
name = "h2"
version = "0.4.13"
@@ -465,7 +852,7 @@ dependencies = [
"fnv",
"futures-core",
"futures-sink",
- "http",
+ "http 1.4.0",
"indexmap",
"slab",
"tokio",
@@ -491,6 +878,12 @@ dependencies = [
"hashbrown",
]
+[[package]]
+name = "hermit-abi"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
+
[[package]]
name = "hex"
version = "0.4.3"
@@ -515,6 +908,17 @@ dependencies = [
"digest",
]
+[[package]]
+name = "http"
+version = "0.2.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "601cbb57e577e2f5ef5be8e7b83f0f63994f25aa94d673e54a92d5c516d101f1"
+dependencies = [
+ "bytes",
+ "fnv",
+ "itoa",
+]
+
[[package]]
name = "http"
version = "1.4.0"
@@ -525,6 +929,17 @@ dependencies = [
"itoa",
]
+[[package]]
+name = "http-body"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2"
+dependencies = [
+ "bytes",
+ "http 0.2.12",
+ "pin-project-lite",
+]
+
[[package]]
name = "http-body"
version = "1.0.1"
@@ -532,28 +947,85 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184"
dependencies = [
"bytes",
- "http",
+ "http 1.4.0",
]
[[package]]
name = "http-body-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
+checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a"
+dependencies = [
+ "bytes",
+ "futures-core",
+ "http 1.4.0",
+ "http-body 1.0.1",
+ "pin-project-lite",
+]
+
+[[package]]
+name = "httparse"
+version = "1.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
+
+[[package]]
+name = "httpdate"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
+
+[[package]]
+name = "httpmock"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08ec9586ee0910472dec1a1f0f8acf52f0fdde93aea74d70d4a3107b4be0fd5b"
+dependencies = [
+ "assert-json-diff",
+ "async-object-pool",
+ "async-std",
+ "async-trait",
+ "base64 0.21.7",
+ "basic-cookies",
+ "crossbeam-utils",
+ "form_urlencoded",
+ "futures-util",
+ "hyper 0.14.32",
+ "lazy_static",
+ "levenshtein",
+ "log",
+ "regex",
+ "serde",
+ "serde_json",
+ "serde_regex",
+ "similar",
+ "tokio",
+ "url",
+]
+
+[[package]]
+name = "hyper"
+version = "0.14.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41dfc780fdec9373c01bae43289ea34c972e40ee3c9f6b3c8801a35f35586ce7"
dependencies = [
"bytes",
+ "futures-channel",
"futures-core",
- "http",
- "http-body",
+ "futures-util",
+ "http 0.2.12",
+ "http-body 0.4.6",
+ "httparse",
+ "httpdate",
+ "itoa",
"pin-project-lite",
+ "socket2 0.5.10",
+ "tokio",
+ "tower-service",
+ "tracing",
+ "want",
]
-[[package]]
-name = "httparse"
-version = "1.10.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
-
[[package]]
name = "hyper"
version = "1.8.1"
@@ -565,8 +1037,8 @@ dependencies = [
"futures-channel",
"futures-core",
"h2",
- "http",
- "http-body",
+ "http 1.4.0",
+ "http-body 1.0.1",
"httparse",
"itoa",
"pin-project-lite",
@@ -582,8 +1054,8 @@ version = "0.27.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58"
dependencies = [
- "http",
- "hyper",
+ "http 1.4.0",
+ "hyper 1.8.1",
"hyper-util",
"rustls",
"rustls-native-certs",
@@ -599,19 +1071,19 @@ version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "727805d60e7938b76b826a6ef209eb70eaa1812794f9424d4a4e2d740662df5f"
dependencies = [
- "base64",
+ "base64 0.22.1",
"bytes",
"futures-channel",
"futures-core",
"futures-util",
- "http",
- "http-body",
- "hyper",
+ "http 1.4.0",
+ "http-body 1.0.1",
+ "hyper 1.8.1",
"ipnet",
"libc",
"percent-encoding",
"pin-project-lite",
- "socket2",
+ "socket2 0.6.1",
"system-configuration",
"tokio",
"tower-service",
@@ -757,6 +1229,15 @@ dependencies = [
"serde",
]
+[[package]]
+name = "itertools"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
+dependencies = [
+ "either",
+]
+
[[package]]
name = "itoa"
version = "1.0.17"
@@ -773,12 +1254,74 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "kv-log-macro"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
+dependencies = [
+ "log",
+]
+
+[[package]]
+name = "lalrpop"
+version = "0.20.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "55cb077ad656299f160924eb2912aa147d7339ea7d69e1b5517326fdcec3c1ca"
+dependencies = [
+ "ascii-canvas",
+ "bit-set",
+ "ena",
+ "itertools",
+ "lalrpop-util",
+ "petgraph",
+ "pico-args",
+ "regex",
+ "regex-syntax",
+ "string_cache",
+ "term",
+ "tiny-keccak",
+ "unicode-xid",
+ "walkdir",
+]
+
+[[package]]
+name = "lalrpop-util"
+version = "0.20.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "507460a910eb7b32ee961886ff48539633b788a36b65692b95f225b844c82553"
+dependencies = [
+ "regex-automata",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
+
+[[package]]
+name = "levenshtein"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760"
+
[[package]]
name = "libc"
version = "0.2.180"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
+[[package]]
+name = "libredox"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
+dependencies = [
+ "bitflags",
+ "libc",
+]
+
[[package]]
name = "libsqlite3-sys"
version = "0.36.0"
@@ -810,7 +1353,7 @@ dependencies = [
"rusqlite",
"serde",
"serde_json",
- "thiserror",
+ "thiserror 2.0.18",
"uuid",
"zeroize",
]
@@ -821,14 +1364,25 @@ version = "0.1.0"
dependencies = [
"ente-core",
"hkdf",
+ "httpmock",
"llmchat-db",
"reqwest",
"serde",
"serde_json",
"sha2",
"tempfile",
- "thiserror",
+ "thiserror 2.0.18",
"uuid",
+ "zeroize",
+]
+
+[[package]]
+name = "lock_api"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
+dependencies = [
+ "scopeguard",
]
[[package]]
@@ -836,6 +1390,9 @@ name = "log"
version = "0.4.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
+dependencies = [
+ "value-bag",
+]
[[package]]
name = "lru-slab"
@@ -876,6 +1433,12 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "new_debug_unreachable"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
+
[[package]]
name = "once_cell"
version = "1.21.3"
@@ -894,6 +1457,35 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
+[[package]]
+name = "parking"
+version = "2.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba"
+
+[[package]]
+name = "parking_lot"
+version = "0.12.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
+dependencies = [
+ "lock_api",
+ "parking_lot_core",
+]
+
+[[package]]
+name = "parking_lot_core"
+version = "0.9.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "redox_syscall",
+ "smallvec",
+ "windows-link",
+]
+
[[package]]
name = "password-hash"
version = "0.5.0"
@@ -911,6 +1503,31 @@ version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
+[[package]]
+name = "petgraph"
+version = "0.6.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
+dependencies = [
+ "fixedbitset",
+ "indexmap",
+]
+
+[[package]]
+name = "phf_shared"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67eabc2ef2a60eb7faa00097bd1ffdb5bd28e62bf39990626a582201b7a754e5"
+dependencies = [
+ "siphasher",
+]
+
+[[package]]
+name = "pico-args"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
+
[[package]]
name = "pin-project-lite"
version = "0.2.16"
@@ -923,12 +1540,37 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
+[[package]]
+name = "piper"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96c8c490f422ef9a4efd2cb5b42b76c8613d7e7dfc1caf667b8a3350a5acc066"
+dependencies = [
+ "atomic-waker",
+ "fastrand",
+ "futures-io",
+]
+
[[package]]
name = "pkg-config"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
+[[package]]
+name = "polling"
+version = "3.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218"
+dependencies = [
+ "cfg-if",
+ "concurrent-queue",
+ "hermit-abi",
+ "pin-project-lite",
+ "rustix",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "poly1305"
version = "0.8.0"
@@ -958,6 +1600,12 @@ dependencies = [
"zerocopy",
]
+[[package]]
+name = "precomputed-hash"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
+
[[package]]
name = "proc-macro2"
version = "1.0.105"
@@ -980,8 +1628,8 @@ dependencies = [
"quinn-udp",
"rustc-hash",
"rustls",
- "socket2",
- "thiserror",
+ "socket2 0.6.1",
+ "thiserror 2.0.18",
"tokio",
"tracing",
"web-time",
@@ -1002,7 +1650,7 @@ dependencies = [
"rustls",
"rustls-pki-types",
"slab",
- "thiserror",
+ "thiserror 2.0.18",
"tinyvec",
"tracing",
"web-time",
@@ -1017,7 +1665,7 @@ dependencies = [
"cfg_aliases",
"libc",
"once_cell",
- "socket2",
+ "socket2 0.6.1",
"tracing",
"windows-sys 0.60.2",
]
@@ -1075,23 +1723,72 @@ dependencies = [
"getrandom 0.3.4",
]
+[[package]]
+name = "redox_syscall"
+version = "0.5.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
+dependencies = [
+ "bitflags",
+]
+
+[[package]]
+name = "redox_users"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
+dependencies = [
+ "getrandom 0.2.17",
+ "libredox",
+ "thiserror 1.0.69",
+]
+
+[[package]]
+name = "regex"
+version = "1.12.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-automata",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-automata"
+version = "0.4.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.8.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
+
[[package]]
name = "reqwest"
version = "0.12.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147"
dependencies = [
- "base64",
+ "base64 0.22.1",
"bytes",
"encoding_rs",
"futures-channel",
"futures-core",
"futures-util",
"h2",
- "http",
- "http-body",
+ "http 1.4.0",
+ "http-body 1.0.1",
"http-body-util",
- "hyper",
+ "hyper 1.8.1",
"hyper-rustls",
"hyper-util",
"js-sys",
@@ -1139,7 +1836,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8a1f2315036ef6b1fbacd1972e8ee7688030b0a2121edfc2a6550febd41574d"
dependencies = [
"hashbrown",
- "thiserror",
+ "thiserror 2.0.18",
]
[[package]]
@@ -1253,6 +1950,15 @@ dependencies = [
"cipher",
]
+[[package]]
+name = "same-file"
+version = "1.0.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
+dependencies = [
+ "winapi-util",
+]
+
[[package]]
name = "schannel"
version = "0.1.28"
@@ -1262,6 +1968,12 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "scopeguard"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
+
[[package]]
name = "security-framework"
version = "3.5.1"
@@ -1318,7 +2030,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
]
[[package]]
@@ -1334,6 +2046,16 @@ dependencies = [
"zmij",
]
+[[package]]
+name = "serde_regex"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a8136f1a4ea815d7eac4101cfd0b16dc0cb5e1fe1b8609dfd728058656b7badf"
+dependencies = [
+ "regex",
+ "serde",
+]
+
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@@ -1363,6 +2085,28 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
+[[package]]
+name = "signal-hook-registry"
+version = "1.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
+dependencies = [
+ "errno",
+ "libc",
+]
+
+[[package]]
+name = "similar"
+version = "2.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"
+
+[[package]]
+name = "siphasher"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e"
+
[[package]]
name = "slab"
version = "0.4.11"
@@ -1375,6 +2119,16 @@ version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
+[[package]]
+name = "socket2"
+version = "0.5.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678"
+dependencies = [
+ "libc",
+ "windows-sys 0.52.0",
+]
+
[[package]]
name = "socket2"
version = "0.6.1"
@@ -1403,12 +2157,35 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
+[[package]]
+name = "string_cache"
+version = "0.8.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bf776ba3fa74f83bf4b63c3dcbbf82173db2632ed8452cb2d891d33f459de70f"
+dependencies = [
+ "new_debug_unreachable",
+ "parking_lot",
+ "phf_shared",
+ "precomputed-hash",
+]
+
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
+[[package]]
+name = "syn"
+version = "1.0.109"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
[[package]]
name = "syn"
version = "2.0.114"
@@ -1437,7 +2214,7 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
]
[[package]]
@@ -1474,13 +2251,44 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "term"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f"
+dependencies = [
+ "dirs-next",
+ "rustversion",
+ "winapi",
+]
+
+[[package]]
+name = "thiserror"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
+dependencies = [
+ "thiserror-impl 1.0.69",
+]
+
[[package]]
name = "thiserror"
version = "2.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
dependencies = [
- "thiserror-impl",
+ "thiserror-impl 2.0.18",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.114",
]
[[package]]
@@ -1491,7 +2299,16 @@ checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
+]
+
+[[package]]
+name = "tiny-keccak"
+version = "2.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
+dependencies = [
+ "crunchy",
]
[[package]]
@@ -1529,10 +2346,23 @@ dependencies = [
"libc",
"mio",
"pin-project-lite",
- "socket2",
+ "signal-hook-registry",
+ "socket2 0.6.1",
+ "tokio-macros",
"windows-sys 0.61.2",
]
+[[package]]
+name = "tokio-macros"
+version = "2.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.114",
+]
+
[[package]]
name = "tokio-rustls"
version = "0.26.4"
@@ -1580,8 +2410,8 @@ dependencies = [
"bitflags",
"bytes",
"futures-util",
- "http",
- "http-body",
+ "http 1.4.0",
+ "http-body 1.0.1",
"iri-string",
"pin-project-lite",
"tower",
@@ -1638,6 +2468,12 @@ version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
+[[package]]
+name = "unicode-xid"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
+
[[package]]
name = "universal-hash"
version = "0.5.1"
@@ -1693,6 +2529,12 @@ dependencies = [
"getrandom 0.3.4",
]
+[[package]]
+name = "value-bag"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ba6f5989077681266825251a52748b8c1d8a4ad098cc37e440103d0ea717fc0"
+
[[package]]
name = "vcpkg"
version = "0.2.15"
@@ -1705,6 +2547,16 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+[[package]]
+name = "walkdir"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
+dependencies = [
+ "same-file",
+ "winapi-util",
+]
+
[[package]]
name = "want"
version = "0.3.1"
@@ -1775,7 +2627,7 @@ dependencies = [
"bumpalo",
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
"wasm-bindgen-shared",
]
@@ -1808,6 +2660,37 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
+dependencies = [
+ "windows-sys 0.61.2",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
+
[[package]]
name = "windows-link"
version = "0.2.1"
@@ -2055,7 +2938,7 @@ checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
"synstructure",
]
@@ -2076,7 +2959,7 @@ checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
]
[[package]]
@@ -2096,7 +2979,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
"synstructure",
]
@@ -2117,7 +3000,7 @@ checksum = "85a5b4158499876c763cb03bc4e49185d3cccbabb15b33c627f7884f43db852e"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
]
[[package]]
@@ -2150,7 +3033,7 @@ checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3"
dependencies = [
"proc-macro2",
"quote",
- "syn",
+ "syn 2.0.114",
]
[[package]]
diff --git a/rust/llmchat/sync/Cargo.toml b/rust/llmchat/sync/Cargo.toml
index a95c2aef8b..aff7a8a8d8 100644
--- a/rust/llmchat/sync/Cargo.toml
+++ b/rust/llmchat/sync/Cargo.toml
@@ -13,6 +13,8 @@ thiserror = "2"
uuid = { version = "1", features = ["v4", "rng-getrandom"] }
hkdf = "0.12"
sha2 = "0.10"
+zeroize = "1"
[dev-dependencies]
+httpmock = "0.7"
tempfile = "3"
diff --git a/rust/llmchat/sync/src/conflict.rs b/rust/llmchat/sync/src/conflict.rs
index b294de134d..4ffb367311 100644
--- a/rust/llmchat/sync/src/conflict.rs
+++ b/rust/llmchat/sync/src/conflict.rs
@@ -9,6 +9,7 @@ pub fn find_duplicate_message(
text: &str,
attachments: &[AttachmentMeta],
created_at: i64,
+ parent: Option<Uuid>,
) -> Option<Uuid> {
let signature = attachments_signature(attachments);
local_messages.iter().find_map(|message| {
@@ -18,6 +19,9 @@ pub fn find_duplicate_message(
if message.text != text {
return None;
}
+ if message.parent_message_uuid != parent {
+ return None;
+ }
if attachments_signature(&message.attachments) != signature {
return None;
}
@@ -83,6 +87,71 @@ fn dfs_order(
fn attachments_signature(attachments: &[AttachmentMeta]) -> Vec<String> {
attachments
.iter()
- .map(|att| format!("{:?}:{}", att.kind, att.name))
+ .map(|att| format!("{:?}:{}:{}", att.kind, att.name, att.size))
.collect()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use llmchat_db::{AttachmentKind, AttachmentMeta, Message, Sender};
+ use uuid::Uuid;
+
+ #[test]
+ fn duplicate_message_respects_parent_and_attachment_size() {
+ let session_uuid = Uuid::new_v4();
+ let parent_uuid = Uuid::new_v4();
+ let message_uuid = Uuid::new_v4();
+ let attachment = AttachmentMeta {
+ id: "att-1".to_string(),
+ kind: AttachmentKind::Image,
+ size: 512,
+ name: "photo.png".to_string(),
+ };
+
+ let local = Message {
+ uuid: message_uuid,
+ session_uuid,
+ parent_message_uuid: Some(parent_uuid),
+ sender: Sender::SelfUser,
+ text: "hello".to_string(),
+ attachments: vec![attachment.clone()],
+ created_at: 10_000,
+ needs_sync: true,
+ deleted_at: None,
+ };
+
+ let wrong_size = AttachmentMeta { size: 256, ..attachment.clone() };
+ assert!(find_duplicate_message(
+ &[local.clone()],
+ &Sender::SelfUser,
+ "hello",
+ &[wrong_size],
+ 10_000,
+ Some(parent_uuid),
+ )
+ .is_none());
+
+ assert!(find_duplicate_message(
+ &[local.clone()],
+ &Sender::SelfUser,
+ "hello",
+ &[attachment.clone()],
+ 10_000,
+ None,
+ )
+ .is_none());
+
+ assert_eq!(
+ find_duplicate_message(
+ &[local],
+ &Sender::SelfUser,
+ "hello",
+ &[attachment],
+ 10_000,
+ Some(parent_uuid),
+ ),
+ Some(message_uuid)
+ );
+ }
+}
diff --git a/rust/llmchat/sync/src/diff_cursor.rs b/rust/llmchat/sync/src/diff_cursor.rs
index e23ba19e68..2f1bc37ede 100644
--- a/rust/llmchat/sync/src/diff_cursor.rs
+++ b/rust/llmchat/sync/src/diff_cursor.rs
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
-#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub struct SyncCursor {
pub base_since_time: i64,
diff --git a/rust/llmchat/sync/src/errors.rs b/rust/llmchat/sync/src/errors.rs
index 832f495b3e..5b81ee881d 100644
--- a/rust/llmchat/sync/src/errors.rs
+++ b/rust/llmchat/sync/src/errors.rs
@@ -7,7 +7,7 @@ pub enum SyncError {
#[error("unauthorized")]
Unauthorized,
#[error("db error: {0}")]
- Db(String),
+ Db(#[from] llmchat_db::Error),
#[error("crypto error: {0}")]
Crypto(String),
#[error("http {status}: {message}")]
@@ -30,12 +30,6 @@ pub enum SyncError {
Io(String),
}
-impl From<llmchat_db::Error> for SyncError {
- fn from(err: llmchat_db::Error) -> Self {
- SyncError::Db(err.to_string())
- }
-}
-
impl From<ente_core::crypto::CryptoError> for SyncError {
fn from(err: ente_core::crypto::CryptoError) -> Self {
SyncError::Crypto(err.to_string())
diff --git a/rust/llmchat/sync/src/http.rs b/rust/llmchat/sync/src/http.rs
index 71cdb10909..4da9866b21 100644
--- a/rust/llmchat/sync/src/http.rs
+++ b/rust/llmchat/sync/src/http.rs
@@ -2,12 +2,16 @@ use std::time::Duration;
use reqwest::blocking::{Client, Response};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue, LOCATION};
+use reqwest::redirect::Policy;
+use reqwest::Url;
use serde::de::DeserializeOwned;
use serde::Serialize;
use uuid::Uuid;
use crate::errors::SyncError;
+const DEFAULT_BASE_URL: &str = "https://api.ente.io";
+
#[derive(Debug, Clone)]
pub struct HttpConfig {
pub base_url: String,
@@ -20,6 +24,7 @@ pub struct HttpConfig {
pub struct HttpClient {
base_url: String,
+ base_origin: Url,
client: Client,
auth_token: String,
user_agent: Option<String>,
@@ -29,11 +34,18 @@ pub struct HttpClient {
impl HttpClient {
pub fn new(config: HttpConfig) -> Result<Self, SyncError> {
- let mut builder = Client::builder();
+ let base_url = config.base_url.trim_end_matches('/').to_string();
+ let base_origin = Url::parse(&base_url)
+ .map_err(|_| SyncError::InvalidResponse("invalid base url".to_string()))?;
+ let is_http = base_url.starts_with("http://");
+ let is_default = base_url.starts_with(DEFAULT_BASE_URL);
+ let allow_insecure = is_http || !is_default;
+
+ let mut builder = Client::builder().redirect(Policy::none());
if let Some(timeout) = config.timeout_secs {
builder = builder.timeout(Duration::from_secs(timeout));
}
- if config.base_url.contains("ngrok-free.app") {
+ if allow_insecure {
builder = builder.danger_accept_invalid_certs(true);
}
let client = builder.build().map_err(|e| SyncError::Http {
@@ -42,7 +54,8 @@ impl HttpClient {
code: None,
})?;
Ok(Self {
- base_url: config.base_url.trim_end_matches('/').to_string(),
+ base_url,
+ base_origin,
client,
auth_token: config.auth_token,
user_agent: config.user_agent,
@@ -127,20 +140,18 @@ impl HttpClient {
}
pub fn get_bytes(&self, url: &str) -> Result<Vec<u8>, SyncError> {
- let mut current_url = url.to_string();
+ let mut current_url = self.parse_url(url)?;
for _ in 0..5 {
+ let headers = self.build_headers_for_url(&current_url);
let resp = self
.client
- .get(&current_url)
- .headers(self.build_headers())
+ .get(current_url.clone())
+ .headers(headers)
.send()
.map_err(map_reqwest_error)?;
if resp.status().is_redirection() {
if let Some(location) = resp.headers().get(LOCATION) {
- let next = location
- .to_str()
- .map_err(|_| SyncError::InvalidResponse("invalid redirect location".to_string()))?;
- current_url = next.to_string();
+ current_url = self.resolve_redirect(&current_url, location)?;
continue;
}
}
@@ -155,12 +166,13 @@ impl HttpClient {
}
fn head_status_url(&self, url: &str) -> Result<u16, SyncError> {
- let mut current_url = url.to_string();
+ let mut current_url = self.parse_url(url)?;
for _ in 0..5 {
+ let headers = self.build_headers_for_url(&current_url);
let resp = self
.client
- .head(&current_url)
- .headers(self.build_headers())
+ .head(current_url.clone())
+ .headers(headers)
.send()
.map_err(map_reqwest_error)?;
let status = resp.status().as_u16();
@@ -169,10 +181,7 @@ impl HttpClient {
}
if resp.status().is_redirection() {
if let Some(location) = resp.headers().get(LOCATION) {
- let next = location
- .to_str()
- .map_err(|_| SyncError::InvalidResponse("invalid redirect location".to_string()))?;
- current_url = next.to_string();
+ current_url = self.resolve_redirect(&current_url, location)?;
continue;
}
}
@@ -196,11 +205,11 @@ impl HttpClient {
header_map.insert(header_name, header_value);
}
- let mut current_url = url.to_string();
+ let mut current_url = self.parse_url(url)?;
for _ in 0..5 {
let resp = self
.client
- .put(&current_url)
+ .put(current_url.clone())
.headers(header_map.clone())
.body(body.to_vec())
.send()
@@ -208,10 +217,7 @@ impl HttpClient {
if resp.status().is_redirection() {
if let Some(location) = resp.headers().get(LOCATION) {
- let next = location
- .to_str()
- .map_err(|_| SyncError::InvalidResponse("invalid redirect location".to_string()))?;
- current_url = next.to_string();
+ current_url = self.resolve_redirect(&current_url, location)?;
continue;
}
}
@@ -222,6 +228,43 @@ impl HttpClient {
Err(SyncError::InvalidResponse("too many redirects".to_string()))
}
+ fn parse_url(&self, url: &str) -> Result<Url, SyncError> {
+ Url::parse(url).map_err(|_| SyncError::InvalidResponse("invalid url".to_string()))
+ }
+
+ fn resolve_redirect(&self, current_url: &Url, location: &HeaderValue) -> Result<Url, SyncError> {
+ let next = location
+ .to_str()
+ .map_err(|_| SyncError::InvalidResponse("invalid redirect location".to_string()))?;
+ Url::parse(next)
+ .or_else(|_| current_url.join(next))
+ .map_err(|_| SyncError::InvalidResponse("invalid redirect location".to_string()))
+ }
+
+ fn is_same_origin(&self, url: &Url) -> bool {
+ self.base_origin.scheme() == url.scheme()
+ && self.base_origin.host_str() == url.host_str()
+ && self.base_origin.port_or_known_default() == url.port_or_known_default()
+ }
+
+ fn build_headers_for_url(&self, url: &Url) -> HeaderMap {
+ if self.is_same_origin(url) {
+ self.build_headers()
+ } else {
+ self.build_public_headers()
+ }
+ }
+
+ fn build_public_headers(&self) -> HeaderMap {
+ let mut headers = HeaderMap::new();
+ if let Some(agent) = &self.user_agent {
+ if let Ok(value) = HeaderValue::from_str(agent) {
+ headers.insert("User-Agent", value);
+ }
+ }
+ headers
+ }
+
fn build_headers(&self) -> HeaderMap {
let mut headers = HeaderMap::new();
if let Some(agent) = &self.user_agent {
@@ -258,7 +301,7 @@ fn parse_json_response<T: DeserializeOwned>(resp: Response) -> Result<T, SyncErr
if status == 401 {
return Err(SyncError::Unauthorized);
}
- if status >= 400 {
+ if status >= 300 {
let body = resp.text().unwrap_or_default();
return Err(map_error_response(status, &body));
}
@@ -282,7 +325,7 @@ fn parse_bytes_response(resp: Response) -> Result<Vec<u8>, SyncError> {
if status == 401 {
return Err(SyncError::Unauthorized);
}
- if status >= 400 {
+ if status >= 300 {
let body = resp.text().unwrap_or_default();
return Err(map_error_response(status, &body));
}
diff --git a/rust/llmchat/sync/src/sync.rs b/rust/llmchat/sync/src/sync.rs
index 80db8b7101..8660a3d709 100644
--- a/rust/llmchat/sync/src/sync.rs
+++ b/rust/llmchat/sync/src/sync.rs
@@ -1,5 +1,8 @@
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
+use std::sync::Arc;
+
+use zeroize::Zeroizing;
use ente_core::crypto::keys;
use llmchat_db::{
@@ -26,7 +29,7 @@ const CHAT_KEY_META_KEY: &str = "llmchat.chat.key";
pub struct SyncAuth {
pub base_url: String,
pub auth_token: String,
- pub master_key: Vec<u8>,
+ pub master_key: Zeroizing<Vec<u8>>,
pub user_agent: Option<String>,
pub client_package: Option<String>,
pub client_version: Option<String>,
@@ -116,6 +119,13 @@ impl SyncEngine {
Ok(result)
}
+ pub fn sync_in_background(
+ self: Arc<Self>,
+ auth: SyncAuth,
+ ) -> std::thread::JoinHandle<Result<SyncResult, SyncError>> {
+ std::thread::spawn(move || self.sync(auth))
+ }
+
pub fn download_attachment(
&self,
auth: SyncAuth,
@@ -189,6 +199,15 @@ impl SyncEngine {
Ok(true)
}
+ pub fn download_attachment_in_background(
+ self: Arc<Self>,
+ auth: SyncAuth,
+ attachment_id: String,
+ session_uuid: String,
+ ) -> std::thread::JoinHandle<Result<bool, SyncError>> {
+ std::thread::spawn(move || self.download_attachment(auth, attachment_id, session_uuid))
+ }
+
fn pull(
&self,
http: &HttpClient,
@@ -196,7 +215,7 @@ impl SyncEngine {
result: &mut SyncResult,
) -> Result<(), SyncError> {
let mut cursor = self.load_cursor()?;
- let mut seen_cursor = None;
+ let mut seen_cursors: HashSet<SyncCursor> = HashSet::new();
loop {
let query = vec![
@@ -226,7 +245,7 @@ impl SyncEngine {
}
};
- if Some(next_cursor.clone()) == seen_cursor {
+ if !seen_cursors.insert(next_cursor.clone()) {
break;
}
@@ -236,8 +255,6 @@ impl SyncEngine {
if cursor.is_complete_cycle() {
break;
}
-
- seen_cursor = Some(cursor.clone());
}
Ok(())
@@ -326,19 +343,24 @@ impl SyncEngine {
self.db.get_messages(session_uuid).unwrap_or_default()
});
- if let Some(dup_uuid) =
- find_duplicate_message(local_messages, &sender, &payload.text, &attachments, message.created_at)
- {
- duplicate_map.insert(message_uuid, dup_uuid);
- continue;
- }
-
let parent_uuid = message
.parent_message_uuid
.as_ref()
.and_then(|parent| Uuid::parse_str(parent).ok())
.and_then(|parent| duplicate_map.get(&parent).cloned().or(Some(parent)));
+ if let Some(dup_uuid) = find_duplicate_message(
+ local_messages,
+ &sender,
+ &payload.text,
+ &attachments,
+ message.created_at,
+ parent_uuid,
+ ) {
+ duplicate_map.insert(message_uuid, dup_uuid);
+ continue;
+ }
+
let inserted = self.db.upsert_message_from_remote(
message_uuid,
session_uuid,
@@ -413,43 +435,70 @@ impl SyncEngine {
let sessions = self.db.get_sessions_needing_sync()?;
for session in sessions {
let session_uuid = session.uuid;
- let messages = self.db.get_messages(session_uuid)?;
- self.reconcile_attachments(session_uuid, &messages)?;
-
- let force_uploads = self.revalidate_uploaded_attachments(http, session_uuid, &messages)?;
- let uploaded =
- self.upload_pending_attachments(http, chat_key, session_uuid, &force_uploads)?;
- result.uploaded_attachments += uploaded;
+ let messages = self.db.get_messages_needing_sync(session_uuid)?;
+ let should_upsert_session = session.needs_sync || session.remote_id.is_none();
- let upload_states = self.collect_attachment_states(&messages)?;
- let blocked = blocked_messages(&messages, &upload_states);
+ if !messages.is_empty() {
+ self.reconcile_attachments(session_uuid, &messages)?;
- let ordered = order_for_sync(&messages);
- let filtered: Vec<Message> = ordered
- .into_iter()
- .filter(|message| !blocked.contains(&message.uuid))
- .collect();
+ let force_uploads = self.revalidate_uploaded_attachments(http, session_uuid, &messages)?;
+ let uploaded =
+ self.upload_pending_attachments(http, chat_key, session_uuid, &force_uploads)?;
+ result.uploaded_attachments += uploaded;
- let session_payload = SessionPayload {
- title: session.title.clone(),
- };
- let encrypted = encrypt_payload(&session_payload, chat_key)?;
- let session_request = UpsertSessionRequest {
- session_uuid: session_uuid.to_string(),
- root_session_uuid: session_uuid.to_string(),
- branch_from_message_uuid: None,
- encrypted_data: encrypted.encrypted_data,
- header: encrypted.header,
- created_at: session.created_at,
- };
- let _response: serde_json::Value = http.post_json("/llmchat/chat/session", &session_request)?;
- result.pushed.sessions += 1;
+ let upload_states = self.collect_attachment_states(&messages)?;
+ let blocked = blocked_messages(&messages, &upload_states);
+
+ let ordered = order_for_sync(&messages);
+ let filtered: Vec<Message> = ordered
+ .into_iter()
+ .filter(|message| !blocked.contains(&message.uuid))
+ .collect();
+
+ if should_upsert_session {
+ let session_payload = SessionPayload {
+ title: session.title.clone(),
+ };
+ let encrypted = encrypt_payload(&session_payload, chat_key)?;
+ let session_request = UpsertSessionRequest {
+ session_uuid: session_uuid.to_string(),
+ root_session_uuid: session_uuid.to_string(),
+ branch_from_message_uuid: None,
+ encrypted_data: encrypted.encrypted_data,
+ header: encrypted.header,
+ created_at: session.created_at,
+ };
+ let _response: serde_json::Value =
+ http.post_json("/llmchat/chat/session", &session_request)?;
+ result.pushed.sessions += 1;
+ }
- for message in filtered.iter() {
- self.push_message_with_retry(http, chat_key, message, result)?;
- }
+ for message in filtered.iter() {
+ self.push_message_with_retry(http, chat_key, message, result)?;
+ let _ = self.db.mark_message_synced(message.uuid);
+ }
- if blocked.is_empty() && filtered.len() == messages.len() {
+ if blocked.is_empty() && filtered.len() == messages.len() {
+ let _ = self
+ .db
+ .mark_session_synced(session_uuid, &session_uuid.to_string());
+ }
+ } else if should_upsert_session {
+ let session_payload = SessionPayload {
+ title: session.title.clone(),
+ };
+ let encrypted = encrypt_payload(&session_payload, chat_key)?;
+ let session_request = UpsertSessionRequest {
+ session_uuid: session_uuid.to_string(),
+ root_session_uuid: session_uuid.to_string(),
+ branch_from_message_uuid: None,
+ encrypted_data: encrypted.encrypted_data,
+ header: encrypted.header,
+ created_at: session.created_at,
+ };
+ let _response: serde_json::Value =
+ http.post_json("/llmchat/chat/session", &session_request)?;
+ result.pushed.sessions += 1;
let _ = self
.db
.mark_session_synced(session_uuid, &session_uuid.to_string());
@@ -692,9 +741,10 @@ impl SyncEngine {
session_uuid: Uuid,
attachment_id: &str,
) -> Result<(), SyncError> {
- let encrypted = self.ensure_encrypted_bytes(attachment_id, session_uuid, chat_key)?;
+ let (encrypted, content_length) =
+ self.encrypted_attachment_bytes(attachment_id, session_uuid, chat_key)?;
let request = UploadUrlRequest {
- content_length: encrypted.len() as i64,
+ content_length,
content_md5: None,
};
let upload_url = http.post_json::<UploadUrlResponse, _>(
@@ -725,16 +775,19 @@ impl SyncEngine {
self.db
.set_attachment_upload_state(&row.attachment_id, UploadState::Uploading)?;
- let encrypted = match self.ensure_encrypted_bytes(&row.attachment_id, session_uuid, chat_key) {
- Ok(data) => data,
- Err(err) => {
- let _ = self.db.set_attachment_upload_state(&row.attachment_id, UploadState::Failed);
- return Err(err);
- }
- };
+ let (encrypted, content_length) =
+ match self.encrypted_attachment_bytes(&row.attachment_id, session_uuid, chat_key) {
+ Ok(data) => data,
+ Err(err) => {
+ let _ = self
+ .db
+ .set_attachment_upload_state(&row.attachment_id, UploadState::Failed);
+ return Err(err);
+ }
+ };
let request = UploadUrlRequest {
- content_length: encrypted.len() as i64,
+ content_length,
content_md5: None,
};
@@ -780,6 +833,17 @@ impl SyncEngine {
Ok(uploaded)
}
+ fn encrypted_attachment_bytes(
+ &self,
+ attachment_id: &str,
+ session_uuid: Uuid,
+ chat_key: &[u8],
+ ) -> Result<(Vec<u8>, i64), SyncError> {
+ let bytes = self.ensure_encrypted_bytes(attachment_id, session_uuid, chat_key)?;
+ let size = bytes.len() as i64;
+ Ok((bytes, size))
+ }
+
fn encrypted_attachment_size(
&self,
attachment_id: &str,
@@ -791,8 +855,8 @@ impl SyncEngine {
return Ok(size as i64);
}
- let encrypted = self.ensure_encrypted_bytes(attachment_id, session_uuid, chat_key)?;
- Ok(encrypted.len() as i64)
+ let (_bytes, size) = self.encrypted_attachment_bytes(attachment_id, session_uuid, chat_key)?;
+ Ok(size)
}
fn ensure_encrypted_bytes(
diff --git a/rust/llmchat/sync/tests/sync_integration.rs b/rust/llmchat/sync/tests/sync_integration.rs
new file mode 100644
index 0000000000..a0230cf73e
--- /dev/null
+++ b/rust/llmchat/sync/tests/sync_integration.rs
@@ -0,0 +1,315 @@
+use httpmock::Method::{DELETE, GET, POST};
+use httpmock::MockServer;
+use llmchat_db::LlmChatDb;
+use llmchat_db::crypto::KEY_BYTES;
+use llmchat_sync::SyncEngine;
+use llmchat_sync::SyncAuth;
+use llmchat_sync::crypto::{encrypt_chat_key, encrypt_payload};
+use llmchat_sync::models::{
+ ChatKeyPayload, DiffResponse, DiffTombstones, MessagePayload, MessageTombstone, RemoteMessage,
+ RemoteSession, SessionPayload,
+};
+use tempfile::TempDir;
+use uuid::Uuid;
+use zeroize::Zeroizing;
+
+struct TestEnv {
+ _dir: TempDir,
+ main_db_path: String,
+ attachments_db_path: String,
+ attachments_dir: String,
+ meta_dir: String,
+ plaintext_dir: Option<String>,
+}
+
+impl TestEnv {
+ fn new() -> Self {
+ let dir = TempDir::new().expect("temp dir");
+ let main_db_path = dir.path().join("main.db");
+ let attachments_db_path = dir.path().join("attachments.db");
+ let attachments_dir = dir.path().join("attachments");
+ let meta_dir = dir.path().join("meta");
+ Self {
+ _dir: dir,
+ main_db_path: main_db_path.to_string_lossy().to_string(),
+ attachments_db_path: attachments_db_path.to_string_lossy().to_string(),
+ attachments_dir: attachments_dir.to_string_lossy().to_string(),
+ meta_dir: meta_dir.to_string_lossy().to_string(),
+ plaintext_dir: None,
+ }
+ }
+}
+
+fn make_auth(server: &MockServer, master_key: Vec<u8>) -> SyncAuth {
+ SyncAuth {
+ base_url: server.base_url(),
+ auth_token: "token".to_string(),
+ master_key: Zeroizing::new(master_key),
+ user_agent: None,
+ client_package: None,
+ client_version: None,
+ }
+}
+
+fn mock_chat_key(server: &MockServer, master_key: &[u8], chat_key: &[u8]) {
+ let encrypted = encrypt_chat_key(chat_key, master_key).expect("encrypt chat key");
+ let payload = ChatKeyPayload::from_encrypted(&encrypted);
+ server.mock(|when, then| {
+ when.method(GET).path("/llmchat/chat/key");
+ then.status(200).json_body_obj(&payload);
+ });
+}
+
+fn mock_empty_diff(server: &MockServer, timestamp: i64) {
+ let diff = DiffResponse {
+ sessions: Vec::new(),
+ messages: Vec::new(),
+ tombstones: DiffTombstones::default(),
+ cursor: None,
+ timestamp: Some(timestamp),
+ };
+ server.mock(|when, then| {
+ when.method(GET).path("/llmchat/chat/diff");
+ then.status(200).json_body_obj(&diff);
+ });
+}
+
+#[test]
+fn pull_applies_remote_sessions_and_messages() {
+ let env = TestEnv::new();
+ let db_key = vec![42u8; KEY_BYTES];
+ let engine = SyncEngine::new(
+ env.main_db_path.clone(),
+ env.attachments_db_path.clone(),
+ db_key.clone(),
+ env.attachments_dir.clone(),
+ env.meta_dir.clone(),
+ env.plaintext_dir.clone(),
+ )
+ .expect("sync engine");
+
+ let master_key = vec![9u8; KEY_BYTES];
+ let chat_key = ente_core::crypto::keys::generate_stream_key();
+
+ let server = MockServer::start();
+ mock_chat_key(&server, &master_key, &chat_key);
+
+ let session_uuid = Uuid::new_v4();
+ let message_uuid = Uuid::new_v4();
+
+ let session_payload = SessionPayload {
+ title: "Remote Session".to_string(),
+ };
+ let encrypted_session =
+ encrypt_payload(&session_payload, &chat_key).expect("encrypt session");
+
+ let message_payload = MessagePayload {
+ text: "hello".to_string(),
+ };
+ let encrypted_message =
+ encrypt_payload(&message_payload, &chat_key).expect("encrypt message");
+
+ let diff = DiffResponse {
+ sessions: vec![RemoteSession {
+ session_uuid: session_uuid.to_string(),
+ root_session_uuid: None,
+ branch_from_message_uuid: None,
+ encrypted_data: encrypted_session.encrypted_data,
+ header: encrypted_session.header,
+ created_at: 10,
+ updated_at: 11,
+ is_deleted: None,
+ }],
+ messages: vec![RemoteMessage {
+ message_uuid: message_uuid.to_string(),
+ session_uuid: session_uuid.to_string(),
+ parent_message_uuid: None,
+ sender: "self".to_string(),
+ attachments: Vec::new(),
+ encrypted_data: encrypted_message.encrypted_data,
+ header: encrypted_message.header,
+ created_at: 12,
+ updated_at: None,
+ is_deleted: None,
+ }],
+ tombstones: DiffTombstones::default(),
+ cursor: None,
+ timestamp: Some(12),
+ };
+
+ server.mock(|when, then| {
+ when.method(GET).path("/llmchat/chat/diff");
+ then.status(200).json_body_obj(&diff);
+ });
+
+ let auth = make_auth(&server, master_key);
+ engine.sync(auth).expect("sync");
+
+ let db = LlmChatDb::open_sqlite_with_defaults(
+ env.main_db_path.clone(),
+ env.attachments_db_path.clone(),
+ db_key,
+ )
+ .expect("db");
+
+ let sessions = db.list_sessions().expect("sessions");
+ assert_eq!(sessions.len(), 1);
+ assert_eq!(sessions[0].uuid, session_uuid);
+ assert_eq!(sessions[0].title, "Remote Session");
+
+ let messages = db.get_messages(session_uuid).expect("messages");
+ assert_eq!(messages.len(), 1);
+ assert_eq!(messages[0].uuid, message_uuid);
+ assert_eq!(messages[0].text, "hello");
+ assert!(!messages[0].needs_sync);
+}
+
+#[test]
+fn push_only_needs_sync_messages() {
+ let env = TestEnv::new();
+ let db_key = vec![11u8; KEY_BYTES];
+ let engine = SyncEngine::new(
+ env.main_db_path.clone(),
+ env.attachments_db_path.clone(),
+ db_key.clone(),
+ env.attachments_dir.clone(),
+ env.meta_dir.clone(),
+ env.plaintext_dir.clone(),
+ )
+ .expect("sync engine");
+
+ let db = LlmChatDb::open_sqlite_with_defaults(
+ env.main_db_path.clone(),
+ env.attachments_db_path.clone(),
+ db_key.clone(),
+ )
+ .expect("db");
+
+ let session = db.create_session("Local Session").expect("session");
+ let message_one = db
+ .insert_message(session.uuid, "self", "one", None, Vec::new())
+ .expect("message one");
+ let _message_two = db
+ .insert_message(session.uuid, "self", "two", None, Vec::new())
+ .expect("message two");
+ db.mark_message_synced(message_one.uuid)
+ .expect("mark synced");
+
+ let master_key = vec![7u8; KEY_BYTES];
+ let chat_key = ente_core::crypto::keys::generate_stream_key();
+ let server = MockServer::start();
+ mock_chat_key(&server, &master_key, &chat_key);
+ mock_empty_diff(&server, 10);
+
+ let session_mock = server.mock(|when, then| {
+ when.method(POST).path("/llmchat/chat/session");
+ then.status(200).json_body_obj(&serde_json::json!({}));
+ });
+
+ let message_mock = server.mock(|when, then| {
+ when.method(POST).path("/llmchat/chat/message");
+ then.status(200).json_body_obj(&serde_json::json!({}));
+ });
+
+ let auth = make_auth(&server, master_key);
+ engine.sync(auth).expect("sync");
+
+ assert_eq!(session_mock.hits(), 1);
+ assert_eq!(message_mock.hits(), 1);
+
+ let messages = db.get_messages(session.uuid).expect("messages");
+ assert!(messages.iter().all(|message| !message.needs_sync));
+}
+
+#[test]
+fn tombstone_removes_messages() {
+ let env = TestEnv::new();
+ let db_key = vec![3u8; KEY_BYTES];
+ let engine = SyncEngine::new(
+ env.main_db_path.clone(),
+ env.attachments_db_path.clone(),
+ db_key.clone(),
+ env.attachments_dir.clone(),
+ env.meta_dir.clone(),
+ env.plaintext_dir.clone(),
+ )
+ .expect("sync engine");
+
+ let master_key = vec![12u8; KEY_BYTES];
+ let chat_key = ente_core::crypto::keys::generate_stream_key();
+
+ let server = MockServer::start();
+ mock_chat_key(&server, &master_key, &chat_key);
+
+ let session_uuid = Uuid::new_v4();
+ let message_uuid = Uuid::new_v4();
+
+ let session_payload = SessionPayload {
+ title: "Remote Session".to_string(),
+ };
+ let encrypted_session =
+ encrypt_payload(&session_payload, &chat_key).expect("encrypt session");
+
+ let message_payload = MessagePayload {
+ text: "to delete".to_string(),
+ };
+ let encrypted_message =
+ encrypt_payload(&message_payload, &chat_key).expect("encrypt message");
+
+ let diff = DiffResponse {
+ sessions: vec![RemoteSession {
+ session_uuid: session_uuid.to_string(),
+ root_session_uuid: None,
+ branch_from_message_uuid: None,
+ encrypted_data: encrypted_session.encrypted_data,
+ header: encrypted_session.header,
+ created_at: 10,
+ updated_at: 11,
+ is_deleted: None,
+ }],
+ messages: vec![RemoteMessage {
+ message_uuid: message_uuid.to_string(),
+ session_uuid: session_uuid.to_string(),
+ parent_message_uuid: None,
+ sender: "self".to_string(),
+ attachments: Vec::new(),
+ encrypted_data: encrypted_message.encrypted_data,
+ header: encrypted_message.header,
+ created_at: 12,
+ updated_at: None,
+ is_deleted: None,
+ }],
+ tombstones: DiffTombstones {
+ sessions: Vec::new(),
+ messages: vec![MessageTombstone {
+ message_uuid: message_uuid.to_string(),
+ deleted_at: Some(15),
+ }],
+ },
+ cursor: None,
+ timestamp: Some(15),
+ };
+
+ server.mock(|when, then| {
+ when.method(GET).path("/llmchat/chat/diff");
+ then.status(200).json_body_obj(&diff);
+ });
+
+ server.mock(|when, then| {
+ when.method(DELETE).path("/llmchat/chat/message");
+ then.status(200).json_body_obj(&serde_json::json!({}));
+ });
+
+ let auth = make_auth(&server, master_key);
+ engine.sync(auth).expect("sync");
+
+ let db = LlmChatDb::open_sqlite_with_defaults(
+ env.main_db_path.clone(),
+ env.attachments_db_path.clone(),
+ db_key,
+ )
+ .expect("db");
+
+ let messages = db.get_messages(session_uuid).expect("messages");
+ assert!(messages.is_empty());
+}
diff --git a/rust/llmchat/uniffi/llmchat_sync_uniffi/src/api.rs b/rust/llmchat/uniffi/llmchat_sync_uniffi/src/api.rs
index 01a74ac363..15c6a1a61c 100644
--- a/rust/llmchat/uniffi/llmchat_sync_uniffi/src/api.rs
+++ b/rust/llmchat/uniffi/llmchat_sync_uniffi/src/api.rs
@@ -28,7 +28,7 @@ impl From<SyncAuth> for core::SyncAuth {
core::SyncAuth {
base_url: value.base_url,
auth_token: value.auth_token,
- master_key: value.master_key,
+ master_key: value.master_key.into(),
user_agent: value.user_agent,
client_package: value.client_package,
client_version: value.client_version,