Api

The Extensions api’s handlers, which the web console’s Extensions pages and the REST api share, and the REST api under /api/v1/extensions/.

The web console’s Extensions pages, and their api.

Superusers manage extensions on two pages of the single-page console (ADR-035), which renders @smarter/extension-console’s list or an extension’s page at their paths. Every other user sees the same pages read-only: the extensions that their account may use, without their settings, their availability or the platform admin’s Secrets, and without any action.

  • Extensions (/extensions/): the installed extensions, and the registry’s, which can be installed, and an archive’s upload.

  • An extension (/extensions/<slug>/): its documentation, tools, settings, availability, versions and resources, and every action on it.

The api is a set of POST endpoints in the web console’s api, under /api/web/llmtool/extensions/ (see smarter.apps.llmtool.extension.api.web.urls). The actions are for superusers only; the list, the registry and an extension’s detail are read-only for everyone else. Each validates its request and builds its response with the models of contract, and carries out its action with the ExtensionInstaller or the registry client, as the admin console and the management commands do. An action’s failure is a 400 with {"error": "..."}, whose message is meant for the person who tried it.

class smarter.apps.llmtool.extension.views.ExtensionAccountsApiView(**kwargs)[source]

Bases: ExtensionAccountsHandler, ExtensionApiView

Every account, for the availability picker.

class smarter.apps.llmtool.extension.views.ExtensionAccountsHandler[source]

Bases: ExtensionApiHandler

Every account, for the availability picker.

handle(request, body, **kwargs)[source]

List every account, by account number.

Parameters:
  • request – The request.

  • body – None: the request has no body.

Return type:

AccountsResponse

Returns:

The accounts.

class smarter.apps.llmtool.extension.views.ExtensionActionHandler[source]

Bases: ExtensionApiHandler

The base class of an action on one extension: act() returns the message of its success.

act(request, extension, body)[source]

Carry out the action, and return what happened.

Parameters:
  • request – The request.

  • extension (Extension) – The extension to act on.

  • body – The request’s body, validated by request_model, or None.

Return type:

str

Returns:

The message of the action’s success.

handle(request, body, slug='', **kwargs)[source]

Act on an extension, then return the message and the extension as it now is.

Parameters:
  • request – The request.

  • body – The request’s body, validated by request_model, or None.

  • slug (str) – The extension’s slug, from the url.

Return type:

ActionResponse

Returns:

The message, and the extension’s detail, or None if the action uninstalled it.

class smarter.apps.llmtool.extension.views.ExtensionApiHandler[source]

Bases: object

The logic of one endpoint of the Extensions api, apart from how its request arrives and its response leaves.

Two transports mix it in: ExtensionApiView, the web console’s api (POST only, a Django session, errors as {"error": ...}), and smarter.apps.llmtool.extension.api.v1.views.ExtensionApiV1View, the REST api under /api/v1/extensions/ (an api key, errors as {"detail": ...}). A transport implements request_data(), calls respond(), and turns its result into a response.

Subclasses implement handle(), which returns a contract model, and set request_model if the request has a body.

extension(slug)[source]

Return an installed extension, or raise Extension.DoesNotExist.

Return type:

Extension

handle(request, body, **kwargs)[source]

Carry out the request, and return its response’s model.

Parameters:
  • request – The request.

  • body (BaseModel | None) – The request’s body, validated by request_model, or None if it has none.

  • kwargs – Keyword arguments from the url.

Return type:

ExtensionConsoleModel

Returns:

The response’s contract model.

Raises:
installer(request)[source]

Return an installer that acts as the requesting superuser.

Return type:

ExtensionInstaller

parse(request)[source]

Validate the request’s data with request_model.

A request without a model is not read at all: a multipart upload’s body can’t be read again.

Parameters:

request – The request.

Return type:

BaseModel | None

Returns:

The validated request, or None if the endpoint takes none.

Raises:

ValueError – If the request is not valid.

read_only: bool = False

Whether every user may call it.

It then shows a user who is not a superuser only what they may see.

request_data(request)[source]

Return the request’s data, as a dict, for request_model to validate: its JSON body.

