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:
parent
458cd7dfda
commit
02c86e9050
1 changed files with 22 additions and 1 deletions
|
|
@ -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])
|
logger.info("Auto-research triggered: %s", query[:80])
|
||||||
|
|
||||||
# Post response (without LEARNING lines)
|
# 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)
|
# Update conversation state: reset window, store history (Ganymede+Rhea)
|
||||||
if user:
|
if user:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue