Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

6.8 KB
Raw Blame
use crate::{VectorDBError, VectorDBState};
use crate::db::{
    self, AttachmentFileInfo, MemoryHit, MinimalChunkInput, SearchResult,
};
use serde::{Deserialize, Serialize};
use tauri::State;

#[derive(Debug, Serialize, Deserialize)]
pub struct Status {
    pub ann_available: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct FileInput {
    pub path: String,
    pub name: Option<String>,
    #[serde(rename = "type")]
    pub file_type: Option<String>,
    pub size: Option<i64>,
}

// ============================================================================
// Tauri Command Handlers
// ============================================================================

#[tauri::command]
pub async fn get_status(state: State<'_, VectorDBState>) -> Result<Status, VectorDBError> {
    println!("[VectorDB] Checking ANN availability...");
    let temp = db::collection_path(&state.base_dir, "__status__")?;
    let conn = db::open_or_init_conn(&temp)?;

    let ann = db::try_load_sqlite_vec_verbose(&conn);

    println!("[VectorDB] ANN status: {}", if ann { "AVAILABLE" } else { "NOT AVAILABLE" });
    Ok(Status { ann_available: ann })
}

#[tauri::command]
pub async fn create_collection<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    name: String,
    dimension: usize,
) -> Result<(), VectorDBError> {
    let path = db::collection_path(&state.base_dir, &name)?;
    let conn = db::open_or_init_conn(&path)?;

    let has_ann = db::create_schema(&conn, dimension)?;
    if has_ann {
        println!("[VectorDB] ✓ Collection '{}' created with ANN support", name);
    } else {
        println!("[VectorDB] ⚠ Collection '{}' created WITHOUT ANN support (will use linear search)", name);
    }
    Ok(())
}

#[tauri::command]
pub async fn create_file<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    file: FileInput,
) -> Result<AttachmentFileInfo, VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    db::create_file(
        &conn,
        &file.path,
        file.name.as_deref(),
        file.file_type.as_deref(),
        file.size,
    )
}

#[tauri::command]
pub async fn insert_chunks<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    file_id: String,
    chunks: Vec<MinimalChunkInput>,
) -> Result<(), VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    let vec_loaded = db::try_load_sqlite_vec(&conn);
    db::insert_chunks(&conn, &file_id, chunks, vec_loaded)
}

#[tauri::command]
pub async fn delete_file<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    file_id: String,
) -> Result<(), VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    db::delete_file(&conn, &file_id)
}

#[tauri::command]
#[allow(clippy::too_many_arguments)]
pub async fn search_collection<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    query_embedding: Vec<f32>,
    limit: usize,
    threshold: f32,
    mode: Option<String>,
    file_ids: Option<Vec<String>>,
) -> Result<Vec<SearchResult>, VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    let vec_loaded = db::try_load_sqlite_vec(&conn);
    db::search_collection(&conn, &query_embedding, limit, threshold, mode, vec_loaded, file_ids)
}

#[tauri::command]
pub async fn list_attachments<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    limit: Option<usize>,
) -> Result<Vec<AttachmentFileInfo>, VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    db::list_attachments(&conn, limit)
}

#[tauri::command]
pub async fn delete_chunks<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    ids: Vec<String>,
) -> Result<(), VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    db::delete_chunks(&conn, ids)
}

#[tauri::command]
pub async fn delete_collection<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
) -> Result<(), VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    if path.exists() {
        std::fs::remove_file(path).ok();
    }
    Ok(())
}

#[tauri::command]
pub async fn chunk_text<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    text: String,
    chunk_size: usize,
    chunk_overlap: usize,
) -> Result<Vec<String>, VectorDBError> {
    Ok(db::chunk_text(text, chunk_size, chunk_overlap))
}

#[tauri::command]
pub async fn get_chunks<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    collection: String,
    file_id: String,
    start_order: i64,
    end_order: i64,
) -> Result<Vec<SearchResult>, VectorDBError> {
    let path = db::collection_path(&state.base_dir, &collection)?;
    let conn = db::open_or_init_conn(&path)?;
    db::get_chunks(&conn, file_id, start_order, end_order)
}

// ============================================================================
// Project-scoped agent memory (FTS5 / BM25)
// ============================================================================

fn memory_conn(state: &VectorDBState) -> Result<rusqlite::Connection, VectorDBError> {
    let path = db::collection_path(&state.base_dir, db::MEMORY_COLLECTION)?;
    db::open_or_init_conn(&path)
}

#[tauri::command]
pub async fn memory_index<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    msg_id: String,
    project_id: String,
    text: String,
    role: String,
    ts: i64,
) -> Result<(), VectorDBError> {
    let conn = memory_conn(&state)?;
    db::memory_index(&conn, &msg_id, &project_id, &text, &role, ts)
}

#[tauri::command]
pub async fn memory_search<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    project_id: String,
    query: String,
    top_k: usize,
) -> Result<Vec<MemoryHit>, VectorDBError> {
    let conn = memory_conn(&state)?;
    db::memory_search(&conn, &project_id, &query, top_k)
}

#[tauri::command]
pub async fn memory_clear<R: tauri::Runtime>(
    _app: tauri::AppHandle<R>,
    state: State<'_, VectorDBState>,
    project_id: Option<String>,
) -> Result<(), VectorDBError> {
    let conn = memory_conn(&state)?;
    db::memory_clear(&conn, project_id.as_deref())
}