feat: prepend time-gap notice to user message when >5 min elapsed #10

Merged
wylab merged 4 commits from feature/time-gap-injection into main 2026-02-19 01:13:11 +01:00
+25 -1
View File
@@ -3,6 +3,7 @@
import asyncio import asyncio
import json import json
import time import time
from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
@@ -290,10 +291,33 @@ class AgentLoop:
if isinstance(cron_tool, CronTool): if isinstance(cron_tool, CronTool):
cron_tool.set_context(msg.channel, msg.chat_id) 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) # Build initial messages (use get_history for LLM-formatted messages)
messages = self.context.build_messages( messages = self.context.build_messages(
history=session.get_history(), history=session.get_history(),
current_message=msg.content, current_message=current_message,
media=msg.media if msg.media else None, media=msg.media if msg.media else None,
channel=msg.channel, channel=msg.channel,
chat_id=msg.chat_id, chat_id=msg.chat_id,