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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
"""

Hermes Tools β€” Unified compliance pipeline and engine access.



This module provides:

- `CompliancePipeline`: Thread-safe orchestrator for PDF parsing, HS classification,

  and sanctions screening in a single call.

- Re-exports from `base` for backward compatibility.



Usage:

    from hermes.tools import CompliancePipeline, get_pipeline



    pipeline = get_pipeline()

    review_item = pipeline.process_incoming_document(

        file_path="invoice.pdf",

        context={"shipper": "Acme Corp", "consignee": "Global Imports LLC"},

    )

"""

from __future__ import annotations

import logging
import tempfile
import threading
from pathlib import Path
from typing import Annotated, Any, Final, Optional

from hermes.tools.base.registry import tool_registry
from hermes.tools.base.tool import BaseTool

logger: Final = logging.getLogger(__name__)

# ── Domain Exceptions ─────────────────────────────────────────────────


class PipelineError(Exception):
    """Base exception for pipeline execution errors."""


class PipelineValidationError(PipelineError):
    """Raised when pipeline input validation fails.



    Attributes:

        field: The field that failed validation.

        reason: Description of the validation failure.

    """

    def __init__(self, field: str, reason: str) -> None:
        self.field = field
        self.reason = reason
        super().__init__(f"Validation failed for '{field}': {reason}")


class PipelinePDFParsingError(PipelineError):
    """Raised when PDF parsing fails during pipeline execution.



    Attributes:

        file_path: Path to the PDF that failed parsing.

        original_error: The underlying parsing exception.

    """

    def __init__(self, file_path: str, original_error: Exception) -> None:
        self.file_path = file_path
        self.original_error = original_error
        super().__init__(f"PDF parsing failed for {file_path}: {original_error}")


class PipelineClassificationError(PipelineError):
    """Raised when HS code classification fails.



    Attributes:

        description: The product description that failed classification.

        original_error: The underlying classification exception.

    """

    def __init__(self, description: str, original_error: Exception) -> None:
        self.description = description
        self.original_error = original_error
        super().__init__(
            f"HS classification failed for '{description[:50]}...': {original_error}"
        )


class PipelineSanctionsError(PipelineError):
    """Raised when sanctions screening fails.



    Attributes:

        party_name: The party name that failed screening.

        original_error: The underlying screening exception.

    """

    def __init__(self, party_name: str, original_error: Exception) -> None:
        self.party_name = party_name
        self.original_error = original_error
        super().__init__(
            f"Sanctions screening failed for '{party_name}': {original_error}"
        )


# ── Constants ─────────────────────────────────────────────────────────

MAX_TEMP_FILE_SIZE: Final[int] = 50 * 1024 * 1024  # 50 MB


# ── Compliance Pipeline ───────────────────────────────────────────────


