|
| 1 | +"""Immutable background outputs, excluded from the live retrieval corpus.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Self |
| 5 | + |
| 6 | +from pydantic import BaseModel, Field, JsonValue |
| 7 | + |
| 8 | +from shared.services.storage.result_storage import ResultStorage |
| 9 | + |
| 10 | + |
| 11 | +class StagedEnrichment(BaseModel): |
| 12 | + job_result_id: str |
| 13 | + scope_id: str |
| 14 | + claim_generation: str |
| 15 | + chunks: list[dict[str, JsonValue]] = Field(default_factory=list) |
| 16 | + section_summaries: dict[str, str] = Field(default_factory=dict) |
| 17 | + embeddings: dict[int, list[float]] = Field(default_factory=dict) |
| 18 | + |
| 19 | + def upload( |
| 20 | + self, |
| 21 | + *, |
| 22 | + storage: ResultStorage, |
| 23 | + job_id: str, |
| 24 | + raw_prefix: str | None, |
| 25 | + workspace_dir: str, |
| 26 | + ) -> str: |
| 27 | + reference: str = ( |
| 28 | + f"staged/{self.job_result_id}/{self.scope_id}/{self.claim_generation}.json" |
| 29 | + ) |
| 30 | + path: Path = Path(workspace_dir) / reference |
| 31 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 32 | + path.write_text(self.model_dump_json(), encoding="utf-8") |
| 33 | + storage.upload_raw_file( |
| 34 | + job_id=job_id, |
| 35 | + relative_path=reference, |
| 36 | + raw_prefix=raw_prefix, |
| 37 | + local_file_path=str(path), |
| 38 | + ) |
| 39 | + return reference |
| 40 | + |
| 41 | + @classmethod |
| 42 | + def download( |
| 43 | + cls, |
| 44 | + *, |
| 45 | + storage: ResultStorage, |
| 46 | + job_id: str, |
| 47 | + raw_prefix: str | None, |
| 48 | + reference: str, |
| 49 | + workspace_dir: str, |
| 50 | + job_result_id: str, |
| 51 | + scope_id: str, |
| 52 | + claim_generation: str | None = None, |
| 53 | + ) -> Self: |
| 54 | + path: str = storage.download_raw_to_temp( |
| 55 | + job_id=job_id, |
| 56 | + relative_path=reference, |
| 57 | + raw_prefix=raw_prefix, |
| 58 | + suffix=".json", |
| 59 | + temp_dir=workspace_dir, |
| 60 | + ) |
| 61 | + output: Self = cls.model_validate_json(Path(path).read_bytes()) |
| 62 | + if ( |
| 63 | + output.job_result_id != job_result_id |
| 64 | + or output.scope_id != scope_id |
| 65 | + or ( |
| 66 | + claim_generation is not None |
| 67 | + and output.claim_generation != claim_generation |
| 68 | + ) |
| 69 | + ): |
| 70 | + raise ValueError( |
| 71 | + "Staged enhancement does not belong to the committed claim" |
| 72 | + ) |
| 73 | + return output |
0 commit comments