diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 6cf65bb..ed92421 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -3,6 +3,7 @@ import asyncio import json import time +from datetime import datetime from pathlib import Path from typing import Any @@ -290,10 +291,33 @@ class AgentLoop: if isinstance(cron_tool, CronTool): cron_tool.set_context(msg.channel, msg.chat_id) + # Prepend time-gap notice if >5 minutes since last user message + current_message = msg.content + last_user_ts = None + for m in reversed(session.messages): + if m.get("role") == "user": + last_user_ts = m.get("timestamp") + break + if last_user_ts: + try: + last_dt = datetime.fromisoformat(last_user_ts) + now_dt = datetime.now() + elapsed_seconds = (now_dt - last_dt).total_seconds() + if elapsed_seconds > 300: # 5 minutes + if elapsed_seconds < 3600: + gap_str = f"{int(elapsed_seconds // 60)} minutes" + elif elapsed_seconds < 86400: + gap_str = f"{int(elapsed_seconds // 3600)} hours" + else: + gap_str = f"{int(elapsed_seconds // 86400)} days" + current_message = f"[SYSTEM ANNOUNCEMENT: {gap_str} have elapsed since last user message; take this into account when replying to user]\n\n{msg.content}" + except (ValueError, TypeError): + pass # Malformed timestamp — skip silently + # Build initial messages (use get_history for LLM-formatted messages) messages = self.context.build_messages( history=session.get_history(), - current_message=msg.content, + current_message=current_message, media=msg.media if msg.media else None, channel=msg.channel, chat_id=msg.chat_id,