feat: add desktop document search and verified native CPU embeddings
Core CI / core (ubuntu-latest) (push) Failing after 1m22s
Core CI / core (macos-latest) (push) Canceled after 0s
Core CI / core (windows-latest) (push) Canceled after 0s
Core CI / desktop (ubuntu-latest) (push) Successful in 3m46s
Core CI / desktop (macos-latest) (push) Canceled after 0s
Core CI / desktop (windows-latest) (push) Canceled after 0s
Core CI / core (ubuntu-latest) (push) Failing after 1m22s
Core CI / core (macos-latest) (push) Canceled after 0s
Core CI / core (windows-latest) (push) Canceled after 0s
Core CI / desktop (ubuntu-latest) (push) Successful in 3m46s
Core CI / desktop (macos-latest) (push) Canceled after 0s
Core CI / desktop (windows-latest) (push) Canceled after 0s
This commit is contained in:
1 parent
f77112db97
commit
a9b6592330
46 files changed
+10794
-77
No files matched your search
@@ -0,0 +1,220 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use searchi_core::{IndexReport, IndexStats, SearchIndex, SearchOptions, SearchResult};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
use tauri::{Manager, State};
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
use tauri_plugin_opener::OpenerExt;
|
||||
|
||||
struct AppState {
|
||||
database: PathBuf,
|
||||
indexing: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
struct Filters {
|
||||
folder: Option<PathBuf>,
|
||||
extension: Option<String>,
|
||||
min_size: Option<u64>,
|
||||
max_size: Option<u64>,
|
||||
modified_after_unix_ms: Option<i64>,
|
||||
modified_before_unix_ms: Option<i64>,
|
||||
}
|
||||
|
||||
impl From<Filters> for SearchOptions {
|
||||
fn from(value: Filters) -> Self {
|
||||
Self {
|
||||
folder: value.folder,
|
||||
extension: value.extension,
|
||||
min_size: value.min_size,
|
||||
max_size: value.max_size,
|
||||
modified_after_unix_ms: value.modified_after_unix_ms,
|
||||
modified_before_unix_ms: value.modified_before_unix_ms,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each job owns its connection. Slow indexing never holds a desktop mutex.
|
||||
#[tauri::command]
|
||||
async fn search(
|
||||
query: String,
|
||||
filters: Option<Filters>,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<Vec<SearchResult>, String> {
|
||||
let database = state.database.clone();
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
SearchIndex::open(database)
|
||||
.and_then(|index| {
|
||||
index.search_with_options(&query, 50, &filters.unwrap_or_default().into())
|
||||
})
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn pick_folder(app: tauri::AppHandle) -> Result<Option<String>, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
app.dialog()
|
||||
.file()
|
||||
.set_title("Choose a folder to index")
|
||||
.blocking_pick_folder()
|
||||
.map(|path| {
|
||||
path.into_path()
|
||||
.map(|path| path.to_string_lossy().into_owned())
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
.transpose()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
// No broad opener permission is given to the webview. Every action is checked
|
||||
// against the index and the current filesystem before invoking a native opener.
|
||||
fn validated_result(index: &SearchIndex, path: &str) -> Result<PathBuf, String> {
|
||||
let canonical = std::fs::canonicalize(path).map_err(|e| format!("File is unavailable: {e}"))?;
|
||||
if !canonical.is_file() {
|
||||
return Err("Result is no longer a file.".into());
|
||||
}
|
||||
if !index
|
||||
.contains_path(&canonical.to_string_lossy())
|
||||
.map_err(|e| e.to_string())?
|
||||
{
|
||||
return Err("Only files in your search index can be opened.".into());
|
||||
}
|
||||
Ok(canonical)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn open_result(
|
||||
path: String,
|
||||
reveal: bool,
|
||||
app: tauri::AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<(), String> {
|
||||
let database = state.database.clone();
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let index = SearchIndex::open(database).map_err(|e| e.to_string())?;
|
||||
let canonical = validated_result(&index, &path)?;
|
||||
if reveal {
|
||||
app.opener()
|
||||
.reveal_item_in_dir(canonical)
|
||||
.map_err(|e| e.to_string())
|
||||
} else {
|
||||
app.opener()
|
||||
.open_path(canonical.to_string_lossy().into_owned(), None::<&str>)
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn stats(state: State<'_, AppState>) -> Result<IndexStats, String> {
|
||||
let database = state.database.clone();
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
SearchIndex::open(database)
|
||||
.and_then(|index| index.stats())
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
// Reset the gate on every exit, including extractor failures or task panics.
|
||||
struct IndexGuard(Arc<AtomicBool>);
|
||||
impl Drop for IndexGuard {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(false, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn index_folder(root: String, state: State<'_, AppState>) -> Result<IndexReport, String> {
|
||||
if state
|
||||
.indexing
|
||||
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
|
||||
.is_err()
|
||||
{
|
||||
return Err("A folder is already being indexed. Wait for it to finish.".into());
|
||||
}
|
||||
let guard = IndexGuard(state.indexing.clone());
|
||||
let database = state.database.clone();
|
||||
tauri::async_runtime::spawn_blocking(move || {
|
||||
let _guard = guard;
|
||||
SearchIndex::open(database)
|
||||
.and_then(|mut index| index.index_folder(root))
|
||||
.map_err(|e| e.to_string())
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.setup(|app| {
|
||||
let directory = app.path().app_data_dir()?;
|
||||
std::fs::create_dir_all(&directory)?;
|
||||
let database = directory.join("searchi.sqlite");
|
||||
SearchIndex::open(&database)?;
|
||||
app.manage(AppState {
|
||||
database,
|
||||
indexing: Arc::new(AtomicBool::new(false)),
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
search,
|
||||
stats,
|
||||
index_folder,
|
||||
pick_folder,
|
||||
open_result
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("Searchi desktop could not start");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn indexing_gate_is_released_when_worker_unwinds() {
|
||||
let indexing = Arc::new(AtomicBool::new(true));
|
||||
let gate = indexing.clone();
|
||||
let result = std::panic::catch_unwind(move || {
|
||||
let _guard = IndexGuard(gate);
|
||||
panic!("extractor failure");
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert!(!indexing.load(Ordering::Acquire));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opener_rejects_unindexed_and_deleted_files() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let indexed = root.path().join("indexed.txt");
|
||||
std::fs::write(&indexed, "retrieval fixture").unwrap();
|
||||
let mut index = SearchIndex::open(":memory:").unwrap();
|
||||
index.index_folder(root.path()).unwrap();
|
||||
assert!(validated_result(&index, &indexed.to_string_lossy()).is_ok());
|
||||
let unindexed = root.path().join("new.txt");
|
||||
std::fs::write(&unindexed, "not indexed").unwrap();
|
||||
assert!(validated_result(&index, &unindexed.to_string_lossy()).is_err());
|
||||
std::fs::remove_file(&indexed).unwrap();
|
||||
assert!(validated_result(&index, &indexed.to_string_lossy()).is_err());
|
||||
assert!(validated_result(&index, &root.path().to_string_lossy()).is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user