paperscope 0.4.3

Local-first AI document search with exact page and line citations. One binary: CLI + MCP server for LLMs. PDF, DOCX and 15+ formats, no cloud, no telemetry.
Documentation
use super::{Workspace, EXIT_FOUND};
use crate::core::index::Connection;
use crate::core::models::ModelManager;
use anyhow::Result;

const NOTICE: &str = include_str!("../../NOTICE");

pub async fn run() -> Result<i32> {
    let ws = Workspace::new();
    println!("paper doctor");
    println!("  data dir:   {}", ws.paths.base_dir().display());
    println!("  database:   {}", ws.paths.db_path().display());
    println!("  config:     {}", ws.paths.config_path().display());

    if let Some(phase) = ws.setup_status() {
        println!("  setup:      in progress, {phase}");
    }

    match Connection::open(&ws.paths.db_path()) {
        Ok(conn) => {
            crate::core::index::init_db(&conn)?;
            let chunks = conn.query_i64("SELECT COUNT(*) FROM chunks").unwrap_or(0);
            let files = conn
                .query_i64("SELECT COUNT(DISTINCT path) FROM chunks")
                .unwrap_or(0);
            let vectors = conn
                .query_i64("SELECT COUNT(*) FROM chunks_vec")
                .unwrap_or(0);
            let version: String = conn
                .inner()
                .query_row("SELECT vec_version()", [], |r| r.get(0))
                .unwrap_or_else(|_| "unavailable".into());
            println!("  documents:  {files} files, {chunks} chunks, {vectors} embeddings");
            println!("  sqlite-vec: {version}");
            if vectors == 0 && chunks > 0 {
                println!("  note: no embeddings yet; run `paper index --embeddings` after installing the model");
            }
        }
        Err(e) => println!("  database:   unavailable ({e:#})"),
    }

    print_remote_config(&ws);

    for line in crate::core::update_state::UpdateSummary::load(
        &ws.paths.base_dir(),
        &ws.config,
        ws.remote.as_ref(),
    )
    .doctor_lines()
    {
        println!("{line}");
    }

    let manager = ModelManager::new();
    for model in manager.list() {
        println!(
            "  model:      {:<22} {} ({} MB)",
            model.id,
            if model.installed {
                // `installed` is a cheap size check; doctor does the full hash.
                if manager.is_verified(&model.id) {
                    "installed"
                } else {
                    "installed but FAILS CHECKSUM (run `paper models install`)"
                }
            } else {
                "not installed"
            },
            model.size_mb
        );
    }

    #[cfg(feature = "ocr")]
    {
        let present = crate::core::extract::ocr::models_present();
        let state = if crate::core::extract::ocr::available() {
            "enabled"
        } else if std::env::var_os("PAPER_NO_OCR").is_some() {
            "disabled by PAPER_NO_OCR"
        } else {
            "waiting for models"
        };
        println!(
            "  ocr:        {state} (paddleocr det+rec, {}); models at {}",
            if present {
                "installed"
            } else {
                "not installed"
            },
            crate::core::extract::ocr::model_dir().display()
        );
    }
    #[cfg(not(feature = "ocr"))]
    println!("  ocr:        not compiled into this build");

    println!("  telemetry:  disabled (there is no telemetry code in this binary)");
    crate::cli::update::warn_if_shadowed();

    println!();
    println!("{}", NOTICE);
    Ok(EXIT_FOUND)
}

fn print_remote_config(ws: &Workspace) {
    use crate::core::remote_config::{compiled_keys, load_cached, read_fetch_state};
    let dir = ws.paths.base_dir();
    let today = chrono::Local::now().date_naive();
    // Read the cache directly: `ws.remote` drops an expired copy, doctor still shows it.
    match load_cached(&dir, &compiled_keys(), today) {
        Some(cfg) => {
            let fetch = match read_fetch_state(&dir) {
                Some(s) => format!(
                    "{} at {}{}",
                    s.outcome,
                    s.at,
                    s.detail.map(|d| format!(" ({d})")).unwrap_or_default()
                ),
                None => "none yet".into(),
            };
            println!(
                "  remote config: v{}, expires {}{}, last fetch {fetch}",
                cfg.version,
                cfg.expires,
                if cfg.is_expired(today) {
                    " (EXPIRED)"
                } else {
                    ""
                },
            );
        }
        None => println!("  remote config: none yet"),
    }
    let e = &ws.effective;
    println!(
        "  embedder:   {} ({})",
        e.embedder,
        e.embedder_source.label()
    );
    println!(
        "  reranker:   {} ({})",
        e.reranker,
        e.reranker_source.label()
    );
    for d in crate::core::effective::deprecations_in_use(ws.remote.as_ref(), e) {
        let hint = d
            .replacement
            .as_ref()
            .map(|r| format!(" (replacement: {r})"))
            .unwrap_or_default();
        println!("  deprecated: {}: {}{hint}", d.id, d.message);
    }
}