Spectre.Rule (Spectre v0.3.0)

Copy Markdown View Source

Declarative routing rule compiled from Spectre.Agent DSL.

Rules are internal domain data. They normalize DSL options such as regexes, examples, checks, and handler declarations before any router plug sees them.

rule =
  Spectre.Rule.new(%{
    label: :help,
    flow: :support,
    regex: ~r/^help$/i,
    handler: {:reply, :help, []}
  })

Summary

Functions

Returns true when all checks pass against an input.

Returns true when any deterministic regex on the rule matches the text.

Builds a normalized routing rule from DSL metadata.

Types

handler()

@type handler() ::
  {:ask, term(), keyword()}
  | {:reason, term(), keyword()}
  | {:act, term(), keyword()}
  | {:run, atom(), keyword()}
  | {:work, module(), keyword()}
  | {:reply, term(), keyword()}
  | {:action, Spectre.Action.ref(), keyword()}
  | {:operation, term(), keyword()}

t()

@type t() :: %Spectre.Rule{
  bag: [String.t()],
  cache: boolean(),
  checks: [{atom() | String.t(), term()}],
  constraints: [Spectre.Flow.Constraint.t()],
  definition_injections: [Spectre.Prompt.Operation.t()],
  embedding: [String.t()],
  flow: atom() | nil,
  flow_path: [atom()],
  global?: boolean(),
  handler: handler(),
  injections: [Spectre.Prompt.Operation.t()],
  jaro: [String.t()],
  label: atom(),
  learn: boolean(),
  opts: keyword(),
  owner: module() | nil,
  regex: [Regex.t()],
  scope: Spectre.Definition.scope(),
  via: [atom()]
}

Functions

checks_match?(rule, input)

@spec checks_match?(t(), Spectre.Input.t()) :: boolean()

Returns true when all checks pass against an input.

Spectre.Rule.checks_match?(rule, input)

match?(rule, text)

@spec match?(t(), String.t()) :: boolean()

Returns true when any deterministic regex on the rule matches the text.

Spectre.Rule.match?(rule, "hello")

new(attrs)

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

Builds a normalized routing rule from DSL metadata.

%Spectre.Rule{} = Spectre.Rule.new(%{label: :hello, regex: ~r/hello/})