A2UIAgent.SchemaPruning (A2UIAgent v0.1.1)

Copy Markdown View Source

Prunes the protocol schemas a prompt shows the model.

Catalogs are pruned with A2UIAgent.CatalogTransformer; the agent-to-renderer and common types schemas belong to the protocol, so they are pruned here.

Summary

Functions

Keeps only the common type definitions in use: those the referencing schemas refer to (as common_types.json#/$defs/<name> or #/$defs/<name>) and those a definition in use refers to.

Keeps only the allowed messages of an agent-to-renderer schema.

Functions

prune_common_types(common_types, referencing)

(since 0.1.0)
@spec prune_common_types(map(), [map()]) :: map()

Keeps only the common type definitions in use: those the referencing schemas refer to (as common_types.json#/$defs/<name> or #/$defs/<name>) and those a definition in use refers to.

prune_messages(schema, allowed)

(since 0.1.0)
@spec prune_messages(map(), [String.t()]) :: map()

Keeps only the allowed messages of an agent-to-renderer schema.

allowed names message actions ("createSurface") or message definitions ("CreateSurfaceMessage"). The allowlist is read literally: an empty one admits no message and unknown names are ignored. Definitions only the removed messages use are removed too.

iex> schema = %{
...>   "oneOf" => [%{"$ref" => "#/$defs/A"}, %{"$ref" => "#/$defs/B"}],
...>   "$defs" => %{
...>     "A" => %{"properties" => %{"createSurface" => %{"$ref" => "#/$defs/Shared"}}},
...>     "B" => %{"properties" => %{"deleteSurface" => %{}}},
...>     "Shared" => %{}
...>   }
...> }
iex> pruned = A2UIAgent.SchemaPruning.prune_messages(schema, ["createSurface"])
iex> {pruned["oneOf"], Map.keys(pruned["$defs"])}
{[%{"$ref" => "#/$defs/A"}], ["A", "Shared"]}