artifacts.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from __future__ import annotations
  2. from dataclasses import dataclass
  3. from typing import Any
  4. from cyber_agent.core.artifacts import (
  5. ArtifactRef,
  6. ValidationMaterial,
  7. material_content_hash,
  8. )
  9. @dataclass(frozen=True)
  10. class _StoredArtifact:
  11. ref: ArtifactRef
  12. root_trace_id: str
  13. uid: str | None
  14. content: Any
  15. class ReferenceArtifactStore:
  16. """Example-owned content storage exposed through the framework resolver port."""
  17. def __init__(self) -> None:
  18. self._items: dict[tuple[str, str], _StoredArtifact] = {}
  19. def put(
  20. self,
  21. *,
  22. artifact_id: str,
  23. version: str,
  24. kind: str,
  25. root_trace_id: str,
  26. uid: str | None,
  27. content: Any,
  28. ) -> ArtifactRef:
  29. ref = ArtifactRef(
  30. artifact_id=artifact_id,
  31. version=version,
  32. content_hash=material_content_hash(content),
  33. kind=kind,
  34. mime_type="application/json",
  35. )
  36. self._items[(artifact_id, version)] = _StoredArtifact(
  37. ref=ref,
  38. root_trace_id=root_trace_id,
  39. uid=uid,
  40. content=content,
  41. )
  42. return ref
  43. async def resolve(self, ref, root_trace_id, uid):
  44. item = self._items[(ref.artifact_id, ref.version)]
  45. if item.ref != ref:
  46. raise ValueError("ArtifactRef content hash does not match storage")
  47. if item.root_trace_id != root_trace_id or item.uid != uid:
  48. raise ValueError("Artifact belongs to another root or owner")
  49. return ValidationMaterial(
  50. **ref.model_dump(mode="json"),
  51. root_trace_id=root_trace_id,
  52. uid=uid,
  53. content=item.content,
  54. )