Skip to content

Commit 74e803f

Browse files
committed
fix(runner, telegram): enhance logging for message processing and command handling
Signed-off-by: Frost Ming <me@frostming.com>
1 parent b496cc0 commit 74e803f

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

src/bub/channels/runner.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ async def _run(self) -> None:
2525
await self._event.wait()
2626
prompt = f"channel: ${self.channel.output_channel}\n" + "\n".join(self._prompts)
2727
self._prompts.clear()
28-
self._last_received_at = None
2928
self._running_task = None
29+
if (
30+
self._last_received_at is not None
31+
and self._loop.time() - self._last_received_at > self.message_delay_seconds
32+
):
33+
self._last_received_at = None
3034
try:
3135
result = await self.channel.run_prompt(self.session_id, prompt)
3236
await self.channel.process_output(self.session_id, result)
@@ -46,15 +50,26 @@ async def process_message(self, message: Any) -> None:
4650
if not is_mentioned and self._last_received_at is None:
4751
return
4852
self._prompts.append(prompt)
49-
if is_mentioned:
53+
if prompt.startswith(","):
54+
logger.info("session.receive.command session_id={} message={}", self.session_id, prompt)
55+
try:
56+
result = await self.channel.run_prompt(self.session_id, prompt)
57+
await self.channel.process_output(self.session_id, result)
58+
except Exception:
59+
logger.exception("session.run.error session_id={}", self.session_id)
60+
elif is_mentioned:
5061
# wait at most 1 second to reply to mentioned messages.
5162
self._last_received_at = now
63+
logger.info("session.receive.mentioned session_id={} message={}", self.session_id, prompt)
5264
self.reset_timer(self.debounce_seconds)
5365
if self._running_task is None:
5466
self._running_task = asyncio.create_task(self._run())
5567
return await self._running_task
5668
elif self._last_received_at is not None and self._running_task is None:
5769
# Otherwise if bot is mentioned before, we will keep reading messages for at most 30 seconds.
70+
logger.info("session.receive followup session_id={} message={}", self.session_id, prompt)
5871
self.reset_timer(self.message_delay_seconds)
5972
self._running_task = asyncio.create_task(self._run())
6073
return await self._running_task
74+
else:
75+
logger.info("session.receive ignored session_id={} message={}", self.session_id, prompt)

src/bub/channels/telegram.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ async def get_session_prompt(self, message: Message) -> tuple[str, str]:
173173

174174
# Pass comma commands directly to the input handler
175175
if content.strip().startswith(","):
176-
logger.info("telegram.inbound.command chat_id={} content={}", chat_id, content)
177176
return session_id, content
178177

179178
metadata: dict[str, Any] = {
@@ -185,13 +184,6 @@ async def get_session_prompt(self, message: Message) -> tuple[str, str]:
185184
"sender_is_bot": message.from_user.is_bot if message.from_user else None,
186185
"date": message.date.timestamp() if message.date else None,
187186
}
188-
logger.info(
189-
"telegram.inbound.message chat_id={} user_id={} username={} content={}",
190-
chat_id,
191-
metadata["sender_id"],
192-
metadata["username"],
193-
content,
194-
)
195187

196188
if media:
197189
metadata["media"] = media

0 commit comments

Comments
 (0)