The web console’s api sends JSON. The REST api’s transport overrides it with DRF’s parsed data.

Parameters:

request – The request.

Return type:

dict[str, Any]

Returns:

The body, or an empty dict if it has none, or it is not an object.

Raises:

ValueError – If the body is not JSON.

respond(request, **kwargs)[source]

Check permission, validate the request, and call handle(), turning its exceptions into errors.

Parameters:
  • request – The request.

  • kwargs – Keyword arguments from the url, e.g. slug, passed to handle().

Return type:

tuple[HTTPStatus, ExtensionConsoleModel | str]

Returns:

The status and the model that handle() returns, or an error’s status and message: 403 for a user who is not a superuser, unless read_only is set, 400 for a request that is not valid or an extension error, 404 for an extension that is not installed, and 500 for anything else.

visible(request, extension)[source]

Return whether the requesting user may see an extension: a superuser sees every one.

Parameters:
  • request – The request.

  • extension (Extension) – The extension.

Return type:

bool

Returns:

True for a superuser, or if the extension is available to the user’s account.

class smarter.apps.llmtool.extension.views.ExtensionApiView(**kwargs)[source]

Bases: ExtensionApiHandler, SmarterAuthenticatedNeverCachedWebView

The base class of the web console’s Extensions api: POST only, JSON in and out, and a Django session.

Its concrete views mix a handler in before it, e.g. ExtensionEnableApiView(ExtensionEnableHandler, ExtensionApiView). Errors are {"error": ...}.

error(message, status=HTTPStatus.BAD_REQUEST)[source]

Return an error response.

Parameters:
  • message (str) – What went wrong.

  • status (HTTPStatus) – The HTTP status.

Return type:

JsonResponse

Returns:

{"error": message}, with the status.

property formatted_class_name: str

The class name, for logging.

post(request, *args, **kwargs)[source]

Carry out the request with respond().

Parameters:
  • request (ASGIRequest) – The request.

  • kwargs – Keyword arguments from the url, e.g. slug.

Return type:

JsonResponse

Returns:

The JSON of the model that handle() returns, or an error.

class smarter.apps.llmtool.extension.views.ExtensionAvailabilityApiView(**kwargs)[source]

Bases: ExtensionAvailabilityHandler, ExtensionApiView

Change which accounts may use an extension.

class smarter.apps.llmtool.extension.views.ExtensionAvailabilityHandler[source]

Bases: ExtensionActionHandler

Change which accounts may use an extension.

act(request, extension, body)[source]

Make the extension available to every account, or to the given accounts.

Raises:

ValueError – If some of the accounts do not exist.

Return type:

str

request_model

alias of AvailabilityRequest

class smarter.apps.llmtool.extension.views.ExtensionCheckUpdatesApiView(**kwargs)[source]

Bases: ExtensionCheckUpdatesHandler, ExtensionApiView

Check the registry for updates now.

class smarter.apps.llmtool.extension.views.ExtensionCheckUpdatesHandler[source]

Bases: ExtensionApiHandler

Check the registry for updates now.

handle(request, body, **kwargs)[source]

Check the registry for updates, refreshing its index, and record each extension’s latest version.

Parameters:
  • request – The request.

  • body – None: the request has no body.

Return type:

ActionResponse

Returns:

A message that names the available updates.

class smarter.apps.llmtool.extension.views.ExtensionConfigureApiView(**kwargs)[source]

Bases: ExtensionConfigureHandler, ExtensionApiView

Change some of an extension’s settings.

class smarter.apps.llmtool.extension.views.ExtensionConfigureHandler[source]

Bases: ExtensionActionHandler

Change some of an extension’s settings.

act(request, extension, body)[source]

Save the extension’s settings.

A secret setting is given a new value, which is saved, encrypted, in a Secret owned by the platform admin, or the id of one of the platform admin’s Secrets, or neither, which clears it.

Raises:

ValueError – If the extension has no installed version, a secret setting does not exist, or a Secret is not one of the platform admin’s.

Return type:

str

request_model

alias of ConfigureRequest

class smarter.apps.llmtool.extension.views.ExtensionDetailApiView(**kwargs)[source]

