Files
prime-core/prime_core/checkin_loop.py
T

78 lines
1.9 KiB
Python

from __future__ import annotations
import json
import time
from pathlib import Path
from datetime import datetime
from zoneinfo import ZoneInfo
from .notify import send_telegram
from .queue import list_items
from .personality import format_checkin
STATE = Path('/srv/prime-core/state/checkin-state.json')
GOALS = Path('/srv/prime-core/state/goals.json')
TZ = ZoneInfo('America/New_York')
QUIET_START = 23
QUIET_END = 6
def quiet_hours_now() -> bool:
hr = datetime.now(TZ).hour
return hr >= QUIET_START or hr < QUIET_END
def load_json(path: Path, default):
if not path.exists():
return default
try:
return json.loads(path.read_text())
except Exception:
return default
def save_state(s: dict):
STATE.parent.mkdir(parents=True, exist_ok=True)
STATE.write_text(json.dumps(s, indent=2))
def summarize() -> str:
items = list_items()
goals = load_json(GOALS, [])
queued = sum(1 for i in items if i.get('status') == 'queued')
blocked = sum(1 for i in items if i.get('status') == 'blocked_approval')
done = sum(1 for i in items if i.get('status') == 'done')
if not goals:
return "I don't have active goals yet. Want to set one? (goals add <text>)"
if queued == 0 and blocked == 0:
return f"Current goals: {len(goals)}. Queue is clear. Want me to advance a specific goal next?"
return f"Goals: {len(goals)} | queued: {queued} | blocked: {blocked} | done: {done}."
def maybe_checkin():
if quiet_hours_now():
return
st = load_json(STATE, {'last_checkin_ts': 0})
now = int(time.time())
# periodic check-in every 3 hours outside quiet window
if now - int(st.get('last_checkin_ts', 0)) < 10800:
return
msg = format_checkin(summarize())
if send_telegram(msg):
st['last_checkin_ts'] = now
save_state(st)
def loop():
while True:
maybe_checkin()
time.sleep(300)
if __name__ == '__main__':
loop()