commit_artifacts.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from __future__ import annotations
  4. from typing import Any
  5. EXTRACTION_FIELDS = (
  6. "accepted_events",
  7. "state_deltas",
  8. "entity_deltas",
  9. "entities_appeared",
  10. "scenes",
  11. "chapter_meta",
  12. "dominant_strand",
  13. "summary_text",
  14. )
  15. def extraction_result_from_commit(commit_payload: dict[str, Any]) -> dict[str, Any]:
  16. """Return the canonical extraction artifact from a commit.
  17. New commits store the extraction snapshot under ``extraction_result``.
  18. Older commits stored these fields at top level, so this helper keeps
  19. projections readable without preserving two write shapes. If the
  20. canonical nested artifact exists, it is the only source of truth.
  21. """
  22. nested = commit_payload.get("extraction_result")
  23. if isinstance(nested, dict):
  24. return dict(nested)
  25. result: dict[str, Any] = {}
  26. for field in EXTRACTION_FIELDS:
  27. if field in commit_payload:
  28. result[field] = commit_payload.get(field)
  29. return result
  30. def extraction_list(commit_payload: dict[str, Any], field: str) -> list[Any]:
  31. value = extraction_result_from_commit(commit_payload).get(field)
  32. return value if isinstance(value, list) else []
  33. def extraction_dict(commit_payload: dict[str, Any], field: str) -> dict[str, Any]:
  34. value = extraction_result_from_commit(commit_payload).get(field)
  35. return value if isinstance(value, dict) else {}
  36. def extraction_text(commit_payload: dict[str, Any], field: str) -> str:
  37. value = extraction_result_from_commit(commit_payload).get(field)
  38. return str(value or "").strip()