"""Artifact storage module — per-session CRUD operations on the filesystem.""" from pathlib import Path ARTIFACTS_DIR = Path("/app/artifacts") def list_artifacts(session_id: str) -> list[dict]: """Return sorted list of artifact dicts for a session. Each dict contains: name, session_id, size. Returns [] if the session directory does not exist. """ session_dir = ARTIFACTS_DIR / session_id if not session_dir.exists(): return [] return sorted( [ { "name": f.name, "session_id": session_id, "size": f.stat().st_size, } for f in session_dir.iterdir() if f.is_file() ], key=lambda d: d["name"], ) def get_artifact(session_id: str, name: str) -> str | None: """Return file content (utf-8) or None if the artifact does not exist.""" artifact_path = ARTIFACTS_DIR / session_id / name if not artifact_path.exists(): return None return artifact_path.read_text(encoding="utf-8") def save_artifact(session_id: str, name: str, content: str) -> Path: """Write content to the artifact file, creating parent dirs as needed. Returns the Path of the written file. """ session_dir = ARTIFACTS_DIR / session_id session_dir.mkdir(parents=True, exist_ok=True) artifact_path = session_dir / name artifact_path.write_text(content, encoding="utf-8") return artifact_path