Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(telegram): dispatch updates concurrently
cyh396bd9263762
diff --git a/modules/im/telegram.py b/modules/im/telegram.pyindex 0611665db..e47aa7fa1 100644--- a/modules/im/telegram.py+++ b/modules/im/telegram.py@@ -95,6 +95,7 @@ class TelegramBot(BaseIMClient):self._bot_user: Optional[dict[str, Any]] = Noneself._on_ready: Optional[Callable] = Noneself._loop: Optional[asyncio.AbstractEventLoop] = None+ self._update_tasks: set[asyncio.Task[Any]] = set()self._cwd_prompts: dict[str, _TelegramCwdPrompt] = {}self._resume_states: dict[str, _TelegramResumeSessionState] = {}self._routing_states: dict[str, _TelegramRoutingState] = {}@@ -154,20 +155,43 @@ class TelegramBot(BaseIMClient):async def _run(self) -> None:self._loop = asyncio.get_running_loop()- self._bot_user = (await telegram_api.get_me(self.config.bot_token)).get("result")- logger.info("Telegram bot connected as @%s", self._bot_user.get("username") if self._bot_user else "unknown")- if self._on_ready:- await self._on_ready()+ try:+ self._bot_user = (await telegram_api.get_me(self.config.bot_token)).get("result")+ logger.info("Telegram bot connected as @%s", self._bot_user.get("username") if self._bot_user else "unknown")+ if self._on_ready:+ await self._on_ready()++ while not self._stop_event.is_set():+ try:+ updates = await telegram_api.get_updates(self.config.bot_token, self._offset)+ for update in updates.get("result", []):+ self._offset = int(update["update_id"]) + 1+ self._spawn_update_task(update)+ except Exception as err:+ logger.warning("Telegram poll loop error: %s", err, exc_info=True)+ await asyncio.sleep(2)+ finally:+ await self._drain_update_tasks()- while not self._stop_event.is_set():- try:- updates = await telegram_api.get_updates(self.config.bot_token, self._offset)- for update in updates.get("result", []):- self._offset = int(update["update_id"]) + 1- await self._handle_update(update)- except Exception as err:- logger.warning("Telegram poll loop error: %s", err, exc_info=True)- await asyncio.sleep(2)+ def _spawn_update_task(self, update: dict[str, Any]) -> None:+ task = asyncio.create_task(self._handle_update(update))+ self._update_tasks.add(task)+ task.add_done_callback(self._handle_update_task_done)++ def _handle_update_task_done(self, task: asyncio.Task[Any]) -> None:+ self._update_tasks.discard(task)+ try:+ task.result()+ except asyncio.CancelledError:+ return+ except Exception:+ logger.exception("Telegram update task failed")++ async def _drain_update_tasks(self) -> None:+ if not self._update_tasks:+ return+ pending = tuple(self._update_tasks)+ await asyncio.gather(*pending, return_exceptions=True)async def _handle_update(self, update: dict[str, Any]) -> None:if update.get("callback_query"):diff --git a/tests/test_telegram_bot.py b/tests/test_telegram_bot.pyindex 1a12e3ea8..b338ffc03 100644--- a/tests/test_telegram_bot.py+++ b/tests/test_telegram_bot.py@@ -264,6 +264,38 @@ def test_handle_message_ignores_foreign_bot_command() -> None:bot.on_message_callback.assert_not_awaited()+def test_run_dispatches_telegram_updates_concurrently() -> None:+ bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))+ started: list[int] = []+ release_first = asyncio.Event()+ second_started = asyncio.Event()+ poll_calls = 0++ async def fake_get_updates(_token: str, _offset=None):+ nonlocal poll_calls+ poll_calls += 1+ if poll_calls == 1:+ return {"result": [{"update_id": 1}, {"update_id": 2}]}+ await asyncio.sleep(0)+ return {"result": []}++ async def fake_handle(update: dict[str, int]) -> None:+ started.append(update["update_id"])+ if update["update_id"] == 2:+ second_started.set()+ release_first.set()+ bot.stop()+ await release_first.wait()++ with patch("modules.im.telegram.telegram_api.get_me", new=AsyncMock(return_value={"result": {"username": "bot"}})):+ with patch("modules.im.telegram.telegram_api.get_updates", new=AsyncMock(side_effect=fake_get_updates)):+ with patch.object(bot, "_handle_update", new=fake_handle):+ asyncio.run(asyncio.wait_for(bot._run(), timeout=0.2))++ assert second_started.is_set()+ assert started == [1, 2]++def test_pending_cwd_prompt_consumes_next_plain_message() -> None:bot = TelegramBot(TelegramConfig(bot_token="123456:test-token"))context = MessageContext(