Skip to main content

qualia_client_core/engine/
pdf_processor.rs

1use pdf_extract::extract_text;
2use std::fs;
3use std::path::Path;
4use uuid::Uuid;
5
6/// Extracts text from a PDF file and stores it into the local library,
7/// supporting underlying systems like LLMs, graph schemas, and ontological CML assertions.
8pub async fn ingest_pdf_to_library(file_path: &str) -> Result<String, String> {
9    let path = Path::new(file_path);
10    if !path.exists() {
11        return Err(format!("File does not exist: {}", file_path));
12    }
13
14    // 1. Parse document using pdf_extract
15    let text = extract_text(path).map_err(|e| format!("Failed to extract PDF text: {}", e))?;
16
17    // 2. Store to library
18    let config_path = crate::state::config_file_path();
19    let storage_path = if let Ok(config_str) = fs::read_to_string(&config_path) {
20        if let Ok(config) = serde_json::from_str::<crate::state::AgentConfig>(&config_str) {
21            config.storage_path
22        } else {
23            crate::state::dirs_default_path()
24        }
25    } else {
26        crate::state::dirs_default_path()
27    };
28
29    let library_dir = std::path::PathBuf::from(&storage_path).join("library");
30    if !library_dir.exists() {
31        let _ = fs::create_dir_all(&library_dir);
32    }
33
34    let file_id = Uuid::new_v4().to_string();
35    let txt_path = library_dir.join(format!("{}.txt", file_id));
36
37    fs::write(&txt_path, &text).map_err(|e| format!("Failed to write text to library: {}", e))?;
38
39    Ok(format!(
40        "PDF ingested successfully. Text saved to {:?}",
41        txt_path
42    ))
43}