Bobbin.Node behaviour (bobbin v0.2.2)

Copy Markdown View Source

Defines the behavior and macros for creating Bobbin nodes.

A node is the fundamental unit of logic in a flow. It consists of a definition/0 describing its interface and a handler/2 to process data.

Example

defmodule MyNodes.HttpNode do
  use Bobbin.Node

  definition do
    %{
      name: "http_request",
      title: "HTTP Request",
      category: :network,
      color: "bg-blue-500",
      icon: "hero-globe-alt",
      inputs: [:exec, :url],
      outputs: [:success, :error]
    }
  end

  @impl true
  def handler(context, input) do
    {:ok, context, :success}
  end
end

Summary

Callbacks

Returns the metadata and interface definition for the node.

Processes the node logic.

Functions

Macro to define the node metadata.

Converts a node module's definition to a Rete.js-compatible map.

Types

context()

@type context() :: map()

input()

@type input() :: any()

node_definition()

@type node_definition() :: %{
  :name => String.t(),
  :title => String.t(),
  optional(:category) => atom() | String.t(),
  optional(:color) => String.t(),
  optional(:icon) => String.t(),
  optional(:inputs) => [map() | atom() | String.t()],
  optional(:outputs) => [map() | atom() | String.t()],
  optional(atom()) => any()
}

output_port()

@type output_port() :: atom() | String.t()

Callbacks

definition()

@callback definition() :: node_definition()

Returns the metadata and interface definition for the node.

handler(context, input)

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

Processes the node logic.

Returns {:ok, new_context, output_port} on success, or {:error, reason} on failure.

Functions

definition(list)

(macro)

Macro to define the node metadata.

to_rete(module)

@spec to_rete(module()) :: map()

Converts a node module's definition to a Rete.js-compatible map.

The returned map follows the Rete.js node schema with id, label, inputs, and outputs structured as socket definitions.

Example

Bobbin.Node.to_rete(MyNodes.HttpNode)
# => %{
#   id: "http_request",
#   label: "HTTP Request",
#   color: "bg-blue-500",
#   inputs: [%{id: "exec", label: "Exec"}, %{id: "url", label: "Url"}],
#   outputs: [%{id: "success", label: "Success"}, %{id: "error", label: "Error"}]
# }