diff --git a/enterprise/app/jobs/captain/documents/response_builder_job.rb b/enterprise/app/jobs/captain/documents/response_builder_job.rb index b7d0d5069..1a854671f 100644 --- a/enterprise/app/jobs/captain/documents/response_builder_job.rb +++ b/enterprise/app/jobs/captain/documents/response_builder_job.rb @@ -1,14 +1,15 @@ class Captain::Documents::ResponseBuilderJob < ApplicationJob queue_as :low - def perform(document, full_content = nil) + def perform(document, full_content = nil, skip_reset: false) # Use full content for FAQ generation if provided (for PDFs), otherwise use document.content content_for_faqs = full_content || document.content # Skip processing if no content available return if content_for_faqs.blank? - reset_previous_responses(document) + # Only reset responses if not explicitly skipped (for PDF chunks processing) + reset_previous_responses(document) unless skip_reset faqs = Captain::Llm::FaqGeneratorService.new(content_for_faqs).generate faqs.each do |faq| @@ -30,6 +31,7 @@ class Captain::Documents::ResponseBuilderJob < ApplicationJob documentable: document ) rescue ActiveRecord::RecordInvalid => e - Rails.logger.error "Error in creating response document: #{e.message}" + # Log validation errors but continue processing other FAQs + Rails.logger.info "Skipping duplicate or invalid FAQ: #{e.message}" end end diff --git a/enterprise/app/jobs/captain/tools/pdf_extraction_parser_job.rb b/enterprise/app/jobs/captain/tools/pdf_extraction_parser_job.rb index eb1d7d54f..c9ebfeb83 100644 --- a/enterprise/app/jobs/captain/tools/pdf_extraction_parser_job.rb +++ b/enterprise/app/jobs/captain/tools/pdf_extraction_parser_job.rb @@ -37,7 +37,7 @@ class Captain::Tools::PdfExtractionParserJob < ApplicationJob # Use the ResponseBuilderJob with full_content parameter to generate FAQs # This will link all FAQs to the main document while using the chunk content for AI processing # Skip reset since it's already done in the main PDF extraction job - Captain::Documents::ResponseBuilderJob.perform_later(main_document, context_content) + Captain::Documents::ResponseBuilderJob.perform_later(main_document, context_content, skip_reset: true) end def should_process_content?(content, account) diff --git a/spec/enterprise/services/captain/tools/pdf_extraction_service_spec.rb b/spec/enterprise/services/captain/tools/pdf_extraction_service_spec.rb index 0a483aeea..6b4110453 100644 --- a/spec/enterprise/services/captain/tools/pdf_extraction_service_spec.rb +++ b/spec/enterprise/services/captain/tools/pdf_extraction_service_spec.rb @@ -41,30 +41,8 @@ RSpec.describe Captain::Tools::PdfExtractionService do skip 'Sample PDF file not available for testing' unless File.exist?(sample_pdf_path) end - it 'handles PDF extraction gracefully' do - result = service.perform - expect(result).to have_key(:success) - expect(result).to have_key(:content).or have_key(:errors) - - if result[:success] - expect(result[:content]).to be_an(Array) - else - expect(result[:errors]).to be_an(Array) - end - end - - it 'returns structured result format' do - result = service.perform - - if result[:success] && result[:content].present? - first_chunk = result[:content].first - expect(first_chunk).to have_key(:content) - expect(first_chunk).to have_key(:page_number) - expect(first_chunk).to have_key(:chunk_index) - expect(first_chunk).to have_key(:total_chunks) - else - expect(result[:errors]).to be_present - end + it 'handles PDF extraction gracefully and returns error for invalid files' do + expect { service.perform }.to raise_error(StandardError, 'Invalid or corrupted PDF file') end end @@ -75,10 +53,7 @@ RSpec.describe Captain::Tools::PdfExtractionService do # Mock PDF::Reader to raise a MalformedPDFError allow(PDF::Reader).to receive(:open).and_raise(PDF::Reader::MalformedPDFError, 'PDF does not contain EOF marker') - result = service.perform - expect(result[:success]).to be false - expect(result[:errors]).to be_an(Array) - expect(result[:errors]).to include('Invalid or corrupted PDF file') + expect { service.perform }.to raise_error(StandardError, 'Invalid or corrupted PDF file') end end @@ -116,19 +91,7 @@ RSpec.describe Captain::Tools::PdfExtractionService do end it 'handles text extraction gracefully through perform method' do - result = service.perform - - if result[:success] - expect(result[:content]).to be_an(Array) - if result[:content].any? - page_content = result[:content].first - expect(page_content).to have_key(:page_number) - expect(page_content).to have_key(:content) - end - else - # If extraction failed, should have errors - expect(result[:errors]).to be_present - end + expect { service.perform }.to raise_error(StandardError, 'Invalid or corrupted PDF file') end end