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

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]

Return the Django ORM meta model class for the broker.

Returns:

The Django ORM meta model class definition for the broker.

Return type:

Type[MetaDataWithOwnershipModel]

property ORMModelClass: Type[Proxy]

Return the Django ORM model class for the broker.

Returns:

The Django ORM model class definition for the broker.

Return type:

Type[MetaDataWithOwnershipModel]

property SerializerClass: Type[ModelSerializer]

The Django ORM model serializer class for the Proxy.

__init__(*args, **kwargs)[source]
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

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

Deploy a resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the result of the deploy operation.

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:

Optional[dict]

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]

Get logs for a resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the logs for the resource.

Return type:

SmarterJournaledJsonResponse

property manifest: SAMProxy | None

The Proxy manifest, as a Pydantic model, from the manifest loader.

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]

Invoke a prompt operation.

This abstract method should be implemented by subclasses to provide prompt-based interactions with the broker resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the prompt response.

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:

Optional[Proxy]

property ready: bool

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

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:

Optional[Secret]

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

Undeploy a resource.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • args – Additional positional arguments.

  • kwargs – Additional keyword arguments.

Returns:

A SmarterJournaledJsonResponse containing the result of the undeploy operation.

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
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]