Bases: ExtensionDetailHandler, ExtensionApiView

An extension’s detail page.

class smarter.apps.llmtool.extension.views.ExtensionDetailHandler[source]

Bases: ExtensionApiHandler

Everything that an extension’s detail page shows, and less to a user who is not a superuser.

handle(request, body, slug='', **kwargs)[source]

Return an extension’s detail, or its read-only detail to a user who is not a superuser.

Parameters:
  • request – The request.

  • body – None: the request has no body.

  • slug (str) – The extension’s slug, from the url.

Return type:

ExtensionDetailResponse

Returns:

The extension’s detail.

Raises:

Extension.DoesNotExist – If it is not installed, or the requesting user may not see it.

read_only: bool = True

Whether every user may call it.

It then shows a user who is not a superuser only what they may see.

class smarter.apps.llmtool.extension.views.ExtensionDetailView(**kwargs)[source]

Bases: ConsoleAppView

An extension’s page, /extensions/<slug>/: the single-page console’s.

The console shows anyone the page, and the api refuses an extension that the requesting user may not see, as ExtensionApiView.visible() decides.

class smarter.apps.llmtool.extension.views.ExtensionDisableApiView(**kwargs)[source]

Bases: ExtensionDisableHandler, ExtensionApiView

Disable an extension.

class smarter.apps.llmtool.extension.views.ExtensionDisableHandler[source]

Bases: ExtensionActionHandler

Disable an extension.

act(request, extension, body)[source]

Disable the extension.

Return type:

str

class smarter.apps.llmtool.extension.views.ExtensionEnableApiView(**kwargs)[source]

Bases: ExtensionEnableHandler, ExtensionApiView

Enable an extension.

class smarter.apps.llmtool.extension.views.ExtensionEnableHandler[source]

Bases: ExtensionActionHandler

Enable an extension.

act(request, extension, body)[source]

Enable the extension, after checking that it is compatible, loads, and has its required settings.

Return type:

str

class smarter.apps.llmtool.extension.views.ExtensionInstallApiView(**kwargs)[source]

Bases: ExtensionInstallHandler, ExtensionApiView

Install, or update, an extension from the registry.

class smarter.apps.llmtool.extension.views.ExtensionInstallHandler[source]

Bases: ExtensionApiHandler

Install, or update, an extension from the registry.

handle(request, body, **kwargs)[source]

Install, or update, an extension from the registry.

Parameters:
  • request – The request.

  • body (BaseModel | None) – The extension’s slug, and optionally its version.

Return type:

ActionResponse

Returns:

A message, and the installed extension.

request_model

alias of InstallRequest

class smarter.apps.llmtool.extension.views.ExtensionListApiView(**kwargs)[source]

Bases: ExtensionListHandler, ExtensionApiView

The installed extensions that the requesting user may see, and the state of the registry.

class smarter.apps.llmtool.extension.views.ExtensionListHandler[source]

Bases: ExtensionApiHandler

The installed extensions that the requesting user may see, and the state of the registry.

handle(request, body, **kwargs)[source]

List the installed extensions that the requesting user may see, by display name.

Parameters:
  • request – The request.

  • body – None: the request has no body.

Return type:

ExtensionListResponse

Returns:

The extensions, whether the registry is enabled, and how many updates are available.

read_only: bool = True

Whether every user may call it.

It then shows a user who is not a superuser only what they may see.

class smarter.apps.llmtool.extension.views.ExtensionListView(**kwargs)[source]

Bases: ConsoleAppView

The Extensions page, /extensions/: the single-page console’s, at the path that Django served it at.

class smarter.apps.llmtool.extension.views.ExtensionRegistryApiView(**kwargs)[source]

Bases: ExtensionRegistryHandler, ExtensionApiView

The registry’s extensions, with what each offers on this platform.

class smarter.apps.llmtool.extension.views.ExtensionRegistryHandler[source]

Bases: ExtensionApiHandler

The registry’s extensions, with what each offers on this platform.

Only a superuser may refresh it.

handle(request, body, **kwargs)[source]

