Skip to content

Your lightweight open-source personal agent.

Merge branch 'main' into pr-815

Re-bin44eb1bdca2bf
diff --cc nanobot/agent/loop.py
index 78552973a,c68d74dbf..4d1316241
--- a/nanobot/agent/loop.py
+++ b/nanobot/agent/loop.py
@@@ -318,10 -331,18 +331,18 @@@ class AgentLoop
return OutboundMessage(channel=msg.channel, chat_id=msg.chat_id,
content="🐈 nanobot commands:\n/new β€” Start a new conversation\n/help β€” Show available commands")
- if len(session.messages) > self.memory_window:
- asyncio.create_task(self._consolidate_memory(session))
+ if len(session.messages) > self.memory_window and session.key not in self._consolidating:
+ self._consolidating.add(session.key)
+
+ async def _consolidate_and_unlock():
+ try:
+ await self._consolidate_memory(session)
+ finally:
+ self._consolidating.discard(session.key)
+
+ asyncio.create_task(_consolidate_and_unlock())
- self._set_tool_context(msg.channel, msg.chat_id)
+ self._set_tool_context(msg.channel, msg.chat_id, msg.metadata.get("message_id"))
initial_messages = self.context.build_messages(
history=session.get_history(max_messages=self.memory_window),
current_message=msg.content,
diff --cc nanobot/channels/telegram.py
index 3a90d423a,768e565bd..d29fdfda4
--- a/nanobot/channels/telegram.py
+++ b/nanobot/channels/telegram.py
@@@ -221,18 -221,9 +221,18 @@@ class TelegramChannel(BaseChannel)
try:
chat_id = int(msg.chat_id)
except ValueError:
- logger.error(f"Invalid chat_id: {msg.chat_id}")
+ logger.error("Invalid chat_id: {}", msg.chat_id)
return
+ # Build reply parameters (Will reply to the message if it exists)
+ reply_to_message_id = msg.metadata.get("message_id")
+ reply_params = None
+ if reply_to_message_id:
+ reply_params = ReplyParameters(
+ message_id=reply_to_message_id,
+ allow_sending_without_reply=True
+ )
+
# Send media files
for media_path in (msg.media or []):
try:
@@@ -244,41 -235,24 +244,41 @@@
}.get(media_type, self._app.bot.send_document)
param = "photo" if media_type == "photo" else media_type if media_type in ("voice", "audio") else "document"
with open(media_path, 'rb') as f:
- await sender(chat_id=chat_id, **{param: f})
+ await sender(
+ chat_id=chat_id,
+ **{param: f},
+ reply_parameters=reply_params
+ )
except Exception as e:
filename = media_path.rsplit("/", 1)[-1]
- logger.error(f"Failed to send media {media_path}: {e}")
+ logger.error("Failed to send media {}: {}", media_path, e)
- await self._app.bot.send_message(chat_id=chat_id, text=f"[Failed to send: {filename}]")
+ await self._app.bot.send_message(
- chat_id=chat_id,
++ chat_id=chat_id,
+ text=f"[Failed to send: {filename}]",
+ reply_parameters=reply_params
+ )
# Send text content
if msg.content and msg.content != "[empty message]":
for chunk in _split_message(msg.content):
try:
html = _markdown_to_telegram_html(chunk)
- await self._app.bot.send_message(chat_id=chat_id, text=html, parse_mode="HTML")
+ await self._app.bot.send_message(
+ chat_id=chat_id,
+ text=html,
+ parse_mode="HTML",
+ reply_parameters=reply_params
+ )
except Exception as e:
- logger.warning(f"HTML parse failed, falling back to plain text: {e}")
+ logger.warning("HTML parse failed, falling back to plain text: {}", e)
try:
- await self._app.bot.send_message(chat_id=chat_id, text=chunk)
+ await self._app.bot.send_message(
+ chat_id=chat_id,
+ text=chunk,
+ reply_parameters=reply_params
+ )
except Exception as e2:
- logger.error(f"Error sending Telegram message: {e2}")
+ logger.error("Error sending Telegram message: {}", e2)
async def _on_start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle /start command."""