"""Pydantic v2 request/response models for the FastAPI surface. This module defines the wire-level contract for the public HTTP API: * ``AuditStatusResponse`` — body of ``GET /api/audits/{job_id}``. * ``AuditSlidesResponse`` — body of ``GET /api/audits/{job_id}/slides`` (extended shape, includes ``finding_meta`` per finding). * ``AuditExportJson`` — body of ``GET /api/audits/{job_id}/export?fmt=json`` (strict PRD shape, NO ``finding_meta``). Every model pins ``model_config = ConfigDict(extra='forbid')`` so the generated JSON Schema sets ``additionalProperties: false`` — required by the acceptance test ``Finding.model_json_schema()['additionalProperties'] is False``. The audit identifier is named ``job_id`` everywhere — matching both the ``:jobId`` URL parameter and the POST ``/api/audits`` response shape from the PRD. We do NOT use ``audit_id`` or ``id`` anywhere on the wire. """ from __future__ import annotations from typing import Literal from pydantic import BaseModel, ConfigDict # --------------------------------------------------------------------------- # Status / stage enums (string-typed so JSON Schema is friendly) # --------------------------------------------------------------------------- JobStatus = Literal["queued", "processing", "reviewed", "partial", "failed"] JobStage = Literal["uploading", "parsing", "rendering", "reviewing", "finalizing"] # --------------------------------------------------------------------------- # Shared per-finding shapes (mirror agents.output_schemas, but redefined here # so the API surface is independent of the agent module — see the FindingMeta # coupling in the spec). # --------------------------------------------------------------------------- class Finding(BaseModel): """Strict PRD per-finding shape — eight fields, nothing else. A test asserts ``Finding.model_json_schema()['additionalProperties'] is False``. """ model_config = ConfigDict(extra="forbid") category: str severity: Literal["high", "medium", "low"] confidence: float title: str description: str evidence: str suggested_fix: str guideline_reference: str class FindingMeta(BaseModel): """Internal provenance metadata used only on the extended shape.""" model_config = ConfigDict(extra="forbid") source: Literal["agent_slide", "agent_deck", "safety_net", "merged"] must_flag: bool class FindingExtended(Finding): """``Finding`` plus ``finding_meta`` — used by ``/slides`` only.""" model_config = ConfigDict(extra="forbid") finding_meta: FindingMeta # --------------------------------------------------------------------------- # Per-slide envelopes # --------------------------------------------------------------------------- class SlideEnvelope(BaseModel): """Strict PRD per-slide envelope — used by ``/export?fmt=json``.""" model_config = ConfigDict(extra="forbid") slide_number: int findings: list[Finding] = [] class SlideEnvelopeExtended(BaseModel): """Extended per-slide envelope — used by ``/slides``.""" model_config = ConfigDict(extra="forbid") slide_number: int image_url: str | None = None findings: list[FindingExtended] = [] # --------------------------------------------------------------------------- # Aggregate / progress shapes # --------------------------------------------------------------------------- class SeverityCounts(BaseModel): model_config = ConfigDict(extra="forbid") high: int = 0 medium: int = 0 low: int = 0 class StageProgress(BaseModel): """Render or review progress within the current pipeline stage.""" model_config = ConfigDict(extra="forbid") current_completed: int total: int active_slides: list[int] = [] class ErrorEntry(BaseModel): """One per-slide or per-stage error captured during the pipeline run.""" model_config = ConfigDict(extra="forbid") slide_number: int | None = None stage: str | None = None message: str # --------------------------------------------------------------------------- # Top-level response shapes # --------------------------------------------------------------------------- class AuditCreateResponse(BaseModel): """Body of ``POST /api/audits`` — minimal handshake.""" model_config = ConfigDict(extra="forbid") job_id: str status: JobStatus class AuditStatusResponse(BaseModel): """Body of ``GET /api/audits/{job_id}``.""" model_config = ConfigDict(extra="forbid") job_id: str file_name: str status: JobStatus stage: JobStage | None = None stage_progress: StageProgress | None = None slide_count: int = 0 errors: list[ErrorEntry] = [] summary: SeverityCounts = SeverityCounts() class AuditSlidesResponse(BaseModel): """Body of ``GET /api/audits/{job_id}/slides`` — extended shape.""" model_config = ConfigDict(extra="forbid") job_id: str file_name: str slide_count: int summary: SeverityCounts = SeverityCounts() slides: list[SlideEnvelopeExtended] = [] class AuditExportJson(BaseModel): """Body of ``GET /api/audits/{job_id}/export?fmt=json`` — STRICT shape.""" model_config = ConfigDict(extra="forbid") job_id: str file_name: str slide_count: int summary: SeverityCounts = SeverityCounts() slides: list[SlideEnvelope] = [] class AuditListEntry(BaseModel): """One row in ``GET /api/audits`` — a slim view of a job for the listing.""" model_config = ConfigDict(extra="forbid") job_id: str file_name: str status: JobStatus slide_count: int summary: SeverityCounts = SeverityCounts() created_at: str completed_at: str | None = None errors_count: int = 0 class AuditListResponse(BaseModel): """Body of ``GET /api/audits``.""" model_config = ConfigDict(extra="forbid") jobs: list[AuditListEntry] = [] class PreflightChecks(BaseModel): model_config = ConfigDict(extra="forbid") jobs_root_writable: bool openai_api_key: bool soffice: bool pdftoppm: bool class PreflightResponse(BaseModel): model_config = ConfigDict(extra="forbid") ready: bool checks: PreflightChecks warnings: list[str] = [] jobs_root: str __all__ = [ "JobStatus", "JobStage", "Finding", "FindingMeta", "FindingExtended", "SlideEnvelope", "SlideEnvelopeExtended", "SeverityCounts", "StageProgress", "ErrorEntry", "AuditCreateResponse", "AuditStatusResponse", "AuditSlidesResponse", "AuditExportJson", "AuditListEntry", "AuditListResponse", "PreflightChecks", "PreflightResponse", ]