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