AgentForge.Primitives (AgentForge v0.2.2)

View Source

Provides basic primitives for building dynamic workflows. Includes fundamental building blocks like branching, transformation, and iteration.

Summary

Functions

Creates a branch primitive that executes different handlers based on a condition.

Creates a loop primitive that iterates over items in the signal data. Accumulates results and maintains state across iterations.

Creates a notify primitive that sends notifications through configured channels.

Creates a sequence primitive that executes multiple handlers in order, passing the result of each to the next.

Creates a transform primitive that modifies signal data.

Creates a wait primitive that pauses execution until a condition is met. Returns {:wait, reason} while waiting, {:emit, signal} when condition is met.

Functions

branch(condition, then_flow, else_flow)

Creates a branch primitive that executes different handlers based on a condition.

Options

  • :condition - A function that takes a signal and state and returns a boolean
  • :then_flow - Flow to execute when condition is true
  • :else_flow - Flow to execute when condition is false

Examples

iex> branch = AgentForge.Primitives.branch(
...>   fn signal, _ -> signal.data > 10 end,
...>   [fn signal, state -> {AgentForge.Signal.emit(:high, signal.data), state} end],
...>   [fn signal, state -> {AgentForge.Signal.emit(:low, signal.data), state} end]
...> )
iex> signal = AgentForge.Signal.new(:value, 15)
iex> {result, _} = branch.(signal, %{})
iex> match?({:emit, %{type: :high}}, result)
true

loop(item_handler)

Creates a loop primitive that iterates over items in the signal data. Accumulates results and maintains state across iterations.

Examples

iex> handler = fn item, state ->
...>   {AgentForge.Signal.emit(:item, item), Map.update(state, :sum, item, &(&1 + item))}
...> end
iex> loop = AgentForge.Primitives.loop(handler)
iex> signal = AgentForge.Signal.new(:list, [1, 2, 3])
iex> {result, state} = loop.(signal, %{})
iex> match?({:emit_many, _}, result) and state.sum == 6
true

notify(channels, opts \\ [])

Creates a notify primitive that sends notifications through configured channels.

Options

  • :channels - List of notification channels (e.g. [:console, :slack])
  • :format - Optional function to format the notification message
  • :config - Optional configuration map for channels

Examples

iex> notify = AgentForge.Primitives.notify([:console])
iex> signal = AgentForge.Signal.new(:event, "System alert")
iex> {{:emit, result}, _} = notify.(signal, %{})
iex> result.type == :notification
true

sequence(handlers)

Creates a sequence primitive that executes multiple handlers in order, passing the result of each to the next.

Examples

iex> handlers = [
...>   fn signal, state -> {AgentForge.Signal.emit(:step1, signal.data <> "_1"), state} end,
...>   fn signal, state -> {AgentForge.Signal.emit(:step2, signal.data <> "_2"), state} end
...> ]
iex> sequence = AgentForge.Primitives.sequence(handlers)
iex> signal = AgentForge.Signal.new(:start, "data")
iex> {{:emit, result}, _} = sequence.(signal, %{})
iex> result.data
"data_1_2"

transform(transform_fn)

Creates a transform primitive that modifies signal data.

Examples

iex> transform = AgentForge.Primitives.transform(fn data -> String.upcase(data) end)
iex> signal = AgentForge.Signal.new(:text, "hello")
iex> {{:emit, result}, _} = transform.(signal, %{})
iex> result.data
"HELLO"

wait(condition, opts \\ [])

Creates a wait primitive that pauses execution until a condition is met. Returns {:wait, reason} while waiting, {:emit, signal} when condition is met.

Options

  • :condition - A function that takes a signal and state and returns a boolean
  • :timeout - Optional timeout in milliseconds, defaults to 5000
  • :retry_interval - Optional retry interval in milliseconds, defaults to 100

Examples

iex> wait = AgentForge.Primitives.wait(
...>   fn _, state -> Map.get(state, :ready, false) end,
...>   timeout: 1000
...> )
iex> signal = AgentForge.Signal.new(:check, "waiting")
iex> state = %{ready: false}
iex> {{:wait, reason}, _state} = wait.(signal, state)
iex> is_binary(reason)
true