2026-10-12 16:07:53 +02:00
|
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
|
|
|
|
2026-10-12 17:44:56 +02:00
|
|
|
mod model_download;
|
|
|
|
|
mod semantic_commands;
|
|
|
|
|
mod settings;
|
|
|
|
|
|
2026-10-12 16:07:53 +02:00
|
|
|
use searchi_core::{IndexReport, IndexStats, SearchIndex, SearchOptions, SearchResult};
|
2026-10-12 17:44:56 +02:00
|
|
|
use semantic_commands::{
|
|
|
|
|
hybrid_search, index_embeddings, semantic_status, setup_model, SemanticState,
|
|
|
|
|
};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use settings::{Settings, SettingsStore};
|
2026-10-12 16:07:53 +02:00
|
|
|
use std::{
|
|
|
|
|
path::PathBuf,
|
|
|
|
|
sync::{
|
|
|
|
|
atomic::{AtomicBool, Ordering},
|
2026-10-12 17:44:56 +02:00
|
|
|
Arc, Mutex,
|
2026-10-12 16:07:53 +02:00
|
|
|
},
|
|
|
|
|
};
|
2026-10-12 17:44:56 +02:00
|
|
|
use tauri::{Emitter, Manager, State};
|
2026-10-12 16:07:53 +02:00
|
|
|
use tauri_plugin_dialog::DialogExt;
|
2026-10-12 17:44:56 +02:00
|
|
|
use tauri_plugin_global_shortcut::{GlobalShortcutExt, ShortcutState};
|
2026-10-12 16:07:53 +02:00
|
|
|
use tauri_plugin_opener::OpenerExt;
|
|
|
|
|
|
|
|
|
|
struct AppState {
|
|
|
|
|
database: PathBuf,
|
2026-10-12 17:44:56 +02:00
|
|
|
lexical: Arc<Mutex<SearchIndex>>,
|
2026-10-12 16:07:53 +02:00
|
|
|
indexing: Arc<AtomicBool>,
|
2026-10-12 17:44:56 +02:00
|
|
|
settings: Arc<Mutex<SettingsStore>>,
|
|
|
|
|
shortcut_status: String,
|
|
|
|
|
semantic: Arc<SemanticState>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
|
struct DesktopStatus {
|
|
|
|
|
settings: Settings,
|
|
|
|
|
indexing: bool,
|
|
|
|
|
shortcut_status: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
fn desktop_status(state: State<'_, AppState>) -> Result<DesktopStatus, String> {
|
|
|
|
|
Ok(DesktopStatus {
|
|
|
|
|
settings: state
|
|
|
|
|
.settings
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| "Settings are unavailable.")?
|
|
|
|
|
.value
|
|
|
|
|
.clone(),
|
|
|
|
|
indexing: state.indexing.load(Ordering::Acquire),
|
|
|
|
|
shortcut_status: state.shortcut_status.clone(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
fn set_cpu_threads(threads: usize, state: State<'_, AppState>) -> Result<Settings, String> {
|
|
|
|
|
if state.indexing.load(Ordering::Acquire) {
|
|
|
|
|
return Err("Wait for indexing to finish before changing resource settings.".into());
|
|
|
|
|
}
|
|
|
|
|
let mut store = state
|
|
|
|
|
.settings
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| "Settings are unavailable.")?;
|
|
|
|
|
let mut next = store.value.clone();
|
|
|
|
|
next.cpu_threads = threads;
|
|
|
|
|
next.validate()?;
|
|
|
|
|
state.semantic.reset_session()?;
|
|
|
|
|
store.save(next)?;
|
|
|
|
|
state
|
|
|
|
|
.semantic
|
|
|
|
|
.begin_automatic_setup(state.database.clone(), threads);
|
|
|
|
|
Ok(store.value.clone())
|
2026-10-12 16:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[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> {
|
2026-10-12 17:44:56 +02:00
|
|
|
let lexical = state.lexical.clone();
|
2026-10-12 16:07:53 +02:00
|
|
|
tauri::async_runtime::spawn_blocking(move || {
|
2026-10-12 17:44:56 +02:00
|
|
|
lexical
|
|
|
|
|
.try_lock()
|
|
|
|
|
.map_err(|_| "Keyword worker is busy; try the latest query again.".to_owned())?
|
|
|
|
|
.search_with_options(&query, 50, &filters.unwrap_or_default().into())
|
2026-10-12 16:07:53 +02:00
|
|
|
.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();
|
2026-10-12 17:44:56 +02:00
|
|
|
let settings = state.settings.clone();
|
|
|
|
|
let lexical = state.lexical.clone();
|
2026-10-12 16:07:53 +02:00
|
|
|
tauri::async_runtime::spawn_blocking(move || {
|
|
|
|
|
let _guard = guard;
|
2026-10-12 17:44:56 +02:00
|
|
|
settings
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| "Settings are unavailable.")?
|
|
|
|
|
.add_root(std::path::Path::new(&root))?;
|
|
|
|
|
let mut index = SearchIndex::open(database).map_err(|error| error.to_string())?;
|
|
|
|
|
let report = index
|
|
|
|
|
.index_folder(root)
|
|
|
|
|
.map_err(|error| error.to_string())?;
|
|
|
|
|
index.prewarm().map_err(|error| error.to_string())?;
|
|
|
|
|
*lexical
|
|
|
|
|
.lock()
|
|
|
|
|
.map_err(|_| "Keyword engine is unavailable.")? = index;
|
|
|
|
|
Ok(report)
|
2026-10-12 16:07:53 +02:00
|
|
|
})
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|e| e.to_string())?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
tauri::Builder::default()
|
|
|
|
|
.plugin(tauri_plugin_dialog::init())
|
|
|
|
|
.plugin(tauri_plugin_opener::init())
|
2026-10-12 17:44:56 +02:00
|
|
|
.plugin(tauri_plugin_global_shortcut::Builder::new().with_handler(|app, _, event| {
|
|
|
|
|
if event.state == ShortcutState::Pressed {
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
|
|
|
|
let result = window.show().and_then(|_| window.unminimize()).and_then(|_| window.set_focus());
|
|
|
|
|
if let Err(error) = result { eprintln!("Could not focus Searchi: {error}"); }
|
|
|
|
|
let _ = window.emit("focus-search", ());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}).build())
|
2026-10-12 16:07:53 +02:00
|
|
|
.setup(|app| {
|
|
|
|
|
let directory = app.path().app_data_dir()?;
|
|
|
|
|
std::fs::create_dir_all(&directory)?;
|
|
|
|
|
let database = directory.join("searchi.sqlite");
|
2026-10-12 17:44:56 +02:00
|
|
|
let lexical = Arc::new(Mutex::new(SearchIndex::open(&database)?));
|
|
|
|
|
let warm_database = database.clone();
|
|
|
|
|
let warm_target = lexical.clone();
|
|
|
|
|
tauri::async_runtime::spawn_blocking(move || {
|
|
|
|
|
// Prepare a separate reader and swap it in briefly; foreground
|
|
|
|
|
// queries never wait on the long statistics-preparation lock.
|
|
|
|
|
if let Ok(mut index) = SearchIndex::open(warm_database) {
|
|
|
|
|
if index.prewarm().is_ok() {
|
|
|
|
|
if let Ok(mut current) = warm_target.lock() { *current = index; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let settings = SettingsStore::load(directory.join("settings.json")).map_err(std::io::Error::other)?;
|
|
|
|
|
let development_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../..");
|
|
|
|
|
let development_models = development_root.join(".searchi/models/embeddinggemma2");
|
|
|
|
|
let bundled_models = app.path().resource_dir()?.join("embeddinggemma2");
|
|
|
|
|
let model_directory = settings.value.model_directory.clone().unwrap_or_else(|| {
|
|
|
|
|
if cfg!(debug_assertions) && development_models.is_dir() {
|
|
|
|
|
development_models.canonicalize().unwrap_or(development_models)
|
|
|
|
|
} else if bundled_models.is_dir() {
|
|
|
|
|
bundled_models
|
|
|
|
|
} else {
|
|
|
|
|
searchi_assets::default_model_directory().unwrap_or_else(|_| directory.join("Models"))
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let worker_name = if cfg!(windows) { "searchi-gemma-worker.exe" } else { "searchi-gemma-worker" };
|
|
|
|
|
let development_worker = development_root.join(".searchi/runtime/cpu")
|
|
|
|
|
.join(if cfg!(windows) { "Release" } else { "" }).join(worker_name);
|
|
|
|
|
let worker = std::env::var_os("SEARCHI_EMBEDDING_WORKER").map(PathBuf::from)
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
if cfg!(debug_assertions) && development_worker.is_file() {
|
|
|
|
|
development_worker.canonicalize().unwrap_or(development_worker)
|
|
|
|
|
} else {
|
|
|
|
|
app.path().resource_dir().unwrap_or_else(|_| directory.clone()).join(worker_name)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
let semantic = Arc::new(SemanticState::new(model_directory, worker));
|
|
|
|
|
semantic.begin_automatic_setup(database.clone(), settings.value.cpu_threads);
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
let shortcut = "Shift+Super+Space";
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
let shortcut = "Control+Shift+Space";
|
|
|
|
|
let wayland = cfg!(target_os = "linux") &&
|
|
|
|
|
(std::env::var("XDG_SESSION_TYPE").is_ok_and(|value| value == "wayland") || std::env::var_os("WAYLAND_DISPLAY").is_some());
|
|
|
|
|
let shortcut_status = if wayland {
|
|
|
|
|
"Global shortcut unavailable on Wayland. Open Searchi from your application launcher.".into()
|
|
|
|
|
} else {
|
|
|
|
|
match app.global_shortcut().register(shortcut) {
|
|
|
|
|
Ok(()) => format!("Global shortcut: {shortcut}"),
|
|
|
|
|
Err(error) => format!("Global shortcut unavailable: {error}"),
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-10-12 16:07:53 +02:00
|
|
|
app.manage(AppState {
|
|
|
|
|
database,
|
2026-10-12 17:44:56 +02:00
|
|
|
lexical,
|
2026-10-12 16:07:53 +02:00
|
|
|
indexing: Arc::new(AtomicBool::new(false)),
|
2026-10-12 17:44:56 +02:00
|
|
|
settings: Arc::new(Mutex::new(settings)),
|
|
|
|
|
shortcut_status,
|
|
|
|
|
semantic,
|
2026-10-12 16:07:53 +02:00
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
})
|
|
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
search,
|
|
|
|
|
stats,
|
|
|
|
|
index_folder,
|
|
|
|
|
pick_folder,
|
2026-10-12 17:44:56 +02:00
|
|
|
open_result,
|
|
|
|
|
desktop_status,
|
|
|
|
|
set_cpu_threads,
|
|
|
|
|
semantic_status,
|
|
|
|
|
setup_model,
|
|
|
|
|
index_embeddings,
|
|
|
|
|
hybrid_search
|
2026-10-12 16:07:53 +02:00
|
|
|
])
|
|
|
|
|
.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());
|
|
|
|
|
}
|
|
|
|
|
}
|