|
| 1 | +"""Document processing pipeline for end-to-end document workflows.""" |
| 2 | + |
| 3 | +from collections.abc import Callable |
| 4 | +import logging |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from ..agents.document_agent import DocumentAgent |
| 9 | +from ..memory.short_term import ShortTermMemory |
| 10 | +from ..utils.file_utils import get_file_hash |
| 11 | +from .base_pipeline import BasePipeline |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class DocumentPipeline(BasePipeline): |
| 17 | + """Complete pipeline for document processing workflows.""" |
| 18 | + |
| 19 | + def __init__(self, config: dict[str, Any] | None = None): |
| 20 | + super().__init__(config) |
| 21 | + self.document_agent = DocumentAgent(config.get("agent", {}) if config else {}) |
| 22 | + self.memory = ShortTermMemory() |
| 23 | + self.processors = [] |
| 24 | + self.output_handlers = [] |
| 25 | + |
| 26 | + def process(self, input_data: Any) -> dict[str, Any]: |
| 27 | + """Process input through the pipeline (implements BasePipeline interface).""" |
| 28 | + if isinstance(input_data, str | Path): |
| 29 | + return self.process_single_document(input_data) |
| 30 | + elif isinstance(input_data, list): |
| 31 | + return self.process_batch(input_data) |
| 32 | + else: |
| 33 | + return {"error": "Unsupported input type for document pipeline"} |
| 34 | + |
| 35 | + def add_processor(self, processor: Callable[[dict[str, Any]], dict[str, Any]]) -> "DocumentPipeline": |
| 36 | + """Add a custom processor to the pipeline.""" |
| 37 | + self.processors.append(processor) |
| 38 | + return self |
| 39 | + |
| 40 | + def add_output_handler(self, handler: Callable[[dict[str, Any]], None]) -> "DocumentPipeline": |
| 41 | + """Add an output handler to the pipeline.""" |
| 42 | + self.output_handlers.append(handler) |
| 43 | + return self |
| 44 | + |
| 45 | + def process_single_document(self, file_path: str | Path) -> dict[str, Any]: |
| 46 | + """Process a single document through the complete pipeline.""" |
| 47 | + file_path = Path(file_path) |
| 48 | + logger.info(f"Starting document pipeline for: {file_path}") |
| 49 | + |
| 50 | + try: |
| 51 | + # Check if already processed (using file hash) |
| 52 | + file_hash = get_file_hash(file_path) |
| 53 | + cached_result = self.memory.get(f"doc_{file_hash}") |
| 54 | + |
| 55 | + if cached_result and self.config.get("use_cache", True): |
| 56 | + logger.info(f"Using cached result for {file_path}") |
| 57 | + return cached_result |
| 58 | + |
| 59 | + # Process with document agent |
| 60 | + result = self.document_agent.extract_requirements(str(file_path)) |
| 61 | + |
| 62 | + if not result["success"]: |
| 63 | + return result |
| 64 | + |
| 65 | + # Apply custom processors |
| 66 | + for processor in self.processors: |
| 67 | + try: |
| 68 | + result = processor(result) |
| 69 | + except Exception as e: |
| 70 | + logger.error(f"Processor failed: {e}") |
| 71 | + result["processing_errors"] = result.get("processing_errors", []) + [str(e)] |
| 72 | + |
| 73 | + # Store in memory |
| 74 | + if self.config.get("use_cache", True): |
| 75 | + self.memory.store(f"doc_{file_hash}", result) |
| 76 | + |
| 77 | + # Apply output handlers |
| 78 | + for handler in self.output_handlers: |
| 79 | + try: |
| 80 | + handler(result) |
| 81 | + except Exception as e: |
| 82 | + logger.error(f"Output handler failed: {e}") |
| 83 | + |
| 84 | + logger.info(f"Successfully processed document: {file_path}") |
| 85 | + return result |
| 86 | + |
| 87 | + except Exception as e: |
| 88 | + logger.error(f"Document pipeline failed for {file_path}: {e}") |
| 89 | + return { |
| 90 | + "success": False, |
| 91 | + "file_path": str(file_path), |
| 92 | + "error": str(e), |
| 93 | + "pipeline": "DocumentPipeline" |
| 94 | + } |
| 95 | + |
| 96 | + def process_batch(self, file_paths: list[str | Path]) -> dict[str, Any]: |
| 97 | + """Process multiple documents.""" |
| 98 | + logger.info(f"Starting batch processing for {len(file_paths)} documents") |
| 99 | + |
| 100 | + results = [] |
| 101 | + success_count = 0 |
| 102 | + |
| 103 | + for file_path in file_paths: |
| 104 | + try: |
| 105 | + result = self.process_single_document(file_path) |
| 106 | + results.append(result) |
| 107 | + |
| 108 | + if result["success"]: |
| 109 | + success_count += 1 |
| 110 | + |
| 111 | + except Exception as e: |
| 112 | + logger.error(f"Batch item failed {file_path}: {e}") |
| 113 | + results.append({ |
| 114 | + "success": False, |
| 115 | + "file_path": str(file_path), |
| 116 | + "error": str(e) |
| 117 | + }) |
| 118 | + |
| 119 | + batch_result = { |
| 120 | + "success": success_count > 0, |
| 121 | + "total_documents": len(file_paths), |
| 122 | + "successful_documents": success_count, |
| 123 | + "failed_documents": len(file_paths) - success_count, |
| 124 | + "results": results, |
| 125 | + "pipeline": "DocumentPipeline" |
| 126 | + } |
| 127 | + |
| 128 | + logger.info(f"Batch processing complete: {success_count}/{len(file_paths)} successful") |
| 129 | + return batch_result |
| 130 | + |
| 131 | + def process_directory(self, directory_path: str | Path, |
| 132 | + pattern: str = "**/*", |
| 133 | + recursive: bool = True) -> dict[str, Any]: |
| 134 | + """Process all documents in a directory.""" |
| 135 | + directory_path = Path(directory_path) |
| 136 | + |
| 137 | + if not directory_path.exists(): |
| 138 | + raise FileNotFoundError(f"Directory not found: {directory_path}") |
| 139 | + |
| 140 | + # Find all supported files (Docling supports these formats) |
| 141 | + supported_formats = [".pdf", ".docx", ".pptx", ".html", ".md"] |
| 142 | + file_paths = [] |
| 143 | + |
| 144 | + for file_path in directory_path.glob(pattern): |
| 145 | + if file_path.is_file() and file_path.suffix.lower() in supported_formats: |
| 146 | + file_paths.append(file_path) |
| 147 | + |
| 148 | + logger.info(f"Found {len(file_paths)} documents in {directory_path}") |
| 149 | + |
| 150 | + if not file_paths: |
| 151 | + return { |
| 152 | + "success": False, |
| 153 | + "error": "No supported documents found", |
| 154 | + "directory": str(directory_path), |
| 155 | + "supported_formats": supported_formats |
| 156 | + } |
| 157 | + |
| 158 | + return self.process_batch(file_paths) |
| 159 | + |
| 160 | + def extract_requirements(self, processed_docs: list[dict[str, Any]]) -> dict[str, Any]: |
| 161 | + """Extract and consolidate requirements from processed documents.""" |
| 162 | + logger.info(f"Extracting requirements from {len(processed_docs)} documents") |
| 163 | + |
| 164 | + requirements = { |
| 165 | + "functional": [], |
| 166 | + "non_functional": [], |
| 167 | + "business": [], |
| 168 | + "technical": [], |
| 169 | + "constraints": [], |
| 170 | + "assumptions": [] |
| 171 | + } |
| 172 | + |
| 173 | + sources = [] |
| 174 | + |
| 175 | + for doc in processed_docs: |
| 176 | + if not doc.get("success"): |
| 177 | + continue |
| 178 | + |
| 179 | + content = doc.get("processed_content", {}) |
| 180 | + |
| 181 | + # Extract from AI analysis if available |
| 182 | + if "ai_analysis" in content: |
| 183 | + ai_analysis = content["ai_analysis"] |
| 184 | + if "key_info" in ai_analysis: |
| 185 | + # Parse requirements from key information |
| 186 | + self._parse_requirements_from_text(ai_analysis["key_info"], requirements) |
| 187 | + |
| 188 | + # Extract from structured content |
| 189 | + if "content" in content: |
| 190 | + self._parse_requirements_from_text(content["content"], requirements) |
| 191 | + |
| 192 | + sources.append({ |
| 193 | + "file": doc.get("file_path"), |
| 194 | + "title": content.get("metadata", {}).get("title", "Unknown") |
| 195 | + }) |
| 196 | + |
| 197 | + return { |
| 198 | + "requirements": requirements, |
| 199 | + "sources": sources, |
| 200 | + "extraction_method": "DocumentPipeline", |
| 201 | + "total_documents": len(processed_docs), |
| 202 | + "timestamp": self._get_timestamp() |
| 203 | + } |
| 204 | + |
| 205 | + def _parse_requirements_from_text(self, text: str, requirements: dict[str, list]) -> None: |
| 206 | + """Parse requirements from text content (basic implementation).""" |
| 207 | + # This is a basic implementation - can be enhanced with NLP/LLM |
| 208 | + text.lower() |
| 209 | + |
| 210 | + # Simple keyword-based classification |
| 211 | + lines = text.split('\n') |
| 212 | + |
| 213 | + for line in lines: |
| 214 | + line = line.strip() |
| 215 | + if not line: |
| 216 | + continue |
| 217 | + |
| 218 | + line_lower = line.lower() |
| 219 | + |
| 220 | + # Functional requirements |
| 221 | + if any(keyword in line_lower for keyword in ['shall', 'must', 'will', 'should']): |
| 222 | + if any(keyword in line_lower for keyword in ['system', 'user', 'function', 'feature']): |
| 223 | + requirements['functional'].append(line) |
| 224 | + |
| 225 | + # Non-functional requirements |
| 226 | + elif any(keyword in line_lower for keyword in ['performance', 'security', 'usability', 'reliability']): |
| 227 | + requirements['non_functional'].append(line) |
| 228 | + |
| 229 | + # Business requirements |
| 230 | + elif any(keyword in line_lower for keyword in ['business', 'stakeholder', 'goal', 'objective']): |
| 231 | + requirements['business'].append(line) |
| 232 | + |
| 233 | + # Technical requirements |
| 234 | + elif any(keyword in line_lower for keyword in ['technical', 'architecture', 'platform', 'technology']): |
| 235 | + requirements['technical'].append(line) |
| 236 | + |
| 237 | + # Constraints |
| 238 | + elif any(keyword in line_lower for keyword in ['constraint', 'limitation', 'restriction']): |
| 239 | + requirements['constraints'].append(line) |
| 240 | + |
| 241 | + # Assumptions |
| 242 | + elif any(keyword in line_lower for keyword in ['assumption', 'assume', 'presume']): |
| 243 | + requirements['assumptions'].append(line) |
| 244 | + |
| 245 | + def get_pipeline_info(self) -> dict[str, Any]: |
| 246 | + """Get information about the pipeline configuration.""" |
| 247 | + return { |
| 248 | + "name": "DocumentPipeline", |
| 249 | + "agent": "DocumentAgent", |
| 250 | + "processors_count": len(self.processors), |
| 251 | + "output_handlers_count": len(self.output_handlers), |
| 252 | + "caching_enabled": self.config.get("use_cache", True) |
| 253 | + } |
0 commit comments