feat(backend): deliver Phase 3 document and voice integration
Add authenticated upload, review, retry and knowledge-selection HTTP flows with bounded JSON processing and frozen OCR policy. Preserve Gemini/OpenAI provider selection and fenced lifecycle semantics; retain FAQ preambles and uncertain vision candidates, and expose opt-in uncalibrated voice transport diagnostics. Include frontend integration examples, aligned contracts and deterministic regressions. Validation: 1767 provider-free tests passed; all 404 PostgreSQL scenarios have passing evidence across the documented runs.
This commit is contained in:
@@ -10,7 +10,7 @@ from threading import Event, Thread
|
||||
from .chunking import chunk_extraction
|
||||
from .embeddings import EmbeddingError, stage_document
|
||||
from .extraction import ExtractionError, ReviewRequired
|
||||
from .extraction_artifacts import extraction_profile_digest
|
||||
from .extraction_artifacts import extraction_profile_digest, ocr_review_required
|
||||
from .review import FactualReviewRequired, prepare_chunks
|
||||
from .operational_limits import validate_worker_durations
|
||||
|
||||
@@ -63,21 +63,36 @@ def validate_source(envelope, document, content):
|
||||
if envelope.title != document["title"]:
|
||||
raise ExtractionError("Extraction title differs from immutable source")
|
||||
warnings = (*envelope.warnings, *(w for b in envelope.blocks for w in b.warnings))
|
||||
if warnings and envelope.quality_decision != "reviewed":
|
||||
from .extraction import unreadable, PLACEHOLDER
|
||||
from .review import factual_groups
|
||||
groups = factual_groups(envelope)
|
||||
if any(unreadable(b.text) or (PLACEHOLDER.search(b.text) and b.group_id not in groups)
|
||||
for b in envelope.blocks):
|
||||
raise ExtractionError("Unreadable or placeholder extracted evidence")
|
||||
if warnings and envelope.quality_decision not in ("reviewed", "partially_reviewed"):
|
||||
raise ExtractionError("Unreviewed extraction warnings")
|
||||
if any("unreadable" in w.lower() or "\u0646\u0627\u062e\u0648\u0627\u0646\u0627" in w for w in warnings):
|
||||
raise ExtractionError("Unreadable retained evidence")
|
||||
mime = document["mime_type"]
|
||||
if envelope.quality_decision == "partially_reviewed":
|
||||
inspected = {annotation.locator for annotation in envelope.review_annotations}
|
||||
automatic = [b for b in envelope.blocks if not any(
|
||||
b.source_locator.startswith(locator + "/") for locator in inspected)]
|
||||
if (mime != "application/pdf" or ocr_review_required(document["processing_profile"])
|
||||
or not inspected or not automatic or envelope.warnings
|
||||
or any(b.method not in ("vision", "ocr") or b.warnings for b in automatic)):
|
||||
raise ExtractionError("Mixed extraction requires exact inspected non-automatic evidence")
|
||||
automatic_ocr = (not ocr_review_required(document["processing_profile"])
|
||||
and envelope.quality_decision == "passed" and not warnings
|
||||
and all(b.method in ("vision", "ocr") for b in envelope.blocks))
|
||||
if mime == "application/pdf":
|
||||
import pymupdf
|
||||
with pymupdf.open(stream=content, filetype="pdf") as source:
|
||||
if (source.page_count > document["processing_profile"]["extraction"]["max_pdf_pages"]
|
||||
or {b.page for b in envelope.blocks} != set(range(1, source.page_count + 1))):
|
||||
raise ExtractionError("Incomplete PDF source coverage")
|
||||
if envelope.quality_decision != "reviewed":
|
||||
if envelope.quality_decision not in ("reviewed", "partially_reviewed") and not automatic_ocr:
|
||||
raise ExtractionError("PDF extraction requires inspection")
|
||||
elif mime in ("image/jpeg", "image/png"):
|
||||
if envelope.quality_decision != "reviewed" or any(
|
||||
if (envelope.quality_decision != "reviewed" and not automatic_ocr) or any(
|
||||
b.page is not None or b.method not in ("vision", "ocr") for b in envelope.blocks):
|
||||
raise ExtractionError("Image extraction requires inspected complete evidence")
|
||||
elif mime in ("text/markdown", "text/plain",
|
||||
|
||||
Reference in New Issue
Block a user