| 1 | """ |
| 2 | Read attachment files from execution runtime. |
| 3 | |
| 4 | No agent/tool dependencies. |
| 5 | """ |
| 6 | |
| 7 | import base64 |
| 8 | import os |
| 9 | from typing import TypedDict |
| 10 | |
| 11 | |
| 12 | # ------------------------------------------------------------------ |
| 13 | # Data models |
| 14 | # ------------------------------------------------------------------ |
| 15 | |
| 16 | class AttachmentData(TypedDict): |
| 17 | name: str |
| 18 | content_b64: str |
| 19 | error: str |
| 20 | |
| 21 | |
| 22 | # ------------------------------------------------------------------ |
| 23 | # File reader |
| 24 | # ------------------------------------------------------------------ |
| 25 | |
| 26 | def read_attachment(path: str) -> AttachmentData: |
| 27 | try: |
| 28 | if not os.path.isfile(path): |
| 29 | return AttachmentData( |
| 30 | name="", content_b64="", error=f"file not found: {path}") |
| 31 | name = os.path.basename(path) |
| 32 | with open(path, "rb") as f: |
| 33 | content = f.read() |
| 34 | return AttachmentData( |
| 35 | name=name, |
| 36 | content_b64=base64.b64encode(content).decode(), |
| 37 | error="", |
| 38 | ) |
| 39 | except Exception as e: |
| 40 | return AttachmentData(name="", content_b64="", error=str(e)) |