Return the registry’s extensions, from the cached index, or from the registry if a superuser asks to refresh it.

Parameters:
  • request – The request.

  • body (BaseModel | None) – Whether to refresh the index.

Return type:

RegistryResponse

Returns:

The registry’s extensions, or an error if the registry is disabled or cannot be read.

read_only: bool = True

Whether every user may call it.

It then shows a user who is not a superuser only what they may see.

request_model

alias of RegistryRequest

class smarter.apps.llmtool.extension.views.ExtensionRollbackApiView(**kwargs)[source]

Bases: ExtensionRollbackHandler, ExtensionApiView

Make an earlier version of an extension the one that runs.

class smarter.apps.llmtool.extension.views.ExtensionRollbackHandler[source]

Bases: ExtensionActionHandler

Make an earlier version of an extension the one that runs.

act(request, extension, body)[source]

Make one of the extension’s installed versions the one that runs.

Raises:

ValueError – If the version is not one of the extension’s.

Return type:

str

request_model

alias of RollbackRequest

class smarter.apps.llmtool.extension.views.ExtensionUninstallApiView(**kwargs)[source]

Bases: ExtensionUninstallHandler, ExtensionApiView

Uninstall an extension.

class smarter.apps.llmtool.extension.views.ExtensionUninstallHandler[source]

Bases: ExtensionActionHandler

Uninstall an extension.

act(request, extension, body)[source]

Uninstall the extension.

Return type:

str

class smarter.apps.llmtool.extension.views.ExtensionUpdateApiView(**kwargs)[source]

Bases: ExtensionUpdateHandler, ExtensionApiView

Update an extension to the registry’s newest compatible version.

class smarter.apps.llmtool.extension.views.ExtensionUpdateHandler[source]

Bases: ExtensionActionHandler

Update an extension to the registry’s newest compatible version.

act(request, extension, body)[source]

Install the registry’s newest compatible version of the extension.

Return type:

str

class smarter.apps.llmtool.extension.views.ExtensionUploadApiView(**kwargs)[source]

Bases: ExtensionUploadHandler, ExtensionApiView

Install, or update, an extension from an uploaded archive.

class smarter.apps.llmtool.extension.views.ExtensionUploadHandler[source]

Bases: ExtensionApiHandler

Install, or update, an extension from an uploaded archive: a multipart request with the file archive.

handle(request, body, **kwargs)[source]

Install an extension from the uploaded archive.

Parameters:
  • request – The multipart request, with the file archive.

  • body – None: the archive is a file, not a JSON body.

Return type:

ActionResponse

Returns:

A message, and the installed extension.

Raises:

ValueError – If no archive was uploaded, or it is larger than MAX_UPLOAD_BYTES.

smarter.apps.llmtool.extension.views.extension_detail(extension)[source]

Return everything that an extension’s detail page shows.

Parameters:

extension (Extension) – The extension.

Return type:

ExtensionDetail

smarter.apps.llmtool.extension.views.read_only_detail(detail)[source]

Return what a user who is not a superuser may see of an extension’s detail page.

Its settings, the accounts that may use it, the platform admin’s Secrets and who installed each version are left out.

Parameters:

detail (ExtensionDetail) – Everything that the detail page shows a superuser.

Return type:

ExtensionDetail

smarter.apps.llmtool.extension.views.registry_extensions(index)[source]

Return the registry’s extensions, with what each card offers on this platform.

Parameters:

index (RegistryIndex) – The registry’s verified index.

Return type:

list[RegistryExtension]

The Extensions REST api, under /api/v1/extensions/: what the web console’s Extensions pages do, with an api key.

  • GET extensions/: the installed extensions that the user may see, and the state of the registry.

  • GET extensions/registry/: the registry’s extensions. ?refresh=true downloads the index again, for a superuser.

  • POST extensions/check-updates/: check the registry for updates now.

  • POST extensions/install/: install, or update, an extension from the registry, {"slug": ..., "version": ...}.

  • POST extensions/upload/: install, or update, an extension from a .zip archive, as multipart/form-data with the file archive.

  • GET extensions/<slug>/: an extension’s detail, and less of it to a user who is not a superuser.

  • POST extensions/<slug>/enable/, disable/, update/, rollback/, configure/, availability/ and uninstall/: the actions on an extension.

The reads are for every user, who sees only the extensions that their account may use. Everything else is for superusers only. Each view mixes one of the handlers of smarter.apps.llmtool.extension.views, which the web console’s api uses too, into ExtensionApiV1View. The responses are the models of smarter.apps.llmtool.extension.contract, which the Smarter api’s OpenAPI document publishes. Errors are Django REST framework’s {"detail": ...}.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionApiV1ActionView(*args, **kwargs)[source]

Bases: ExtensionApiV1View

An action: POST.

post(request, *args, **kwargs)[source]

Carry out the action.

Parameters:
  • request (Request) – The request.

  • kwargs – Keyword arguments from the url, e.g. slug.

Return type:

Response

Returns:

The response.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionApiV1ReadView(*args, **kwargs)[source]

Bases: ExtensionApiV1View

A read: GET.

get(request, *args, **kwargs)[source]

Return the read.

Parameters:
  • request (Request) – The request.

  • kwargs – Keyword arguments from the url, e.g. slug.

Return type:

Response

Returns:

The response.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionApiV1View(*args, **kwargs)[source]

Bases: ExtensionApiHandler, SmarterAuthenticatedAPIView

The base class of the Extensions REST api: an api key, JSON or multipart in, JSON out.

The data of a GET is its query string, and of a POST its body. Errors are {"detail": ...}.

reply(request, **kwargs)[source]

Carry out the request with respond(), and return its model’s JSON or {"detail": ...}.

Parameters:
  • request (Request) – The request.

  • kwargs – Keyword arguments from the url, e.g. slug.

Return type:

Response

Returns:

The response.

request_data(request)[source]

Return the request’s query parameters, for a GET, or its parsed body.

Parameters:

request (Request) – The request.

Return type:

dict[str, Any]

Returns:

The data, as a dict.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1AvailabilityView(*args, **kwargs)[source]

Bases: ExtensionAvailabilityHandler, ExtensionApiV1ActionView

Change which accounts may use an extension.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1CheckUpdatesView(*args, **kwargs)[source]

Bases: ExtensionCheckUpdatesHandler, ExtensionApiV1ActionView

Check the registry for updates now.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1ConfigureView(*args, **kwargs)[source]

Bases: ExtensionConfigureHandler, ExtensionApiV1ActionView

Change some of an extension’s settings.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1DetailView(*args, **kwargs)[source]

Bases: ExtensionDetailHandler, ExtensionApiV1ReadView

An installed extension.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1DisableView(*args, **kwargs)[source]

Bases: ExtensionDisableHandler, ExtensionApiV1ActionView

Disable an extension.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1EnableView(*args, **kwargs)[source]

Bases: ExtensionEnableHandler, ExtensionApiV1ActionView

Enable an extension.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1InstallView(*args, **kwargs)[source]

Bases: ExtensionInstallHandler, ExtensionApiV1ActionView

Install, or update, an extension from the registry.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1ListView(*args, **kwargs)[source]

Bases: ExtensionListHandler, ExtensionApiV1ReadView

The installed extensions that the user may see.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1RegistryView(*args, **kwargs)[source]

Bases: ExtensionRegistryHandler, ExtensionApiV1ReadView

The registry’s extensions.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1RollbackView(*args, **kwargs)[source]

Bases: ExtensionRollbackHandler, ExtensionApiV1ActionView

Make an earlier version of an extension the one that runs.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1UninstallView(*args, **kwargs)[source]

Bases: ExtensionUninstallHandler, ExtensionApiV1ActionView

Uninstall an extension.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1UpdateView(*args, **kwargs)[source]

Bases: ExtensionUpdateHandler, ExtensionApiV1ActionView

Update an extension to the registry’s newest compatible version.

class smarter.apps.llmtool.extension.api.v1.views.ExtensionV1UploadView(*args, **kwargs)[source]

Bases: ExtensionUploadHandler, ExtensionApiV1ActionView

Install, or update, an extension from an uploaded .zip archive.