| 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), |
| 95 | sha256=sha256, |
| 96 | preview=preview, |
| 97 | summary=summary, |
| 98 | ) |
| 99 | self._content_path(artifact_id).write_text(content) |
| 100 | self._meta_path(artifact_id).write_text(json.dumps(asdict(record), indent=2)) |
| 101 | return record |
| 102 | |
| 103 | def update_summary(self, artifact_id: str, summary: str) -> dict[str, Any] | None: |
| 104 | artifact = self.get(artifact_id) |