feat(ingestion): extend domain types, ports, and configuration for ingestion pipeline

- Add ExtractedBlock, ExtractionEnvelope, and StagedDocument data models in types.py
- Define ingestion repository, file store, vision, and extraction ports in ports.py
- Extend IngestionSettings with limits, policies, vision, and embedding configuration
- Update database compatibility verification to support worker and review schema revisions
- Add PyMuPDF, Pillow, defusedxml, and tiktoken dependencies to pyproject.toml
This commit is contained in:
amirhosein mazaheri
2026-10-07 16:20:19 +03:30
parent ca8833dd2f
commit 0a998ac94a
6 changed files with 333 additions and 31 deletions
+22 -3
View File
@@ -9,7 +9,9 @@ from sqlalchemy.orm import sessionmaker
from app.modules.documents.types import EmbeddingProfile
SCHEMA_REVISION = "0001_ingestion_foundation"
SCHEMA_REVISION = "0003_extraction_review"
WORKER_REVISION = "0002_worker_artifact"
FOUNDATION_REVISION = "0001_ingestion_foundation"
FOUNDATION_TABLES = (
"organizations", "users", "organization_settings", "documents",
"ingestion_jobs", "document_chunks", "audit_events",
@@ -56,12 +58,29 @@ def session_factory(engine: Engine):
return sessionmaker(engine, expire_on_commit=False)
def check_compatibility(connection: Connection, profile: EmbeddingProfile) -> None:
def check_compatibility(connection: Connection, profile: EmbeddingProfile, *, require_worker=False) -> None:
"""Fail closed on schema, dimension or full embedding identity mismatch."""
schema = connection.execute(text("SELECT current_schema()")).scalar_one()
assert_foundation_scope(connection, schema)
if connection.execute(text("SELECT version_num FROM alembic_version")).scalar_one() != SCHEMA_REVISION:
revision = connection.execute(text("SELECT version_num FROM alembic_version")).scalar_one()
allowed = (SCHEMA_REVISION,) if require_worker else (FOUNDATION_REVISION, WORKER_REVISION, SCHEMA_REVISION)
if revision not in allowed:
raise CompatibilityError("Unexpected ingestion schema revision")
if revision in (WORKER_REVISION, SCHEMA_REVISION):
artifact_type = connection.execute(text("""
SELECT format_type(atttypid,atttypmod) FROM pg_attribute
WHERE attrelid=to_regclass('documents') AND attname='extraction_state' AND NOT attisdropped
""")).scalar_one_or_none()
if artifact_type != "jsonb":
raise CompatibilityError("Missing worker artifact attachment column")
if revision == SCHEMA_REVISION:
definitions = connection.execute(text("""
SELECT pg_get_constraintdef(oid) FROM pg_constraint
WHERE conrelid=to_regclass(\'audit_events\')
AND conname IN (\'audit_events_action_check\',\'audit_events_extraction_review_replay\')
""")).scalars().all()
if len(definitions) != 2 or not all("extraction_review" in d for d in definitions):
raise CompatibilityError("Missing review audit/replay checks")
row = connection.execute(text("""
SELECT format_type(a.atttypid,a.atttypmod) AS vector_type,
col_description(a.attrelid,a.attnum) AS profile