A2UIAgent.CatalogTransformer behaviour (A2UIAgent v0.1.1)

Copy Markdown View Source

Rules applied to a catalog before it reaches a prompt or a validator.

A transformer is either a module implementing this behaviour paired with its argument, {module, argument}, or a one-argument function. Either way it returns a new catalog. Transformers are registered on an A2UIAgent.CatalogConfig, which applies them in order.

Two are built in:

The allowlist is read literally: an empty list (or nil) keeps nothing, and a name the catalog does not declare is ignored. To keep everything, apply no transformer. The pruned catalog's schema is rebuilt from what it keeps, so its anyComponent, anyFunction and helper $defs refer only to those entries.

iex> {:ok, catalog} =
...>   A2UICore.Catalog.from_json(%{
...>     "catalogId" => "demo",
...>     "protocolVersion" => "v1.0",
...>     "components" => %{
...>       "Label" => %{"type" => "object", "properties" => %{"text" => %{"type" => "string"}}},
...>       "Badge" => %{"type" => "object", "properties" => %{"count" => %{"type" => "number"}}}
...>     }
...>   })
iex> pruned =
...>   A2UIAgent.CatalogTransformer.apply_all(catalog, [
...>     A2UIAgent.CatalogTransformer.component_pruning(["Label", "Unknown"])
...>   ])
iex> Map.keys(pruned.components)
["Label"]

Summary

Types

t()

A transformer: {module, argument} or a function.

Callbacks

Returns a new catalog derived from catalog.

Functions

Applies transformers to catalog one after another.

A transformer that keeps only the named components. Functions and other definitions are kept.

A transformer that keeps only the named functions. Components and other definitions are kept.

Types

t()

(since 0.1.0)
@type t() :: {module(), term()} | (A2UICore.Catalog.t() -> A2UICore.Catalog.t())

A transformer: {module, argument} or a function.

Callbacks

transform(t, argument)

(since 0.1.0)
@callback transform(A2UICore.Catalog.t(), argument :: term()) :: A2UICore.Catalog.t()

Returns a new catalog derived from catalog.

Functions

apply_all(catalog, transformers)

(since 0.1.0)
@spec apply_all(A2UICore.Catalog.t(), [t()] | nil) :: A2UICore.Catalog.t()

Applies transformers to catalog one after another.

component_pruning(names)

(since 0.1.0)
@spec component_pruning([String.t()] | nil) :: t()

A transformer that keeps only the named components. Functions and other definitions are kept.

Raises ArgumentError for a single string, which is almost always a mistake for a one-element list.

function_pruning(names)

(since 0.1.0)
@spec function_pruning([String.t()] | nil) :: t()

A transformer that keeps only the named functions. Components and other definitions are kept.

Raises ArgumentError for a single string.