Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refine Your Notes integration

setalpfca7ae4ecbb0
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index e69506f6b2..a22e495af7 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2381,6 +2381,7 @@ dependencies = [
"tauri-plugin-fs",
"tauri-plugin-opener",
"tauri-plugin-process",
+ "tauri-plugin-single-instance",
"tauri-plugin-updater",
"uuid",
"windows-sys 0.61.2",
@@ -2540,6 +2541,7 @@ dependencies = [
"self_cell",
"serde",
"serde_json",
+ "sha2 0.11.0",
"tempfile",
"thiserror 2.0.18",
"tokio",
@@ -9455,6 +9457,22 @@ dependencies = [
"tauri-plugin",
]
+[[package]]
+name = "tauri-plugin-single-instance"
+version = "2.4.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5cd0cb5c412a5071b69bab6a6df1583cbb89460d4a83b6a24769b08d15b6b1e1"
+dependencies = [
+ "serde",
+ "serde_json",
+ "tauri",
+ "thiserror 2.0.18",
+ "tokio",
+ "tracing",
+ "windows-sys 0.60.2",
+ "zbus 5.15.0",
+]
+
[[package]]
name = "tauri-plugin-updater"
version = "2.10.1"
diff --git a/rust/apps/ensu/src-tauri/Cargo.toml b/rust/apps/ensu/src-tauri/Cargo.toml
index 261eb359e8..7c9aae59b4 100644
--- a/rust/apps/ensu/src-tauri/Cargo.toml
+++ b/rust/apps/ensu/src-tauri/Cargo.toml
@@ -21,6 +21,7 @@ tauri-plugin-dialog = "2.7.1"
tauri-plugin-fs = "2.5.1"
tauri-plugin-opener = "2.5.4"
tauri-plugin-process = "2.3.1"
+tauri-plugin-single-instance = "2.4.4"
tauri-plugin-updater = "2.10.1"
uuid = { version = "1", features = ["v4"] }
diff --git a/rust/apps/ensu/src-tauri/src/commands/knowledge.rs b/rust/apps/ensu/src-tauri/src/commands/knowledge.rs
index bf914c569d..a7239645ea 100644
--- a/rust/apps/ensu/src-tauri/src/commands/knowledge.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/knowledge.rs
@@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex, PoisonError};
use ente_assets::{AssetStore, download};
use ente_ensu::config::{AttributionConfig, KnowledgeDatasetConfig};
-use ente_ensu::{llm, retrieval};
+use ente_ensu::retrieval;
use serde::{Deserialize, Serialize};
use tauri::async_runtime;
use tauri::{AppHandle, Emitter, Manager, State as TauriState, WebviewWindow};
@@ -514,8 +514,17 @@ pub async fn knowledge_retrieve(
retrieval_epoch: u64,
) -> Result<Option<GroundedPromptContextDto>, ApiError> {
let knowledge_state = app.state::<State>();
- let notes_state = app.state::<crate::commands::notes::State>();
- let note_collection_ids = notes_state.available_index_collection_ids();
+ let (note_collection_ids, notes) = app
+ .try_state::<crate::commands::notes::State>()
+ .map_or_else(
+ || (Vec::new(), None),
+ |state| {
+ (
+ state.available_index_collection_ids(),
+ Some(state.retrieval_handle()),
+ )
+ },
+ );
if query.trim().is_empty()
|| (enabled_stable_ids.is_empty() && note_collection_ids.is_empty())
|| max_context_utf8_bytes == 0
@@ -543,7 +552,6 @@ pub async fn knowledge_retrieve(
let embedding_path = ente_ensu::model::llm_model_path(&store, &embedding_asset)
.ok_or_else(|| ApiError::new("embedding_missing", "Embedding model path is missing"))?;
let indexes = Arc::clone(&knowledge_state.indexes);
- let notes = notes_state.retrieval_handle();
let cancellation_epoch = llm_state.retrieval_epoch();
if cancellation_epoch.load(Ordering::Relaxed) != retrieval_epoch {
return Err(ApiError::new("cancelled", "Knowledge retrieval cancelled"));
@@ -563,20 +571,10 @@ pub async fn knowledge_retrieve(
}
};
check_cancelled()?;
- let model = llm::Model::load(llm::ModelLoadParams {
- model_path: embedding_path.display().to_string(),
- n_gpu_layers: Some(0),
- use_mmap: Some(true),
- use_mlock: Some(false),
- })
- .map_err(crate::commands::llm::llm_api_error)?;
- check_cancelled()?;
- let threads = std::thread::available_parallelism()
- .map(|count| count.get().saturating_sub(1).max(1))
- .unwrap_or(1);
- let threads = i32::try_from(threads).unwrap_or(1);
- let context = llm::Context::new_knowledge_embedding(&model, Some(threads))
- .map_err(crate::commands::llm::llm_api_error)?;
+ let context = crate::commands::llm::load_knowledge_embedding_context(
+ &embedding_path,
+ check_cancelled,
+ )?;
check_cancelled()?;
let query_embedding = context
.embed(query.trim())
@@ -616,14 +614,16 @@ pub async fn knowledge_retrieve(
check_cancelled()?;
let mut note_hits = Vec::new();
- for collection_id in &note_collection_ids {
- check_cancelled()?;
- match notes.search_collection(collection_id, &query_embedding) {
- Ok(collection_hits) => note_hits.extend(collection_hits),
- Err(error) => logging::log(
- "Knowledge",
- format!("Notes search skipped collection={collection_id} error={error}"),
- ),
+ if let Some(notes) = &notes {
+ for collection_id in &note_collection_ids {
+ check_cancelled()?;
+ match notes.search_collection(collection_id, &query_embedding) {
+ Ok(collection_hits) => note_hits.extend(collection_hits),
+ Err(error) => logging::log(
+ "Knowledge",
+ format!("Notes search skipped collection={collection_id} error={error}"),
+ ),
+ }
}
}
check_cancelled()?;
@@ -631,6 +631,9 @@ pub async fn knowledge_retrieve(
let context_budget = max_context_utf8_bytes.min(embedding.max_context_utf8_bytes);
let mut excerpts = select_verified_mixed_grounding(&pack_hits, &note_hits, |reference| {
check_cancelled()?;
+ let Some(notes) = &notes else {
+ return Ok(false);
+ };
let verified = notes.verify_source_reference(reference);
if !verified {
crate::commands::notes::mark_reference_stale(
@@ -642,7 +645,9 @@ pub async fn knowledge_retrieve(
Ok(verified)
})?;
for excerpt in &mut excerpts {
- if let retrieval::GroundedSource::LocalNote { reference } = &mut excerpt.source {
+ if let (Some(notes), retrieval::GroundedSource::LocalNote { reference }) =
+ (&notes, &mut excerpt.source)
+ {
reference.collection_label = notes.collection_label(&reference.collection_id);
}
}
diff --git a/rust/apps/ensu/src-tauri/src/commands/llm.rs b/rust/apps/ensu/src-tauri/src/commands/llm.rs
index ea1fc3c30e..8fd018ea87 100644
--- a/rust/apps/ensu/src-tauri/src/commands/llm.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/llm.rs
@@ -1,5 +1,5 @@
use std::panic::{AssertUnwindSafe, catch_unwind};
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};
@@ -155,6 +155,25 @@ fn default_threads() -> i32 {
i32::try_from(threads).unwrap_or(1)
}
+pub(crate) fn load_knowledge_embedding_context(
+ model_path: &Path,
+ check_cancelled: impl Fn() -> Result<(), ApiError>,
+) -> Result<llm::ContextRef, ApiError> {
+ let model = llm::Model::load(llm::ModelLoadParams {
+ model_path: model_path.display().to_string(),
+ n_gpu_layers: Some(0),
+ use_mmap: Some(true),
+ use_mlock: Some(false),
+ })
+ .map_err(llm_api_error)?;
+ check_cancelled()?;
+ let threads = std::thread::available_parallelism()
+ .map(|count| count.get().saturating_sub(1).max(1))
+ .unwrap_or(1);
+ llm::Context::new_knowledge_embedding(&model, Some(i32::try_from(threads).unwrap_or(1)))
+ .map_err(llm_api_error)
+}
+
#[derive(Serialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
enum Event {
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes.rs b/rust/apps/ensu/src-tauri/src/commands/notes.rs
index ea5a0ff752..48f85463c4 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes.rs
@@ -547,7 +547,12 @@ impl State {
.unwrap_or_else(PoisonError::into_inner)
.iter()
.filter_map(|(collection_id, runtime)| {
- runtime.index_available.then_some(collection_id.clone())
+ (runtime.index_available
+ && !matches!(
+ runtime.status,
+ NotesCollectionStatusDto::Unavailable | NotesCollectionStatusDto::Error
+ ))
+ .then_some(collection_id.clone())
})
.collect::<BTreeSet<_>>();
let collections = self
@@ -559,10 +564,7 @@ impl State {
.clone();
collections
.into_iter()
- .filter(|collection| {
- available_ids.contains(&collection.id)
- && source_root_is_available(&collection.source_root)
- })
+ .filter(|collection| available_ids.contains(&collection.id))
.map(|collection| collection.id)
.collect()
}
@@ -648,9 +650,8 @@ fn collection_dto(
}
#[tauri::command]
-pub fn notes_list_collections(
- notes_state: TauriState<'_, State>,
-) -> Result<Vec<NotesCollectionDto>, ApiError> {
+pub fn notes_list_collections(app: AppHandle) -> Result<Vec<NotesCollectionDto>, ApiError> {
+ let notes_state = available_state(&app)?;
let store = notes_state
.registry
.lock()
@@ -685,32 +686,30 @@ pub fn notes_list_collections(
}
#[tauri::command]
-pub fn notes_has_available_index(notes_state: TauriState<'_, State>) -> bool {
- !notes_state.available_index_collection_ids().is_empty()
-}
-
-#[tauri::command]
-pub fn notes_add_collection(
+pub async fn notes_add_collection(
app: AppHandle,
- notes_state: TauriState<'_, State>,
source_root: String,
) -> Result<NotesCollectionDto, ApiError> {
- let root = canonical_source_root(Path::new(&source_root))?;
- if !app.fs_scope().is_allowed(&root) {
- return Err(ApiError::new(
- "io_scope",
- "Notes folder is outside the allowed filesystem scope",
- ));
- }
- register_collection(&app, &notes_state, &root)
+ async_runtime::spawn_blocking(move || {
+ let root = canonical_source_root(Path::new(&source_root))?;
+ if !app.fs_scope().is_allowed(&root) {
+ return Err(ApiError::new(
+ "io_scope",
+ "Notes folder is outside the allowed filesystem scope",
+ ));
+ }
+ let notes_state = available_state(&app)?;
+ register_collection(&app, &notes_state, root)
+ })
+ .await
+ .map_err(|_| ApiError::new("io_thread", "Notes folder registration task failed"))?
}
fn register_collection(
app: &AppHandle,
notes_state: &State,
- source_root: &Path,
+ root: PathBuf,
) -> Result<NotesCollectionDto, ApiError> {
- let root = canonical_source_root(source_root)?;
let notes_directory = notes_state
.index_root
.parent()
@@ -753,12 +752,22 @@ fn register_collection(
#[tauri::command]
pub async fn notes_remove_collection(
- notes_state: TauriState<'_, State>,
+ app: AppHandle,
collection_id: String,
) -> Result<(), ApiError> {
+ let notes_state = available_state(&app)?;
let lifecycle = notes_state.collection_lifecycle(&collection_id);
let _lifecycle = lifecycle.lock().await;
- let collection = notes_state.registered_collection(&collection_id)?;
+ async_runtime::spawn_blocking(move || {
+ let notes_state = available_state(&app)?;
+ remove_collection(&notes_state, &collection_id)
+ })
+ .await
+ .map_err(|_| ApiError::new("io_thread", "Notes folder removal task failed"))?
+}
+
+fn remove_collection(notes_state: &State, collection_id: &str) -> Result<(), ApiError> {
+ let collection = notes_state.registered_collection(collection_id)?;
let derived_directory = notes_state.index_root.join(&collection.id);
{
let mut store = notes_state
@@ -775,19 +784,19 @@ pub async fn notes_remove_collection(
.watchers
.lock()
.unwrap_or_else(PoisonError::into_inner)
- .remove(&collection_id);
+ .remove(collection_id);
notes_state
.watch_work
.lock()
.unwrap_or_else(PoisonError::into_inner)
- .remove(&collection_id);
- notes_state.cancel_update(&collection_id);
- notes_state.evict_cached_index(&collection_id);
+ .remove(collection_id);
+ notes_state.cancel_update(collection_id);
+ notes_state.evict_cached_index(collection_id);
notes_state
.runtimes
.lock()
.unwrap_or_else(PoisonError::into_inner)
- .remove(&collection_id);
+ .remove(collection_id);
let cleanup_result = (|| -> Result<(), ApiError> {
if path_exists(&derived_directory)? {
remove_owned_entry(&derived_directory)?;
@@ -807,7 +816,7 @@ pub async fn notes_remove_collection(
.collection_lifecycles
.lock()
.unwrap_or_else(PoisonError::into_inner)
- .remove(&collection_id);
+ .remove(collection_id);
Ok(())
}
@@ -849,11 +858,11 @@ pub async fn notes_index_collection(
app: AppHandle,
model_state: TauriState<'_, ModelDownloadState>,
llm_state: TauriState<'_, crate::commands::llm::State>,
- notes_state: TauriState<'_, State>,
collection_id: String,
force: bool,
retrieval_epoch: u64,
) -> Result<NotesIndexResultDto, ApiError> {
+ let notes_state = available_state(&app)?;
let lifecycle = notes_state.collection_lifecycle(&collection_id);
let _collection_lifecycle = lifecycle.lock().await;
let collection = notes_state.registered_collection(&collection_id)?;
@@ -914,22 +923,9 @@ pub async fn notes_index_collection(
},
)
})
- .await;
- let result = match result {
- Ok(result) => result,
- Err(_) => {
- let error = ApiError::new("index_thread", "Notes indexing task failed");
- report_index_error(
- &app,
- &notes_state,
- &collection_id,
- Some(update_generation),
- &error,
- );
- return Err(error);
- }
- };
-
+ .await
+ .map_err(|_| ApiError::new("index_thread", "Notes indexing task failed"))
+ .and_then(|result| result);
let outcome = match result {
Ok(outcome) => outcome,
Err(error) => {
@@ -1002,27 +998,33 @@ pub async fn notes_index_collection(
}
#[tauri::command]
-pub fn notes_open_document(
+pub async fn notes_open_document(
app: AppHandle,
- notes_state: TauriState<'_, State>,
collection_id: String,
document_id: String,
indexed_revision: String,
) -> Result<(), ApiError> {
- let collection = notes_state.registered_collection(&collection_id)?;
- let (path, bytes, _) = read_collection_source(&collection.source_root, &document_id)?;
- if !is_supported_file(&path) {
- return Err(ApiError::new(
- "invalid_document",
- "Unsupported note document",
- ));
- }
- if notes_content_revision(&bytes) != indexed_revision {
- return Err(ApiError::new(
- "source_changed",
- "Source note changed after this answer was generated",
- ));
- }
+ let verification_app = app.clone();
+ let path = async_runtime::spawn_blocking(move || {
+ let notes_state = available_state(&verification_app)?;
+ let collection = notes_state.registered_collection(&collection_id)?;
+ let (path, bytes, _) = read_collection_source(&collection.source_root, &document_id)?;
+ if !is_supported_file(&path) {
+ return Err(ApiError::new(
+ "invalid_document",
+ "Unsupported note document",
+ ));
+ }
+ if notes_content_revision(&bytes) != indexed_revision {
+ return Err(ApiError::new(
+ "source_changed",
+ "Source note changed after this answer was generated",
+ ));
+ }
+ Ok(path)
+ })
+ .await
+ .map_err(|_| ApiError::new("io_thread", "Source note verification task failed"))??;
app.opener()
.open_path(path.to_string_lossy().into_owned(), None::<String>)
.map_err(|_| ApiError::new("open_failed", "Source note could not be opened"))
@@ -1056,6 +1058,15 @@ fn notes_error(error: NotesError) -> ApiError {
ApiError::new(code, message)
}
+fn available_state(app: &AppHandle) -> Result<TauriState<'_, State>, ApiError> {
+ app.try_state::<State>().ok_or_else(|| {
+ ApiError::new(
+ "notes_unavailable",
+ "Notes are unavailable because local storage could not be initialized",
+ )
+ })
+}
+
fn ensure_directory(path: &Path) -> Result<(), ApiError> {
if !path_exists(path)? {
fs::create_dir_all(path).map_err(io_error)?;
@@ -1158,34 +1169,6 @@ mod tests {
writer.publish(true).unwrap();
}
- #[test]
- fn retrieval_cache_retains_multiple_collection_indexes_within_budget() {
- let temp = TestDirectory::new();
- let index_root = temp.path().join("indexes");
- publish_test_index(&index_root, COLLECTION_ID, "first.md");
- publish_test_index(&index_root, SECOND_COLLECTION_ID, "other.md");
- let cached_indexes = Arc::new(Mutex::new(IndexCache::new(NOTES_INDEX_CACHE_MAX_BYTES)));
- let handle = RetrievalHandle {
- registry: Arc::new(Mutex::new(
- RegistryStore::open(temp.path().join("registry")).unwrap(),
- )),
- cached_indexes: Arc::clone(&cached_indexes),
- index_root,
- };
- let mut query = vec![0.0; 512];
- query[0] = 1.0;
-
- handle.search_collection(COLLECTION_ID, &query).unwrap();
- handle
- .search_collection(SECOND_COLLECTION_ID, &query)
- .unwrap();
- let cached_indexes = cached_indexes
- .lock()
- .unwrap_or_else(PoisonError::into_inner);
- assert!(cached_indexes.entries.contains_key(COLLECTION_ID));
- assert!(cached_indexes.entries.contains_key(SECOND_COLLECTION_ID));
- }
-
#[test]
fn retrieval_cache_evicts_the_least_recently_used_index_at_its_budget() {
let temp = TestDirectory::new();
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs b/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs
index 6aaf6a03db..c552686386 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs
@@ -63,23 +63,10 @@ pub(super) fn index_collection(
)
})?;
if embedding_context.is_none() {
- let model = llm::Model::load(llm::ModelLoadParams {
- model_path: embedding_path.display().to_string(),
- n_gpu_layers: Some(0),
- use_mmap: Some(true),
- use_mlock: Some(false),
- })
- .map_err(crate::commands::llm::llm_api_error)?;
- let threads = std::thread::available_parallelism()
- .map(|count| count.get().saturating_sub(1).max(1))
- .unwrap_or(1);
- embedding_context = Some(
- llm::Context::new_knowledge_embedding(
- &model,
- Some(i32::try_from(threads).unwrap_or(1)),
- )
- .map_err(crate::commands::llm::llm_api_error)?,
- );
+ embedding_context = Some(crate::commands::llm::load_knowledge_embedding_context(
+ embedding_path,
+ || check_cancelled(cancellation_epoch, retrieval_epoch),
+ )?);
}
let context = embedding_context
.as_ref()
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/watcher.rs b/rust/apps/ensu/src-tauri/src/commands/notes/watcher.rs
index e664e4a47b..72610e6311 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes/watcher.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes/watcher.rs
@@ -52,8 +52,10 @@ pub(super) struct StartupInspection {
}
pub async fn initialize_for_app(app: AppHandle) {
- let collections = app
- .state::<State>()
+ let Some(state) = app.try_state::<State>() else {
+ return;
+ };
+ let collections = state
.registry
.lock()
.unwrap_or_else(PoisonError::into_inner)
@@ -682,49 +684,37 @@ mod tests {
}
#[test]
- fn root_watch_events_request_identity_reattachment() {
- let root = PathBuf::from("/notes");
- let event = Event::new(EventKind::Create(CreateKind::Folder)).add_path(root.clone());
+ fn structural_watch_events_force_reconciliation_and_refresh() {
+ let temp = TestDirectory::new();
+ let root = temp.path().join("root");
+ fs::create_dir(&root).unwrap();
+ let event = Event::new(EventKind::Create(CreateKind::Folder)).add_path(root.clone());
let change = classify_watch_event(&root, &event).unwrap();
-
assert!(change.force_full_hash);
+ assert!(change.refresh_watches);
assert!(change.reattach_root);
- }
- #[test]
- fn moved_out_directories_force_reconciliation_and_watch_refresh() {
- let temp = TestDirectory::new();
- let root = temp.path().join("root");
- fs::create_dir(&root).unwrap();
let path = root.join("moved");
let event =
Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::From))).add_path(path);
-
let change = classify_watch_event(&root, &event).unwrap();
-
assert!(change.force_full_hash);
assert!(change.refresh_watches);
- }
-
- #[cfg(unix)]
- #[test]
- fn symlink_replacements_force_reconciliation_and_watch_refresh() {
- use std::os::unix::fs::symlink;
-
- let temp = TestDirectory::new();
- let root = temp.path().join("root");
- let outside = temp.path().join("outside");
- fs::create_dir(&root).unwrap();
- fs::write(&outside, "outside").unwrap();
- let path = root.join("note.md");
- symlink(outside, &path).unwrap();
- let event = Event::new(EventKind::Modify(ModifyKind::Any)).add_path(path);
-
- let change = classify_watch_event(&root, &event).unwrap();
- assert!(change.force_full_hash);
- assert!(change.refresh_watches);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::symlink;
+
+ let outside = temp.path().join("outside");
+ fs::write(&outside, "outside").unwrap();
+ let path = root.join("note.md");
+ symlink(outside, &path).unwrap();
+ let event = Event::new(EventKind::Modify(ModifyKind::Any)).add_path(path);
+ let change = classify_watch_event(&root, &event).unwrap();
+ assert!(change.force_full_hash);
+ assert!(change.refresh_watches);
+ }
}
#[test]
diff --git a/rust/apps/ensu/src-tauri/src/main.rs b/rust/apps/ensu/src-tauri/src/main.rs
index fcf4704969..4942a7be87 100644
--- a/rust/apps/ensu/src-tauri/src/main.rs
+++ b/rust/apps/ensu/src-tauri/src/main.rs
@@ -10,6 +10,12 @@ fn main() {
logging::log("App", "starting Tauri backend");
let app = tauri::Builder::default()
+ .plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
+ if let Some(window) = app.get_webview_window("main") {
+ let _ = window.show();
+ let _ = window.set_focus();
+ }
+ }))
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_fs::init())
.plugin(tauri_plugin_opener::init())
@@ -25,20 +31,34 @@ fn main() {
app.manage(commands::llm::ModelDownloadState::new(
app.path().app_data_dir()?,
));
- app.manage(
- commands::notes::State::new(app.path().app_data_dir()?)
- .map_err(|error| std::io::Error::other(error.message))?,
- );
+ let notes_available = match commands::notes::State::new(app.path().app_data_dir()?) {
+ Ok(state) => {
+ app.manage(state);
+ true
+ }
+ Err(error) => {
+ logging::log(
+ "Notes",
+ format!(
+ "initialization failed; Notes disabled error={}",
+ error.message
+ ),
+ );
+ false
+ }
+ };
if let Some(window) = app.get_webview_window("main")
&& let Err(err) = window.show()
{
logging::log("App", format!("failed to show main window error={err}"));
return Err(Box::new(err));
}
- let notes_app = app.handle().clone();
- async_runtime::spawn(async move {
- commands::notes::initialize_for_app(notes_app).await;
- });
+ if notes_available {
+ let notes_app = app.handle().clone();
+ async_runtime::spawn(async move {
+ commands::notes::initialize_for_app(notes_app).await;
+ });
+ }
logging::log("App", "setup complete");
Ok(())
})
@@ -88,7 +108,6 @@ fn main() {
commands::knowledge::knowledge_cancel_pack_download,
commands::knowledge::knowledge_retrieve,
commands::notes::notes_list_collections,
- commands::notes::notes_has_available_index,
commands::notes::notes_add_collection,
commands::notes::notes_remove_collection,
commands::notes::notes_index_collection,
diff --git a/rust/crates/ensu-crypto/src/lib.rs b/rust/crates/ensu-crypto/src/lib.rs
index b934dc8bd5..3d2f6b3662 100644
--- a/rust/crates/ensu-crypto/src/lib.rs
+++ b/rust/crates/ensu-crypto/src/lib.rs
@@ -3,23 +3,13 @@ use ente_core::{
crypto::{self, Header, Key, blob},
};
use hkdf::Hkdf;
-use sha2::{Digest, Sha256};
+use sha2::Sha256;
use thiserror::Error;
const ATTACHMENT_KEY_INFO: &[u8] = b"llmchat_attachment_v1";
pub type Result<T> = std::result::Result<T, Error>;
-pub fn sha256_hex(bytes: &[u8]) -> String {
- const HEX: &[u8; 16] = b"0123456789abcdef";
- let mut encoded = String::with_capacity(64);
- for byte in Sha256::digest(bytes) {
- encoded.push(char::from(HEX[usize::from(byte >> 4)]));
- encoded.push(char::from(HEX[usize::from(byte & 0x0f)]));
- }
- encoded
-}
-
#[derive(Debug, Error)]
pub enum Error {
#[error("invalid blob length: expected at least {minimum} bytes, got {actual}")]
diff --git a/rust/crates/ensu/Cargo.toml b/rust/crates/ensu/Cargo.toml
index beb23708bd..16c4a1fbb1 100644
--- a/rust/crates/ensu/Cargo.toml
+++ b/rust/crates/ensu/Cargo.toml
@@ -14,6 +14,7 @@ rusqlite = { version = "0.38", features = ["bundled"], optional = true }
self_cell = "1"
serde.workspace = true
serde_json.workspace = true
+sha2.workspace = true
thiserror.workspace = true
tokio = { version = "1", features = ["rt", "time"] }
transcribe-rs = { git = "https://github.com/cjpais/transcribe-rs.git", rev = "efc66111d519a434e28d51d94740008a7ce29360", default-features = false, features = ["onnx", "vad-silero"], optional = true }
diff --git a/rust/crates/ensu/src/config/knowledge.rs b/rust/crates/ensu/src/config/knowledge.rs
index a94371a9ba..9a5ccff3af 100644
--- a/rust/crates/ensu/src/config/knowledge.rs
+++ b/rust/crates/ensu/src/config/knowledge.rs
@@ -24,7 +24,7 @@ const SOURCE_DIM: u32 = 768;
const RETRIEVAL_DIM: u32 = 512;
const EMBEDDING_CONTEXT_SIZE: u32 = 2_048;
const MAX_HITS: u32 = 2;
-const MAX_CONTEXT_UTF8_BYTES: u32 = 8_192;
+const MAX_CONTEXT_UTF8_BYTES: u32 = 6_000;
const MODIFICATION_NOTICE: &str = "Adapted by Ente";
#[derive(Debug, Clone, PartialEq)]
diff --git a/rust/crates/ensu/src/notes.rs b/rust/crates/ensu/src/notes.rs
index 8622c37a08..bf31196833 100644
--- a/rust/crates/ensu/src/notes.rs
+++ b/rust/crates/ensu/src/notes.rs
@@ -8,6 +8,7 @@ mod shard;
mod source;
mod writer;
+use sha2::{Digest, Sha256};
use thiserror::Error;
use uuid::Uuid;
@@ -55,7 +56,13 @@ pub fn notes_content_revision(bytes: &[u8]) -> String {
}
fn sha256_hex(bytes: &[u8]) -> String {
- ente_ensu_crypto::sha256_hex(bytes)
+ const HEX: &[u8; 16] = b"0123456789abcdef";
+ let mut encoded = String::with_capacity(64);
+ for byte in Sha256::digest(bytes) {
+ encoded.push(char::from(HEX[usize::from(byte >> 4)]));
+ encoded.push(char::from(HEX[usize::from(byte & 0x0f)]));
+ }
+ encoded
}
pub fn validate_document_id(document_id: &str) -> Result<(), NotesError> {
diff --git a/rust/crates/ensu/src/notes/document.rs b/rust/crates/ensu/src/notes/document.rs
index 7b17ab5828..44010f52c2 100644
--- a/rust/crates/ensu/src/notes/document.rs
+++ b/rust/crates/ensu/src/notes/document.rs
@@ -121,10 +121,7 @@ mod tests {
assert!(overlap.len() <= NOTES_CHUNK_OVERLAP_UTF8_BYTES);
assert_eq!(overlap, next);
}
- }
- #[test]
- fn preserves_heading_only_outlines() {
let prepared =
prepare_notes_document("outline.md", b"# Project\n## Alice\n## Bob\n## Carol\n")
.unwrap();
@@ -141,13 +138,10 @@ mod tests {
}
#[test]
- fn rejects_controls_that_expand_unboundedly_in_json() {
+ fn rejects_unsafe_or_oversized_content() {
let error = prepare_notes_document("controls.md", b"safe\x1funsafe").unwrap_err();
assert!(matches!(error, NotesError::InvalidInput(_)));
- }
- #[test]
- fn rejects_headings_above_the_serialization_bound() {
let source = format!("# {}\n\nbody", "a".repeat(NOTES_HEADING_MAX_UTF8_BYTES + 1));
assert!(matches!(
prepare_notes_document("heading.md", source.as_bytes()),
@@ -156,7 +150,7 @@ mod tests {
}
#[test]
- fn ignores_heading_syntax_until_a_valid_fence_closer() {
+ fn handles_fenced_and_indented_code() {
let source = "```rust\n```not-a-closer\n# Code heading\n```\n# Real title\nbody";
let prepared = prepare_notes_document("fenced.md", source.as_bytes()).unwrap();
assert_eq!(prepared.title, "Real title");
@@ -164,10 +158,7 @@ mod tests {
chunk.section.as_deref() != Some("Code heading")
&& !chunk.text.starts_with("Code heading")
}));
- }
- #[test]
- fn does_not_treat_indented_code_as_a_fence() {
let source = " ```\n# Real title\nbody";
let prepared = prepare_notes_document("indented.md", source.as_bytes()).unwrap();
assert_eq!(prepared.title, "Real title");
diff --git a/rust/crates/ensu/src/notes/indexing.rs b/rust/crates/ensu/src/notes/indexing.rs
index 6023f3abe3..42eca251ca 100644
--- a/rust/crates/ensu/src/notes/indexing.rs
+++ b/rust/crates/ensu/src/notes/indexing.rs
@@ -344,12 +344,13 @@ mod tests {
fn coordinates_platform_callbacks_and_publishes_the_index() {
let temp = tempfile::tempdir().unwrap();
let prepared = prepare_notes_document("note.md", b"note").unwrap();
+ let verified_source = source_at(2);
let progress = Cell::new(0);
let outcome = index_notes_collection::<()>(
NotesIndexInput {
index_root: temp.path(),
collection_id: COLLECTION_ID.to_string(),
- inventory: vec![source()],
+ inventory: vec![source_at(1)],
forced_document_ids: &[],
force_full_hash: false,
},
@@ -357,35 +358,50 @@ mod tests {
|_| {
Ok(NotesDocumentLoad::Prepared {
document: prepared.clone(),
- source: source(),
+ source: verified_source.clone(),
})
},
|document| Ok(Some(embedding(document))),
- |_, _| Ok(NotesRevisionStatus::Matches { source: source() }),
+ |_, _| {
+ Ok(NotesRevisionStatus::Matches {
+ source: verified_source.clone(),
+ })
+ },
|value| progress.set(value),
)
.unwrap();
assert!(outcome.ready());
assert_eq!(progress.get(), 100);
- assert_eq!(
- NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string())
+ let index = NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();
+ assert_eq!(index.document_count(), 1);
+ let writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();
+ assert!(
+ writer
+ .plan_reconciliation(&[verified_source], &[], false)
.unwrap()
- .document_count(),
- 1
+ .is_up_to_date()
);
}
#[test]
fn preserves_the_previous_publication_when_a_source_changes() {
let temp = tempfile::tempdir().unwrap();
- let prepared = prepare_notes_document("note.md", b"note").unwrap();
+ let previous = prepare_notes_document("note.md", b"previous note").unwrap();
+ let mut writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();
+ writer
+ .commit_document(&previous, &embedding(&previous), &source())
+ .unwrap();
+ writer.publish(true).unwrap();
+
+ let prepared = prepare_notes_document("note.md", b"updated note").unwrap();
+ let updated_source = source_at(2);
let load_count = Cell::new(0);
let outcome = index_notes_collection::<()>(
NotesIndexInput {
index_root: temp.path(),
collection_id: COLLECTION_ID.to_string(),
- inventory: vec![source()],
+ inventory: vec![updated_source.clone()],
forced_document_ids: &[],
force_full_hash: false,
},
@@ -395,64 +411,28 @@ mod tests {
Ok(if load_count.get() == 1 {
NotesDocumentLoad::Prepared {
document: prepared.clone(),
- source: source(),
+ source: updated_source.clone(),
}
} else {
NotesDocumentLoad::Changed
})
},
|document| Ok(Some(embedding(document))),
- |_, _| Ok(NotesRevisionStatus::Matches { source: source() }),
- |_| {},
- )
- .unwrap();
-
- assert_eq!(outcome.changed_during_indexing.as_deref(), Some("note.md"));
- assert_eq!(outcome.unchecked_document_ids, ["note.md"]);
- assert!(matches!(
- NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()),
- Err(NotesError::NotReady)
- ));
- }
-
- #[test]
- fn commits_metadata_from_the_verified_source_reads() {
- let temp = tempfile::tempdir().unwrap();
- let prepared = prepare_notes_document("note.md", b"note").unwrap();
- let verified_source = source_at(2);
- let outcome = index_notes_collection::<()>(
- NotesIndexInput {
- index_root: temp.path(),
- collection_id: COLLECTION_ID.to_string(),
- inventory: vec![source_at(1)],
- forced_document_ids: &[],
- force_full_hash: false,
- },
- || Ok(()),
- |_| {
- Ok(NotesDocumentLoad::Prepared {
- document: prepared.clone(),
- source: verified_source.clone(),
- })
- },
- |document| Ok(Some(embedding(document))),
|_, _| {
Ok(NotesRevisionStatus::Matches {
- source: verified_source.clone(),
+ source: updated_source.clone(),
})
},
|_| {},
)
.unwrap();
- assert!(outcome.ready());
- let writer = NotesIndexWriter::open(temp.path(), COLLECTION_ID.to_string()).unwrap();
- assert!(
- writer
- .plan_reconciliation(&[verified_source], &[], false)
- .unwrap()
- .is_up_to_date()
- );
+ assert_eq!(outcome.changed_during_indexing.as_deref(), Some("note.md"));
+ assert_eq!(outcome.unchecked_document_ids, ["note.md"]);
+ let index = NotesCollectionIndex::open(temp.path(), COLLECTION_ID.to_string()).unwrap();
+ let hits = index.search(&embedding(&previous)[0]).unwrap();
+ assert_eq!(hits.len(), 1);
+ assert_eq!(hits[0].text, "previous note");
}
#[test]
diff --git a/rust/crates/ensu/src/notes/writer.rs b/rust/crates/ensu/src/notes/writer.rs
index 2944c62563..79803830c2 100644
--- a/rust/crates/ensu/src/notes/writer.rs
+++ b/rust/crates/ensu/src/notes/writer.rs
@@ -688,52 +688,19 @@ mod tests {
}
#[test]
- fn maximum_supported_sources_fit_a_shard() {
- let heading = "\\".repeat(crate::notes::NOTES_HEADING_MAX_UTF8_BYTES);
- let prefix = format!("## {heading}\n");
- let temp = tempfile::tempdir().unwrap();
- let writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();
- for unit in ["\\", "πŸ˜€"] {
- let remaining = NOTES_MAX_SOURCE_BYTES - prefix.len();
- assert_eq!(remaining % unit.len(), 0);
- let source = format!("{prefix}{}", unit.repeat(remaining / unit.len()));
- assert_eq!(source.len(), NOTES_MAX_SOURCE_BYTES);
- let prepared = prepare_notes_document("maximum.md", source.as_bytes()).unwrap();
- let metadata = metadata(&prepared, source.len());
- writer.validate_document(&prepared, &metadata).unwrap();
- }
- validate_maximum_source_with_many_small_sections();
- }
-
- fn validate_maximum_source_with_many_small_sections() {
- let unit = "## \\\n\\\n";
- let source = unit.repeat(NOTES_MAX_SOURCE_BYTES / unit.len());
- assert!(source.len() <= NOTES_MAX_SOURCE_BYTES);
- let prepared = prepare_notes_document("many-sections.md", source.as_bytes()).unwrap();
- let metadata = metadata(&prepared, source.len());
- let temp = tempfile::tempdir().unwrap();
- let writer = NotesIndexWriter::open(temp.path(), collection()).unwrap();
- writer.validate_document(&prepared, &metadata).unwrap();
- }
-
- #[test]
- fn rejects_an_inventory_that_cannot_fit_the_manifest_before_indexing() {
- let inventory = (0..25_000)
+ fn rejects_collections_over_capacity_before_embedding() {
+ let inventory = (0..=NOTES_MAX_COLLECTION_DOCUMENTS)
.map(|index| NotesSourceDocument {
document_id: format!("note-{index:05}.md"),
size: 1,
modified_at_ms: Some(1),
})
.collect::<Vec<_>>();
- NotesIndexWriter::validate_inventory_capacity(&collection(), &inventory[..20_000]).unwrap();
assert!(matches!(
NotesIndexWriter::validate_inventory_capacity(&collection(), &inventory),
Err(NotesError::CollectionTooLarge(_))
));
- }
- #[test]
- fn rejects_aggregate_source_content_before_indexing() {
let supported_count = NOTES_MAX_COLLECTION_SOURCE_BYTES / NOTES_MAX_SOURCE_BYTES as u64;
let inventory = (0..=supported_count)
.map(|index| NotesSourceDocument {
@@ -742,19 +709,11 @@ mod tests {
modified_at_ms: Some(1),
})
.collect::<Vec<_>>();
- NotesIndexWriter::validate_inventory_capacity(
- &collection(),
- &inventory[..supported_count as usize],
- )
- .unwrap();
assert!(matches!(
NotesIndexWriter::validate_inventory_capacity(&collection(), &inventory),
Err(NotesError::CollectionTooLarge(_))
));
- }
- #[test]
- fn rejects_exact_aggregate_chunks_before_embedding() {
let inventory = (0..129)
.map(|index| NotesSourceDocument {
document_id: format!("note-{index:03}.md"),
diff --git a/rust/crates/ensu/src/retrieval/citation.rs b/rust/crates/ensu/src/retrieval/citation.rs
index 620a4fb24c..133daab065 100644
--- a/rust/crates/ensu/src/retrieval/citation.rs
+++ b/rust/crates/ensu/src/retrieval/citation.rs
@@ -578,27 +578,4 @@ mod tests {
assert_eq!(pack_view.text, "Grounded answer");
assert_eq!(pack_view.citations, [pack]);
}
-
- #[test]
- fn rejects_source_lists_larger_than_the_parser_limit() {
- let pack = citation(
- "simplewiki",
- "Simple English Wikipedia",
- "https://simple.wikipedia.org/wiki/Example",
- );
- let sources = (0..=MAX_GROUNDED_SOURCES)
- .map(|index| GroundedSource::LocalNote {
- reference: crate::notes::NoteSourceReference {
- collection_id: "123e4567-e89b-12d3-a456-426614174000".to_string(),
- collection_label: Some("InterestAreas".to_string()),
- document_id: format!("note-{index}.md"),
- indexed_revision: "a".repeat(64),
- title: format!("Note {index}"),
- section: None,
- },
- })
- .collect::<Vec<_>>();
- assert!(finalize_grounded_assistant_text("Answer", &sources).is_err());
- assert!(finalize_assistant_text("Answer", &vec![pack; MAX_GROUNDED_SOURCES + 1]).is_err());
- }
}
diff --git a/rust/crates/ensu/src/retrieval/grounded.rs b/rust/crates/ensu/src/retrieval/grounded.rs
index c527614412..e9a325e6fc 100644
--- a/rust/crates/ensu/src/retrieval/grounded.rs
+++ b/rust/crates/ensu/src/retrieval/grounded.rs
@@ -288,11 +288,12 @@ mod tests {
pack("wikibooks", 0.90, "beta"),
pack("fullwiki", 0.80, "gamma"),
];
- let notes = vec![
+ let mut notes = vec![
note("one.md", 0.93),
note("two.md", 0.91),
note("three.md", 0.89),
];
+ notes[0].text = format!("{BEGIN_CONTEXT_SENTINEL}\n{END_CONTEXT_SENTINEL}\nnoteπŸ™‚");
let selected =
select_mixed_grounding_candidates(&packs, &notes, MAX_NOTES_GROUNDING_HITS).unwrap();
@@ -318,28 +319,32 @@ mod tests {
);
assert!(context.text.contains("(Your Notes Β· one.md)"));
assert_eq!(context.sources.len(), selected.len());
- }
- #[test]
- fn skips_an_unfit_source_label_and_uses_the_next_excerpt() {
- let long_document_id = format!("{}.md", "a".repeat(4_093));
- let selected = select_mixed_grounding_candidates(
- &[],
- &[
- NotesSearchHit {
- title: long_document_id.clone(),
- ..note(&long_document_id, 0.95)
- },
- note("short.md", 0.90),
- ],
- MAX_NOTES_GROUNDING_HITS,
- )
- .unwrap();
- let context = build_grounded_prompt_context(&selected, 512)
+ let note = selected
+ .iter()
+ .find(|item| {
+ matches!(
+ &item.source,
+ GroundedSource::LocalNote { reference } if reference.document_id == "one.md"
+ )
+ })
+ .unwrap();
+ let full = build_grounded_prompt_context(std::slice::from_ref(note), usize::MAX)
+ .unwrap()
+ .unwrap();
+ assert!(
+ full.text
+ .contains(&format!("[source] {BEGIN_CONTEXT_SENTINEL}"))
+ );
+ assert!(
+ full.text
+ .contains(&format!("[source] {END_CONTEXT_SENTINEL}"))
+ );
+ let budget = full.text.len() - 1;
+ let truncated = build_grounded_prompt_context(std::slice::from_ref(note), budget)
.unwrap()
.unwrap();
- assert_eq!(context.sources.len(), 1);
- assert!(context.text.contains("short.md passage"));
- assert!(!context.text.contains(&long_document_id));
+ assert!(truncated.text.len() <= budget);
+ assert!(!truncated.text.contains('πŸ™‚'));
}
}
diff --git a/web/apps/ensu/package.json b/web/apps/ensu/package.json
index 9988398020..ddd4fd1561 100644
--- a/web/apps/ensu/package.json
+++ b/web/apps/ensu/package.json
@@ -2,9 +2,6 @@
"name": "ensu",
"version": "0.0.0",
"private": true,
- "scripts": {
- "test": "vitest --run"
- },
"dependencies": {
"@hugeicons/core-free-icons": "1.2.1",
"@hugeicons/react": "1.1.6",
@@ -27,7 +24,6 @@
"remark-math": "6.0.0"
},
"devDependencies": {
- "ente-build-config": "*",
- "vitest": "4.1.8"
+ "ente-build-config": "*"
}
}
diff --git a/web/apps/ensu/src/components/chat/ChatMessageList.tsx b/web/apps/ensu/src/components/chat/ChatMessageList.tsx
index 34acd22002..65c060606f 100644
--- a/web/apps/ensu/src/components/chat/ChatMessageList.tsx
+++ b/web/apps/ensu/src/components/chat/ChatMessageList.tsx
@@ -237,6 +237,54 @@ type MessageSource = NonNullable<ChatMessage["sources"]>[number];
type PackSource = Extract<MessageSource, { type: "ensuPack" }>;
type LocalNoteSource = Extract<MessageSource, { type: "localNote" }>;
+const PackSourceCard = memo(
+ ({ source, number }: { source: PackSource; number: number }) => {
+ const { citation } = source;
+ return (
+ <Stack
+ sx={{
+ gap: 1.25,
+ p: 2,
+ borderRadius: 1.5,
+ bgcolor: "fill.faint",
+ }}
+ >
+ <Typography variant="mini" sx={{ color: "text.muted" }}>
+ SOURCE {number} Β· ENSU PACK Β·{" "}
+ {citation.datasetLabel.toUpperCase()}
+ </Typography>
+ <Typography variant="h6">{citation.title}</Typography>
+ <Divider sx={{ my: 0.25 }} />
+ <Typography variant="small" sx={{ color: "text.muted" }}>
+ {citation.credit}
+ </Typography>
+ <Stack direction="row" sx={{ gap: 2, flexWrap: "wrap" }}>
+ <Link
+ component="button"
+ type="button"
+ underline="hover"
+ onClick={() => void openExternalUrl(citation.sourceUrl)}
+ sx={sourceLinkSx}
+ >
+ Open source β†—
+ </Link>
+ <Link
+ component="button"
+ type="button"
+ underline="hover"
+ onClick={() =>
+ void openExternalUrl(citation.licenseUrl)
+ }
+ sx={sourceLinkSx}
+ >
+ {citation.licenseLabel} β†—
+ </Link>
+ </Stack>
+ </Stack>
+ );
+ },
+);
+
const LocalNoteSourceCard = memo(
({ source, number }: { source: LocalNoteSource; number: number }) => {
const [openError, setOpenError] = useState<string | null>(null);
@@ -251,8 +299,10 @@ const LocalNoteSourceCard = memo(
}}
>
<Typography variant="mini" sx={{ color: "text.muted" }}>
- SOURCE {number} Β·{" "}
- {(reference.collectionLabel ?? "Your Notes").toUpperCase()}
+ SOURCE {number} Β· YOUR NOTES
+ {reference.collectionLabel && (
+ <> Β· {reference.collectionLabel.toUpperCase()}</>
+ )}
</Typography>
<Typography variant="h6">{reference.title}</Typography>
{reference.section && (
@@ -294,15 +344,13 @@ const LocalNoteSourceCard = memo(
const KnowledgeSourcesDialog = memo(
({
- citations,
- localNotes,
+ sources,
onClose,
dialogTitleSx,
closeButtonSx,
closeIconProps,
}: {
- citations: PackSource["citation"][];
- localNotes: LocalNoteSource[];
+ sources: MessageSource[];
onClose: () => void;
dialogTitleSx: SystemStyleObject<Theme>;
closeButtonSx: SystemStyleObject<Theme>;
@@ -339,87 +387,21 @@ const KnowledgeSourcesDialog = memo(
</IconButton>
</DialogTitle>
<DialogContent sx={{ px: 3, pt: 1, pb: 3 }}>
- <Stack sx={{ gap: 2.5 }}>
- {citations.length > 0 && (
- <Stack sx={{ gap: 1.5 }}>
- <Typography component="h3" variant="h6">
- Ensu Packs
- </Typography>
- {citations.map((citation, index) => (
- <Stack
- key={`${citation.datasetId}:${citation.sourceUrl}:${index}`}
- sx={{
- gap: 1.25,
- p: 2,
- borderRadius: 1.5,
- bgcolor: "fill.faint",
- }}
- >
- <Typography
- variant="mini"
- sx={{ color: "text.muted" }}
- >
- SOURCE {index + 1} Β·{" "}
- {citation.datasetLabel.toUpperCase()}
- </Typography>
- <Typography variant="h6">
- {citation.title}
- </Typography>
- <Divider sx={{ my: 0.25 }} />
- <Typography
- variant="small"
- sx={{ color: "text.muted" }}
- >
- {citation.credit}
- </Typography>
- <Stack
- direction="row"
- sx={{ gap: 2, flexWrap: "wrap" }}
- >
- <Link
- component="button"
- type="button"
- underline="hover"
- onClick={() =>
- void openExternalUrl(
- citation.sourceUrl,
- )
- }
- sx={sourceLinkSx}
- >
- Open source β†—
- </Link>
- <Link
- component="button"
- type="button"
- underline="hover"
- onClick={() =>
- void openExternalUrl(
- citation.licenseUrl,
- )
- }
- sx={sourceLinkSx}
- >
- {citation.licenseLabel} β†—
- </Link>
- </Stack>
- </Stack>
- ))}
- </Stack>
- )}
- {localNotes.length > 0 && (
- <Stack sx={{ gap: 1.5 }}>
- <Typography component="h3" variant="h6">
- Your Notes
- </Typography>
- {localNotes.map((source, index) => (
- <LocalNoteSourceCard
- key={`${source.reference.collectionId}:${source.reference.documentId}:${index}`}
- source={source}
- number={citations.length + index + 1}
- />
- ))}
- </Stack>
+ <Stack sx={{ gap: 1.5 }}>
+ {sources.map((source, index) =>
+ source.type === "ensuPack" ? (
+ <PackSourceCard
+ key={`${source.type}:${source.citation.datasetId}:${source.citation.sourceUrl}:${index}`}
+ source={source}
+ number={index + 1}
+ />
+ ) : (
+ <LocalNoteSourceCard
+ key={`${source.type}:${source.reference.collectionId}:${source.reference.documentId}:${index}`}
+ source={source}
+ number={index + 1}
+ />
+ ),
)}
</Stack>
</DialogContent>
@@ -714,8 +696,7 @@ const MessageRow = memo(
)}
{showSources && (
<KnowledgeSourcesDialog
- citations={citations}
- localNotes={localNotes}
+ sources={sources}
onClose={() =>
setShowSources(false)
}
diff --git a/web/apps/ensu/src/hooks/use-notes-collections.ts b/web/apps/ensu/src/hooks/use-notes-collections.ts
index 1a65b50bc8..57413e0aa4 100644
--- a/web/apps/ensu/src/hooks/use-notes-collections.ts
+++ b/web/apps/ensu/src/hooks/use-notes-collections.ts
@@ -10,7 +10,7 @@ import {
type NotesCollection,
} from "@/services/notes";
import { NotesLifecycleController } from "@/services/notes-lifecycle";
-import { isNamedError } from "ente-base/error";
+import { tauriCommandError } from "@/services/tauri-error";
import log from "ente-base/log";
import { useCallback, useEffect, useRef, useState } from "react";
@@ -170,9 +170,10 @@ export const useNotesCollections = ({
)
.catch((error: unknown) => {
if (controller.isRemoving(collectionId)) return;
- if (isNamedError(error, "cancelled")) {
+ const { name } = tauriCommandError(error);
+ if (name === "cancelled") {
controller.queueIndex(collectionId, force);
- } else if (!isNamedError(error, "not_due")) {
+ } else if (name !== "not_due") {
setOperationError(notesErrorMessage(error));
log.error("Failed to index Your Notes", error);
}
diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsx
index 008cefba90..ab9cec8044 100644
--- a/web/apps/ensu/src/pages/chat.tsx
+++ b/web/apps/ensu/src/pages/chat.tsx
@@ -62,7 +62,7 @@ import type {
ModelInfo,
ModelSettings,
} from "@/services/llm/types";
-import { hasAvailableNotesIndex } from "@/services/notes";
+import { tauriCommandError } from "@/services/tauri-error";
import { isTauriRuntime as detectTauriAppRuntime } from "@/services/tauri-runtime";
import { Menu01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
@@ -109,20 +109,6 @@ const ADVANCED_SETTINGS_UNLOCK_KEY = "ensu.advancedSettingsUnlocked";
const MODEL_SETTINGS_STORAGE_KEY = "ensu.modelSettings";
const SYSTEM_PROMPT_STORAGE_KEY = "ensu.systemPrompt";
-interface TauriCommandError {
- name?: string;
- message?: string;
-}
-
-const tauriCommandError = (error: unknown): TauriCommandError => {
- if (!error || typeof error != "object") return {};
- const record = error as Record<string, unknown>;
- return {
- name: typeof record.name == "string" ? record.name : undefined,
- message: typeof record.message == "string" ? record.message : undefined,
- };
-};
-
const formatImageProcessingErrorForLog = (error: unknown) => {
const { name, message } = tauriCommandError(error);
if (name == "io") return "io: selected image file could not be read";
@@ -3059,15 +3045,6 @@ const Page: React.FC = () => {
enabledKnowledgePackIds.has(pack.stableId),
)
.map((pack) => pack.stableId);
- let hasKnowledgeSource = enabledReadyPackIds.length > 0;
- if (isTauriRuntime && !hasKnowledgeSource) {
- try {
- hasKnowledgeSource = await hasAvailableNotesIndex();
- if (!isActiveGeneration()) return;
- } catch (error) {
- log.warn("Your Notes availability check failed", error);
- }
- }
const knowledgeQuery = parseDocumentBlocks(promptText)
.text.replaceAll(MEDIA_MARKER, "")
.replace(/\[\d+ image attachments? provided\]/gi, "")
@@ -3078,7 +3055,6 @@ const Page: React.FC = () => {
);
if (
isTauriRuntime &&
- hasKnowledgeSource &&
knowledgeQuery &&
remainingKnowledgeBytes > 0
) {
diff --git a/web/apps/ensu/src/services/knowledge.ts b/web/apps/ensu/src/services/knowledge.ts
index 92babe8f94..e03328c81a 100644
--- a/web/apps/ensu/src/services/knowledge.ts
+++ b/web/apps/ensu/src/services/knowledge.ts
@@ -1,3 +1,4 @@
+import { tauriCommandError } from "@/services/tauri-error";
import { isTauriRuntime } from "@/services/tauri-runtime";
import log from "ente-base/log";
@@ -141,11 +142,7 @@ export const saveEnabledKnowledgePacks = (stableIds: Set<string>) => {
};
export const knowledgeErrorMessage = (error: unknown) => {
- const record =
- error && typeof error === "object"
- ? (error as { name?: unknown })
- : undefined;
- const name = typeof record?.name === "string" ? record.name : undefined;
+ const { name } = tauriCommandError(error);
switch (name) {
case "storage_full":
return "Not enough storage space to download this knowledge pack.";
diff --git a/web/apps/ensu/src/services/notes.ts b/web/apps/ensu/src/services/notes.ts
index af412f3cec..e3b1cdd42c 100644
--- a/web/apps/ensu/src/services/notes.ts
+++ b/web/apps/ensu/src/services/notes.ts
@@ -1,3 +1,4 @@
+import { tauriCommandError } from "@/services/tauri-error";
import { isTauriRuntime } from "@/services/tauri-runtime";
export interface NotesCollection {
@@ -41,9 +42,6 @@ export const selectNotesFolder = async () => {
export const listNotesCollections = () =>
invokeNotes<NotesCollection[]>("notes_list_collections");
-export const hasAvailableNotesIndex = () =>
- invokeNotes<boolean>("notes_has_available_index");
-
export const addNotesCollection = (sourceRoot: string) =>
invokeNotes<NotesCollection>("notes_add_collection", { sourceRoot });
@@ -80,14 +78,8 @@ export const openNoteDocument = (
indexedRevision,
});
-const commandError = (error: unknown) =>
- (error && typeof error === "object" ? error : {}) as {
- name?: unknown;
- message?: unknown;
- };
-
export const notesErrorMessage = (error: unknown) => {
- const { name, message } = commandError(error);
+ const { name, message } = tauriCommandError(error);
switch (name) {
case "duplicate":
return "That folder is already in Your Notes.";
@@ -109,7 +101,7 @@ export const notesErrorMessage = (error: unknown) => {
};
export const noteSourceErrorMessage = (error: unknown) => {
- const { name } = commandError(error);
+ const { name } = tauriCommandError(error);
switch (name) {
case "open_failed":
return "The note could not be opened.";
diff --git a/web/apps/ensu/src/services/tauri-error.ts b/web/apps/ensu/src/services/tauri-error.ts
new file mode 100644
index 0000000000..b84fa8b50f
--- /dev/null
+++ b/web/apps/ensu/src/services/tauri-error.ts
@@ -0,0 +1,14 @@
+export interface TauriCommandError {
+ name?: string;
+ message?: string;
+}
+
+export const tauriCommandError = (error: unknown): TauriCommandError => {
+ if (!error || typeof error !== "object") return {};
+ const record = error as Record<string, unknown>;
+ return {
+ name: typeof record.name === "string" ? record.name : undefined,
+ message:
+ typeof record.message === "string" ? record.message : undefined,
+ };
+};
diff --git a/web/apps/ensu/tests/notes-lifecycle.test.ts b/web/apps/ensu/tests/notes-lifecycle.test.ts
deleted file mode 100644
index 6b16779c90..0000000000
--- a/web/apps/ensu/tests/notes-lifecycle.test.ts
+++ /dev/null
@@ -1,56 +0,0 @@
-import { describe, expect, test } from "vitest";
-import { NotesLifecycleController } from "../src/services/notes-lifecycle";
-
-describe("NotesLifecycleController", () => {
- test("coalesces active and blocked requests without losing force", () => {
- const controller = new NotesLifecycleController();
-
- expect(controller.requestIndex("a", false, false)).toBe("start");
- expect(controller.requestIndex("a", false, false)).toBe("queued");
- expect(controller.requestIndex("a", true, false)).toBe("queued");
- expect(controller.finishIndex("a").queuedForce).toBe(true);
- expect(controller.requestIndex("b", false, true)).toBe("queued");
- expect(controller.drainQueued(false)).toEqual([["b", false]]);
- });
-
- test("keeps removal separate from active and queued indexing", () => {
- const controller = new NotesLifecycleController();
-
- controller.requestIndex("a", false, false);
- expect(controller.beginRemoval("a")).toEqual({ kind: "wait" });
- expect(controller.finishIndex("a").clearedRemovalWait).toBe(true);
-
- controller.queueIndex("a", true);
- expect(controller.beginRemoval("a")).toEqual({
- kind: "start",
- queuedForce: true,
- });
- expect(controller.requestIndex("a", false, false)).toBe("ignored");
- controller.cancelRemoval("a");
- expect(controller.requestIndex("a", false, false)).toBe("start");
- });
-
- test("rejects stale refreshes after a mutation or newer request", () => {
- const controller = new NotesLifecycleController();
- const first = controller.beginRefresh();
- const firstMutation = controller.refreshMutationSnapshot();
- controller.markMutation();
- expect(controller.canApplyRefresh(first, firstMutation)).toBe(false);
-
- const secondMutation = controller.refreshMutationSnapshot();
- const second = controller.beginRefresh();
- expect(controller.isRefreshCurrent(first)).toBe(false);
- expect(controller.canApplyRefresh(second, secondMutation)).toBe(true);
- });
-
- test("resumes each pending collection once and stops work after disposal", () => {
- const controller = new NotesLifecycleController();
- expect(controller.markResumed("a")).toBe(true);
- expect(controller.markResumed("a")).toBe(false);
- controller.dispose();
- expect(controller.requestIndex("b", false, false)).toBe("ignored");
- expect(controller.drainQueued(false)).toEqual([]);
- controller.activate();
- expect(controller.requestIndex("b", false, false)).toBe("start");
- });
-});
diff --git a/web/apps/ensu/tsconfig.json b/web/apps/ensu/tsconfig.json
index 6a2a667238..2ff1f734c6 100644
--- a/web/apps/ensu/tsconfig.json
+++ b/web/apps/ensu/tsconfig.json
@@ -9,7 +9,6 @@
},
"include": [
"src",
- "tests",
"next-env.d.ts",
"next.config.mjs",
"../../packages/base/global-electron.d.ts",
diff --git a/web/package-lock.json b/web/package-lock.json
index 7df478b760..4e037b9968 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -191,8 +191,7 @@
"remark-math": "6.0.0"
},
"devDependencies": {
- "ente-build-config": "*",
- "vitest": "4.1.8"
+ "ente-build-config": "*"
}
},
"apps/ensu/node_modules/katex": {
diff --git a/web/package.json b/web/package.json
index 11ea951759..3bee38bffc 100644
--- a/web/package.json
+++ b/web/package.json
@@ -52,7 +52,7 @@
"dev:twoof3": "npm exec --workspace twoof3 -- next dev --webpack -p 3012",
"lint": "concurrently --names 'prettier,eslint,tsc,wasm-imports' \"prettier --check --log-level warn .\" \"npm exec --workspaces -- eslint --max-warnings 0\" \"npm exec --workspaces -- tsc\" \"npm run check:wasm-imports\"",
"lint:fix": "concurrently --names 'prettier,eslint,tsc' \"prettier --write --log-level warn .\" \"npm exec --workspaces -- eslint --fix --max-warnings 0\" \"npm exec --workspaces -- tsc\"",
- "test": "npm run test --workspace ensu && npm run test --workspace ente-core-wasm && npm run test --workspace ente-paste-wasm && npm run test --workspace ente-contacts && npm run test --workspace locker && npm run test --workspace photos"
+ "test": "npm run test --workspace ente-core-wasm && npm run test --workspace ente-paste-wasm && npm run test --workspace ente-contacts && npm run test --workspace locker && npm run test --workspace photos"
},
"overrides": {
"@types/react": "19.2.17",