Better logging

This commit is contained in:
2025-10-09 12:43:27 +02:00
parent e7c32cc227
commit 2642b0a221
7 changed files with 111 additions and 57 deletions

View File

@@ -2,10 +2,10 @@ import io
import os
import json
import httpx
import logging
import warnings
from enum import Enum
from typing import Any
from agno.utils.log import log_info # type: ignore
from markdown_pdf import MarkdownPdf, Section
from telegram import CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup, Message, Update, User
from telegram.constants import ChatAction
@@ -15,6 +15,7 @@ from app.agents.pipeline import Pipeline
# per per_message di ConversationHandler che rompe sempre qualunque input tu metta
warnings.filterwarnings("ignore")
logging = logging.getLogger(__name__)
# Lo stato cambia in base al valore di ritorno delle funzioni async
@@ -70,7 +71,7 @@ class BotFunctions:
if miniapp_url: BotFunctions.update_miniapp_url(miniapp_url, token)
app = Application.builder().token(token).build()
conv_handler = ConversationHandler(
app.add_handler(ConversationHandler(
per_message=False, # capire a cosa serve perchè da un warning quando parte il server
entry_points=[CommandHandler('start', BotFunctions.__start)],
states={
@@ -86,11 +87,7 @@ class BotFunctions:
]
},
fallbacks=[CommandHandler('start', BotFunctions.__start)],
)
app.add_handler(conv_handler)
log_info("Telegram bot application created successfully.")
))
return app
########################################
@@ -154,7 +151,7 @@ class BotFunctions:
})}
httpx.post(endpoint, data=payload)
except httpx.HTTPError as e:
log_info(f"Failed to update mini app URL: {e}")
logging.info(f"Failed to update mini app URL: {e}")
#########################################
# Funzioni async per i comandi e messaggi
@@ -162,7 +159,7 @@ class BotFunctions:
@staticmethod
async def __start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
message, user = await BotFunctions.handle_message(update)
log_info(f"@{user.username} started the conversation.")
logging.info(f"@{user.username} started the conversation.")
await BotFunctions.start_message(user, message)
return CONFIGS
@@ -187,7 +184,7 @@ class BotFunctions:
@staticmethod
async def __select_config(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
query, user = await BotFunctions.handle_callbackquery(update)
log_info(f"@{user.username} --> {query.data}")
logging.info(f"@{user.username} --> {query.data}")
req = BotFunctions.users_req[user]
@@ -209,16 +206,16 @@ class BotFunctions:
confs = BotFunctions.users_req[user]
confs.user_query = message.text or ""
log_info(f"@{user.username} started the team with [{confs.model_team}, {confs.model_output}, {confs.strategy}]")
logging.info(f"@{user.username} started the team with [{confs.model_team}, {confs.model_output}, {confs.strategy}]")
await BotFunctions.__run_team(update, confs)
log_info(f"@{user.username} team finished.")
logging.info(f"@{user.username} team finished.")
return ConversationHandler.END
@staticmethod
async def __cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
query, user = await BotFunctions.handle_callbackquery(update)
log_info(f"@{user.username} canceled the conversation.")
logging.info(f"@{user.username} canceled the conversation.")
if user in BotFunctions.users_req:
del BotFunctions.users_req[user]
await query.edit_message_text("Conversation canceled. Use /start to begin again.")
@@ -246,12 +243,12 @@ class BotFunctions:
# Remove user query and bot message
await bot.delete_message(chat_id=chat_id, message_id=update.message.id)
# Start TEAM
# TODO migliorare messaggi di attesa
# TODO settare correttamente i modelli
pipeline = Pipeline()
pipeline.choose_predictor(Pipeline.available_models.index(confs.model_team))
#pipeline.choose_predictor(Pipeline.available_models.index(confs.model_team))
pipeline.choose_style(Pipeline.all_styles.index(confs.strategy))
# TODO migliorare messaggi di attesa
await bot.send_chat_action(chat_id=chat_id, action=ChatAction.TYPING)
report_content = pipeline.interact(confs.user_query)
await msg.delete()

View File

@@ -1,9 +1,10 @@
import inspect
import logging
import time
import traceback
from typing import Any, Callable, Generic, TypeVar
from agno.utils.log import log_info, log_warning #type: ignore
logging = logging.getLogger(__name__)
WrapperType = TypeVar("WrapperType")
WrapperClassType = TypeVar("WrapperClassType")
OutputType = TypeVar("OutputType")
@@ -76,7 +77,7 @@ class WrapperHandler(Generic[WrapperType]):
Exception: If all wrappers fail after retries.
"""
log_info(f"{inspect.getsource(func).strip()} {inspect.getclosurevars(func).nonlocals}")
logging.info(f"{inspect.getsource(func).strip()} {inspect.getclosurevars(func).nonlocals}")
results: dict[str, OutputType] = {}
starting_index = self.index
@@ -86,18 +87,18 @@ class WrapperHandler(Generic[WrapperType]):
wrapper_name = wrapper.__class__.__name__
if not try_all:
log_info(f"try_call {wrapper_name}")
logging.info(f"try_call {wrapper_name}")
for try_count in range(1, self.retry_per_wrapper + 1):
try:
result = func(wrapper)
log_info(f"{wrapper_name} succeeded")
logging.info(f"{wrapper_name} succeeded")
results[wrapper_name] = result
break
except Exception as e:
error = WrapperHandler.__concise_error(e)
log_warning(f"{wrapper_name} failed {try_count}/{self.retry_per_wrapper}: {error}")
logging.warning(f"{wrapper_name} failed {try_count}/{self.retry_per_wrapper}: {error}")
time.sleep(self.retry_delay)
if not try_all and results:
@@ -143,6 +144,6 @@ class WrapperHandler(Generic[WrapperType]):
wrapper = wrapper_class(**(kwargs or {}))
result.append(wrapper)
except Exception as e:
log_warning(f"{wrapper_class} cannot be initialized: {e}")
logging.warning(f"'{wrapper_class.__name__}' cannot be initialized: {e}")
return WrapperHandler(result, try_per_wrapper, retry_delay)