Brokers

Smarter API Proxy Manifest handler.

The broker implements the smarter CLI commands for Proxies:

  • apply: create or update the Proxy from its manifest. spec.provider and spec.apiKey are resolved to the Provider and Secret of those names that the user may read.

  • describe: the manifest, with the Proxy’s URL, and the URL and Secret it forwards with.

  • get: the Proxies that the user may read, including the built-in ones.

  • delete: delete the user’s Proxy.

Proxies are not deployed: they work as soon as they are applied. So deploy, undeploy, logs and prompt are not implemented.

class smarter.apps.proxy.manifest.brokers.proxy.SAMProxyBroker(*args, **kwargs)[source]

Bases: AbstractBroker[Proxy, Proxy]

Broker for SAM Proxy manifests.

It converts between Proxy manifests and the Proxy Django ORM model.

spec.provider is the name of a Provider that the manifest’s owner may read, e.g. a built-in one. spec.apiKey is the name of a Secret of the owner’s account. The user’s own takes precedence. They are stored as foreign keys, and rendered as names, never the API key itself.

property ORMMetaModelClass: Type[Proxy]

Proxy.

Type:

The Django model of the Proxy manifest’s metadata

property ORMModelClass: Type[Proxy]

Proxy.

Type:

The Django model that this broker manages

property SerializerClass: Type[ModelSerializer]

The Django ORM model serializer class for the Proxy.

__init__(*args, **kwargs)[source]

Initialize the broker from a manifest, a manifest file, or the Django ORM.

A manifest takes precedence over a file path. With neither, the broker is initialized from the ORM instance named name and owned by the user profile, if there is one.

Parameters:
  • request – The request, or None. It may also be passed positionally in args.

  • args – Positional arguments, among which the request, user, account, user profile and manifest are found by their types.

  • name – The resource’s name.

  • kind – The resource’s SAM kind, e.g. Guardrail.

  • api_version – The manifest’s api version.

  • manifest – The manifest, as a Pydantic model or as a dict, e.g. from parse_manifest().

  • file_path – The path of a manifest file, read when no manifest is passed.

  • url – Accepted, for compatibility, but not used.

  • kwargs – user, account and user_profile, and the rest, which are passed on to SmarterRequestMixin.

Raises:
  • SmarterValueError – If the kind, name or manifest is invalid.

  • SAMBrokerError – If the manifest’s kind is not the broker’s kind.

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

Create or update the user’s Proxy from the manifest.

Note

tags are handled separately because they are of type TaggableManager and require a different method to set them.

Return type:

SmarterJournaledJsonResponse

cache_invalidations()[source]

Invalidate the cached Proxy, and the user’s cached Proxy lists.

Return type:

None

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

Delete the user’s Proxy.

Its Provider and Secret are not deleted.

Return type:

SmarterJournaledJsonResponse

dependencies()[source]

Return brokers for the resources that depend on this Proxy.

No other resource refers to a Proxy.

Returns:

An empty list.

Return type:

List[AbstractBroker | SAMDependencyReference]

classmethod dependents(instance)[source]

Return the resources that depend on a Proxy.

No other resource refers to a Proxy.

Parameters:

instance (Proxy) – The Proxy.

Returns:

An empty list.

Return type:

List[Tuple[str | None, Iterable[Model]]]

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

Not implemented: a Proxy works as soon as it is applied.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

Return type:

SmarterJournaledJsonResponse

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

Return the Proxy as a manifest: the user’s own, else their account’s, else the built-in one.

Return type:

SmarterJournaledJsonResponse

django_orm_to_manifest_dict(proxy=None)[source]

Convert a Proxy, by default the user’s, into a manifest dict, with its status.

Return type:

dict[str, Any] | None

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

Return an example Proxy manifest, for Anthropic’s Messages API.

Return type:

SmarterJournaledJsonResponse

property formatted_class_name: str

The class name, for logging.

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

Return the Proxies that the user may read, optionally filtered by name.

Return type:

SmarterJournaledJsonResponse

property kind: str

Proxy.

Type:

The manifest kind

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

Not implemented for Proxies.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

Return type:

SmarterJournaledJsonResponse

property manifest: SAMProxy | None

The Proxy manifest, as a Pydantic model, from the client’s manifest data.

manifest_to_django_orm()[source]

Convert the manifest into a dict of Django ORM Proxy fields.

Raises:

SAMBrokerErrorNotFound – if the Provider or Secret is not found.

Return type:

dict[str, Any]

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

Not implemented: call the Proxy’s URL with the provider’s SDK instead.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

Return type:

SmarterJournaledJsonResponse

property proxy: Proxy | None

The user’s own Proxy with the broker’s name, if it exists.

It is never created here: apply() does that.

readable_proxy()[source]

The Proxy that the user means by the broker’s name: their own, else their account’s, else the built-in one.

describe uses it, so that users can read the built-in Proxies’ manifests.

Return type:

Proxy | None

property ready: bool

A broker is ready if it has a manifest, or an account.

classmethod references(instance)[source]

Return the resources that a Proxy uses: its Provider, and the Secret of its api key.

Parameters:

instance (Proxy) – The Proxy.

Returns:

Its Provider and Secret.

Return type:

List[Tuple[str | None, Iterable[Model]]]

resolve_provider(name)[source]

The Provider named by spec.provider: the user’s own, else the most recently updated one that they may read.

Return type:

Provider

resolve_secret(name)[source]

The Secret named by spec.apiKey, or None if it is not set: the user’s own, else their account’s.

Unlike the Provider, the Secret must belong to the user’s account, so that a Proxy cannot send another account’s API key, e.g. the platform’s, to a base URL of its owner’s choosing. See may_use_secret().

Return type:

Secret | None

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

Not implemented: set the Proxy’s spec.isActive to false instead.

Parameters:

request (HttpRequest) – The request.

Raises:

SAMBrokerErrorNotImplemented – always.

Return type:

SmarterJournaledJsonResponse

exception smarter.apps.proxy.manifest.brokers.proxy.SAMProxyBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]

Bases: SAMBrokerError

Base exception for Smarter API Proxy Broker handling.

property get_formatted_err_message

The heading of this error’s message.

smarter.apps.proxy.manifest.brokers.proxy.django_orm_to_proxy_spec(proxy)[source]

The spec of a Proxy, as it would be applied: its Secret is named only if it is the Proxy’s own.

Return type:

SAMProxySpec

smarter.apps.proxy.manifest.brokers.proxy.proxy_spec_to_django_orm(spec)[source]

The Proxy fields of a spec, other than its Provider and Secret, which the broker resolves by name.

Return type:

dict[str, Any]