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.providerandspec.apiKeyare 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
SAMProxy manifests.It converts between Proxy manifests and the
ProxyDjango ORM model.spec.provideris the name of a Provider that the manifest’s owner may read, e.g. a built-in one.spec.apiKeyis 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 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
nameand 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,accountanduser_profile, and the rest, which are passed on toSmarterRequestMixin.
- 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:
- cache_invalidations()[source]
Invalidate the cached Proxy, and the user’s cached Proxy lists.
- Return type:
- delete(request, *args, **kwargs)[source]
Delete the user’s Proxy.
Its Provider and Secret are not deleted.
- Return type:
- 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:
- classmethod dependents(instance)[source]
Return the resources that depend on a Proxy.
No other resource refers to a Proxy.
- 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:
- 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:
- django_orm_to_manifest_dict(proxy=None)[source]
Convert a Proxy, by default the user’s, into a manifest dict, with its status.
- example_manifest(request, *args, **kwargs)[source]
Return an example Proxy manifest, for Anthropic’s Messages API.
- Return type:
- get(request, *args, **kwargs)[source]
Return the Proxies that the user may read, optionally filtered by name.
- Return type:
- logs(request, *args, **kwargs)[source]
Not implemented for Proxies.
- Parameters:
request (
HttpRequest) – The request.- Raises:
SAMBrokerErrorNotImplemented – always.
- Return type:
- property manifest: SAMProxy | None
The Proxy manifest, as a Pydantic model, from the client’s manifest data.
- 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:
- 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.
describeuses it, so that users can read the built-in Proxies’ manifests.
- classmethod references(instance)[source]
Return the resources that a Proxy uses: its Provider, and the Secret of its api key.
- 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:
- resolve_secret(name)[source]
The Secret named by spec.apiKey, or
Noneif 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().
- undeploy(request, *args, **kwargs)[source]
Not implemented: set the Proxy’s
spec.isActiveto false instead.- Parameters:
request (
HttpRequest) – The request.- Raises:
SAMBrokerErrorNotImplemented – always.
- Return type:
- exception smarter.apps.proxy.manifest.brokers.proxy.SAMProxyBrokerError(message=None, thing=None, command=None, stack_trace=None)[source]
Bases:
SAMBrokerErrorBase exception for Smarter API Proxy Broker handling.
- property get_formatted_err_message
The heading of this error’s message.