Source code for smarter.apps.llmtool.views.listview.api

# pylint: disable=W0613
"""
This module contains views to implement the React.

Plugin list view in the Smarter Dashboard.
"""

from http import HTTPStatus

from django.core.handlers.asgi import ASGIRequest
from django.db import models
from django.http import HttpRequest, JsonResponse

from smarter.apps.llmtool.caching import (
    invalidate_all_cached_plugins_for_user_profile,
)
from smarter.apps.llmtool.manifest.controller import PluginController
from smarter.apps.llmtool.models import Extension, PluginMeta
from smarter.apps.llmtool.plugin.base import SmarterPluginError
from smarter.apps.llmtool.serializers import PluginSerializer
from smarter.lib import logging
from smarter.lib.django.http.client_errors import (
    CLIENT_ERROR_TYPES,
    client_error_message,
)
from smarter.lib.django.listview import SmarterListApiView
from smarter.lib.django.pagination import (
    DEFAULT_SORT_FIELDS,
    ListViewPagination,
    paginate_listview,
)
from smarter.lib.django.views import SmarterAuthenticatedNeverCachedWebView
from smarter.lib.django.waffle import SmarterWaffleSwitches

from .contract import ExtensionListItem, ExtensionListPage

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

SORT_FIELDS = {
    **DEFAULT_SORT_FIELDS,
    "kind": "plugin_class",
    "selector": "plugin_selector_plugin__directive",
}
"""The columns of the Plugin list that it may be sorted by, and the fields that sort them."""

EXTENSION_SORT_FIELDS = {
    "displayName": "display_name",
    "description": "description",
    "publisher": "publisher",
    "version": "installed_version__version",
    "trustBadge": "trust_badge",
    "enabled": "enabled",
    "createdAt": "created_at",
    "updatedAt": "updated_at",
}
"""The columns of the Extension list that it may be sorted by, and the fields that sort them."""

EXTENSION_SEARCH_FIELDS = ("slug", "display_name", "description", "publisher")
"""The fields of an Extension that a search of the Extension list matches."""