class CompliancePipeline:
    """Thread-safe pipeline orchestrating PDF parsing, HS classification, and sanctions screening.



    Provides a single method `process_incoming_document` that:

    1. Parses the PDF to extract document fields

    2. Classifies extracted items to HS codes

    3. Screens all parties against sanctions lists

    4. Assembles a ReviewItem for human approval



    All errors are wrapped in pipeline-specific exceptions for consistent

    error handling upstream.



    Attributes:

        _parser: PDF parser singleton.

        _hs_engine: HS classification engine singleton.

        _sanctions_engine: Sanctions screening engine singleton.

    """

    def __init__(self) -> None:
        """Initialize the pipeline with engine singletons."""
        from hermes.tools.pdf_parser import get_parser
        from hermes.tools.hs_classifier import get_engine as get_hs_engine
        from hermes.tools.sanctions import get_engine as get_sanctions_engine

        self._parser = get_parser()
        self._hs_engine = get_hs_engine()
        self._sanctions_engine = get_sanctions_engine()
        self._lock: threading.Lock = threading.Lock()

    def process_incoming_document(

        self,

        file_path: str,

        context: Optional[dict[str, Any]] = None,

    ) -> dict[str, Any]:
        """Process a trade document through the full compliance pipeline.



        Orchestrates PDF parsing β†’ HS classification β†’ sanctions screening

        and returns a ReviewItem dict ready for human review.



        Args:

            file_path: Path to the PDF document to process.

            context: Optional context dict with overrides:

                - shipper (str): Override shipper name.

                - consignee (str): Override consignee name.

                - country_origin (str): Override country of origin.

                - country_destination (str): Override destination country.

                - invoice_number (str): Override invoice number.



        Returns:

            Dict representing a ReviewItem with all extracted and classified data.



        Raises:

            FileNotFoundError: If the file does not exist.

            PipelineValidationError: If input validation fails.

            PipelinePDFParsingError: If PDF parsing fails.

            PipelineClassificationError: If HS classification fails.

            PipelineSanctionsError: If sanctions screening fails.

        """
        ctx: dict[str, Any] = context or {}
        path: Path = Path(file_path)

        if not path.exists():
            raise FileNotFoundError(f"Document not found: {file_path}")

        if not path.suffix.lower() == ".pdf":
            raise PipelineValidationError(
                "file_path",
                f"Expected PDF file, got {path.suffix or 'no extension'}",
            )

        # Step 1: Parse PDF
        parsed_doc = self._parse_pdf(file_path)

        # Step 2: Extract and classify items
        classifications: list[dict[str, Any]] = self._classify_items(
            parsed_doc, ctx
        )

        # Step 3: Screen parties
        sanctions_results: dict[str, Any] = self._screen_parties(parsed_doc, ctx)

        # Step 4: Assemble ReviewItem
        review_item: dict[str, Any] = self._assemble_review_item(
            file_path=file_path,
            parsed_doc=parsed_doc,
            classifications=classifications,
            sanctions_results=sanctions_results,
            context=ctx,
        )

        logger.info(
            "Pipeline processed %s β†’ review item %s",
            file_path,
            review_item.get("item_id", "pending"),
        )
        return review_item

    def _parse_pdf(self, file_path: str) -> Any:
        """Parse the PDF and extract document fields.



        Args:

            file_path: Path to the PDF file.



        Returns:

            ParsedDocument with extracted fields.



        Raises:

            PipelinePDFParsingError: If parsing fails.

        """
        try:
            from hermes.tools.pdf_parser import (
                PDFSizeLimitExceededError,
                PDFPageLimitExceededError,
                PDFNotValidError,
                PDFEmptyError,
            )

            return self._parser.parse(file_path)
        except (PDFSizeLimitExceededError, PDFPageLimitExceededError) as exc:
            raise PipelinePDFParsingError(file_path, exc) from exc
        except (PDFNotValidError, PDFEmptyError) as exc:
            raise PipelinePDFParsingError(file_path, exc) from exc
        except FileNotFoundError:
            raise
        except Exception as exc:
            raise PipelinePDFParsingError(file_path, exc) from exc

    def _classify_items(self, parsed_doc: Any, ctx: dict[str, Any]) -> list[dict[str, Any]]:
        """Classify extracted items to HS codes.



        Args:

            parsed_doc: ParsedDocument from PDF parsing.

            ctx: Context dict with optional overrides.



        Returns:

            List of classification result dicts.

        """
        from hermes.tools.hs_classifier import HSClassificationHallucinationError
        from hermes.tools.pdf_parser import InvoiceItem

        classifications: list[dict[str, Any]] = []

        items: list[InvoiceItem] = getattr(parsed_doc, "items", [])
        if not items:
            # If no items extracted, classify based on document description
            description: str = self._build_description_from_doc(parsed_doc, ctx)
            if description:
                cls_result = self._classify_single(description, ctx)
                if cls_result:
                    classifications.append(cls_result)
            return classifications

        country_origin: str = ctx.get(
            "country_origin",
            getattr(
                parsed_doc.country_origin, "value", ""
            ) if parsed_doc.country_origin else "",
        )
        country_dest: str = ctx.get(
            "country_destination",
            getattr(
                parsed_doc.country_destination, "value", ""
            ) if parsed_doc.country_destination else "",
        )

        for item in items:
            if not item.description:
                continue
            try:
                response = self._hs_engine.classify(
                    description=item.description,
                    country_origin=country_origin,
                    country_destination=country_dest,
                    use_llm=False,
                    target_digits=6,
                )
                alt_list: list[dict[str, Any]] = []
                for alt in response.alternatives:
                    alt_list.append({
                        "hs_code": alt.hs_code,
                        "description": alt.description,
                        "confidence": round(alt.confidence, 3),
                    })
                classifications.append({
                    "description": item.description,
                    "hs_code": response.primary.hs_code,
                    "hs_description": response.primary.description,
                    "confidence": round(response.primary.confidence, 3),
                    "source": response.primary.source,
                    "needs_human_review": response.primary.needs_human_review,
                    "alternatives": alt_list,
                    "quantity": item.quantity,
                    "unit_price": item.unit_price,
                    "total_price": item.total_price,
                })
            except HSClassificationHallucinationError as exc:
                logger.warning(
                    "Classification hallucination detected for '%s': %s",
                    item.description[:50],
                    exc,
                )
                classifications.append({
                    "description": item.description,
                    "hs_code": "999999",
                    "hs_description": "Classification failed - hallucination detected",
                    "confidence": 0.0,
                    "source": "error",
                    "needs_human_review": True,
                    "alternatives": [],
                    "quantity": item.quantity,
                    "unit_price": item.unit_price,
                    "total_price": item.total_price,
                })
            except Exception as exc:
                logger.warning(
                    "Classification failed for '%s': %s",
                    item.description[:50],
                    exc,
                )
                classifications.append({
                    "description": item.description,
                    "hs_code": "",
                    "hs_description": f"Classification error: {exc}",
                    "confidence": 0.0,
                    "source": "error",
                    "needs_human_review": True,
                    "alternatives": [],
                    "quantity": item.quantity,
                    "unit_price": item.unit_price,
                    "total_price": item.total_price,
                })

        return classifications

    def _classify_single(

        self, description: str, ctx: dict[str, Any]

    ) -> Optional[dict[str, Any]]:
        """Classify a single description.



        Args:

            description: Product description.

            ctx: Context dict with optional overrides.



        Returns:

            Classification result dict, or None if classification fails.

        """
        try:
            country_origin: str = ctx.get("country_origin", "")
            country_dest: str = ctx.get("country_destination", "")

            response = self._hs_engine.classify(
                description=description,
                country_origin=country_origin,
                country_destination=country_dest,
                use_llm=False,
                target_digits=6,
            )
            alt_list: list[dict[str, Any]] = []
            for alt in response.alternatives:
                alt_list.append({
                    "hs_code": alt.hs_code,
                    "description": alt.description,
                    "confidence": round(alt.confidence, 3),
                })
            return {
                "description": description,
                "hs_code": response.primary.hs_code,
                "hs_description": response.primary.description,
                "confidence": round(response.primary.confidence, 3),
                "source": response.primary.source,
                "needs_human_review": response.primary.needs_human_review,
                "alternatives": alt_list,
                "quantity": 0,
                "unit_price": 0,
                "total_price": 0,
            }
        except Exception as exc:
            logger.warning("Single classification failed: %s", exc)
            return None

    def _build_description_from_doc(

        self, parsed_doc: Any, ctx: dict[str, Any]

    ) -> str:
        """Build a product description from parsed document fields.



        Args:

            parsed_doc: ParsedDocument.

            ctx: Context dict.



        Returns:

            Description string for classification.

        """
        parts: list[str] = []
        if parsed_doc.document_type and parsed_doc.document_type != "unknown":
            parts.append(parsed_doc.document_type.replace("_", " ").title())
        if ctx.get("description"):
            parts.append(ctx["description"])
        if ctx.get("product_description"):
            parts.append(ctx["product_description"])
        return " ".join(parts) if parts else ""

    def _screen_parties(

        self, parsed_doc: Any, ctx: dict[str, Any]

    ) -> dict[str, Any]:
        """Screen all parties against sanctions lists.



        Args:

            parsed_doc: ParsedDocument with party information.

            ctx: Context dict with optional overrides.



        Returns:

            Dict mapping party names to their screening results.

        """
        from hermes.tools.sanctions import ScreeningResult

        parties: dict[str, str] = {}

        shipper: str = ctx.get("shipper", "")
        if not shipper and parsed_doc.shipper:
            shipper = getattr(parsed_doc.shipper, "name", "")
        if shipper:
            parties["shipper"] = shipper

        consignee: str = ctx.get("consignee", "")
        if not consignee and parsed_doc.consignee:
            consignee = getattr(parsed_doc.consignee, "name", "")
        if consignee:
            parties["consignee"] = consignee

        notify_party: str = ctx.get("notify_party", "")
        if not notify_party and parsed_doc.notify_party:
            notify_party = getattr(parsed_doc.notify_party, "name", "")
        if notify_party:
            parties["notify_party"] = notify_party

        results: dict[str, Any] = {}
        for role, name in parties.items():
            try:
                result: ScreeningResult = self._sanctions_engine.screen(name)
                matches_data: list[dict[str, Any]] = []
                for match in result.matches:
                    matches_data.append({
                        "entity_name": match.entity.name,
                        "confidence": round(match.confidence, 3),
                        "match_type": match.match_type,
                        "matched_name": match.matched_name,
                        "source": match.source,
                    })
                results[role] = {
                    "name": name,
                    "risk_level": result.risk_level,
                    "has_matches": result.has_matches,
                    "matches": matches_data,
                    "sources_checked": result.sources_checked,
                }
            except Exception as exc:
                logger.warning(
                    "Sanctions screening failed for %s '%s': %s",
                    role,
                    name[:50],
                    exc,
                )
                results[role] = {
                    "name": name,
                    "risk_level": "unknown",
                    "has_matches": False,
                    "matches": [],
                    "sources_checked": [],
                    "error": str(exc),
                }

        return results

    def _assemble_review_item(

        self,

        file_path: str,

        parsed_doc: Any,

        classifications: list[dict[str, Any]],

        sanctions_results: dict[str, Any],

        context: dict[str, Any],

    ) -> dict[str, Any]:
        """Assemble a ReviewItem dict from pipeline results.



        Args:

            file_path: Original PDF file path.

            parsed_doc: ParsedDocument from PDF parsing.

            classifications: List of HS classification results.

            sanctions_results: Dict of sanctions screening results.

            context: Context dict with overrides.



        Returns:

            Dict representing a ReviewItem.

        """
        invoice_number: str = context.get("invoice_number", "")
        if not invoice_number and parsed_doc.invoice_number:
            invoice_number = getattr(parsed_doc.invoice_number, "value", "")

        invoice_date: str = context.get("invoice_date", "")
        if not invoice_date and parsed_doc.invoice_date:
            invoice_date = getattr(parsed_doc.invoice_date, "value", "")

        total_amount: str = context.get("total_amount", "")
        if not total_amount and parsed_doc.total_amount:
            total_amount = getattr(parsed_doc.total_amount, "value", "")

        shipper: str = context.get("shipper", "")
        if not shipper and parsed_doc.shipper:
            shipper = getattr(parsed_doc.shipper, "name", "")

        consignee: str = context.get("consignee", "")
        if not consignee and parsed_doc.consignee:
            consignee = getattr(parsed_doc.consignee, "name", "")

        country_origin: str = context.get("country_origin", "")
        if not country_origin and parsed_doc.country_origin:
            country_origin = getattr(parsed_doc.country_origin, "value", "")

        country_dest: str = context.get("country_destination", "")
        if not country_dest and parsed_doc.country_destination:
            country_dest = getattr(parsed_doc.country_destination, "value", "")

        primary_hs: str = ""
        hs_description: str = ""
        hs_confidence: float = 0.0
        hs_alternatives: list[dict[str, Any]] = []
        needs_human_review: bool = False

        if classifications:
            best: dict[str, Any] = max(
                classifications, key=lambda c: c.get("confidence", 0)
            )
            primary_hs = best.get("hs_code", "")
            hs_description = best.get("hs_description", "")
            hs_confidence = best.get("confidence", 0.0)
            needs_human_review = best.get("needs_human_review", False)
            hs_alternatives = best.get("alternatives", [])

        sanctions_risk: str = "clear"
        all_sanctions_matches: list[dict[str, Any]] = []
        for role, result in sanctions_results.items():
            level: str = result.get("risk_level", "clear")
            if level in ("high", "blocked"):
                sanctions_risk = level
            for match in result.get("matches", []):
                all_sanctions_matches.append({
                    "role": role,
                    **match,
                })

        if needs_human_review or sanctions_risk in ("high", "blocked"):
            priority: int = 2 if sanctions_risk == "blocked" else 1
        else:
            priority = 0

        return {
            "item_id": "",
            "document_path": file_path,
            "document_type": parsed_doc.document_type or "unknown",
            "invoice_number": invoice_number,
            "invoice_date": invoice_date,
            "total_amount": total_amount,
            "shipper": shipper,
            "consignee": consignee,
            "country_origin": country_origin,
            "country_destination": country_dest,
            "hs_code_suggested": primary_hs,
            "hs_code_description": hs_description,
            "hs_code_confidence": hs_confidence,
            "hs_code_alternatives": hs_alternatives,
            "sanctions_risk_level": sanctions_risk,
            "sanctions_matches": all_sanctions_matches,
            "status": "pending",
            "assigned_to": "",
            "priority": priority,
            "comments": [],
            "actions": [],
            "final_hs_code": "",
            "reviewer_notes": "",
        }


# ── Singleton ─────────────────────────────────────────────────────────

_pipeline: Optional[CompliancePipeline] = None
_pipeline_lock: threading.Lock = threading.Lock()


def get_pipeline() -> CompliancePipeline:
    """Get or create the global compliance pipeline (thread-safe).



    Returns:

        The singleton CompliancePipeline instance.

    """
    global _pipeline
    if _pipeline is None:
        with _pipeline_lock:
            if _pipeline is None:
                _pipeline = CompliancePipeline()
    return _pipeline


# ── Backward Compatibility ────────────────────────────────────────────

__all__ = [
    "BaseTool",
    "tool_registry",
    "CompliancePipeline",
    "PipelineError",
    "PipelineValidationError",
    "PipelinePDFParsingError",
    "PipelineClassificationError",
    "PipelineSanctionsError",
    "get_pipeline",
]