fix: split long messages for Telegram 4096 char limit

Bot crashed with "Message is too long" when sending full DP-00002 text
(8K+ chars). Now splits on paragraph boundaries. Also prevents silent
message drops from unhandled BadRequest exceptions.

Pentagon-Agent: Epimetheus <3D35839A-7722-4740-B93D-51157F7D5E70>
This commit is contained in:
m3taversal 2026-03-24 16:22:53 +00:00
parent 458cd7dfda
commit 02c86e9050

View file

@ -720,7 +720,28 @@ IMPORTANT: Two special tags you can append at the end of your response (after yo
logger.info("Auto-research triggered: %s", query[:80])
# Post response (without LEARNING lines)
await msg.reply_text(display_response)
# Telegram has a 4096 char limit — split long messages
if len(display_response) <= 4096:
await msg.reply_text(display_response)
else:
# Split on paragraph boundaries where possible
chunks = []
remaining = display_response
while remaining:
if len(remaining) <= 4096:
chunks.append(remaining)
break
# Find a good split point (paragraph break near 4000 chars)
split_at = remaining.rfind("\n\n", 0, 4000)
if split_at == -1:
split_at = remaining.rfind("\n", 0, 4096)
if split_at == -1:
split_at = 4096
chunks.append(remaining[:split_at])
remaining = remaining[split_at:].lstrip("\n")
for chunk in chunks:
if chunk.strip():
await msg.reply_text(chunk)
# Update conversation state: reset window, store history (Ganymede+Rhea)
if user: