File size: 6,713 Bytes
fdfe81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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",
]