Source code for smarter.apps.llmtool.receivers.plugin

# pylint: disable=W0613
"""Django signal receivers of the lifecycle of every plugin: create, clone, update, delete, select and call."""

from typing import Optional, Union

from django.dispatch import receiver

from smarter.common.helpers.console_helpers import SmarterFormattedText
from smarter.lib import json, logging
from smarter.lib.django.waffle import SmarterWaffleSwitches
from smarter.lib.manifest.broker import AbstractBroker
from smarter.lib.typing import SmarterDictType

from ..models import (
    PluginMeta,
)
from ..plugin.static import PluginBase
from ..signals.plugin import (
    broker_ready,
    plugin_called,
    plugin_cloned,
    plugin_created,
    plugin_deleted,
    plugin_deleting,
    plugin_ready,
    plugin_responded,
    plugin_selected,
    plugin_updated,
    search_term_matched,
)
from ..tasks import create_plugin_selector_history
from .utils import response_characters

logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.RECEIVER_LOGGING])


[docs] @receiver(plugin_created, dispatch_uid="plugin_created") def handle_plugin_created(sender, plugin: PluginBase, **kwargs): """ Handle plugin created signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. """ logger.info("%s for %s", plugin.name, plugin.user_profile) logger.debug( "%s data: %s", plugin.name, SmarterFormattedText.json(plugin.data) if plugin.data else None, )
[docs] @receiver(plugin_cloned, dispatch_uid="plugin_cloned") def handle_plugin_cloned(sender, plugin: PluginBase, **kwargs): """ Handle plugin cloned signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. """ logger.info("%s data: %s", plugin.name, plugin.data)
[docs] @receiver(plugin_updated, dispatch_uid="plugin_updated") def handle_plugin_updated(sender, plugin: PluginBase, **kwargs): """ Handle plugin updated signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. """ logger.info("%s for %s", plugin.name, plugin.user_profile) logger.debug( "%s data: %s", plugin.name, SmarterFormattedText.json(plugin.data) if plugin.data else None, )
[docs] @receiver(plugin_deleting, dispatch_uid="plugin_deleting") def handle_plugin_deleting(sender, plugin, plugin_meta: PluginMeta, **kwargs): """ Handle plugin deleting signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. :param plugin_meta: The plugin's PluginMeta, whose name is logged. """ logger.info( "%s is being deleted.", plugin_meta.name, )
[docs] @receiver(plugin_deleted, dispatch_uid="plugin_deleted") def handle_plugin_deleted(sender, plugin: PluginBase, plugin_name: str, **kwargs): """ Handle plugin deleted signal. Logs it at INFO. :param sender: The class that sent the signal. :param plugin: The plugin. :param plugin_name: The deleted plugin's name. """ logger.info("%s", plugin_name)
[docs] @receiver(plugin_called, dispatch_uid="plugin_called") def handle_plugin_called(sender, plugin: PluginBase, **kwargs): """ Handle plugin called 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 ``inquiry_type`` are logged. """ inquiry_type: Optional[str] = kwargs.get("inquiry_type") logger.info("%s inquiry_type: %s", plugin.name, inquiry_type)
[docs] @receiver(plugin_responded, dispatch_uid="plugin_responded") def handle_plugin_responded(sender, plugin: PluginBase, **kwargs): """ Handle plugin responded signal. Logs the plugin's name, the inquiry type and the result's length at INFO, and the whole result at DEBUG only: a tool's result can hold customer data, or a credential in a url. :param sender: The class that sent the signal. :param plugin: The plugin. :param kwargs: The signal's other arguments, of which ``inquiry_type``, ``response``, ``inquiry_return`` are logged. """ inquiry_type: Optional[str] = kwargs.get("inquiry_type") # plugins send their response as ``response``. inquiry_return: Optional[Union[SmarterDictType, list, str]] = kwargs.get("response", kwargs.get("inquiry_return")) characters = response_characters(inquiry_return) logger.info( "%s inquiry_type: %s characters: %s", plugin.name, inquiry_type, characters, ) try: inquiry_return = json.loads(inquiry_return) if isinstance(inquiry_return, str) else inquiry_return except (TypeError, json.JSONDecodeError): pass # DEBUG: the result can hold customer data, or a credential in a url. logger.debug( "%s inquiry_return: %s", plugin.name, SmarterFormattedText.json(inquiry_return) if isinstance(inquiry_return, (dict, list)) else inquiry_return, )
[docs] @receiver(plugin_ready, dispatch_uid="plugin_ready") def handle_plugin_ready(sender, plugin: PluginBase, **kwargs): """ Handle plugin ready signal. DEBUG, not INFO: the signal is sent each time :meth:`PluginBase.ready` passes, which happens several times for every prompt that uses the plugin. """ logger.debug("%s", plugin.name)
[docs] @receiver(plugin_selected, dispatch_uid="plugin_selected") def handle_plugin_selected(sender, *args, **kwargs): """Handle plugin selected signal.""" # plugin: PluginBase, user, messages: list[dict], search_term: str, messages: list[dict] = None input_text: Optional[str] = kwargs.get("input_text") plugin: Optional[PluginBase] = kwargs.get("plugin") user = kwargs.get("user") messages: list[SmarterDictType] = kwargs.get("messages", []) search_term: str = kwargs.get("search_term", "") user_id: int = user.id if user else None # type: ignore prompt = input_text if input_text else SmarterFormattedText.json(messages) logger.info( "%s search_term: %s", plugin.name if plugin else "Unknown Plugin", search_term, ) # DEBUG: the prompt is the user's conversation. logger.debug("prompt(s): %s", prompt) if plugin is None or plugin.id is None: logger.warning( "received without a plugin id. Plugin selector history is not recorded.", ) return create_plugin_selector_history.delay( plugin_id=plugin.id, user_id=user_id, input_text=input_text, messages=messages, search_term=search_term, )
[docs] @receiver(search_term_matched, dispatch_uid="search_term_matched") def handle_search_term_matched(sender, prompt: str, search_term: str, method: str, matched_text=None, **kwargs): """ Handle search term matched signal. Logs it at INFO. :param sender: The class that sent the signal. :param prompt: The user's prompt, which is logged at DEBUG only, because it is the user's conversation. :param search_term: The plugin's search term that matched. :param method: How it matched, e.g. ``thesaurus``. :param matched_text: The text of the prompt that matched, if any. """ logger.info( "search_term: %s method: %s matched_text: %s", search_term, method, matched_text, ) # DEBUG: the prompt is the user's conversation. logger.debug("prompt: %s", prompt)
[docs] @receiver(broker_ready, dispatch_uid="broker_ready") def handle_broker_ready(sender, broker: AbstractBroker, **kwargs): """ Log that a broker is ready. Brokers of every kind send this signal, so the message names the broker's class, e.g. ``SAMGuardrailBroker``. """ logger.debug("%s %s for %s is ready.", sender.__name__, broker.kind, broker.name)