34 lines
942 B
Python
34 lines
942 B
Python
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
_WARNED_PATHS: set[Path] = set()
|
|
|
|
|
|
def load_system_prompt(*, default: str, path: str = "SYSTEM.md") -> str:
|
|
"""Load the system prompt from disk, with a fallback default."""
|
|
prompt_path = Path(path)
|
|
try:
|
|
content = prompt_path.read_text(encoding="utf-8").strip()
|
|
except FileNotFoundError:
|
|
_warn_once(prompt_path, "not found")
|
|
return default
|
|
except OSError as exc: # pragma: no cover - environment specific
|
|
_warn_once(prompt_path, f"unreadable ({exc})")
|
|
return default
|
|
|
|
if not content:
|
|
_warn_once(prompt_path, "empty")
|
|
return default
|
|
return content
|
|
|
|
|
|
def _warn_once(path: Path, reason: str) -> None:
|
|
if path in _WARNED_PATHS:
|
|
return
|
|
_WARNED_PATHS.add(path)
|
|
print(
|
|
f"[Prompt] {path} is {reason}; using built-in default prompt.",
|
|
file=sys.stderr,
|
|
)
|