import logging from sqlalchemy.orm import Session from app.chat.notice_inbox import post_notice_to_latest_chat from app.chat.notices import format_phase_completed_notice from app.db.models import PomodoroSession from app.pomodoro.cycle import PHASE_LONG_BREAK, PHASE_SHORT_BREAK, PHASE_WORK, CycleManager from app.pomodoro.service import PomodoroService logger = logging.getLogger(__name__) PHASE_LABELS = { PHASE_WORK: "работа", PHASE_SHORT_BREAK: "короткий перерыв", PHASE_LONG_BREAK: "длинный перерыв", } class PomodoroCompletionHandler: def __init__(self, db: Session): self.db = db self.pomodoro = PomodoroService(db) self.cycle = CycleManager(db) def _resolve_next_phase(self, session: PomodoroSession) -> str | None: phase = session.phase cycle = self.cycle.get() if phase == PHASE_WORK: if cycle.completed_work_sessions + 1 >= cycle.sessions_until_long_break: return PHASE_LONG_BREAK return PHASE_SHORT_BREAK if phase == PHASE_SHORT_BREAK: return PHASE_WORK if phase == PHASE_LONG_BREAK: return None return None async def process(self, session: PomodoroSession) -> None: if session.completion_notified: return next_phase = self._resolve_next_phase(session) notice = format_phase_completed_notice(session, next_phase) # Только notice — role=assistant ломает tool/reasoning цепочки OpenRouter. post_notice_to_latest_chat(notice) self.cycle.bump_notify_seq() self.pomodoro.mark_notified(session) self.pomodoro.advance_after_completion(session) logger.info("Pomodoro phase completed notice posted (phase=%s)", session.phase)