From 01fbd5607cc24d916c817a48873a5aaeeafac6d8 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Mon, 13 Oct 2025 00:31:56 +0200 Subject: [PATCH] Rinomina i logger per una migliore identificazione e gestisce le eccezioni nel bot di Telegram --- src/app/__main__.py | 11 ++++++----- src/app/agents/pipeline.py | 8 +++++++- src/app/api/wrapper_handler.py | 2 +- src/app/interface/telegram_app.py | 4 ++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/app/__main__.py b/src/app/__main__.py index 7c6557a..dca46fb 100644 --- a/src/app/__main__.py +++ b/src/app/__main__.py @@ -1,4 +1,3 @@ -# IMPORTANTE: Carichiamo le variabili d'ambiente PRIMA di qualsiasi altra cosa import asyncio import logging from dotenv import load_dotenv @@ -8,7 +7,6 @@ from app.agents import Pipeline if __name__ == "__main__": - # Inizializzazioni load_dotenv() configs = AppConfig.load() @@ -23,8 +21,11 @@ if __name__ == "__main__": telegram = TelegramApp(pipeline) telegram.add_miniapp_url(share_url) telegram.run() - except Exception as _: - logging.warning("Telegram bot could not be started. Continuing without it.") - asyncio.get_event_loop().run_forever() + except AssertionError as e: + try: + logging.warning(f"Telegram bot could not be started: {e}") + asyncio.get_event_loop().run_forever() + except KeyboardInterrupt: + logging.info("Shutting down due to KeyboardInterrupt") finally: gradio.close() diff --git a/src/app/agents/pipeline.py b/src/app/agents/pipeline.py index f52cf28..3338cb8 100644 --- a/src/app/agents/pipeline.py +++ b/src/app/agents/pipeline.py @@ -3,7 +3,7 @@ from app.agents.team import create_team_with from app.agents.prompts import * from app.configs import AppConfig -logging = logging.getLogger(__name__) +logging = logging.getLogger("pipeline") class Pipeline: @@ -30,6 +30,12 @@ class Pipeline: """ self.leader_model = self.configs.models.all_models[index] + def choose_team(self, index: int): + """ + Sceglie il modello LLM da usare per il Team. + """ + self.team_model = self.configs.models.all_models[index] + def choose_strategy(self, index: int): """ Sceglie la strategia da usare per il Predictor. diff --git a/src/app/api/wrapper_handler.py b/src/app/api/wrapper_handler.py index 4760eaf..cf6ce74 100644 --- a/src/app/api/wrapper_handler.py +++ b/src/app/api/wrapper_handler.py @@ -4,7 +4,7 @@ import time import traceback from typing import Any, Callable, Generic, TypeVar -logging = logging.getLogger(__name__) +logging = logging.getLogger("wrapper_handler") WrapperType = TypeVar("WrapperType") WrapperClassType = TypeVar("WrapperClassType") OutputType = TypeVar("OutputType") diff --git a/src/app/interface/telegram_app.py b/src/app/interface/telegram_app.py index 039022c..a9fbfbe 100644 --- a/src/app/interface/telegram_app.py +++ b/src/app/interface/telegram_app.py @@ -14,7 +14,7 @@ from app.configs import AppConfig # per per_message di ConversationHandler che rompe sempre qualunque input tu metta warnings.filterwarnings("ignore") -logging = logging.getLogger(__name__) +logging = logging.getLogger("telegram") # Lo stato cambia in base al valore di ritorno delle funzioni async @@ -69,7 +69,7 @@ class TelegramApp: })} httpx.post(endpoint, data=payload) except httpx.HTTPError as e: - logging.info(f"Failed to update mini app URL: {e}") + logging.warning(f"Failed to update mini app URL: {e}") def create_bot(self) -> None: """