Improved PDFium initialization and error handling logic (#649)

This commit is contained in:
Thorsten Sommer 2026-02-01 19:51:12 +01:00 committed by GitHub
parent 5ea92c9ced
commit 0e14a3cb58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 11 deletions

View File

@ -3,4 +3,5 @@
- Added support for defining document analysis policies (in preview) by configuration plugins, enabling centralized management of policies across entire departments or organizations.
- Added an option to hide the policy definition in the Document Analysis Assistant (in preview) when exporting and distributing that policy by a configuration plugin in organizations, making it easier for users to use.
- Added the policy export functionality to the Document Analysis Assistant (in preview). You can now export policies as Lua code for a configuration plugin to distribute the policy across your organization.
- Improved the error checking & logging behavior when the installed `PDFium` version did not meet the minimum required version.
- Fixed a bug where the global minimum confidence level was not being applied to the assistants.

View File

@ -246,7 +246,13 @@ async fn stream_pdf(file_path: &str) -> Result<ChunkStream> {
let (tx, rx) = mpsc::channel(10);
tokio::task::spawn_blocking(move || {
let pdfium = Pdfium::ai_studio_init();
let pdfium = match Pdfium::ai_studio_init() {
Ok(pdfium) => pdfium,
Err(e) => {
let _ = tx.blocking_send(Err(e));
return;
}
};
let doc = match pdfium.load_pdf_from_file(&path, None) {
Ok(document) => document,
Err(e) => {

View File

@ -1,26 +1,47 @@
use std::sync::Mutex;
use once_cell::sync::Lazy;
use pdfium_render::prelude::Pdfium;
use log::{error, warn};
pub static PDFIUM_LIB_PATH: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None));
pub trait PdfiumInit {
fn ai_studio_init() -> Pdfium;
fn ai_studio_init() -> Result<Pdfium, Box<dyn std::error::Error + Send + Sync>>;
}
impl PdfiumInit for Pdfium {
/// Initializes the PDFium library for AI Studio.
fn ai_studio_init() -> Pdfium {
fn ai_studio_init() -> Result<Pdfium, Box<dyn std::error::Error + Send + Sync>> {
let lib_path = PDFIUM_LIB_PATH.lock().unwrap();
if let Some(path) = lib_path.as_ref() {
return Pdfium::new(
Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path(path))
.or_else(|_| Pdfium::bind_to_system_library())
.unwrap(),
return match Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path(path)) {
Ok(binding) => Ok(Pdfium::new(binding)),
Err(library_error) => {
match Pdfium::bind_to_system_library() {
Ok(binding) => Ok(Pdfium::new(binding)),
Err(system_error) => {
error!(
"Failed to load PDFium from '{path}' and the system library. Developer action (from repo root): run the build script once to download the required PDFium version: `cd app/Build` and `dotnet run build`. Details: library error: '{library_error}'; system error: '{system_error}'."
);
Err(Box::new(system_error))
}
}
}
}
}
Pdfium::new(Pdfium::bind_to_system_library().unwrap())
warn!("No custom PDFium library path set; trying to load PDFium from the system library.");
match Pdfium::bind_to_system_library() {
Ok(binding) => Ok(Pdfium::new(binding)),
Err(system_error) => {
error!(
"Failed to load PDFium from the system library. Developer action (from repo root): run the build script once to download the required PDFium version: `cd app/Build` and `dotnet run build`. Details: '{system_error}'."
);
Err(Box::new(system_error))
}
}
}
}