"""Load map/reduce prompts: env wins, else file, else bundled k8s prompts. Blank ``MAP_PROMPT`` / ``SUMMARY_PROMPT`` (compose ``${VAR:-}``) is treated as unset so a host .env can swap text without rebuilding the image. """ from __future__ import annotations import os from pathlib import Path _BUNDLED = Path(__file__).resolve().parent / "prompt_files" _MAP_FILE = "map.txt" _SUMMARY_FILE = "summary.txt" def resolve_prompt(env_name: str, file_env: str, bundled_name: str) -> str: raw = (os.getenv(env_name) or "").strip() if raw: return raw path = (os.getenv(file_env) or "").strip() or str(_BUNDLED / bundled_name) try: text = Path(path).read_text(encoding="utf-8") except OSError as exc: raise RuntimeError(f"{env_name} unset and prompt file unreadable: {path}") from exc if not text.strip(): raise RuntimeError(f"{env_name} unset and prompt file empty: {path}") return text def map_prompt_template() -> str: return resolve_prompt("MAP_PROMPT", "MAP_PROMPT_FILE", _MAP_FILE) def summary_prompt_template() -> str: return resolve_prompt("SUMMARY_PROMPT", "SUMMARY_PROMPT_FILE", _SUMMARY_FILE)