Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix: attachment parsing error (exit gracefully) (#7093)
Dinh Long Nguyen5ede3436a8c5
diff --git a/extensions/llamacpp-extension/src/util.ts b/extensions/llamacpp-extension/src/util.tsindex f116205bb..203788f9b 100644--- a/extensions/llamacpp-extension/src/util.ts+++ b/extensions/llamacpp-extension/src/util.ts@@ -118,14 +118,14 @@ export type EmbedBatchResult = {usage?: EmbedUsage}-export function estimateTokensFromText(text: string, charsPerToken = 3): number {+export function estimateTokensFromText(text: string, charsPerToken = 2): number {return Math.max(1, Math.ceil(text.length / Math.max(charsPerToken, 1)))}export function buildEmbedBatches(inputs: string[],ubatchSize: number,- charsPerToken = 3+ charsPerToken = 2): EmbedBatch[] {const batches: EmbedBatch[] = []let current: string[] = []diff --git a/src-tauri/plugins/tauri-plugin-rag/src/commands.rs b/src-tauri/plugins/tauri-plugin-rag/src/commands.rsindex 6f2a0f112..ee108daf4 100644--- a/src-tauri/plugins/tauri-plugin-rag/src/commands.rs+++ b/src-tauri/plugins/tauri-plugin-rag/src/commands.rs@@ -1,4 +1,5 @@-use crate::{RagError, parser};+use crate::{parser, RagError};+use std::panic::{catch_unwind, AssertUnwindSafe};#[tauri::command]pub async fn parse_document<R: tauri::Runtime>(@@ -7,6 +8,22 @@ pub async fn parse_document<R: tauri::Runtime>(file_type: String,) -> Result<String, RagError> {log::info!("Parsing document: {} (type: {})", file_path, file_type);- let res = parser::parse_document(&file_path, &file_type);- res+ let res = catch_unwind(AssertUnwindSafe(|| parser::parse_document(&file_path, &file_type)));+ match res {+ Ok(result) => result,+ Err(payload) => {+ let reason = if let Some(s) = payload.downcast_ref::<&str>() {+ *s+ } else if let Some(s) = payload.downcast_ref::<String>() {+ s.as_str()+ } else {+ "unknown panic"+ };+ log::error!("Document parsing panicked: {}", reason);+ Err(RagError::ParseError(format!(+ "Document parsing failed unexpectedly: {}",+ reason+ )))+ }+ }}diff --git a/src-tauri/plugins/tauri-plugin-rag/src/parser.rs b/src-tauri/plugins/tauri-plugin-rag/src/parser.rsindex d21c1de5d..fcf4d91c3 100644--- a/src-tauri/plugins/tauri-plugin-rag/src/parser.rs+++ b/src-tauri/plugins/tauri-plugin-rag/src/parser.rs@@ -1,20 +1,37 @@use crate::RagError;+use std::borrow::Cow;use std::fs;-use std::io::{Read, Cursor};-use zip::read::ZipArchive;-use quick_xml::events::Event;-use quick_xml::Reader;+use std::io::{Cursor, Read};+use std::panic::{catch_unwind, AssertUnwindSafe};+use calamine::{open_workbook_auto, DataType, Reader as _};+use chardetng::EncodingDetector;use csv as csv_crate;-use calamine::{Reader as _, open_workbook_auto, DataType};use html2text;-use chardetng::EncodingDetector;use infer;-use std::borrow::Cow;+use quick_xml::events::Event;+use quick_xml::Reader;+use zip::read::ZipArchive;pub fn parse_pdf(file_path: &str) -> Result<String, RagError> {let bytes = fs::read(file_path)?;- let text = pdf_extract::extract_text_from_mem(&bytes)- .map_err(|e| RagError::ParseError(format!("PDF parse error: {}", e)))?;+ // pdf-extract can panic on some malformed PDFs; guard to avoid crashing the app+ let text = match catch_unwind(AssertUnwindSafe(|| pdf_extract::extract_text_from_mem(&bytes))) {+ Ok(Ok(t)) => t,+ Ok(Err(e)) => return Err(RagError::ParseError(format!("PDF parse error: {}", e))),+ Err(payload) => {+ let reason = if let Some(s) = payload.downcast_ref::<&str>() {+ *s+ } else if let Some(s) = payload.downcast_ref::<String>() {+ s.as_str()+ } else {+ "unknown parser panic"+ };+ return Err(RagError::ParseError(format!(+ "PDF parsing failed unexpectedly: {}",+ reason+ )));+ }+ };// Validate that the PDF has extractable text (not image-based/scanned)// Count meaningful characters (excluding whitespace)diff --git a/web-app/src/lib/attachmentProcessing.ts b/web-app/src/lib/attachmentProcessing.tsindex b4ea232e3..430ddadc0 100644--- a/web-app/src/lib/attachmentProcessing.ts+++ b/web-app/src/lib/attachmentProcessing.ts@@ -29,6 +29,49 @@ export type AttachmentProcessingResult = {hasEmbeddedDocuments: boolean}+const formatAttachmentError = (err: unknown): string => {+ if (!err) return 'Unknown error'+ if (err instanceof Error) return err.message || err.toString()+ if (typeof err === 'string') return err+ if (Array.isArray(err)) {+ const parts = err.map((e) => formatAttachmentError(e)).filter(Boolean)+ return parts.length ? Array.from(new Set(parts)).join('; ') : 'Unknown error'+ }+ if (typeof err === 'object') {+ const obj = err as Record<string, unknown>+ const candidates = [+ obj.message,+ obj.reason,+ obj.detail,+ ]+ for (const val of candidates) {+ if (typeof val === 'string' && val.trim().length > 0) {+ return val+ }+ }+ const nestedSources = [obj.error, obj.cause]+ for (const nested of nestedSources) {+ if (nested && typeof nested === 'object') {+ const nestedMsg = formatAttachmentError(nested)+ if (nestedMsg && nestedMsg !== 'Unknown error') {+ return nestedMsg+ }+ } else if (typeof nested === 'string' && nested.trim().length > 0) {+ return nested+ }+ }+ if (typeof obj.code === 'string' && obj.code.trim().length > 0) {+ return obj.code+ }+ try {+ return JSON.stringify(obj)+ } catch {+ return String(err)+ }+ }+ return String(err)+}+export const processAttachmentsForSend = async (options: AttachmentProcessingOptions): Promise<AttachmentProcessingResult> => {@@ -83,9 +126,9 @@ export const processAttachmentsForSend = async (if (updateAttachmentProcessing) {updateAttachmentProcessing(img.name, 'error')}- const desc = err instanceof Error ? err.message : String(err)+ const desc = formatAttachmentError(err)toast.error('Failed to ingest image attachment', { description: desc })- throw err+ throw err instanceof Error ? err : new Error(desc)}}}@@ -204,9 +247,9 @@ export const processAttachmentsForSend = async (if (updateAttachmentProcessing) {updateAttachmentProcessing(doc.name, 'error')}- const desc = err instanceof Error ? err.message : String(err)+ const desc = formatAttachmentError(err)toast.error('Failed to index attachments', { description: desc })- throw err+ throw err instanceof Error ? err : new Error(desc)}}}