# pylint: disable=W0613
"""Django signal receivers of SqlPlugin tool calls."""
from django.dispatch import receiver
from smarter.lib import logging
from smarter.lib.django.waffle import SmarterWaffleSwitches
from ..plugin.static import PluginBase
from ..signals.sql import (
sql_plugin_authenticated,
sql_plugin_authentication_failed,
sql_plugin_failed,
sql_plugin_request_completed,
sql_plugin_request_started,
sql_plugin_responded,
)
from .utils import response_characters
logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.RECEIVER_LOGGING])
[docs]
@receiver(sql_plugin_request_started, dispatch_uid="sql_plugin_request_started")
def handle_sql_plugin_request_started(sender, plugin: PluginBase, function_args=None, **kwargs):
"""
Handle SqlPlugin request started signal.
Logs it at INFO.
:param sender: The class that sent the signal.
:param plugin: The plugin.
:param function_args: The arguments that the LLM called the tool with.
"""
logger.info(
"%s function_args: %s",
plugin.name,
function_args,
)
[docs]
@receiver(sql_plugin_authenticated, dispatch_uid="sql_plugin_authenticated")
def handle_sql_plugin_authenticated(sender, plugin: PluginBase, **kwargs):
"""
Handle SqlPlugin authenticated signal.
Logs it at INFO.
:param sender: The class that sent the signal.
:param plugin: The plugin.
:param kwargs: The signal's other arguments, of which ``connection`` are logged.
"""
logger.info(
"%s connection: %s",
plugin.name,
kwargs.get("connection"),
)
[docs]
@receiver(sql_plugin_authentication_failed, dispatch_uid="sql_plugin_authentication_failed")
def handle_sql_plugin_authentication_failed(sender, plugin: PluginBase, error: str, **kwargs):
"""
Handle SqlPlugin authentication failed signal.
Logs it at WARNING.
:param sender: The class that sent the signal.
:param plugin: The plugin.
:param error: Why it failed.
:param kwargs: The signal's other arguments, of which ``connection`` are logged.
"""
logger.warning(
"%s connection: %s error: %s",
plugin.name,
kwargs.get("connection"),
error,
)
[docs]
@receiver(sql_plugin_responded, dispatch_uid="sql_plugin_responded")
def handle_sql_plugin_responded(sender, plugin: PluginBase, response=None, cached: bool = False, **kwargs):
"""
Handle SqlPlugin responded signal.
Logs it at INFO.
:param sender: The class that sent the signal.
:param plugin: The plugin.
:param response: The response, whose length in characters is logged.
:param cached: Whether the response was served from the cache.
:param kwargs: The signal's other arguments, of which ``sql`` are logged.
"""
logger.info(
"%s sql: %s characters: %s cached: %s",
plugin.name,
kwargs.get("sql"),
response_characters(response),
cached,
)
[docs]
@receiver(sql_plugin_failed, dispatch_uid="sql_plugin_failed")
def handle_sql_plugin_failed(sender, plugin: PluginBase, error: str, **kwargs):
"""
Handle SqlPlugin failed signal.
Logs it at WARNING.
:param sender: The class that sent the signal.
:param plugin: The plugin.
:param error: Why it failed.
"""
logger.warning("%s failed: %s", plugin.name, error)
[docs]
@receiver(sql_plugin_request_completed, dispatch_uid="sql_plugin_request_completed")
def handle_sql_plugin_request_completed(sender, plugin: PluginBase, success: bool, duration: float, **kwargs):
"""
Handle SqlPlugin request completed signal.
Logs it at INFO.
:param sender: The class that sent the signal.
:param plugin: The plugin.
:param success: Whether the request succeeded.
:param duration: Seconds that the request took.
"""
logger.info(
"%s success: %s duration: %.3fs",
plugin.name,
success,
duration,
)