Jido.Composer.Node behaviour (Jido Composer v0.4.0)

Copy Markdown View Source

Uniform context-in/context-out interface for workflow participants.

Every participant in a composition — actions, agents, nested workflows — implements this behaviour. Nodes form an endomorphism monoid over context maps, composed via Kleisli arrows.

Return Types

  • {:ok, context} — success with implicit outcome :ok
  • {:ok, context, outcome} — success with explicit outcome for FSM transitions
  • {:error, reason} — failure with implicit outcome :error

Directive Generation

The to_directive/3 callback allows strategies to dispatch nodes polymorphically without pattern-matching on concrete struct types. Each node knows how to produce the appropriate directive(s) for execution given a flat context and keyword opts.

Opts carry strategy-specific metadata (e.g. :result_action, :tag, :meta) so the node remains strategy-agnostic.

The to_tool_spec/1 callback lets nodes describe themselves as LLM tool definitions for orchestrator contexts. Nodes that cannot act as tools return nil.

Summary

Functions

Returns true if module is a compiled Jido.Agent module.

Returns true if module is a Jido.AI agent (exports ask_sync/3).

Extracts the name from a Node struct.

Runs a child agent module synchronously.

Returns true if module declares the Jido.Composer.Node behaviour.

Types

context()

@type context() :: map()

directive_result()

@type directive_result() :: {:ok, [struct()]} | {:ok, [struct()], keyword()}

outcome()

@type outcome() :: atom()

result()

@type result() :: {:ok, context()} | {:ok, context(), outcome()} | {:error, term()}

Callbacks

description(node)

@callback description(node :: struct()) :: String.t()

input_type(node)

(optional)
@callback input_type(node :: struct()) :: :map | :text | :object | :any

name(node)

@callback name(node :: struct()) :: String.t()

output_type(node)

(optional)
@callback output_type(node :: struct()) :: :map | :text | :object | :any

run(node, context, opts)

@callback run(node :: struct(), context :: context(), opts :: keyword()) :: result()

schema(node)

(optional)
@callback schema(node :: struct()) :: keyword() | nil

to_directive(node, flat_context, opts)

(optional)
@callback to_directive(node :: struct(), flat_context :: map(), opts :: keyword()) ::
  directive_result()

to_tool_spec(node)

(optional)
@callback to_tool_spec(node :: struct()) :: map() | nil

Functions

agent_module?(module)

@spec agent_module?(module()) :: boolean()

Returns true if module is a compiled Jido.Agent module.

ai_agent_module?(module)

@spec ai_agent_module?(module()) :: boolean()

Returns true if module is a Jido.AI agent (exports ask_sync/3).

Jido.AI agents require a running AgentServer process and communicate via ask_sync(pid, query, opts) rather than struct-based run_sync/2.

dispatch_name(node)

@spec dispatch_name(struct()) :: String.t()

Extracts the name from a Node struct.

Validates that the struct's module implements the Node behaviour before calling name/1. Raises ArgumentError if it does not.

Used by AgentTool, Configure, and Strategy to handle custom Node structs without duplicating the dispatch + validation logic.

execute_child_sync(child_module, spawn_opts)

@spec execute_child_sync(module(), map()) :: {:ok, term()} | {:error, term()}

Runs a child agent module synchronously.

Supports three agent types:

  • Composer Workflow agents (run_sync/2)
  • Composer Orchestrator agents (query_sync/3)
  • Jido.AI agents (ask_sync/3 via a temporary AgentServer process)

node?(module)

@spec node?(module()) :: boolean()

Returns true if module declares the Jido.Composer.Node behaviour.