Lux.Prism behaviour (Lux v0.5.0)

View Source

Modular, composable units of functionality for defining actions.

Prisms are used to define actions that can be executed by agents.

Example

defmodule MyApp.Prisms.WeatherPrism do
  use Lux.Prism,
    name: "Weather Data",
    description: "Fetches weather data for a given location",
    input_schema: %{
      type: :object,
      properties: %{
        location: %{type: :string, description: "City name"},
        units: %{type: :string, description: "Temperature units (C/F)"}
      }
    }

  def handler(input, _ctx) do
    # Implementation
  end
end

Summary

Types

A handler is a function or a module that handles the data.

An optional type of a type t is a type that can be either the type t or nil.

A schema is either a map of key-value pairs that describe the structure of the data, or a module that implements the Lux.SignalSchema behaviour.

t()

A validator is a function or a module that validates the data.

Callbacks

A handler is the function that will be called when the prism is executed.

Functions

Creates a new prism from a map or keyword list.

Resolves a schema reference to its actual schema definition. If the schema is a module that implements Lux.SignalSchema, returns its schema. Otherwise, returns the schema as is.

Types

handler()

@type handler() :: function() | mfa() | binary()

A handler is a function or a module that handles the data.

nullable(t)

@type nullable(t) :: t | nil

An optional type of a type t is a type that can be either the type t or nil.

schema()

@type schema() :: map() | module()

A schema is either a map of key-value pairs that describe the structure of the data, or a module that implements the Lux.SignalSchema behaviour.

t()

@type t() :: %Lux.Prism{
  description: nullable(String.t()),
  examples: nullable([String.t()]),
  handler: handler(),
  id: String.t(),
  input_schema: nullable(schema()),
  name: String.t(),
  output_schema: nullable(schema())
}

validator()

@type validator() :: function() | mfa() | binary()

A validator is a function or a module that validates the data.

Callbacks

handler(input, context)

@callback handler(input :: any(), context :: any()) :: {:ok, any()} | {:error, any()}

A handler is the function that will be called when the prism is executed.

Functions

new(attrs)

@spec new(map() | keyword()) :: t()

Creates a new prism from a map or keyword list.

resolve_schema(schema)

Resolves a schema reference to its actual schema definition. If the schema is a module that implements Lux.SignalSchema, returns its schema. Otherwise, returns the schema as is.

run(schema, input, context \\ nil)

run(path, binary, input, context)

view(path)

view(path, arg2)