"""Diary log tool implementations. Raw, granular, agent-authored entries. Compaction (not in this module) will later distill these into daily/monthly markdown summaries; raw entries remain in sqlite as the lossless archive. """ from datetime import datetime, timedelta, timezone from db import get_conn def _now() -> str: return datetime.now(timezone.utc).astimezone().isoformat(timespec="seconds") def _row(r): return dict(r) if r else None def log(content: str) -> dict: with get_conn() as conn: cur = conn.execute( "INSERT INTO diary_logs (ts, content) VALUES (?, ?)", (_now(), content), ) return _row(conn.execute("SELECT * FROM diary_logs WHERE id=?", (cur.lastrowid,)).fetchone()) def today() -> list[dict]: now = datetime.now(timezone.utc).astimezone() start = now.replace(hour=0, minute=0, second=0, microsecond=0).isoformat() with get_conn() as conn: return [dict(r) for r in conn.execute( "SELECT * FROM diary_logs WHERE ts >= ? ORDER BY ts", (start,) ).fetchall()] def recent(hours: int = 24) -> list[dict]: cutoff = (datetime.now(timezone.utc) - timedelta(hours=hours)).isoformat() with get_conn() as conn: return [dict(r) for r in conn.execute( "SELECT * FROM diary_logs WHERE ts >= ? ORDER BY ts DESC", (cutoff,) ).fetchall()] def range_query(start_ts: str, end_ts: str) -> list[dict]: with get_conn() as conn: return [dict(r) for r in conn.execute( "SELECT * FROM diary_logs WHERE ts >= ? AND ts < ? ORDER BY ts", (start_ts, end_ts), ).fetchall()]