Filesystem-backed artifact store.
| 35 | |
| 36 | |
| 37 | class ArtifactStore: |
| 38 | """Filesystem-backed artifact store.""" |
| 39 | |
| 40 | def __init__( |
| 41 | self, |
| 42 | root: Path | None = None, |
| 43 | *, |
| 44 | now_fn: Any = None, |
| 45 | ) -> None: |
| 46 | self._root = root or DEFAULT_ARTIFACTS_DIR |
| 47 | self._now = now_fn or time.time |
| 48 | self._root.mkdir(parents=True, exist_ok=True, mode=0o700) |
| 49 | |
| 50 | @property |
| 51 | def root(self) -> Path: |
| 52 | return self._root |
| 53 | |
| 54 | def store_text( |
| 55 | self, |
| 56 | content: str, |
| 57 | *, |
| 58 | kind: str = "raw", |
| 59 | role: str, |
| 60 | session_id: str = "", |
| 61 | tool_name: str = "", |
| 62 | tool_call_id: str = "", |
| 63 | content_type: str = "text/plain", |
| 64 | summary: str = "", |
| 65 | ) -> ArtifactRecord: |
| 66 | created_at = self._now() |
| 67 | sha256 = hashlib.sha256(content.encode("utf-8")).hexdigest() |
| 68 | existing = self._find_existing( |
| 69 | sha256=sha256, |
| 70 | kind=kind, |
| 71 | role=role, |
| 72 | session_id=session_id, |
| 73 | tool_name=tool_name, |
| 74 | tool_call_id=tool_call_id, |
| 75 | ) |
| 76 | if existing is not None: |
| 77 | if summary and not existing.summary: |
| 78 | updated = ArtifactRecord(**{**asdict(existing), "summary": summary}) |
| 79 | self._meta_path(existing.id).write_text(json.dumps(asdict(updated), indent=2)) |
| 80 | return updated |
| 81 | return existing |
| 82 | artifact_id = uuid.uuid4().hex[:12] |
| 83 | preview = content[:240].replace("\r\n", "\n").replace("\r", "\n") |
| 84 | record = ArtifactRecord( |
| 85 | id=artifact_id, |
| 86 | created_at=created_at, |
| 87 | kind=kind, |
| 88 | role=role, |
| 89 | session_id=session_id, |
| 90 | tool_name=tool_name, |
| 91 | tool_call_id=tool_call_id, |
| 92 | content_type=content_type, |
| 93 | char_count=len(content), |
| 94 | token_estimate=estimate_tokens(content), |
no outgoing calls