[docs] class PluginListApiView(SmarterListApiView): """ Render the plugin list view for the Smarter Workbench web console. This view displays all plugins available to the authenticated user as cards, providing a quick overview and access to plugin details. :param request: Django HTTP request object. :type request: ASGIRequest :param args: Additional positional arguments. :type args: tuple :param kwargs: Additional keyword arguments. :type kwargs: dict :returns: Rendered HTML page with a card for each plugin, or a 404 error page if the user is not authenticated. :rtype: HttpResponse """ model = PluginMeta serializer_class = PluginSerializer listview_cache_kind = "Plugin" sort_fields = SORT_FIELDS select_related = ( "plugin_selector_plugin", "plugin_prompt_plugin", "plugin_data_base_plugin__plugindatastatic", "plugin_data_base_plugin__plugindatasql", "plugin_data_base_plugin__plugindataapi", )
[docs] class PluginListApiExtensionsView(SmarterAuthenticatedNeverCachedWebView): """ List the installed Extensions that the authenticated user's account may use, read-only. This is the api of the LLMTool list's Extensions tab. An extension is listed if it has an installed version and is available to the user's account, whether or not it is enabled: an LLMClient may list a disabled extension, whose tools are offered once an administrator enables it (see the LLMClient broker's ``resolve_extension()``). A superuser sees every installed extension. It has no clone, rename or delete: superusers manage extensions in the Extensions console. The response is paginated, searched and sorted like the plugin list's, with :func:`~smarter.lib.django.pagination.paginate_listview`, and is an :class:`~smarter.apps.llmtool.views.listview.contract.ExtensionListPage`, whose JSON Schema ``@smarter/llmtool-list`` generates its types from. """ @property def formatted_class_name(self) -> str: """Returns a formatted string of the class name for logging purposes.""" class_name = f"{__name__}.{PluginListApiExtensionsView.__name__}[{id(self)}]" return self.formatted_text(class_name)
[docs] def available_extensions(self, request: ASGIRequest) -> models.QuerySet[Extension]: """ Return the installed extensions that the user's account may use. This is the queryset form of :meth:`~smarter.apps.llmtool.models.Extension.is_available_to`. :param request: The HTTP request, of an authenticated user. :returns: The extensions, every installed one for a superuser. """ qs = Extension.objects.filter(installed_version__isnull=False).select_related("installed_version") qs = qs.defer("installed_version__archive") if request.user.is_superuser: # type: ignore[union-attr] return qs account = self.user_profile.account # type: ignore[union-attr] return qs.filter(models.Q(all_accounts=True) | models.Q(accounts=account)).distinct()
[docs] def post(self, request: ASGIRequest, *args, **kwargs) -> JsonResponse: """ Handle POST requests for a page of the extensions that the user may use. :param request: The HTTP request, whose query parameters are ``page``, ``page_size``, ``search`` and ``ordering``. See :func:`~smarter.lib.django.pagination.paginate_listview`. :returns: An :class:`~smarter.apps.llmtool.views.listview.contract.ExtensionListPage`: ``{"objects": [...], "pagination": {...}}``. :rtype: JsonResponse """ logger.debug( "Listing the extensions available to user %s.", request.user.username if request.user else "None", # type: ignore[union-attr] ) extensions, pagination = paginate_listview( request, self.available_extensions(request).order_by("display_name"), search_fields=EXTENSION_SEARCH_FIELDS, sort_fields=EXTENSION_SORT_FIELDS, ) page = ExtensionListPage( objects=[ExtensionListItem.from_model(extension) for extension in extensions], pagination=ListViewPagination.model_validate(pagination), ) return JsonResponse(page.to_json())
[docs] class PluginListApiCloneView(SmarterAuthenticatedNeverCachedWebView): """Clone a plugin for the authenticated user.""" @property def formatted_class_name(self) -> str: """Returns a formatted string of the class name for logging purposes.""" class_name = f"{__name__}.{PluginListApiCloneView.__name__}[{id(self)}]" return self.formatted_text(class_name)
[docs] def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse: """ Handle POST requests to clone an existing PluginMeta. Validates input parameters, checks for the existence of the PluginMeta to be cloned, and creates a new PluginMeta with the specified name. Invalidates the cache for the user's LLMClients after cloning. :param request: The HTTP request object containing the parameters for cloning. :type request: HttpRequest :param args: Additional positional arguments (not used). :param kwargs: Additional keyword arguments, including: - llmclient_id (str): The ID of the PluginMeta to be cloned. - new_name (str): The new name for the cloned PluginMeta. :returns: A JsonResponse containing the serialized data of the newly cloned PluginMeta if successful, or an error message if the cloning fails. :rtype: JsonResponse """ llmclient_id = kwargs.get("llmclient_id") new_name = kwargs.get("new_name") llmclient: PluginMeta if not llmclient_id or not new_name: logger.warning( "Missing required parameters. llmclient_id: %s, new_name: %s", llmclient_id, new_name, ) return JsonResponse({"error": "llmclient_id and new_name are required."}, status=HTTPStatus.BAD_REQUEST) try: llmclient = PluginMeta.objects.with_read_permission_for(self.user_profile.user).get(id=llmclient_id) # type: ignore except PluginMeta.DoesNotExist: logger.warning("PluginMeta with id %s not found for cloning.", llmclient_id) return JsonResponse({"error": f"PluginMeta with id {llmclient_id} not found."}, status=HTTPStatus.NOT_FOUND) try: new_name = self.to_snake_case(new_name.strip()) # PluginMeta.clone() copies only the PluginMeta, whereas the plugin's # clone() also copies its selector, prompt and data. plugin = PluginController(user_profile=self.user_profile, plugin_meta=llmclient).plugin # type: ignore[arg-type] if not plugin or not plugin.ready: # a refusal for the user, so it is returned here, not raised into the handler below. logger.warning("PluginMeta %s is not ready to clone.", llmclient_id) return JsonResponse( {"error": f"PluginMeta with id {llmclient_id} is not ready, and cannot be cloned."}, status=HTTPStatus.BAD_REQUEST, ) cloned_id = plugin.clone(new_name=new_name, user_profile=self.user_profile) if not cloned_id: raise SmarterPluginError(f"PluginMeta with id {llmclient_id} could not be cloned.") cloned_llmclient = PluginMeta.objects.get(id=cloned_id) invalidate_all_cached_plugins_for_user_profile(user_profile=self.user_profile) # type: ignore data = PluginSerializer(cloned_llmclient, context={"request": request}).data return JsonResponse(data, status=HTTPStatus.OK) # type: ignore except CLIENT_ERROR_TYPES as e: logger.warning( "Error cloning PluginMeta with id %s: %s", llmclient_id, e, ) message = client_error_message(e, "An error occurred while cloning the PluginMeta.") return JsonResponse({"error": message}, status=HTTPStatus.BAD_REQUEST) # pylint: disable=broad-except except Exception: logger.error( "Error cloning PluginMeta with id %s", llmclient_id, exc_info=True, ) return JsonResponse( {"error": "An unknown error occurred while cloning the PluginMeta."}, status=HTTPStatus.INTERNAL_SERVER_ERROR, )
[docs] class PluginListApiDeleteView(SmarterAuthenticatedNeverCachedWebView): """Delete a plugin for the authenticated user.""" @property def formatted_class_name(self) -> str: """Returns a formatted string of the class name for logging purposes.""" class_name = f"{__name__}.{PluginListApiDeleteView.__name__}[{id(self)}]" return self.formatted_text(class_name)
[docs] def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse: """ Handle POST requests to delete an existing PluginMeta. Validates input parameters, checks for the existence of the PluginMeta to be deleted, and deletes the PluginMeta if it exists. Invalidates the cache for the user's LLMClients after deletion. :param request: The HTTP request object containing the parameters for deletion. :type request: HttpRequest :param args: Additional positional arguments (not used). :param kwargs: Additional keyword arguments, including: - llmclient_id (str): The ID of the PluginMeta to be deleted. :returns: A JsonResponse indicating the success or failure of the deletion. :rtype: JsonResponse """ llmclient_id = kwargs.get("llmclient_id") if not llmclient_id: logger.warning("Missing required parameter llmclient_id for deletion.") return JsonResponse({"error": "llmclient_id is required."}, status=HTTPStatus.BAD_REQUEST) try: llmclient = PluginMeta.objects.with_ownership_permission_for(self.user_profile.user).get(id=llmclient_id) # type: ignore except PluginMeta.DoesNotExist: logger.warning( "PluginMeta with id %s not found for deletion.", llmclient_id, ) return JsonResponse({"error": f"PluginMeta with id {llmclient_id} not found."}, status=HTTPStatus.NOT_FOUND) try: llmclient.delete() invalidate_all_cached_plugins_for_user_profile(user_profile=self.user_profile) # type: ignore return JsonResponse( {"message": f"PluginMeta with id {llmclient_id} deleted successfully."}, status=HTTPStatus.OK ) except CLIENT_ERROR_TYPES as e: logger.warning( "Error deleting PluginMeta with id %s: %s", llmclient_id, e, ) message = client_error_message(e, "An error occurred while deleting the PluginMeta.") return JsonResponse({"error": message}, status=HTTPStatus.BAD_REQUEST) # pylint: disable=broad-except except Exception: logger.error( "Error deleting PluginMeta with id %s", llmclient_id, exc_info=True, ) return JsonResponse( {"error": "An unknown error occurred while deleting the PluginMeta."}, status=HTTPStatus.INTERNAL_SERVER_ERROR, )
[docs] class PluginListApiRenameView(SmarterAuthenticatedNeverCachedWebView): """Rename a plugin for the authenticated user.""" @property def formatted_class_name(self) -> str: """Returns a formatted string of the class name for logging purposes.""" class_name = f"{__name__}.{PluginListApiRenameView.__name__}[{id(self)}]" return self.formatted_text(class_name)
[docs] def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse: """ Handle POST requests to rename an existing PluginMeta. Validates input parameters, checks for the existence of the PluginMeta to be renamed, and renames the PluginMeta if it exists. Invalidates the cache for the user's LLMClients after renaming. :param request: The HTTP request object containing the parameters for renaming. :type request: HttpRequest :param args: Additional positional arguments (not used). :param kwargs: Additional keyword arguments, including: - llmclient_id (str): The ID of the PluginMeta to be renamed. - new_name (str): The new name for the PluginMeta. :returns: A JsonResponse indicating the success or failure of the renaming. :rtype: JsonResponse """ llmclient_id = kwargs.get("llmclient_id") new_name = kwargs.get("new_name") if not llmclient_id or not new_name: logger.warning( "Missing required parameters for renaming. llmclient_id: %s, new_name: %s", llmclient_id, new_name, ) return JsonResponse({"error": "llmclient_id and new_name are required."}, status=HTTPStatus.BAD_REQUEST) try: llmclient = PluginMeta.objects.with_ownership_permission_for(self.user_profile.user).get(id=llmclient_id) # type: ignore except PluginMeta.DoesNotExist: logger.warning( "PluginMeta with id %s not found for renaming.", llmclient_id, ) return JsonResponse({"error": f"PluginMeta with id {llmclient_id} not found."}, status=HTTPStatus.NOT_FOUND) try: new_name = self.to_snake_case(new_name.strip()) llmclient.rename(new_name=new_name) invalidate_all_cached_plugins_for_user_profile(user_profile=self.user_profile) # type: ignore data = PluginSerializer(llmclient, context={"request": request}).data return JsonResponse(data, status=HTTPStatus.OK) # type: ignore except CLIENT_ERROR_TYPES as e: logger.warning( "Error renaming PluginMeta with id %s: %s", llmclient_id, e, ) message = client_error_message(e, "An error occurred while renaming the PluginMeta.") return JsonResponse({"error": message}, status=HTTPStatus.BAD_REQUEST) # pylint: disable=broad-except except Exception: logger.error( "Error renaming PluginMeta with id %s", llmclient_id, exc_info=True, ) return JsonResponse( {"error": "An unknown error occurred while renaming the PluginMeta."}, status=HTTPStatus.INTERNAL_SERVER_ERROR, )