Files
timmy-home/evennia_tools/telemetry.py
2026-03-28 13:33:26 -04:00

29 lines
1.1 KiB
Python

import json
from datetime import datetime, timezone
from pathlib import Path
def telemetry_dir(base_dir: str | Path = "~/.timmy/training-data/evennia") -> Path:
return Path(base_dir).expanduser()
def event_log_path(session_id: str, base_dir: str | Path = "~/.timmy/training-data/evennia") -> Path:
session_id = (session_id or "unbound").strip() or "unbound"
day = datetime.now(timezone.utc).strftime("%Y%m%d")
return telemetry_dir(base_dir) / day / f"{session_id}.jsonl"
def append_event(session_id: str, event: dict, base_dir: str | Path = "~/.timmy/training-data/evennia") -> Path:
path = event_log_path(session_id, base_dir)
path.parent.mkdir(parents=True, exist_ok=True)
payload = dict(event)
payload.setdefault("timestamp", datetime.now(timezone.utc).isoformat())
with path.open("a", encoding="utf-8") as f:
f.write(json.dumps(payload, ensure_ascii=False) + "\n")
return path
def excerpt(text: str, limit: int = 240) -> str:
text = " ".join((text or "").split())
return text if len(text) <= limit else text[: limit - 3] + "..."